-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTTserver.js
150 lines (130 loc) · 4.57 KB
/
MQTTserver.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var mosca = require('mosca');
var Mongo = require('./MongoDB');
var dateFormat = require('dateformat');
function pubNewIntrval(bid,value){
server.publish({
topic: bid+'/new_inter',
payload: Buffer.from(value),
qos: 1, // this is important for offline messaging
retained: true
}, null, function done() {})
}
function pubNewHBrange(bid,min_hb,max_hb){
server.publish({
topic: bid+'/new_hb_range',
payload: Buffer.from(min_hb+"-"+max_hb),
qos: 1, // this is important for offline messaging
retained: true
}, null, function done() {})
}
var ascoltatore = {
//using ascoltatore
type: 'mongo',
url: 'mongodb://localhost:27017/mqtt',
pubsubCollection: 'ascoltatori',
mongo: {}
};
var settings = {
port: 1883,
backend: ascoltatore
};
var server = new mosca.Server(settings);
var authenticate = function(client, username, password, callback) {
client.username = username;
if(username[0]!='*'){//If starts with * is an user app client
Mongo.AUTH_bracelet(client.id,password).then((result)=>{
var authorized = result!=null
if (!authorized) console.log("Client autentication faild");
callback(null, authorized);
});
}else{
Mongo.APP_auth(client.id,password.toString()).then((result)=>{
var authorized = result!=null
if (!authorized) console.log("Client autentication faild");
callback(null, authorized);
});
}
}
var authorizeSubscribe = function(client, topic, callback) {
if( client.username[0]!='*' && topic.split('/')[1]=="new_inter"){
var authorized = client.id == topic.split('/')[0]
if (!authorized) console.log("1.Unauthorized Subscribe for "+topic);
else{
Mongo.APP_getBrcInfo(client.id).then((result)=>{
if (result!=null) pubNewIntrval(client.id,result.interval.toString());
})
callback(null, authorized);
}
}
if( client.username[0]!='*' && topic.split('/')[1]=="new_hb_range"){
var authorized = client.id == topic.split('/')[0]
if (!authorized) console.log("1.Unauthorized Subscribe for "+topic);
else{
Mongo.APP_getBrcInfo(client.id).then((result)=>{
if (result!=null) pubNewHBrange(client.id,result.min_hb.toString(),result.max_hb.toString());
})
callback(null, authorized);
}
}
if( client.username[0]!='*' && topic.split('/')[1]=="new_measure"){
var authorized = client.id == topic.split('/')[0]
if (!authorized) console.log("1.Unauthorized Subscribe for "+topic);
callback(null, authorized);
}
if( client.username[0]=='*' && (topic.split('/')[1]=="alert" || topic.split('/')[1]=="alert_hb")){
Mongo.APP_checkIsMyBrc(client.id,topic.split('/')[0]).then((result)=>{
var authorized = result!=null
if (!authorized) console.log("2.Unauthorized Subscribe for "+topic);
callback(null, authorized);
});
}
}
var authorizePublish = function(client, topic, payload, callback) {
if(client.username[0]!='*' && topic.split('/')[1]=="new_measure"){
var authorized = client.id == topic.split('/')[0]
if(authorized)
Mongo.BRC_addMeasurement(client.id,payload.toString())
else
console.log("1.Unauthorized Publish! "+topic);
callback(null, authorized);
}
if(client.username[0]!='*' && topic.split('/')[1]=="alert"){
console.log("ALERT "+payload)
var authorized = client.id == topic.split('/')[0]
if(!authorized) console.log("1.Unauthorized Publish! "+topic);
else{
var now = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
var msg = "Alert type: BUTTON Date: "+now.toString();
Mongo.BRC_addAlert(client.id, msg);
}
callback(null, authorized);
}
if(client.username[0]!='*' && topic.split('/')[1]=="alert_hb"){
console.log("ALERT "+payload)
var authorized = client.id == topic.split('/')[0]
if(!authorized) console.log("1.Unauthorized Publish! "+topic);
else{
var now = new Date();
dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
var msg = "Alert type: BPM Date: "+now.toString();
Mongo.BRC_addAlert(client.id, msg);
}
callback(null, authorized);
}
}
server.on('clientConnected', function(client) {
console.log('client connected', client.id);
});
// fired when a message is received
server.on('published', function(packet, client) {
console.log('Published', packet.topic);
});
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
server.authenticate = authenticate;
server.authorizePublish = authorizePublish;
server.authorizeSubscribe = authorizeSubscribe;
console.log('Mosca server is up and running');
}