Added AFB monitoring and token auto-refresh
[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', [])
36
37     .directive ('tokenRefresh', function($timeout, $http, $location, Notification, ConfigApp) {
38
39     function mymethods(scope, elem, attrs) {
40         
41         scope.status=false;
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         // Check Binder status
54         scope.getping = function() {
55
56             var handler = $http.post(ConfigApp.session.ping+'?token='+ ConfigApp.session.token);
57             handler.success(function(response, errcode, headers, config) {
58                 if (!scope.status)  {
59                     Notification.success ({message: "AppFramework Binder Back to Live", delay: 3000});
60                     scope.online();
61                 }
62                 scope.status = 1;
63             });
64
65             handler.error(function(response, errcode, headers) {
66                 if (scope.status)  {
67                     Notification.warning ({message: "AppFramework Binder Lost", delay: 5000});
68                     scope.offline();
69                 }
70                 scope.status = 0;
71             });
72
73             // restart a new timer for next ping
74             $timeout (scope.getping, ConfigApp.session.pingrate*1000);
75         };
76         
77         // Check Binder status
78         scope.refresh = function() {
79             var handler = $http.post(ConfigApp.session.refresh+'?token='+ ConfigApp.session.token);
80             $timeout (scope.refresh, ConfigApp.session.timeout *250);
81         };
82  
83         scope.icon      = attrs.icon   || "fi-lightbulb";
84         scope.hostname  = $location.host();
85         scope.httpdport = $location.port();
86         
87         scope.getping();
88         scope.refresh();
89     }
90
91     return {
92         template: template,
93         scope: {
94             callback : "="
95         },
96         restrict: 'E',
97         link: mymethods
98     };
99 });
100
101 })();
102 console.log ("Token Refresh Loaded");
103