-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (66 loc) · 2.63 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
63
64
65
66
67
68
69
70
71
72
// mqtt設定
var mqtt = require('mqtt');
var opt = {
port: 1883,
clientId: 'nodejs1',
username: 'PK5WSEH0SBUA13XT4K',
password: 'PK5WSEH0SBUA13XT4K'
};
// sql設定
var mysql = require('mysql');
var conn = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
// database: 'lcm',
database: 'laravel'
});
// 日期取得
var sd = require('silly-datetime');
// 連接MQTT
var hostname = 'tcp://61.58.248.108';
var client = mqtt.connect(hostname, opt);
client.on('connect', function () {
console.log('已連接至MQTT:' + hostname + '\n');
client.subscribe("lcmData/#");
});
// 接收訊息
client.on('message', function (topic, message) {
console.log('收到 ' + topic + ' 主題,訊息:' + message.toString());
console.log('時間 ' + sd.format(new Date, 'YYYY-MM-DD HH:mm:ss'));
dataProcess(message);
});
//資料處理
function dataProcess(message) {
var json = JSON.parse(message);
var ip = json.deviceIP;
let now_date = sd.format(new Date, 'YYYY-MM-DD HH:mm:ss');
if (ip != null) {
var mac = json.deviceMAC;
var color = json.color;
console.log(mac + '\n');
var sql = 'SELECT COUNT(`ip`) as num FROM `lcm_infos` WHERE ip = ?';
var params = [ip];
conn.query(sql, params, function (err, result) {
if (err) throw err;
if (result[0].num == 0) {
sql = 'INSERT INTO `lcm_infos`(`ip`, `created_at`, `updated_at`) VALUES (?, ?, ?)';
params = [ip, now_date, now_date];
conn.query(sql, params, function (err, result) {
if (err) throw err;
sql = 'INSERT INTO `lcm_status`(`lcm_ip`, `lcm_mac`, `color`, `created_at`, `updated_at`)' +
'VALUES (?, ?, "WHITE", ?, ?), (?, ?, "BLACK", ?, ?), (?, ?, "RED", ?, ?), (?, ?, "GREEN", ?, ?), (?, ?, "BLUE", ?, ?)';
params = [ip, mac, now_date, now_date, ip, mac, now_date, now_date, ip, mac, now_date, now_date, ip, mac, now_date, now_date, ip, mac, now_date, now_date];
conn.query(sql, params, function (err, result) {
if (err) throw err;
});
});
}
sql = 'UPDATE `lcm_status` SET `lcm_power` = ?, `lcm_current` = ?, `backlight_power` = ?, `backlight_current` = ?, `updated_at` = ? WHERE `lcm_ip` = ? AND `color` = ?';
params = [json.data[0], json.data[1], json.data[2], json.data[3], now_date, ip, color];
conn.query(sql, params, function (err, result) {
if (err) throw err;
});
});
}
}