From 7b8fe1b305a36f925fc2757a2c126c88fb0a5137 Mon Sep 17 00:00:00 2001 From: Fulup Ar Foll Date: Tue, 24 May 2016 09:23:56 +0200 Subject: [PATCH] Started move to new framework version --- afb-client/app/Frontend/etc/AppConfig.js | 26 ++++++--- afb-client/app/Frontend/pages/Home/HomeModule.js | 67 +++++++++++----------- .../widgets/Notifications/TokenRefreshSvc.js | 26 ++++++--- 3 files changed, 71 insertions(+), 48 deletions(-) diff --git a/afb-client/app/Frontend/etc/AppConfig.js b/afb-client/app/Frontend/etc/AppConfig.js index bd1aae0..5c9283e 100644 --- a/afb-client/app/Frontend/etc/AppConfig.js +++ b/afb-client/app/Frontend/etc/AppConfig.js @@ -23,7 +23,7 @@ ping : '/api/token/check', initial : urlquery.token || '123456789', // typical dev initial token timeout : 3600, // timeout is updated client sessin context creation - pingrate: 60, // Ping rate to check if server is still alive + pingrate: 30, // Ping rate to check if server is still alive uuid : '', // uuid map with cookie or long term session access key token : '' // will be returned from authentication } @@ -33,16 +33,28 @@ }) // Factory is a singleton and share its context within all instances. - .factory('AppCall', function ($http, AppConfig) { + .factory('AppCall', function ($http, AppConfig, $log) { var myCalls = { - get : function(plugin, action, query, callback) { - if (!query.token) query.token = AppConfig.session.token; // add token to provided query - $http.get('/api/' + plugin + '/' + action , {params: query}).then (callback, callback); + get : function(plugin, action, query, cbresponse, cberror) { + if (!query.token) query.token = AppConfig.session.token; // add token to provided query + var handle= $http.get('/api/' + plugin + '/' + action , {params: query}).then (onsuccess, onerror); + + handle.onsuccess (function(data, errcode, headers, config) { + // if token was updated keep it within application cache + if (data.request.token) AppConfig.session.token = data.request.token; + if (data.request.uuid) AppConfig.session.uuid = data.request.uuid; + if (data.request.timeout) AppConfig.session.timeout = data.request.timeout; + + cbresponse(data, errcode, headers, config); + }); + + handle.onerror (function(data, errcode, headers, config) { + if (cberror) cberror(data, errcode, headers, config); + else cbresponse(data, errcode, headers, config); + }); } - }; return myCalls; }); - })(); \ No newline at end of file diff --git a/afb-client/app/Frontend/pages/Home/HomeModule.js b/afb-client/app/Frontend/pages/Home/HomeModule.js index fb03ac8..fdbe520 100644 --- a/afb-client/app/Frontend/pages/Home/HomeModule.js +++ b/afb-client/app/Frontend/pages/Home/HomeModule.js @@ -4,9 +4,9 @@ // WARNING: make sure than app/frontend/services/AppConfig.js match your server // list all rependencies within the page + controler if needed -angular.module('HomeModule', ['SubmitButton', 'TokenRefresh']) +angular.module('HomeModule', ['SubmitButton', 'TokenRefresh','ModalNotification']) - .controller('HomeController', function ($http, AppConfig) { + .controller('HomeController', function (AppCall, Notification) { var scope = this; // I hate JavaScript scope.uuid ="none"; scope.token ="none"; @@ -16,56 +16,59 @@ angular.module('HomeModule', ['SubmitButton', 'TokenRefresh']) console.log ("Home Controller"); scope.ProcessResponse= function(data, errcode, headers, config) { - var apiname= 'API'+ data.request.api.replace('-','_'); - scope.status = "err-ok"; - scope.errcode= errcode; - scope.request = data.request; - scope.response = data.response; - // if token was refresh let's update AppConfig - if (data.request.token) AppConfig.session.token = data.request.token; - if (data.request.uuid) AppConfig.session.uuid = data.request.uuid; - if (data.request.timeout) AppConfig.session.timeout = data.request.timeout; - - // Make sure we clean everything when Open/Close is called - if (apiname === "APIcreate" || apiname === "APIreset") { - scope.APIreset =''; - scope.APIcreate =''; - scope.APIrefresh=''; - scope.APIcheck =''; + if (data.request.status !== "success") { + Notification.error ({message: "Invalid API call:" + data.request.info , delay: 5000}); + return; } - scope[apiname]="success"; - // If we have a new token let's update it - if (data.request.token) scope.token=data.request.token; + // Update UI response global display zone + scope.status = data.request.status; + scope.errcode = errcode; + scope.request = data.request; + scope.response = data.response; + switch (data.request.reqid) { + case 'open': + case 'reset': + scope.APIreset =''; + scope.APIcreate =''; + scope.APIrefresh=''; + scope.APIcheck =''; + break; + + case 'refresh': + case 'check': + break; + + default: + Notification.error ({message: "Invalid RequestID:" + data.request.reqid , delay: 5000}); + return; + } + + scope[reqid]="success"; console.log ("OK: "+ JSON.stringify(data)); }; scope.ProcessError= function(data, errcode, headers, config) { - var apiname= 'API'+data.request.api.replace('-','_'); + Notification.error ({message: "Invalid API:" + data.request.reqid , delay: 5000}); scope.status = "err-fx"; scope.errcode = errcode; scope.request = data.request; - scope.response = ""; - scope[apiname]="fail"; - + scope.response = ""; console.log ("FX: "+ JSON.stringify(data)); }; scope.OpenSession = function() { - console.log ("OpenSession"); - var postdata= {/* any json your application may need */}; - var handler = $http.post(AppConfig.session.create + '?token='+AppConfig.session.initial, postdata); - - handler.success(scope.ProcessResponse); - handler.error(scope.ProcessError); + console.log ("OpenSession"); + AppCall.get ("token", "create", {reqid:"open"}, scope.ProcessResponse, scope.InvalidApiCall); }; scope.CheckSession = function() { console.log ("CloseSession"); + var postdata= {/* any json your application may need */}; - var handler = $http.post(AppConfig.session.check + '?token='+AppConfig.session.token, postdata); + var handler = $http.post(AppConfig.session.check + '?token='+AppConfig.session.token +'?idreq=open', postdata); handler.success(scope.ProcessResponse); handler.error(scope.ProcessError); diff --git a/afb-client/app/Frontend/widgets/Notifications/TokenRefreshSvc.js b/afb-client/app/Frontend/widgets/Notifications/TokenRefreshSvc.js index 59fc763..ec29978 100644 --- a/afb-client/app/Frontend/widgets/Notifications/TokenRefreshSvc.js +++ b/afb-client/app/Frontend/widgets/Notifications/TokenRefreshSvc.js @@ -66,6 +66,13 @@ angular.module('TokenRefresh', ['AppConfig', 'ModalNotification']) }; scope.onsuccess = function(jresp) { + + if (jresp.request.status !== "success") { + Notification.warning ({message: jresp.request.info, delay: 5000}); + scope.offline(); + return; + } + if (jresp.request.token) AppConfig.session.token = jresp.request.token; if (jresp.request.uuid) AppConfig.session.uuid = jresp.request.uuid; if (jresp.request.timeout) AppConfig.session.timeout = jresp.request.timeout; @@ -81,9 +88,9 @@ angular.module('TokenRefresh', ['AppConfig', 'ModalNotification']) // Check Binder status scope.getping = function() { - AppCall.get ("token", "ping", {/*query*/},function(result) { - if (result.status === 200) scope.onsuccess (result.data); - else scope.onerror(); + AppCall.get ("token", "ping", {/*query*/},function(jresp, errcode) { + if (errcode) scope.onerror(); + else scope.onsuccess (jresp); // restart a new timer for next ping $timeout (scope.getping, AppConfig.session.pingrate*1000); }); @@ -92,9 +99,9 @@ angular.module('TokenRefresh', ['AppConfig', 'ModalNotification']) // Check Binder status scope.refresh = function() { - AppCall.get ("token", "refresh", {/*query*/},function(result) { - if (result.status === 200) scope.onsuccess (result.data); - else scope.onerror(); + AppCall.get ("token", "refresh", {/*query*/},function(jresp, errcode) { + if (errcode) scope.onerror(); + else scope.onsuccess (jresp); // restart a new timer for next refresh $timeout (scope.refresh, AppConfig.session.timeout *250); }); @@ -103,9 +110,9 @@ angular.module('TokenRefresh', ['AppConfig', 'ModalNotification']) // Initial connection scope.tkcreate = function() { - AppCall.get ("token", "create", {token: AppConfig.session.initial},function(result) { - if (result.status === 200) scope.onsuccess (result.data); - else scope.onerror(); + AppCall.get ("token", "create", {token: AppConfig.session.initial},function(jresp, errcode) { + if (errcode) scope.onerror(); + else scope.onsuccess (jresp); }); }; @@ -133,3 +140,4 @@ angular.module('TokenRefresh', ['AppConfig', 'ModalNotification']) })(); console.log ("Token Refresh Loaded"); + -- 2.16.6