Skip to content

Commit

Permalink
Fix product proxy reflecting value change
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Hollingworth committed Oct 25, 2024
1 parent b0fcde9 commit e38c824
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 42 deletions.
91 changes: 52 additions & 39 deletions src/packml-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,49 +90,57 @@ exports.productCommand = (logger, topic, message, tags, packmlProducts, changed)
// Products
const bits = topic.match(packmlProducts).filter(match => match !== undefined)
const index = parseInt(bits[1])
if (bits.length === 3) {
productCommandForProduct(logger, index, topic, message, tags, changed)
} else if (bits.length === 5) {
const nextIndex = bits[3]
if (bits[0].indexOf('Ingredient')) {
productCommandForIngredient(nextIndex, bits, message, tags, index)
} else {
productCommandForProductParameter(logger, nextIndex, bits, topic, message, tags, changed)
}
} else if (bits.length === 7) {
productCommandForIngredientParameter(logger, bits, topic, message, tags, changed)
}
}

function productCommandForProduct(logger, index, topic, message, tags, changed) {
// Add in empty product if it doesn't exist
while (tags.status.product.length <= index) {
tags.status.product.push(new Proxy(new packmlTags.Product(tags.status.product.length), {
set (target, prop, value) {
target[prop] = value
changed('Status/Product/' + target._productIndex + '/' + prop, value)
changed('Status/Product/' + target._index + '/', prop, value)
return true
}
}))
}
if (bits.length === 3) {
productCommandForProduct(logger, index, topic, message, tags)
} else if (bits.length === 5) {
const nextIndex = bits[3]
const isIngredient = bits[0].indexOf('Ingredient') !== -1
if (isIngredient) {
productCommandForIngredient(index, nextIndex, bits, message, tags, changed)
} else {
productCommandForProductParameter(logger, index, nextIndex, bits, topic, message, tags, changed)
}
} else if (bits.length === 7) {
productCommandForIngredientParameter(logger, index, bits, topic, message, tags, changed)
}
}

function productCommandForProduct(logger, index, topic, message, tags) {
message = parseInt(message)
if (isNaN(message)) {
logger.error(`Bad request: ${topic} Must be an number`)
return
}
tags.status.product[index].productId = message
tags.status.product[index].id = message
}

function productCommandForIngredient(nextIndex, bits, message, tags, index) {
while (tags.status.product.length <= index) {
tags.status.product.push(new packmlTags.Product(tags.status.product.length))
}
function productCommandForIngredient(index, nextIndex, bits, message, tags, changed) {
while (tags.status.product[index].ingredient.length <= nextIndex) {
tags.status.product[index].ingredient.push(new packmlTags.Ingredient(tags.status.product[index].ingredient.length))
tags.status.product[index].ingredient.push(new Proxy(
new packmlTags.Ingredient(tags.status.product[index].ingredient.length),
{
set (target, prop, value) {
changed('Status/Product/' + index + '/Ingredient/' + target._index + '/', prop, value)
return Reflect.set(...arguments)
}
}
))
}
tags.status.product[index].ingredient[nextIndex][helper.camelCase(bits[4])] = parseInt(message)
}

function productCommandForProductParameter(logger, nextIndex, bits, topic, message, tags, changed) {
function productCommandForProductParameter(logger, index, nextIndex, bits, topic, message, tags, changed) {
if (bits[4] === 'ID') {
message = parseInt(message)
if (isNaN(message)) {
Expand All @@ -146,22 +154,20 @@ function productCommandForProductParameter(logger, nextIndex, bits, topic, messa
return
}
}
while (tags.status.product.length <= index) {
tags.status.product.push(new packmlTags.Product(tags.status.product.length))
}
while (tags.status.product[index].processParameter.length <= nextIndex) {
tags.status.product[index].processParameter.push(new packmlTags.Parameter(tags.status.product[index].processParameter.length), {
tags.status.product[index].processParameter.push(new Proxy(
new packmlTags.Parameter(tags.status.product[index].processParameter.length, index), {
set (target, prop, value) {
changed('Status/Product/' + index + '/ProcessParameter/' + tags.status.product[index].processParameter.length + '/', prop, value)
changed('Status/Product/' + index + '/ProcessParameter/' + target._index + '/', prop, value)
return Reflect.set(...arguments)
}
})
}))
}
tags.status.product[index].processParameter[nextIndex][helper.camelCase(bits[4])] = message
}

function productCommandForIngredientParameter(logger, bits, topic, message, tags, changed) {
const ingredientIndex = parseInt(bits[2])
function productCommandForIngredientParameter(logger, index, bits, topic, message, tags, changed) {
const ingredientIndex = parseInt(bits[3])
const parameterIndex = parseInt(bits[5])
if (bits[6] === 'ID') {
message = parseInt(message)
Expand All @@ -176,19 +182,26 @@ function productCommandForIngredientParameter(logger, bits, topic, message, tags
return
}
}
while (tags.status.product.length <= index) {
tags.status.product.push(new packmlTags.Product(tags.status.product.length))
}
while (tags.status.product[index].ingredient.length <= ingredientIndex) {
tags.status.product[index].ingredient.push(new packmlTags.Parameter(tags.status.product[index].ingredient.length, index), {
set (target, prop, value) {
changed('Status/Product/' + index + '/Ingredient/' + tags.status.product[index].ingredient.length - 1 + '/', prop, value)
return Reflect.set(...arguments)
tags.status.product[index].ingredient.push(new Proxy(
new packmlTags.Ingredient(tags.status.product[index].ingredient.length), {
set (target, prop, value) {
changed('Status/Product/' + index + '/Ingredient/' + target._index + '/', prop, value)
return Reflect.set(...arguments)
}
}
})
))
}

while (tags.status.product[index].ingredient[ingredientIndex].parameter.length <= parameterIndex) {
tags.status.product[index].ingredient[ingredientIndex].parameter.push(new packmlTags.Parameter(tags.status.product[index].ingredient[ingredientIndex].parameter.length))
tags.status.product[index].ingredient[ingredientIndex].parameter.push(new Proxy(
new packmlTags.Parameter(tags.status.product[index].ingredient[ingredientIndex].parameter.length, index, ingredientIndex), {
set(target, prop, value) {
changed('Status/Product/' + index + '/Ingredient/' + ingredientIndex + '/Parameter/' + target._index + '/', prop, value)
return Reflect.set(...arguments)
}
}
))
}
tags.status.product[index].ingredient[ingredientIndex].parameter[parameterIndex][helper.camelCase(bits[6])] = message
}
7 changes: 4 additions & 3 deletions src/packml-tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ function EquipmentInterlock() {
}
}

function Parameter(index ,productIndex) {
function Parameter(index, productIndex, ingredientIndex) {
if (index === undefined || index === null) {
throw TypeError('Must construct a Parameter with an index')
}
return {
_index: index,
_productIndex: productIndex !== undefined && productIndex !== null ? productIndex : null,
_ingredientIndex: ingredientIndex !== undefined && ingredientIndex !== null ? ingredientIndex : null,
id: 0,
name: '',
unit: '',
Expand All @@ -67,8 +68,8 @@ function Product(index) {
}
return {
_index: index,
productId: 0,
parameter: [],
id: 0,
processParameter: [],
ingredient: []
}
}
Expand Down

0 comments on commit e38c824

Please sign in to comment.