Restore basic functionnalities with new API
authorFulup Ar Foll <fulup@iot.bzh>
Tue, 24 May 2016 20:08:43 +0000 (22:08 +0200)
committerFulup Ar Foll <fulup@iot.bzh>
Tue, 24 May 2016 20:08:43 +0000 (22:08 +0200)
afb-client/app/Frontend/etc/AppConfig.js
afb-client/app/Frontend/pages/Home/Home.html
afb-client/app/Frontend/pages/Home/HomeModule.js
afb-client/app/Frontend/widgets/Notifications/TokenRefreshSvc.js

index 5c9283e..c8f1abe 100644 (file)
                     },
                                         
                     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
             
             // 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;
index d3c6719..df20ee2 100644 (file)
@@ -11,15 +11,15 @@ animationIn: slideInRight
   App Framework Binder Simple Client
 </h3>
 
-<token-refresh autolog="true"></token-refresh>
+<token-refresh autolog="true" callback="ctrl.Initialised"></token-refresh>
 
 
 <div class="button-box box-content ">
       
-    <submit-button class="session-button {{ctrl.APIcreate}}"  icon="fi-unlock" label="Open" clicked="ctrl.OpenSession" ></submit-button>
-    <submit-button class="session-button {{ctrl.APIcheck}}" icon="fi-checkbox" label="Check" clicked="ctrl.CheckSession" ></submit-button>
-    <submit-button class="session-button {{ctrl.APIrefresh}}"  icon="fi-arrows-compress" label="Refresh"   clicked="ctrl.RefreshSession" ></submit-button>
-    <submit-button class="session-button {{ctrl.APIreset}}" icon="fi-lock" label="Close" clicked="ctrl.ResetSession" ></submit-button>
+    <submit-button class="session-button {{ctrl.class.create}}"  icon="fi-unlock" label="Open" clicked="ctrl.OpenSession" ></submit-button>
+    <submit-button class="session-button {{ctrl.class.check}}" icon="fi-checkbox" label="Check" clicked="ctrl.CheckSession" ></submit-button>
+    <submit-button class="session-button {{ctrl.class.refresh}}"  icon="fi-arrows-compress" label="Refresh"   clicked="ctrl.RefreshSession" ></submit-button>
+    <submit-button class="session-button {{ctrl.class.reset}}" icon="fi-lock" label="Close" clicked="ctrl.ResetSession" ></submit-button>
     
 </div>
 <div class="message-box box-content vertical grid-frame">
index fdbe520..b082ee0 100644 (file)
@@ -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");
index ec29978..4dff4c9 100644 (file)
@@ -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");
-