Added Autolog to TokenRefresh Widget
[src/app-framework-demo.git] / afb-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', ['ConfigApp', 'ModalNotification'])
36
37     .directive ('tokenRefresh', function($timeout, $http, $location, Notification, ConfigApp) {
38
39     function mymethods(scope, elem, attrs) {
40         scope.status=undefined; // neither thu neither false
41         
42     
43         scope.online = function () {
44             elem.addClass    ("online");
45             elem.removeClass ("offline");
46         };
47
48         scope.offline = function(){
49             elem.addClass    ("offline");
50             elem.removeClass ("online");
51         };
52         
53         scope.onerror = function(data, errcode, headers) {
54             if (scope.status !== false)  {
55                 Notification.warning ({message: "AppFramework Binder Lost", delay: 5000});
56                 scope.offline();
57             }
58             scope.status = 0;
59         };
60         
61         scope.onsuccess = function(data, errcode, headers, config) {
62             if (scope.status !== true)  {
63                 if (data.request.token) ConfigApp.session.token = data.request.token;
64                 if (data.request.uuid)  ConfigApp.session.uuid  = data.request.uuid;
65                 if (data.request.timeout)  ConfigApp.session.timeout  = data.request.timeout;
66
67                 Notification.success ({message: "AppFramework Binder Back to Live", delay: 3000});
68                 scope.online();
69             }
70             scope.status = 1;
71         };
72
73         // Check Binder status
74         scope.getping = function() {
75
76             var handler = $http.post(ConfigApp.session.ping+'?token='+ ConfigApp.session.token);
77             
78             // process success and error
79             handler.success(scope.onsuccess);
80             handler.error(scope.onerror);
81
82             // restart a new timer for next ping
83             $timeout (scope.getping, ConfigApp.session.pingrate*1000);
84         };
85         
86         // Check Binder status
87         scope.refresh = function() {
88             var handler = $http.post(ConfigApp.session.refresh+'?token='+ ConfigApp.session.token);
89             
90             // process success and error
91             handler.success(scope.onsuccess);
92             handler.error(scope.onerror);
93             // restart a new timer for next refresh to 1/4 of timeout session
94             $timeout (scope.refresh, ConfigApp.session.timeout *250);
95         };
96         
97         // Initial connection
98         scope.tkcreate = function() {
99             var handler = $http.post(ConfigApp.session.create+'?token='+ ConfigApp.session.initial);
100             
101             // process success and error
102             handler.success(scope.onsuccess);
103             handler.error(scope.onerror);
104         };
105  
106         scope.icon      = attrs.icon   || "fi-lightbulb";
107         scope.hostname  = $location.host();
108         scope.httpdport = $location.port();
109         scope.autolog   = JSON.parse(attrs.autolog || false);
110         
111         if (scope.autolog) scope.tkcreate();
112
113         // Init ping and refresh process
114         $timeout (scope.getping, ConfigApp.session.pingrate*1000);
115         $timeout (scope.refresh, ConfigApp.session.timeout *250);
116     }
117
118     return {
119         template: template,
120         scope: {
121             callback : "="
122         },
123         restrict: 'E',
124         link: mymethods
125     };
126 });
127
128 })();
129 console.log ("Token Refresh Loaded");
130