Clean up to prepare new version of API
[src/app-framework-demo.git] / afm-client / app / Frontend / widgets / Notifications / TokenRefreshSvc.js
1 /*
2  alsa-gateway -- provide a REST/HTTP interface to ALSA-Mixer
3
4  Copyright (C) 2015, Fulup Ar Foll
5
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with scope program; if not, write to the Free Software
18  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20  References:
21
22  */
23
24 (function () {
25     'use strict';
26
27     var template =
28          '<div class="afb-monitor" ng-click="getping()">' +
29          '<span class="afb-refresh-token"  >afb://{{hostname}}:{{httpdport}}</span>' +
30          '<i class="{{icon}}"></i>' +
31          '</div>';
32
33
34 // scope module is load statically before any route is cativated
35 angular.module('TokenRefresh', ['AppConfig', 'ModalNotification'])
36
37     .directive ('tokenRefresh', function($log, $window, $timeout, $location, Notification, AppConfig, AppCall) {
38
39     function mymethods(scope, elem, attrs) {
40         scope.logged=undefined; // neither thu neither false
41         
42         $window.onbeforeunload = function () {
43             AppCall.get ("token", "reset", {/*query*/}, function () {    
44             $log.log("OPA exit");            
45             });
46         };
47                  
48         scope.online = function () {
49             elem.addClass    ("online");
50             elem.removeClass ("offline");
51             scope.logged=true;
52         };
53
54         scope.offline = function(){
55             elem.addClass    ("offline");
56             elem.removeClass ("online");
57             scope.logged=false;
58         };
59         
60         scope.onerror = function() {
61             if (scope.logged !== false)  {
62                 Notification.warning ({message: "AppFramework Binder Lost", delay: 5000});
63                 scope.offline();
64             }
65             scope.status = 0;
66         };
67         
68         scope.onsuccess = function(jresp, errcode) {
69             
70             if (errcode !== 200 || jresp.request.status !== "success") {
71                 Notification.warning ({message: "auto-connect :" + jresp.request.info, delay: 10000});
72                 scope.offline(); 
73                 return false;
74             }
75             
76             if (scope.logged !== true)  {
77                 Notification.success ({message: "AppFramework Binder Connected", delay: 3000});
78                 scope.online();
79                 if (scope.callback) scope.callback(jresp);
80             }
81             
82             scope.status = 1;            
83             return true;
84         };
85
86         // Check Binder status
87         scope.getping = function() {
88             
89             AppCall.get (scope.plugin, "ping", {/*query*/},function(jresp, errcode) {
90                 if (errcode !== 200 || jresp.request.status !== "success") {
91                     Notification.warning ({message: jresp.request.info, delay: 5000});
92                     scope.offline(); 
93                     return;
94                 }
95                 // restart a new timer for next ping
96                 $timeout (scope.getping, AppConfig.session.pingrate*1000);
97             }, scope.onerror);
98         };
99         
100         // Check Binder status
101         scope.refresh = function() {
102             
103             AppCall.get (scope.plugin, "refresh", {/*query*/}, function(jresp, errcode) {
104
105                 scope.onsuccess (jresp, errcode);
106                 
107                 // restart a new timer for next refresh
108                 $timeout (scope.refresh, AppConfig.session.timeout *250);
109             }, scope.onerror);            
110         };
111         
112         // Initial connection
113         scope.loggin = function() {            
114             AppCall.get (scope.plugin, "connect", {token: AppConfig.session.initial}, function(jresp, errcode) {
115                 
116                 if (!scope.onsuccess (jresp, errcode)) return;
117                 
118                 // Intial token was accepted let's start ping & refresh
119                 $timeout (scope.getping, AppConfig.session.pingrate*1000);
120                 $timeout (scope.refresh, AppConfig.session.timeout *250);
121  
122             }, scope.onerror);
123         };
124
125
126         // Parse Widget Parameters
127         scope.plugin    = attrs.plugin || "auth";
128         scope.icon      = attrs.icon   || "fi-lightbulb";
129         scope.hostname  = $location.host();
130         scope.httpdport = $location.port();
131         scope.autolog   = JSON.parse(attrs.autolog || false);
132         
133         // autostart log if requested
134         if (scope.autolog) scope.loggin();
135         
136     }
137
138     return {
139         template: template,
140         scope: {
141             callback : "="
142         },
143         restrict: 'E',
144         link: mymethods
145     };
146 });
147
148 })();
149 console.log ("Token Refresh Loaded");