-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·62 lines (53 loc) · 1.71 KB
/
index.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
#!/bin/node
const config = require('./config');
const loraLib = require('./lib/lora-lib');
const {ERROR, Log, Models, MQClient, dbClient} = loraLib;
const JoinHandler = require('./lib/joinHandler');
const bluebird = require('bluebird');
const util = require('util');
const db = {
MySQLClient: dbClient.createSequelizeClient(config.database.mysql),
};
//Initialization
const modelIns = {
MySQLModel: {},
};
for (let model in Models.MySQLModels) {
modelIns.MySQLModel[model] = new Models.MySQLModels[model](db.MySQLClient);
}
const log = new Log(config.log);
const mqClient = new MQClient(config.mqClient_js, log);
const handleMessage = () => {
mqClient.message((message) => {
const joinHandler = new JoinHandler(modelIns, config, log);
const data = message.value.rxpk.data;
joinHandler.handler(message.value.rxpk)
.then((acptPHYPayload) => {
message.value.rxpk.data = acptPHYPayload;
return mqClient.publish(config.mqClient_js.producer.joinServerTopic, message.value);
})
.catch((error) => {
log.error(error.stack);
if (error instanceof ERROR.MICMismatchError) {
log.error(error.message);
} else if (error instanceof ERROR.DeviceNotExistError) {
log.error(error.message);
} else if (error instanceof ERROR.InvalidMessageError) {
log.error(error.message);
} else {
log.error(error.stack);
}
});
});
return bluebird.resolve(null);
};
const logBasicMessage = () => {
log.info(`LoRa join server starts to listen topic: ${config.mqClient_js.consumerGroup.topics}`);
return bluebird.resolve();
};
// Run
mqClient.connect().then(logBasicMessage)
.then(handleMessage)
.catch((error) => {
return log.error(error.message);
});