-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigfox.js
87 lines (80 loc) · 2.78 KB
/
sigfox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const request = require('request-promise');
const moment = require('moment');
require('./loadConfig');
var SigfoxMsg = function(msg){
this.deviceId = msg.device;
this.frame = msg.data
this.time = msg.time * 1000;
this.date = moment(this.time).calendar();
this.linkQuality = msg.linkQuality;
this.callbacks = msg.cbStatus ? msg.cbStatus.map(function(cb){return cb.info;}) : null;
this.location = msg.location ? JSON.stringify(msg.location) : null;
};
function getRequest(method, path, body){
return request[method.toLowerCase()]({
uri:'http://backend.sigfox.com/api'+path,
json: true,
auth:{
user: process.env.SIGFOX_USERNAME,
pass: process.env.SIGFOX_PASSWORD
},
body:body
});
}
module.exports = {
getGroupMessages: function(filter){
return new Promise(function(resolve, reject){
this.getDeviceTypes(filter)
.then(function(deviceTypes){
console.log("✅ %s deviceTypes", deviceTypes.length);
deviceTypes = deviceTypes.filter(function(item){return item.name.match(filter)});
console.log("✅ %s filtered deviceTypes", deviceTypes.length);
console.log(deviceTypes)
Promise.all(deviceTypes.map(this.getDeviceTypeMessages))
.then(resolve)
.catch(function(err){
console.warn("🔴 Error retrieving messages")
console.log(err);
process.exit(0);
})
}.bind(this))
.catch(function(err){
console.log("🔴 getGroupMessages", err);
reject(err);
});
}.bind(this));
},
getDeviceTypes: function(filter){
return new Promise(function(resolve, reject){
getRequest('GET', '/devicetypes?includeSubGroups=true')
.then(function(result, err){
if (!result || !result.data || !result.data.length){
return reject(new Error("No devicetypes retrieved"));
}
var output = [];
result.data.forEach(function(item){output.push(item);});
resolve(output);
})
.catch(function(err){
console.log("🔴 getDT", err);
reject("sorry")
});
});
},
getDeviceTypeMessages: function(deviceType){
return new Promise(function(resolve,reject){
getRequest('GET', '/devicetypes/'+deviceType.id+'/messages')
.then(function(result){
if (!result || !result || !result.data.length){
var msg = "🔴 No messages for deviceType "+ deviceType.id;
console.log(msg);
return resolve({deviceType:deviceType, messages:[]});
}
console.log("✅ %s : %s messages", deviceType.id, result.data.length);
return resolve({deviceType:deviceType, messages:result.data.map(function(item){return new SigfoxMsg(item)})});
})
.catch(reject);
});
}
//messages.data.forEach(function(item){output.push(});
};