-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #875 from tarun7singh/master
Added MQTT Monitor type
- Loading branch information
Showing
13 changed files
with
955 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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,16 @@ | ||
-- You should not modify if this have pushed to Github, unless it does serious wrong with the db. | ||
BEGIN TRANSACTION; | ||
|
||
ALTER TABLE monitor | ||
ADD mqtt_topic TEXT; | ||
|
||
ALTER TABLE monitor | ||
ADD mqtt_success_message VARCHAR(255); | ||
|
||
ALTER TABLE monitor | ||
ADD mqtt_username VARCHAR(255); | ||
|
||
ALTER TABLE monitor | ||
ADD mqtt_password VARCHAR(255); | ||
|
||
COMMIT; |
This file contains 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,50 @@ | ||
const { log } = require("../src/util"); | ||
|
||
const mqttUsername = "louis1"; | ||
const mqttPassword = "!@#$LLam"; | ||
|
||
class SimpleMqttServer { | ||
aedes = require("aedes")(); | ||
server = require("net").createServer(this.aedes.handle); | ||
|
||
constructor(port) { | ||
this.port = port; | ||
} | ||
|
||
start() { | ||
this.server.listen(this.port, () => { | ||
console.log("server started and listening on port ", this.port); | ||
}); | ||
} | ||
} | ||
|
||
let server1 = new SimpleMqttServer(10000); | ||
|
||
server1.aedes.authenticate = function (client, username, password, callback) { | ||
if (username && password) { | ||
console.log(password.toString("utf-8")); | ||
callback(null, username === mqttUsername && password.toString("utf-8") === mqttPassword); | ||
} else { | ||
callback(null, false); | ||
} | ||
}; | ||
|
||
server1.aedes.on("subscribe", (subscriptions, client) => { | ||
console.log(subscriptions); | ||
|
||
for (let s of subscriptions) { | ||
if (s.topic === "test") { | ||
server1.aedes.publish({ | ||
topic: "test", | ||
payload: Buffer.from("ok"), | ||
}, (error) => { | ||
if (error) { | ||
log.error("mqtt_server", error); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
}); | ||
|
||
server1.start(); |
Oops, something went wrong.