freifunkmanager/public/components/store.js

158 lines
4.9 KiB
JavaScript
Raw Normal View History

2016-07-06 22:52:31 +02:00
'use strict';
angular.module('ffhb')
2016-07-13 11:43:27 +02:00
.factory('store', function($state, $q, $http, $rootScope,config,$interval,storage,webNotification) {
2016-07-06 22:52:31 +02:00
function notifyNew(nodeid){
webNotification.showNotification('New Node',{
body: '"'+nodeid+'"',
icon: '/favicon.ico',
onClick: function() {
$state.go('app.node', {nodeid: nodeid});
}
},function(){});
}
function notifyOffline(nodeid){
webNotification.showNotification('Offline Node',{
body: '"'+nodeid+'"',
icon: '/favicon.ico',
onClick: function() {
$state.go('app.node', {nodeid: nodeid});
}
},function(){});
}
var myservice = {}, autorefresher;
2016-07-06 22:52:31 +02:00
myservice._initialized = false;
2016-07-13 11:43:27 +02:00
myservice._data = storage.get('data') ||{
2016-07-06 22:52:31 +02:00
nodes: {},nodesCount:0,
2016-07-09 02:58:47 +02:00
merged: {},
2016-07-06 22:52:31 +02:00
aliases: {},aliasesCount:0
};
var geojsonDeferred = $q.defer();
$http.get(config.geojson).success(function(geojson) {
geojsonDeferred.resolve(geojson);
});
myservice.getGeojson = geojsonDeferred.promise;
2016-07-09 02:58:47 +02:00
myservice.refresh = function(notify) {
2016-07-06 22:52:31 +02:00
var dataDeferred = $q.defer();
$http.get(config.api+'/nodes').success(function(nodes) {
$http.get(config.api+'/aliases').success(function(aliases) {
2016-07-09 02:58:47 +02:00
if(notify){
Object.keys(nodes).map(function(key){
if(myservice._data.nodes === undefined || myservice._data.nodes[key] === undefined){
notifyNew(key);
}
if(myservice._data.nodes !== undefined && myservice._data.nodes[key] !== undefined && myservice._data.nodes[key].flags.offline){
notifyOffline(key);
}
myservice._data.nodes[key] = nodes[key];
});
}else {
myservice._data.nodes = nodes;
}
2016-07-06 22:52:31 +02:00
angular.copy(nodes, myservice._data.merged);
Object.keys(aliases).map(function(key){
var node = myservice._data.merged[key],
2016-07-06 22:52:31 +02:00
alias = aliases[key];
node.nodeinfo.hostname = alias.hostname;
if(!node.nodeinfo.owner){
node.nodeinfo.owner = {};
}
node.nodeinfo.owner.contact = alias.owner;
if(!node.nodeinfo.wireless){
node.nodeinfo.wireless = {};
}
if(alias.wireless){
if(alias.wireless.channel24){
node.nodeinfo.wireless.channel24 = alias.wireless.channel24;
}
if(alias.wireless.channel5){
node.nodeinfo.wireless.channel5 = alias.wireless.channel5;
}
if(alias.wireless.txpower24){
node.nodeinfo.wireless.txpower24 = alias.wireless.txpower24;
}
if(alias.wireless.txpower5){
node.nodeinfo.wireless.txpower5 = alias.wireless.txpower5;
}
}
if(!node.nodeinfo.location){
node.nodeinfo.location = {};
}
if(alias.location){
if(alias.location.latitude){
node.nodeinfo.location.latitude = alias.location.latitude;
}
if(alias.location.longitude){
node.nodeinfo.location.longitude = alias.location.longitude;
}
}
});
myservice._data.nodesCount = Object.keys(nodes).length || 0;
myservice._data.aliases = aliases;
myservice._data.aliasesCount = Object.keys(aliases).length || 0;
myservice._data.lastupdate = new Date();
dataDeferred.resolve(myservice._data);
if (myservice._initialized) {
$rootScope.$broadcast('store', dataDeferred.promise);
}
2016-07-13 11:43:27 +02:00
storage.set('data',myservice._data);
2016-07-06 22:52:31 +02:00
myservice._initialized = true;
});
});
myservice.getData = dataDeferred.promise;
return dataDeferred.promise;
};
2016-07-09 02:58:47 +02:00
myservice.refresh(false);
2016-07-06 22:52:31 +02:00
myservice.saveNode = function(nodeid){
var result = $q.defer();
if(myservice._data.merged && myservice._data.merged[nodeid]){
var node = myservice._data.merged[nodeid],
obj = {
'hostname':node.nodeinfo.hostname,
'owner':node.nodeinfo.owner.contact||'',
'location': {},
'wireless': {}
};
if(node.nodeinfo.location){
obj.location = {
'latitude': node.nodeinfo.location.latitude||0,
'longitude': node.nodeinfo.location.longitude||0
};
}
if(node.nodeinfo.wireless){
obj.wireless = {
2016-07-12 08:44:38 +02:00
'channel24': parseInt(node.nodeinfo.wireless.channel24)||0,
'channel5': parseInt(node.nodeinfo.wireless.channel5)||0,
'txpower24': parseInt(node.nodeinfo.wireless.txpower24)||0,
'txpower5': parseInt(node.nodeinfo.wireless.txpower5)||0
};
}
$http.post(config.api+'/aliases/alias/'+nodeid,obj).then(function(){
2016-07-06 22:52:31 +02:00
result.resolve(true);
2016-07-09 02:58:47 +02:00
myservice.refresh(true);
},function(){
2016-07-11 16:24:51 +02:00
$rootScope.globals.passphrase = '';
2016-07-06 22:52:31 +02:00
});
}else{
result.resolve(false);
}
return result.promise;
};
myservice.autorefresh = function(time){
if(autorefresher !== undefined){
2016-07-16 16:12:27 +02:00
$interval.cancel(autorefresher);
}
if(time){
autorefresher = $interval(function () {
myservice.refresh(true);
}, time);
}
};
2016-07-16 16:12:27 +02:00
$rootScope.autorefresh = config.refresh;
myservice.autorefresh($rootScope.autorefresh);
2016-07-06 22:52:31 +02:00
return myservice;
});