16717482e05ad5ba5fd6d99463d6d803818f9c63
[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">'
29         + '<span class="afb-refresh-token" ng-click="getping" >afb://{{hostname}}:{{httpdport}}</span>'
30         + '<i class="{{icon}}"></i>'
31         + '</div>'
32         ;
33
34
35 // scope module is load statically before any route is cativated
36 angular.module('TokenRefresh', [])
37
38     .directive ('tokenRefresh', function($timeout, $http, $location, Notification, ConfigApp) {
39
40     function mymethods(scope, elem, attrs) {
41         
42         scope.status;
43     
44         scope.online = function () {
45             elem.addClass    ("online");
46             elem.removeClass ("offline");
47         };
48
49         scope.offline = function(){
50             elem.addClass    ("offline");
51             elem.removeClass ("online");
52         };
53
54         // Check Binder status
55         scope.getping = function() {
56
57             var handler = $http.get(ConfigApp.api.ping+'xx?token='+ ConfigApp.session.token);
58             handler.success(function(response, errcode, headers, config) {
59                 if (!scope.status)  {
60                     Notification.success ({message: "AFB Back to Live", delay: 3000});
61                     scope.online();
62                 }
63                 scope.status = 1;
64             });
65
66             handler.error(function(response, errcode, headers) {
67                 if (scope.status)  {
68                     Notification.warning ({message: "AFB Lost", delay: 5000});
69                     scope.offline();
70                 }
71                 scope.status = 0;
72             });
73
74             // restart a new timer for next ping
75             $timeout (scope.getping, ConfigApp.session.pingrate*1000);
76         };
77         
78         // Check Binder status
79         scope.refresh = function() {
80             var handler = $http.get(ConfigApp.api.refresh+'?token='+ ConfigApp.session.token);
81             $timeout (scope.refresh, ConfigApp.session.timeout *800);
82         };
83  
84         scope.icon      = attrs.icon   || "fi-lightbulb";
85         scope.hostname  = $location.host();
86         scope.httpdport = $location.port();
87         
88         scope.getping();
89         scope.refresh();
90     }
91
92     return {
93         template: template,
94         scope: {
95             callback : "="
96         },
97         restrict: 'E',
98         link: mymethods
99     };
100 });
101
102 })();
103 console.log ("Token Refresh Loaded");
104