From c8a7784c665e1571400853c0c91b9c47be3ce86f Mon Sep 17 00:00:00 2001 From: Fulup Ar Foll Date: Tue, 24 May 2016 22:08:43 +0200 Subject: Restore basic functionnalities with new API --- afb-client/app/Frontend/etc/AppConfig.js | 41 +++++++------ afb-client/app/Frontend/pages/Home/Home.html | 10 ++-- afb-client/app/Frontend/pages/Home/HomeModule.js | 67 ++++++++++------------ .../widgets/Notifications/TokenRefreshSvc.js | 63 ++++++++++---------- 4 files changed, 92 insertions(+), 89 deletions(-) (limited to 'afb-client/app/Frontend') diff --git a/afb-client/app/Frontend/etc/AppConfig.js b/afb-client/app/Frontend/etc/AppConfig.js index 5c9283e..c8f1abe 100644 --- a/afb-client/app/Frontend/etc/AppConfig.js +++ b/afb-client/app/Frontend/etc/AppConfig.js @@ -16,11 +16,6 @@ }, session: { // Those data are updated by session service - create : '/api/token/create', - refresh : '/api/token/refresh', - check : '/api/token/check', - reset : '/api/token/reset', - ping : '/api/token/check', initial : urlquery.token || '123456789', // typical dev initial token timeout : 3600, // timeout is updated client sessin context creation pingrate: 30, // Ping rate to check if server is still alive @@ -34,24 +29,36 @@ // Factory is a singleton and share its context within all instances. .factory('AppCall', function ($http, AppConfig, $log) { + var myCalls = { 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) { + var onerror = function(response) { + if (cberror) cberror(response.data, response.status, response.config); + else cbresponse(response.data, response.status, response.config); + }; + + var onsuccess =function(response) { + if (!response.data || !response.data.request) { + onerror (response); + return; + } + + var request=response.data.request; + // 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; + if (request.token) AppConfig.session.token = request.token; + if (request.uuid) AppConfig.session.uuid = request.uuid; + if (request.timeout) AppConfig.session.timeout = request.timeout; - cbresponse(data, errcode, headers, config); - }); + cbresponse(response.data, response.status, response.config); + }; + + + if (!query.token) query.token = AppConfig.session.token; // add token to provided query + if (!query.reqid) query.reqid = action; // use action as default requestID + var handle= $http.get('/api/' + plugin + '/' + action , {params: query}).then(onsuccess, onerror); - handle.onerror (function(data, errcode, headers, config) { - if (cberror) cberror(data, errcode, headers, config); - else cbresponse(data, errcode, headers, config); - }); } }; return myCalls; diff --git a/afb-client/app/Frontend/pages/Home/Home.html b/afb-client/app/Frontend/pages/Home/Home.html index d3c6719..df20ee2 100644 --- a/afb-client/app/Frontend/pages/Home/Home.html +++ b/afb-client/app/Frontend/pages/Home/Home.html @@ -11,15 +11,15 @@ animationIn: slideInRight App Framework Binder Simple Client - +
- - - - + + + +
diff --git a/afb-client/app/Frontend/pages/Home/HomeModule.js b/afb-client/app/Frontend/pages/Home/HomeModule.js index fdbe520..b082ee0 100644 --- a/afb-client/app/Frontend/pages/Home/HomeModule.js +++ b/afb-client/app/Frontend/pages/Home/HomeModule.js @@ -15,26 +15,24 @@ angular.module('HomeModule', ['SubmitButton', 'TokenRefresh','ModalNotification' console.log ("Home Controller"); - scope.ProcessResponse= function(data, errcode, headers, config) { - - if (data.request.status !== "success") { - Notification.error ({message: "Invalid API call:" + data.request.info , delay: 5000}); - return; - } + scope.OnResponse= function(jresp, errcode) { // Update UI response global display zone - scope.status = data.request.status; + scope.status = jresp.request.status; scope.errcode = errcode; - scope.request = data.request; - scope.response = data.response; + scope.request = jresp.request; + scope.response = jresp.response; + + if (jresp.request.status !== "success") { + Notification.error ({message: "Invalid API call:" + jresp.request.info , delay: 5000}); + scope.class [jresp.request.reqid]="fail"; + return; + } - switch (data.request.reqid) { - case 'open': + switch (jresp.request.reqid) { + case 'create': case 'reset': - scope.APIreset =''; - scope.APIcreate =''; - scope.APIrefresh=''; - scope.APIcheck =''; + scope.class={}; break; case 'refresh': @@ -42,56 +40,49 @@ angular.module('HomeModule', ['SubmitButton', 'TokenRefresh','ModalNotification' break; default: - Notification.error ({message: "Invalid RequestID:" + data.request.reqid , delay: 5000}); + Notification.error ({message: "Invalid RequestID:" + jresp.request.reqid , delay: 5000}); return; } - scope[reqid]="success"; - console.log ("OK: "+ JSON.stringify(data)); + // update button classes within home.html + scope.class [jresp.request.reqid]="success"; + console.log ("OK: "+ JSON.stringify(jresp)); }; - scope.ProcessError= function(data, errcode, headers, config) { - Notification.error ({message: "Invalid API:" + data.request.reqid , delay: 5000}); + scope.ProcessError= function(response, errcode, config) { + Notification.error ({message: "Invalid API:" + response.request.reqid , delay: 5000}); scope.status = "err-fx"; scope.errcode = errcode; - scope.request = data.request; + scope.request = response.request; scope.response = ""; - console.log ("FX: "+ JSON.stringify(data)); + console.log ("FX: "+ JSON.stringify(response)); }; scope.OpenSession = function() { console.log ("OpenSession"); - AppCall.get ("token", "create", {reqid:"open"}, scope.ProcessResponse, scope.InvalidApiCall); + AppCall.get ("token", "create", {/*query*/}, scope.OnResponse, scope.InvalidApiCall); }; scope.CheckSession = function() { console.log ("CloseSession"); + AppCall.get ("token", "check", {/*query*/}, scope.OnResponse, scope.InvalidApiCall); - var postdata= {/* any json your application may need */}; - var handler = $http.post(AppConfig.session.check + '?token='+AppConfig.session.token +'?idreq=open', postdata); - - handler.success(scope.ProcessResponse); - handler.error(scope.ProcessError); }; scope.RefreshSession = function() { console.log ("RefreshSession"); - var postdata= {/* any json your application may need */}; - var handler = $http.post(AppConfig.session.refresh + '?token='+AppConfig.session.token, postdata); - - handler.success(scope.ProcessResponse); - handler.error(scope.ProcessError); + AppCall.get ("token", "refresh", {/*query*/}, scope.OnResponse, scope.InvalidApiCall); }; scope.ResetSession = function() { console.log ("ResetSession"); - var postdata= {/* any json your application may need */}; - var handler = $http.post(AppConfig.session.reset + '?token='+AppConfig.session.token, postdata); - - handler.success(scope.ProcessResponse); - handler.error(scope.ProcessError); + AppCall.get ("token", "reset", {/*query*/}, scope.OnResponse, scope.InvalidApiCall); }; + scope.Initialised = function () { + scope.class = {create: "success"}; + } + }); console.log ("SampleControler Loaded"); diff --git a/afb-client/app/Frontend/widgets/Notifications/TokenRefreshSvc.js b/afb-client/app/Frontend/widgets/Notifications/TokenRefreshSvc.js index ec29978..4dff4c9 100644 --- a/afb-client/app/Frontend/widgets/Notifications/TokenRefreshSvc.js +++ b/afb-client/app/Frontend/widgets/Notifications/TokenRefreshSvc.js @@ -65,67 +65,73 @@ angular.module('TokenRefresh', ['AppConfig', 'ModalNotification']) scope.status = 0; }; - scope.onsuccess = function(jresp) { + scope.onsuccess = function(jresp, errcode) { - if (jresp.request.status !== "success") { + if (errcode !== 200 || jresp.request.status !== "success") { Notification.warning ({message: jresp.request.info, delay: 5000}); scope.offline(); - return; + return false; } - 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; - if (scope.logged !== true) { - Notification.success ({message: "AppFramework Binder Back to Live", delay: 3000}); + Notification.success ({message: "AppFramework Binder Connected", delay: 3000}); scope.online(); if (scope.callback) scope.callback(jresp); } - scope.status = 1; + + scope.status = 1; + return true; }; // Check Binder status scope.getping = function() { AppCall.get ("token", "ping", {/*query*/},function(jresp, errcode) { - if (errcode) scope.onerror(); - else scope.onsuccess (jresp); + if (errcode !== 200 || jresp.request.status !== "success") { + Notification.warning ({message: jresp.request.info, delay: 5000}); + scope.offline(); + return; + } // restart a new timer for next ping $timeout (scope.getping, AppConfig.session.pingrate*1000); - }); + }, scope.onerror); }; // Check Binder status scope.refresh = function() { - AppCall.get ("token", "refresh", {/*query*/},function(jresp, errcode) { - if (errcode) scope.onerror(); - else scope.onsuccess (jresp); + AppCall.get ("token", "refresh", {/*query*/}, function(jresp, errcode) { + + scope.onsuccess (jresp, errcode); + // restart a new timer for next refresh $timeout (scope.refresh, AppConfig.session.timeout *250); - }); + }, scope.onerror); }; // Initial connection - scope.tkcreate = function() { - - AppCall.get ("token", "create", {token: AppConfig.session.initial},function(jresp, errcode) { - if (errcode) scope.onerror(); - else scope.onsuccess (jresp); - }); - }; + scope.loggin = function() { + AppCall.get ("token", "create", {token: AppConfig.session.initial}, function(jresp, errcode) { + + if (!scope.onsuccess (jresp, errcode)) return; + + // Intial token was accepted let's start ping & refresh + $timeout (scope.getping, AppConfig.session.pingrate*1000); + $timeout (scope.refresh, AppConfig.session.timeout *250); + }, scope.onerror); + }; + + + // Parse Widget Parameters scope.icon = attrs.icon || "fi-lightbulb"; scope.hostname = $location.host(); scope.httpdport = $location.port(); scope.autolog = JSON.parse(attrs.autolog || false); - if (scope.autolog) scope.tkcreate(); - - // Init ping and refresh process - $timeout (scope.getping, AppConfig.session.pingrate*1000); - $timeout (scope.refresh, AppConfig.session.timeout *250); + // autostart log if requested + if (scope.autolog) scope.loggin(); + } return { @@ -140,4 +146,3 @@ angular.module('TokenRefresh', ['AppConfig', 'ModalNotification']) })(); console.log ("Token Refresh Loaded"); - -- cgit 1.2.3-korg