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