This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (72 loc) · 2.59 KB
/
app.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
const client = require('./config')
const db = require('./firebase')
const TOKEN = 'g0rdDIx8YDOvmV7xewGddHMssy2URxgBL9Yx0b7pb7P'
const lineNotify = require('line-notify-nodejs')(TOKEN)
let temp
client.on('message', (topic, payload, packet) => {
const date = Date.now()
if (topic === 'kmutnb-iot-2563-project/temp') {
temp = parseFloat(payload)
db.collection('temp')
.doc(date.toString())
.set({
temperature: temp,
timestamp: date,
})
.catch((err) => {
console.error(err)
})
console.log(`Temp = ${parseFloat(payload)}`)
}
if (topic === 'kmutnb-iot-2563-project/fanStatus') {
if (temp > 35) {
if (payload.toString() === 'OFF') {
client.publish(
'kmutnb-iot-2563-project/fanSwitch',
'ON',
() => {
db.collection('fan')
.add({
system: 'ON',
timestamp: date,
})
.catch((err) => {
console.error(err)
})
lineNotify
.notify({
message: `\n🔥 Temp = ${temp} \n🟢 Fan Status = ON`,
})
.then(() => {
console.log('Send Complete')
})
console.log(`Turn ON 🟢`)
}
)
}
} else {
if (payload.toString() === 'ON') {
client.publish(
'kmutnb-iot-2563-project/fanSwitch',
'OFF',
() => {
db.collection('fan').doc(date.toString()).set({
system: 'OFF',
timestamp: date,
})
lineNotify
.notify({
message: `\n🥶 Temp = ${temp} \n🔴 Fan Status = OFF`,
})
.then(() => {
console.log('Send Complete')
})
console.log(`Turn OFF 🔴`)
}
)
}
}
console.log(`Now Fan : ${payload}.....🛠`)
}
})
client.subscribe('kmutnb-iot-2563-project/+')