forked from balena-io-examples/google-iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (65 loc) · 2.49 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
73
74
75
76
77
const GoogleIoT = require('./lib/GoogleIoT.js')
const MQTTBridge = require('./lib/MQTTBridge.js')
const fs = require('fs')
let google = new GoogleIoT({
device: process.env.RESIN_DEVICE_UUID,
gcpProject: process.env.GOOGLE_IOT_PROJECT,
gcpRegion: process.env.GOOGLE_IOT_REGION,
gcpRegistry: process.env.GOOGLE_IOT_REGISTRY,
gcpServiceAccount: process.env.GOOGLE_IOT_SERVICE_ACCOUNT_TOKEN,
})
let mqtt = new MQTTBridge({
device: process.env.RESIN_DEVICE_UUID,
gcpProject: process.env.GOOGLE_IOT_PROJECT
})
async function start () {
// Ensure our device is properly registered and configured on Google IoT Core cloud service.
await google.registerDevice()
// Configure MQTT client before connecting to IoT Core server
mqtt.setClientId(google.getDevicePath()) // Get full path to device on IoT Core
await mqtt.connect()
// Optional callback, fired every time we receive a message on any topic
mqtt.setMessageCallback((topic, message) => {
console.log(`Received message --- Topic: ${topic} - Message: `, Buffer.from(message, 'base64').toString('ascii'))
// Update the device state upon receiving a config message
// A good practise is to update the device state to reflect that the device got the config.
if (topic.endsWith('config')) {
mqtt.publishState({ deviceId: mqtt.device.id, timestamp: new Date().toString() })
}
})
// Publish device data every X seconds to Google IoT Core
setInterval(() => {
mqtt.publishTelemetry({
cpuLoad: getCpuLoad(),
memoryInfo: getMemoryInfo(),
})
}, 30 * 1000)
console.log('Startup completed!')
}
function getCpuLoad () {
// Will probably fail for non Pi systems
try {
var text = fs.readFileSync("/proc/loadavg", "utf8")
// get load for the last minute
const load = parseFloat(text.match(/(\d+\.\d+)\s+/)[ 1 ])
return load.toString()
} catch (error) {
return "Error getting CPU load"
}
}
function getMemoryInfo () {
// Will probably fail for non Pi systems
try {
var text = fs.readFileSync("/proc/meminfo", "utf8")
// Parse total and free memory from /proc/meminfo, and calculate percentage used
const matchTotal = text.match(/MemTotal:\s+([0-9]+)/)
const matchFree = text.match(/MemAvailable:\s+([0-9]+)/)
const total = parseInt(matchTotal[ 1 ], 10)
const free = parseInt(matchFree[ 1 ], 10)
const percentageUsed = Math.round((total - free) / total * 100)
return percentageUsed.toString()
} catch (error) {
return "Error getting Memory Info"
}
}
start()