-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer_side
More file actions
95 lines (71 loc) · 2.08 KB
/
Server_side
File metadata and controls
95 lines (71 loc) · 2.08 KB
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
import * as mqtt from 'mqtt'
const logger = Logger.getInstance();
var Topic = 'topic/pub'; //subscribe to all topics
var Broker_URL = 'dns:15669';
//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----
import {InfluxDB, Point, HttpError} from '@influxdata/influxdb-client'
import tap = Mocha.reporters.tap;
const token = ''
const org = ''
const bucket = ''
const influxDBClient = new InfluxDB({url: '', token: token});
//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----//----
async function sendPackage(data: any): Promise<any> {
const writeApi = influxDBClient.getWriteApi(org, bucket);
writeApi.useDefaultTags({host: 'test_erick'});
const point = new Point('rpm')
.intField("RPM", parseFloat(data));
writeApi.writePoint(point);
await writeApi.close()
}
let client = mqtt.connect('mqtt://dns:ppt', {
username: "user",
password: "pass",
port: 15629
});
client.on('connect', mqtt_connect);
client.on('reconnect', mqtt_reconnect);
client.on('error', mqtt_error);
client.on('message', mqtt_messsageReceived);
client.on('close', mqtt_close);
function mqtt_connect() {
console.log("Connecting MQTT");
client.subscribe(Topic, mqtt_subscribe);
}
function mqtt_subscribe(err, granted) {
console.log("Subscribed to " + Topic);
if (err) {
console.log(err);
}
}
function mqtt_reconnect(err) {
console.error(err);
console.log("Reconnect MQTT");
if (err) {
console.log(err);
}
client = mqtt.connect(Broker_URL, {
username: "user",
password: "pass",
port: 15629
});
}
function mqtt_error(err) {
console.log("Error!");
if (err) {
console.log(err);
}
}
function after_publish() {
//do nothing
}
function mqtt_messsageReceived(topic, message, packet) {
const data = JSON.parse(message)["DATA"];
logger.info("new collect", data);
gambi(data).then(() => {
}).catch(() => {
})
}
function mqtt_close() {
console.log("Close MQTT");
}