Skip to content

Commit 497bb0e

Browse files
authored
Merge pull request #5 from FlowFuse/first-tests
Auto start and fix docker install tag
2 parents 1a5adfe + bfcfe46 commit 497bb0e

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

docker/Dockerfile

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ FROM node:20-alpine
22

33
ARG REGISTRY
44
ARG REGISTRY_TOKEN
5-
ARG TAG
5+
ARG TAG=latest
66
RUN if [[ ! -z "$REGISTRY_TOKEN" ]]; then echo "//$REGISTRY/:_authToken=$REGISTRY_TOKEN" >> ~/.npmrc ; fi
77
RUN if [[ ! -z "$REGISTRY" ]] ; then npm config set @flowfuse:registry "https://$REGISTRY"; fi
88

99
WORKDIR /usr/src/mqtt-schema-agent
10-
COPY package.json .
11-
RUN npm install --production --no-audit --no-fund
10+
RUN npm install --production --no-audit --no-fund @flowfuse/mqtt-schema-agent@$TAG
1211

1312
LABEL org.label-schema.name="FlowFuse MQTT Schema Agent" \
1413
org.label-schema.url="https://flowfuse.com" \

lib/agent.js

+45-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ class Agent {
2929
this.creds = Object.assign(this.creds, { reconnectPeriod: 0 })
3030
// console.log(this.creds)
3131

32+
if (this.creds.ssl) {
33+
switch (this.creds.protocol) {
34+
case 'mqtt:':
35+
this.creds.protocol = 'mqtts:'
36+
break
37+
case 'ws:':
38+
this.creds.protocol = 'wss:'
39+
break
40+
}
41+
}
42+
3243
const options = {
3344
protocol: this.creds.protocol,
3445
host: this.creds.host,
@@ -39,13 +50,17 @@ class Agent {
3950
password: this.creds.credentials.password
4051
}
4152

53+
if (!this.creds.verifySSL) {
54+
options.rejectUnauthorized = false
55+
}
56+
4257
// console.log(options)
4358

4459
// this.client = mqtt.connect(`${this.creds.protocol}//${this.creds.hostname}:${this.creds.port}`, this.creds)
4560
this.client = mqtt.connect(options)
4661
this.client.on('connect', function () {
4762
agent.connected = true
48-
// console.log('connected')
63+
console.log(`connected to ${options.host}:${options.port} as ${options.clientId}`)
4964
agent.error = null
5065
agent.client.subscribe('#')
5166
})
@@ -73,8 +88,8 @@ class Agent {
7388
// console.log(packet)
7489
})
7590
this.client.on('error', function (error) {
76-
// console.log('error', error.code)
77-
// console.log(error)
91+
console.log('error', error.code)
92+
console.log(error)
7893
switch (error.code) {
7994
case 'ECONNREFUSED': // connection refused
8095
case 'ENOTFOUND': // DNS lookup failed
@@ -102,9 +117,34 @@ class Agent {
102117
}
103118
// console.log(`agent.error set to: ${agent.error}`)
104119
})
105-
this.client.on('message', function (topic) {
120+
this.client.on('message', function (topic, payload) {
106121
// console.log(topic)
107-
agent.topics[topic] = Date.now()
122+
let type = 'unknown'
123+
try {
124+
const stringPayload = payload.toString('utf8')
125+
type = 'string'
126+
switch (stringPayload.charAt(0)) {
127+
case '<':
128+
// probably XML
129+
type = 'xml'
130+
break
131+
case '{':
132+
case '[':
133+
// probably JSON
134+
try {
135+
JSON.parse(stringPayload)
136+
} catch (err) {
137+
138+
}
139+
type = 'json'
140+
}
141+
} catch (err) {
142+
143+
}
144+
agent.topics[topic] = {
145+
timestamp: Date.now(),
146+
type
147+
}
108148
})
109149
} catch (err) {
110150
console.log(err)

lib/api.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class API {
4444

4545
// default to running
4646
this.agent = new Agent(this.options)
47+
this.agent.start()
4748
}
4849

4950
async stop () {

0 commit comments

Comments
 (0)