This repository was archived by the owner on Apr 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Add MQTT service & sample bundle #265
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d30a741
added mqtt client service and sample
Nils-witt fbd5ddd
fixed lint problems
Nils-witt f4590ad
updated schema
Nils-witt 14fcc19
updated schema
Nils-witt ca6b57d
updated package.json
Nils-witt f1746de
Merge branch 'codeoverflow-org:master' into master
Nils-witt c304f00
updated package.json
Nils-witt 5dcbb93
Merge remote-tracking branch 'origin/master'
Nils-witt a0103f1
Fixed to close the client connection after config validation
Nils-witt 0c7341d
Removed unnecessary lines
Nils-witt 86c2b0f
Apply suggestions from code review
Nils-witt 8f134a4
Removed try catch and shortened callbacks
Nils-witt 240e33c
Harmonize event handler shortcuts in mqtt service
hlxid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { NodeCG } from "nodecg-types/types/server"; | ||
import { Result, emptySuccess, success, ServiceBundle, error } from "nodecg-io-core"; | ||
import { MqttClient, connect } from "mqtt"; | ||
|
||
interface MQTTClientServiceConfig { | ||
address: string; | ||
topics: [string]; | ||
username?: string; | ||
password?: string; | ||
} | ||
|
||
export class MQTTClientServiceClient { | ||
client: MqttClient; | ||
once: (event: string, cb: () => void) => void; | ||
close: () => void; | ||
on: (event: string, cb: () => void) => void; | ||
off: (event: string | symbol, listener: (...args: unknown[]) => void) => void; | ||
|
||
connect(url: string, username: string | undefined, password: string | undefined): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
this.client = connect(url, { | ||
username: username, | ||
password: password, | ||
}); | ||
this.client.on("error", (err: Error) => { | ||
this.client.end(); | ||
reject(err.message); | ||
}); | ||
this.client.on("connect", resolve); | ||
|
||
this.once = this.client.once; | ||
this.on = this.client.on; | ||
this.close = this.client.end; | ||
this.off = this.client.off; | ||
}); | ||
} | ||
|
||
subscribe(topics: string[]): void { | ||
topics.forEach((topic: string) => { | ||
this.client.subscribe(topic); | ||
}); | ||
} | ||
|
||
onClose(func: () => void): MqttClient { | ||
return this.client.on("close", func); | ||
} | ||
|
||
onMessage(func: (topic: string, message: Buffer) => void): MqttClient { | ||
return this.client.on("message", func); | ||
} | ||
|
||
onError(func: (error: Error) => void): MqttClient { | ||
return this.client.on("error", func); | ||
} | ||
} | ||
|
||
module.exports = (nodecg: NodeCG) => { | ||
new MQTTClientService(nodecg, "mqtt-client", __dirname, "../mqtt-schema.json").register(); | ||
}; | ||
|
||
class MQTTClientService extends ServiceBundle<MQTTClientServiceConfig, MQTTClientServiceClient> { | ||
async validateConfig(config: MQTTClientServiceConfig): Promise<Result<void>> { | ||
const client = new MQTTClientServiceClient(); | ||
|
||
await client.connect(config.address, config.username, config.password); | ||
client.close(); | ||
return emptySuccess(); | ||
} | ||
|
||
async createClient(config: MQTTClientServiceConfig): Promise<Result<MQTTClientServiceClient>> { | ||
const client = new MQTTClientServiceClient(); | ||
await client.connect(config.address, config.username, config.password); | ||
client.subscribe(config.topics); | ||
this.nodecg.log.info("Successfully connected to the MQTT server."); | ||
return success(client); | ||
} | ||
|
||
stopClient(client: MQTTClientServiceClient): void { | ||
if (client.client.connected) { | ||
client.close(); | ||
} | ||
} | ||
|
||
removeHandlers(client: MQTTClientServiceClient): void { | ||
client.client.removeAllListeners(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"additionalProperties": false, | ||
"properties": { | ||
"address": { | ||
"type": "string", | ||
"description": "The address of the server" | ||
}, | ||
"password": { | ||
"type": "string", | ||
"description": "The password to connect to the server" | ||
}, | ||
"username": { | ||
"type": "string", | ||
"description": "The username to connect to the server" | ||
}, | ||
"topics": { | ||
"type": "array", | ||
"description": "Array of topic you want to subscribe to" | ||
} | ||
}, | ||
"required": ["address", "topics"] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "nodecg-io-mqtt-client", | ||
"version": "0.2.0", | ||
"description": "Allows you to connect with an MQTT server.", | ||
"homepage": "https://nodecg.io/RELEASE/samples/mqtt-client", | ||
"author": { | ||
"name": "Nils Witt", | ||
"url": "https://github.com/Nils-witt" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/codeoverflow-org/nodecg-io.git", | ||
"directory": "nodecg-io-mqtt" | ||
}, | ||
"files": [ | ||
"**/*.js", | ||
"**/*.js.map", | ||
"**/*.d.ts", | ||
"*.json" | ||
], | ||
"main": "extension/index", | ||
"scripts": { | ||
"build": "tsc -b", | ||
"watch": "tsc -b -w", | ||
"clean": "tsc -b --clean" | ||
}, | ||
"keywords": [ | ||
"nodecg-io", | ||
"nodecg-bundle" | ||
], | ||
"nodecg": { | ||
"compatibleRange": "^1.1.1", | ||
"bundleDependencies": { | ||
"nodecg-io-core": "^0.2.0" | ||
} | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/node": "^15.0.2", | ||
"nodecg-types": "^1.8.2", | ||
"typescript": "^4.2.4" | ||
}, | ||
"dependencies": { | ||
"nodecg-io-core": "^0.2.0", | ||
"mqtt": "^4.2.8" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../tsconfig.common.json" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NodeCG } from "nodecg-types/types/server"; | ||
import { requireService } from "nodecg-io-core"; | ||
import { MQTTClientServiceClient } from "nodecg-io-mqtt-client"; | ||
|
||
module.exports = function (nodecg: NodeCG) { | ||
nodecg.log.info("Sample bundle for mqtt-client started"); | ||
|
||
const service = requireService<MQTTClientServiceClient>(nodecg, "mqtt-client"); | ||
|
||
service?.onAvailable((client) => { | ||
nodecg.log.info("Client has been updated, waiting for messages."); | ||
|
||
client.onMessage((topic: string, message: Buffer) => { | ||
nodecg.log.info(`recieved message "${message.toString()}" "${topic}"`); | ||
}); | ||
}); | ||
|
||
service?.onUnavailable(() => { | ||
nodecg.log.info("Client has been unset."); | ||
}); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "mqtt-client", | ||
"version": "0.2.0", | ||
"private": true, | ||
"nodecg": { | ||
"compatibleRange": "^1.1.1", | ||
"bundleDependencies": { | ||
"nodecg-io-mqtt-client": "^0.2.0" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "tsc -b", | ||
"watch": "tsc -b -w", | ||
"clean": "tsc -b --clean" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"@types/node": "^15.0.2", | ||
"nodecg-types": "^1.8.2", | ||
"nodecg-io-core": "^0.2.0", | ||
"nodecg-io-mqtt-client": "^0.2.0", | ||
"typescript": "^4.2.4" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../tsconfig.common.json" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.