-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds mqtt protocol to protofy agents
- Loading branch information
Showing
5 changed files
with
43 additions
and
28 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
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
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 |
---|---|---|
@@ -1,22 +1,27 @@ | ||
import { Agent } from "../Agent"; | ||
|
||
//options is for auth params and similar protocol-specific options | ||
export default async (agent: Agent, params: any, options = {}) => { | ||
const protocol = agent.getProtocol(); | ||
if (protocol.type !== 'mqtt') { | ||
throw new Error('Error: Invalid protocol type, expected http'); | ||
import { AgentProtocol } from "../AgentProtocol"; | ||
export class MQTTProtocol extends AgentProtocol { | ||
mqttClient: any; | ||
constructor(agent: Agent, mqttClient: any) { | ||
super(agent); | ||
this.mqttClient = mqttClient; | ||
} | ||
|
||
if (!protocol.config || !protocol.config.url || !protocol.config.topic) { | ||
throw new Error('Error: Missing URL or topic in protocol config'); | ||
} | ||
async send(params, options?) { | ||
const agent = this.agent | ||
const protocol = agent.getProtocol(); | ||
if (protocol.type !== 'mqtt') { | ||
throw new Error('Error: Invalid protocol type, expected http'); | ||
} | ||
|
||
const { | ||
topic, | ||
} = protocol.config; | ||
|
||
const { | ||
url, | ||
topic, | ||
encoder = 'body', | ||
serializer = 'json', | ||
} = protocol.config; | ||
this.mqttClient.publish(topic, JSON.stringify(params)); | ||
} | ||
|
||
|
||
}; | ||
static create(agent: Agent, mqttClient: any) { | ||
return new MQTTProtocol(agent, mqttClient); | ||
} | ||
} |
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
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