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}
0 commit comments