Skip to content

Commit cf2d9cd

Browse files
v1.0.4 (#13)
* Add NODE_ENV=production to dockerfile & Change CMD to use array for better SIGTERM handling * Add memory limits * Add memory limits * Changed default LINE to hostname * Changed default clientId hostname for MQTTv3.1.1
1 parent 8cfb5e3 commit cf2d9cd

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ COPY package.json /machine
88

99
RUN npm install
1010

11-
COPY ./src/ /machine
11+
COPY --chown=node:node ./src/ /machine
1212

1313
USER node
1414

1515
ENV NODE_ENV=production
1616

17+
ENV NODE_OPTIONS="--max-old-space-size=20"
18+
1719
CMD ["node", "index.js"]

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ PackML MQTT Simulator is a virtual line that interfaces using PackML implemented
88

99
## Getting Started
1010

11-
To start and run the PackML simulation, you'll need an MQTT server running and accessible to the image. Once available, the easiest approach is using docker to run the simulation using environmental variables to control the MQTT host, Site, Area, and line. Once up and running, use an MQTT client to publish to .../Command/Reset and .../Command/Start to get the simulated machine into the execute state.
11+
To start and run the PackML simulation, you'll need an MQTT server running and accessible to the image. Once available, the easiest approach is using docker to run the simulation using environmental variables to control the MQTT connection, Site, Area, and line. Once up and running, use an MQTT client to publish to .../Command/Reset and .../Command/Start to get the simulated machine into the execute state.
1212

1313
### Docker
1414

1515
Start your container with environmental variables
1616

1717
```shell
18-
$ docker run -it -e SITE=Site -e AREA=Area -e LINE=Line -e MQTT_HOST=mqtt://broker.hivemq.com spruiktec/packml-simulator
18+
$ docker run -it -e SITE=Site -e AREA=Area -e LINE=Line -e MQTT_HOST=mqtt://broker.hivemq.com -m 30m spruiktec/packml-simulator
1919
2020-06-22T03:13:49.301Z | info: Initializing
2020
2020-06-22T03:13:49.817Z | info: Connected to mqtt://broker.hivemq.com:1883
2121
2020-06-22T03:13:49.819Z | info: Site/Area/Line/Status/UnitModeCurrent : Production
@@ -28,7 +28,9 @@ $ npm i
2828
...
2929
added 421 packages from 213 contributors and audited 421 packages in 12.337s
3030
found 0 vulnerabilities
31-
$ node ./src/index.js
31+
$ export LINE=Line
32+
33+
$ node --max-old-space-size=20 ./src/index.js
3234
2020-06-22T03:13:49.301Z | info: Initializing
3335
2020-06-22T03:13:49.817Z | info: Connected to mqtt://broker.hivemq.com:1883
3436
2020-06-22T03:13:49.819Z | info: Site/Area/Line/Status/UnitModeCurrent : Production
@@ -162,7 +164,7 @@ The ISA-95 Model area name of this line. AREA used as the second topic in the MQ
162164

163165
### LINE
164166

165-
The ISA-95 model line name of this line. LINE used as the third topic in the MQTT structure. If this is unset, _Line_ will be used.
167+
The ISA-95 model line name of this line. LINE used as the third topic in the MQTT structure. If this is unset, hostname will be used.
166168

167169
### MQTT_URL
168170

@@ -185,7 +187,7 @@ The password for the MQTT user with subscribe and publish permissions.
185187
Use docker-compose to simulate multiple independent lines at once. E.g.
186188

187189
```yml
188-
version: "3.7"
190+
version: "2.4"
189191

190192
services:
191193
greenville-packaging-line1:
@@ -194,13 +196,16 @@ services:
194196
SITE: Greenville
195197
AREA: Packaging
196198
LINE: 'Line 1'
199+
mem_limit: 30MB
200+
197201
greenville-packaging-line2:
198202
image: spruiktec/packml-simulator
199203
environment:
200204
SITE: Greenville
201205
AREA: Packaging
202206
LINE: 'Line 2'
203-
...
207+
mem_limit: 30MB
208+
204209
```
205210

206211
## Contributing
@@ -213,6 +218,11 @@ For any issue, there are fundamentally three ways an individual can contribute:
213218

214219
## Changelog
215220

221+
- 1.0.4
222+
- Add memory limits
223+
- Changed default LINE to hostname
224+
- Changed default clientId to MQTTv3.1.1 conforming hostname
225+
216226
- 1.0.3
217227
- Add NODE_ENV=production to dockerfile
218228
- Change CMD to use array for better SIGTERM handling

docker-compose.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "3.7"
1+
version: "2.4"
22

33
services:
44
greenville-packaging-line1:
@@ -7,10 +7,12 @@ services:
77
SITE: Greenville
88
AREA: Packaging
99
LINE: 'Line 1'
10-
10+
mem_limit: 30MB
11+
1112
greenville-packaging-line2:
1213
image: spruiktec/packml-simulator
1314
environment:
1415
SITE: Greenville
1516
AREA: Packaging
16-
LINE: 'Line 2'
17+
LINE: 'Line 2'
18+
mem_limit: 30MB

src/helper.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ exports.camelCase = (str) => {
1414
return index === 0 ? word.toLowerCase() : word.toUpperCase()
1515
}).replace(/\s+/g, '')
1616
}
17+
18+
exports.getClientId = (hostname) => {
19+
// Confirm to MQTT v3.1.1
20+
const regex = /[^a-zA-Z0-9]/g
21+
return hostname.replace(regex, "").substring(0, 23)
22+
}

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const packmlModel = require('./packml-model')
77
const packmlTags = require('./packml-tags')
88
const simulation = require('./simulation')
99
const helper = require('./helper')
10+
const os = require('os')
1011

1112
var mqtt = require('mqtt')
1213
var seedRandom = require('seedrandom')
@@ -18,7 +19,7 @@ logger.info('Initializing')
1819
global.config = {
1920
site: process.env.SITE || 'Site',
2021
area: process.env.AREA || 'Area',
21-
line: process.env.LINE || 'Line',
22+
line: process.env.LINE || os.hostname(),
2223
startOnLoad: process.env.START || false,
2324
MQTT_URL: process.env.MQTT_URL || 'mqtt://broker.hivemq.com',
2425
MQTT_PORT: process.env.MQTT_PORT || null,
@@ -42,6 +43,7 @@ const packmlProducts = new RegExp(String.raw`^${topicPrefix}\/Command\/Product\/
4243
var mqttClient = mqtt.connect(
4344
global.config.MQTT_URL,
4445
{
46+
clientId: helper.getClientId(os.hostname()),
4547
port: global.config.MQTT_PORT,
4648
username: global.config.MQTT_USERNAME,
4749
password: global.config.MQTT_PASSWORD
@@ -125,7 +127,9 @@ mqttClient.on('connect', (packet) => {
125127
global.sim = simulation.simulate(mode, state, tags)
126128
})
127129

128-
mqttClient.on('close', () => { logger.info(`Disconnected from ${mqttClient.options.href || global.config.MQTT_URL}:${mqttClient.options.port}`) })
130+
mqttClient.on('close', () => {
131+
logger.info(`Disconnected from ${mqttClient.options.href || global.config.MQTT_URL}:${mqttClient.options.port}`)
132+
})
129133

130134
// Handle PackML Commands
131135
mqttClient.on('message', (topic, message) => {

0 commit comments

Comments
 (0)