Skip to content

Commit d8fd030

Browse files
Merge pull request #47 from libremfg/fix/packml-command-module-references
Fix PackML command references
2 parents 0de3085 + 2fb7811 commit d8fd030

File tree

3 files changed

+77
-57
lines changed

3 files changed

+77
-57
lines changed

src/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const logging = require('./logging')
66
const packmlModel = require('./packml-model')
77
const packmlTags = require('./packml-tags')
8+
const packmlCommands = require('./packml-commands')
89
const simulation = require('./simulation')
910
const helper = require('./helper')
1011
const mqtt = require('./clients/mqtt');
@@ -46,7 +47,7 @@ const stateCommandTopic = new RegExp(String.raw`^${global.config.topicPrefix}\/C
4647
const modeCommandTopic = new RegExp(String.raw`^${global.config.topicPrefix}\/Command\/(UnitMode)$`)
4748
const machineSpeedCommandTopic = new RegExp(String.raw`^${global.config.topicPrefix}\/Command\/MachSpeed$`)
4849
const packmlParameters = new RegExp(String.raw`^${global.config.topicPrefix}\/Command\/Parameter\/(\d*)\/(ID|Name|Unit|Value)$`)
49-
const packmlProducts = new RegExp(String.raw`^${global.config.topicPrefix}\/Command\/Product\/(\d*)\/(ProductID|ProcessParameter\/(\d*)\/(ID|Name|Unit|Value)|Ingredient\/(\d*)\/(IngredientID|Parameter\/(\d*)\/(ID|Name|Unit|Value)))$`)
50+
const packmlProducts = new RegExp(String.raw`^${global.config.topicPrefix}\/Command\/Product\/(\d*)\/(ID|ProcessParameter\/(\d*)\/(ID|Name|Unit|Value)|Ingredient\/(\d*)\/(ID|Parameter\/(\d*)\/(ID|Name|Unit|Value)))$`)
5051

5152
// PackML State Model
5253
let state = new packmlModel.StateMachine()
@@ -145,15 +146,15 @@ client.on('close', () => {
145146
// Handle PackML Commands
146147
client.on('message', (topic, message) => {
147148
if (topic.match(stateCommandTopic)) {
148-
stateCommand(topic, message, state)
149+
packmlCommands.stateCommand(logger, topic, message, state, stateCommandTopic)
149150
} else if (topic.match(modeCommandTopic)) {
150-
modeCommand(topic, message, mode)
151+
packmlCommands.modeCommand(logger, message, mode)
151152
} else if (topic.match(machineSpeedCommandTopic)) {
152-
machineSpeedCommand(topic, message, tags)
153+
packmlCommands.machineSpeedCommand(logger, topic, message, tags)
153154
} else if (topic.match(packmlParameters)) {
154-
parameterCommand(topic, message, tags)
155+
packmlCommands.parameterCommand(logger, topic, message, tags, packmlParameters, changed)
155156
} else if (topic.match(packmlProducts)) {
156-
productCommand(topic, message, tags)
157+
packmlCommands.productCommand(logger, topic, message, tags, packmlProducts, changed)
157158
} else {
158159
logger.debug(`No handle defined for ${topic}`)
159160
}

src/packml-commands.js

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
#!/usr/bin/env node
22
'use strict'
33

4+
// Imports
5+
const packmlModel = require('./packml-model')
6+
const packmlTags = require('./packml-tags')
7+
const helper = require('./helper')
8+
49
// handle state commands
5-
function stateCommand(topic, message, state) {
10+
exports.stateCommand = (logger, topic, message, state, stateCommandTopic) => {
611
// State Commands
712
const command = topic.match(stateCommandTopic)[1]
813
try {
@@ -18,21 +23,21 @@ function stateCommand(topic, message, state) {
1823
}
1924

2025
// handle mode commands
21-
function modeCommand(message, mode) {
26+
exports.modeCommand = (logger, message, mode) => {
2227
if (isNaN(message)) {
23-
message = message.toLowerCase()
28+
message = message.toString().toLowerCase()
2429
} else {
2530
message = packmlModel.getModeTextByModeInt(message).toLowerCase()
2631
}
2732
if (packmlModel.isUnitMode(message)) {
2833
mode.goto(message)
2934
} else {
30-
logger.error('Cannot change to unknown UnitMode')
35+
logger.error(`Cannot change to unknown UnitMode: ${message}`)
3136
}
3237
}
3338

3439
// handle machine speed commands
35-
function machineSpeedCommand(topic, message, tags) {
40+
exports.machineSpeedCommand = (logger, topic, message, tags) => {
3641
if (Number.isNaN(message)) {
3742
logger.error(`Bad request: ${topic} Must be an number`)
3843
return
@@ -46,7 +51,7 @@ function machineSpeedCommand(topic, message, tags) {
4651
}
4752

4853
// handle parameter commands
49-
function parameterCommand(topic, message, tags) {
54+
exports.parameterCommand = (logger, topic, message, tags, packmlParameters, changed) => {
5055
// Parameters
5156
const bits = topic.match(packmlParameters)
5257
const index = parseInt(bits[1])
@@ -81,53 +86,61 @@ function parameterCommand(topic, message, tags) {
8186
}
8287

8388
// handle product commands
84-
function productCommand(topic, message, tags) {
89+
exports.productCommand = (logger, topic, message, tags, packmlProducts, changed) => {
8590
// Products
8691
const bits = topic.match(packmlProducts).filter(match => match !== undefined)
8792
const index = parseInt(bits[1])
88-
if (bits.length === 3) {
89-
productCommandForProduct(index, topic, message, tags)
90-
} else if (bits.length === 5) {
91-
const nextIndex = bits[3]
92-
if (bits[0].indexOf('Ingredient')) {
93-
productCommandForIngredient(nextIndex, message, tags)
94-
} else {
95-
productCommandForProductParameter(nextIndex, bits, topic, message, tags)
96-
}
97-
} else if (bits.length === 7) {
98-
productCommandForIngredientParameter(bits, topic, message, tags)
99-
}
100-
}
10193

102-
function productCommandForProduct(index, topic, message, tags) {
94+
// Add in empty product if it doesn't exist
10395
while (tags.status.product.length <= index) {
10496
tags.status.product.push(new Proxy(new packmlTags.Product(tags.status.product.length), {
10597
set (target, prop, value) {
10698
target[prop] = value
107-
changed('Status/Product/' + target._productIndex + '/' + prop, value)
99+
changed('Status/Product/' + target._index + '/', prop, value)
108100
return true
109101
}
110102
}))
111103
}
104+
if (bits.length === 3) {
105+
productCommandForProduct(logger, index, topic, message, tags)
106+
} else if (bits.length === 5) {
107+
const nextIndex = bits[3]
108+
const isIngredient = bits[0].indexOf('Ingredient') !== -1
109+
if (isIngredient) {
110+
productCommandForIngredient(index, nextIndex, bits, message, tags, changed)
111+
} else {
112+
productCommandForProductParameter(logger, index, nextIndex, bits, topic, message, tags, changed)
113+
}
114+
} else if (bits.length === 7) {
115+
productCommandForIngredientParameter(logger, index, bits, topic, message, tags, changed)
116+
}
117+
}
118+
119+
function productCommandForProduct(logger, index, topic, message, tags) {
112120
message = parseInt(message)
113121
if (isNaN(message)) {
114122
logger.error(`Bad request: ${topic} Must be an number`)
115123
return
116124
}
117-
tags.status.product[index].productId = message
125+
tags.status.product[index].id = message
118126
}
119127

120-
function productCommandForIngredient(nextIndex, message, tags) {
121-
while (tags.status.product.length <= index) {
122-
tags.status.product.push(new packmlTags.Product())
123-
}
128+
function productCommandForIngredient(index, nextIndex, bits, message, tags, changed) {
124129
while (tags.status.product[index].ingredient.length <= nextIndex) {
125-
tags.status.product[index].ingredient.push(new packmlTags.Ingredient())
130+
tags.status.product[index].ingredient.push(new Proxy(
131+
new packmlTags.Ingredient(tags.status.product[index].ingredient.length),
132+
{
133+
set (target, prop, value) {
134+
changed('Status/Product/' + index + '/Ingredient/' + target._index + '/', prop, value)
135+
return Reflect.set(...arguments)
136+
}
137+
}
138+
))
126139
}
127140
tags.status.product[index].ingredient[nextIndex][helper.camelCase(bits[4])] = parseInt(message)
128141
}
129142

130-
function productCommandForProductParameter(nextIndex, bits, topic, message, tags) {
143+
function productCommandForProductParameter(logger, index, nextIndex, bits, topic, message, tags, changed) {
131144
if (bits[4] === 'ID') {
132145
message = parseInt(message)
133146
if (isNaN(message)) {
@@ -141,22 +154,20 @@ function productCommandForProductParameter(nextIndex, bits, topic, message, tags
141154
return
142155
}
143156
}
144-
while (tags.status.product.length <= index) {
145-
tags.status.product.push(new packmlTags.Product())
146-
}
147157
while (tags.status.product[index].processParameter.length <= nextIndex) {
148-
tags.status.product[index].processParameter.push(new packmlTags.Parameter(tags.status.product[index].processParameter.length), {
158+
tags.status.product[index].processParameter.push(new Proxy(
159+
new packmlTags.Parameter(tags.status.product[index].processParameter.length, index), {
149160
set (target, prop, value) {
150-
changed('Status/Product/' + index + '/ProcessParameter/' + tags.status.product[index].processParameter.length + '/', prop, value)
161+
changed('Status/Product/' + index + '/ProcessParameter/' + target._index + '/', prop, value)
151162
return Reflect.set(...arguments)
152163
}
153-
})
164+
}))
154165
}
155166
tags.status.product[index].processParameter[nextIndex][helper.camelCase(bits[4])] = message
156167
}
157168

158-
function productCommandForIngredientParameter(bits, topic, message, tags) {
159-
const ingredientIndex = parseInt(bits[2])
169+
function productCommandForIngredientParameter(logger, index, bits, topic, message, tags, changed) {
170+
const ingredientIndex = parseInt(bits[3])
160171
const parameterIndex = parseInt(bits[5])
161172
if (bits[6] === 'ID') {
162173
message = parseInt(message)
@@ -171,19 +182,26 @@ function productCommandForIngredientParameter(bits, topic, message, tags) {
171182
return
172183
}
173184
}
174-
while (tags.status.product.length <= index) {
175-
tags.status.product.push(new packmlTags.Product())
176-
}
177185
while (tags.status.product[index].ingredient.length <= ingredientIndex) {
178-
tags.status.product[index].ingredient.push(new packmlTags.Parameter(), {
179-
set (target, prop, value) {
180-
changed('Status/Product/' + index + '/Ingredient/' + tags.status.product[index].ingredient.length - 1 + '/', prop, value)
181-
return Reflect.set(...arguments)
186+
tags.status.product[index].ingredient.push(new Proxy(
187+
new packmlTags.Ingredient(tags.status.product[index].ingredient.length), {
188+
set (target, prop, value) {
189+
changed('Status/Product/' + index + '/Ingredient/' + target._index + '/', prop, value)
190+
return Reflect.set(...arguments)
191+
}
182192
}
183-
})
193+
))
184194
}
185-
while (tags.status.product[index].ingredient[ingredientIndex].parameter <= parameterIndex) {
186-
tags.status.product[index].ingredient[ingredientIndex].parameter.push(new packmlTags.Parameter())
195+
196+
while (tags.status.product[index].ingredient[ingredientIndex].parameter.length <= parameterIndex) {
197+
tags.status.product[index].ingredient[ingredientIndex].parameter.push(new Proxy(
198+
new packmlTags.Parameter(tags.status.product[index].ingredient[ingredientIndex].parameter.length, index, ingredientIndex), {
199+
set(target, prop, value) {
200+
changed('Status/Product/' + index + '/Ingredient/' + ingredientIndex + '/Parameter/' + target._index + '/', prop, value)
201+
return Reflect.set(...arguments)
202+
}
203+
}
204+
))
187205
}
188206
tags.status.product[index].ingredient[ingredientIndex].parameter[parameterIndex][helper.camelCase(bits[6])] = message
189207
}

src/packml-tags.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ function EquipmentInterlock() {
3535
}
3636
}
3737

38-
function Parameter(index ,productIndex) {
38+
function Parameter(index, productIndex, ingredientIndex) {
3939
if (index === undefined || index === null) {
4040
throw TypeError('Must construct a Parameter with an index')
4141
}
4242
return {
4343
_index: index,
4444
_productIndex: productIndex !== undefined && productIndex !== null ? productIndex : null,
45+
_ingredientIndex: ingredientIndex !== undefined && ingredientIndex !== null ? ingredientIndex : null,
4546
id: 0,
4647
name: '',
4748
unit: '',
@@ -63,12 +64,12 @@ function Ingredient(index, productIndex) {
6364

6465
function Product(index) {
6566
if (index === undefined || index === null) {
66-
throw TypeError('Must construct a Prodct with an index')
67+
throw TypeError('Must construct a Product with an index')
6768
}
6869
return {
6970
_index: index,
70-
productId: 0,
71-
parameter: [],
71+
id: 0,
72+
processParameter: [],
7273
ingredient: []
7374
}
7475
}

0 commit comments

Comments
 (0)