2016-07-06 22:52:31 +02:00
|
|
|
'use strict';
|
|
|
|
angular.module('ffhb')
|
|
|
|
.factory('store', function($state, $q, $http, $rootScope,config,$interval,$cookieStore,webNotification) {
|
|
|
|
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 = {};
|
|
|
|
myservice._initialized = false;
|
|
|
|
myservice._data = $cookieStore.get('data') ||{
|
|
|
|
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){
|
2016-07-11 10:06:12 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
$cookieStore.put('data',myservice._data);
|
|
|
|
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]){
|
2016-07-11 10:06:12 +02:00
|
|
|
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 = {
|
|
|
|
'channel24': node.nodeinfo.wireless.channel24||0,
|
|
|
|
'channel5': node.nodeinfo.wireless.channel5||0,
|
|
|
|
'txpower24': node.nodeinfo.wireless.txpower24||0,
|
|
|
|
'txpower5': 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);
|
2016-07-11 10:06:12 +02:00
|
|
|
},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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if(config.refresh){
|
|
|
|
$interval(function () {
|
2016-07-09 02:58:47 +02:00
|
|
|
myservice.refresh(true);
|
2016-07-06 22:52:31 +02:00
|
|
|
}, config.refresh);
|
|
|
|
}
|
|
|
|
|
|
|
|
return myservice;
|
|
|
|
});
|