Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish to Wirepas 5G Mesh Gateway #5

Open
coderbyheart opened this issue Feb 13, 2023 · 0 comments
Open

Publish to Wirepas 5G Mesh Gateway #5

coderbyheart opened this issue Feb 13, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@coderbyheart
Copy link
Member

Generate static protobuf files

npx pbjs -t static-module -w es6 --root wirepas -o dist/protobuf.js wirepas-5g-mesh-bridge/protobuf/generic_message.proto
npx pbts -o dist/protobuf.d.ts dist/protobuf.js

Use

import mqtt from 'mqtt'
import wpGw from '../dist/protobuf.js'

export const wirepasPublish =
	({ bridgeEndpoint }: { bridgeEndpoint: string }) =>
	({ gateway, device }: { gateway: string; device: string }) =>
		new Promise((resolve, reject) => {
			const client = mqtt.connect(bridgeEndpoint)

			client.on('connect', () => {
				console.log(`Connected.`)

				client.publish(
					`gw-request/send_data/${gateway}/${device}`,
					Buffer.from(
						wpGw.wirepas.proto.gateway_api.SendPacketReq.encode(
							wpGw.wirepas.proto.gateway_api.SendPacketReq.create({
								qos: 1,
								sourceEndpoint: 1,
								destinationAddress: 1,
								destinationEndpoint: 1,
								payload: Buffer.from([1]),
								header: {
									reqId: Date.now(),
								},
							}),
						).finish(),
					),
					(err, res) => {
						if (err !== null) return reject(err)
						return resolve(res)
					},
				)
			})
		})
@coderbyheart coderbyheart added the enhancement New feature or request label Feb 13, 2023
@coderbyheart coderbyheart self-assigned this Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant