Skip to content

Commit 1163c48

Browse files
committed
Use Google Cloud IoT Core instead of CloudMQTT
1 parent d2808ad commit 1163c48

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ It is written in TypeScript and runs on [Google Cloud Run](https://cloud.google.
2222

2323
![home automation](./images/home_automation.png)
2424

25-
I have a Raspberry Pi set up which can [control lights](https://github.com/dtinth/hue.sh), [air conditioner](https://medium.com/@dtinth/remotely-turning-on-my-air-conditioner-through-google-assistant-1a1441471e9d), and [smart plugs](https://ifttt.com/services/kasa). It receives commands via [CloudMQTT](https://www.cloudmqtt.com/) and performs the action, then reports back to automatron via [its API](#cli-api).
25+
I have a Raspberry Pi set up which can [control lights](https://github.com/dtinth/hue.sh), [air conditioner](https://medium.com/@dtinth/remotely-turning-on-my-air-conditioner-through-google-assistant-1a1441471e9d), and [smart plugs](https://ifttt.com/services/kasa). It receives commands via [Google Cloud IoT Core](https://cloud.google.com/iot-core/), performs the action, and then reports back to automatron via [its API](#cli-api).
2626

2727
### expense tracking
2828

src/BotSecrets.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ export interface BotSecrets {
2525
SLACK_WEBHOOK_URL: string
2626
/** My user ID, so that the bot receives commands from me only */
2727
SLACK_USER_ID: string
28+
/** Google Cloud IoT Core device path to send */
29+
CLOUD_IOT_CORE_DEVICE_PATH: string
2830
}

src/HomeAutomation.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
import mqtt from 'mqtt'
1+
import * as iot from '@google-cloud/iot'
22
import { AutomatronContext } from './types'
33

4+
const iotClient = new iot.v1.DeviceManagerClient()
5+
46
export async function sendHomeCommand(
57
context: AutomatronContext,
68
cmd: string | string[]
79
) {
8-
var client = await getMQTTClient(context)
9-
if (Array.isArray(cmd)) {
10-
cmd.forEach(c => client.publish('home', c))
11-
} else {
12-
client.publish('home', cmd)
13-
}
14-
}
15-
16-
async function getMQTTClient(context: AutomatronContext) {
17-
const unsafeGlobal = global as any
18-
if (unsafeGlobal.automatronMqttPromise) {
19-
return unsafeGlobal.automatronMqttPromise
20-
}
21-
const promise = new Promise((resolve, reject) => {
22-
var client = mqtt.connect(context.secrets.MQTT_URL)
23-
client.on('connect', function() {
24-
resolve(client)
25-
})
26-
client.on('error', function(error) {
27-
reject(error)
28-
unsafeGlobal.automatronMqttPromise = null
10+
const cmds = Array.isArray(cmd) ? cmd : [cmd]
11+
const formattedName = context.secrets.CLOUD_IOT_CORE_DEVICE_PATH
12+
return Promise.all(
13+
cmds.map(async (command) => {
14+
const id =
15+
new Date().toJSON() +
16+
Math.floor(Math.random() * 10000)
17+
.toString()
18+
.padStart(2, '0')
19+
const commandMessage = JSON.stringify({
20+
id: id,
21+
topic: 'home',
22+
data: command,
23+
})
24+
const binaryData = Buffer.from(commandMessage)
25+
const request = {
26+
name: formattedName,
27+
binaryData: binaryData,
28+
}
29+
await iotClient.sendCommandToDevice(request)
2930
})
30-
})
31-
unsafeGlobal.automatronMqttPromise = promise
32-
return promise
31+
)
3332
}

0 commit comments

Comments
 (0)