From 66acdaa8a6b326532b61251d1eb4b4866e1c8e7c Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 11 Oct 2024 09:55:32 -0400 Subject: [PATCH 01/15] Revert docker hub container name and add gitub container registry --- .github/workflows/main.yml | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 680942c..9c45338 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,21 +1,30 @@ name: Publish Docker on: - # Trigger the workflow on push request, - # but only for the master branch push: - branches: - - master - release: - # Only use the types keyword to narrow down the activity types that will trigger your workflow. - types: [published, created, edited] + tags: + - v* + branches: # Temporary to test the action + - chore/change-container-build-and-push jobs: build: runs-on: ubuntu-latest + permissions: + contents: read + packages: write steps: - uses: actions/checkout@master - - name: Publish to Registry + - name: Publish to GitHub Container Registry + uses: docker/build-push-action@v6 + with: + username: ${{ secrets.GITHUB_ACTOR }} + password: ${{ secrets.GITHUB_TOKEN }} + repository: ghcr.io/libremfg/packml-simulator + tags: | + libremfg/packml-simulator:${{ github.ref_name }} + libremfg/packml-simulator:latest + - name: Publish to DockerHub Registry uses: elgohr/Publish-Docker-Github-Action@v5 with: - name: libremfg/packml-simulator + name: spruiktec/packml-simulator username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} + password: ${{ secrets.DOCKER_PASSWORD }} \ No newline at end of file From 4a6ef9822ba3cc4a9e77a3d771987faa3b250ad9 Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 11 Oct 2024 09:55:32 -0400 Subject: [PATCH 02/15] Revert docker hub container name and add gitub container registry --- .github/workflows/main.yml | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 680942c..d5a63c5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,21 +1,38 @@ name: Publish Docker on: - # Trigger the workflow on push request, - # but only for the master branch push: - branches: - - master - release: - # Only use the types keyword to narrow down the activity types that will trigger your workflow. - types: [published, created, edited] + tags: + - v* + branches: # Temporary to test the action + - chore/change-container-build-and-push jobs: build: runs-on: ubuntu-latest + permissions: + contents: read + packages: write steps: - uses: actions/checkout@master - - name: Publish to Registry + - name: Publish to GitHub Container Registry + if: ${{ github.github_ref_type}} == 'tag' + uses: docker/build-push-action@v6 + with: + username: ${{ secrets.GITHUB_ACTOR }} + password: ${{ secrets.GITHUB_TOKEN }} + tags: | + ghcr.io/libremfg/packml-simulator:${{ github.ref_name }} + ghcr.io/libremfg/packml-simulator:latest + - name: Publish to GitHub Container Registry + if: ${{ github.github_ref_type}} == 'branch' + uses: docker/build-push-action@v6 + with: + username: ${{ secrets.GITHUB_ACTOR }} + password: ${{ secrets.GITHUB_TOKEN }} + tags: | + ghcr.io/libremfg/packml-simulator:test + - name: Publish to DockerHub Registry uses: elgohr/Publish-Docker-Github-Action@v5 with: - name: libremfg/packml-simulator + name: spruiktec/packml-simulator username: ${{ secrets.DOCKER_USERNAME }} - password: ${{ secrets.DOCKER_PASSWORD }} + password: ${{ secrets.DOCKER_PASSWORD }} \ No newline at end of file From aaff175747519ddb0a5b9c3a933748765a898213 Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 11 Oct 2024 10:23:49 -0400 Subject: [PATCH 03/15] Fix github action conditional expression --- .github/workflows/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d5a63c5..4a86cfb 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,7 +14,7 @@ jobs: steps: - uses: actions/checkout@master - name: Publish to GitHub Container Registry - if: ${{ github.github_ref_type}} == 'tag' + if: ${{ github.ref_type == 'tag' }} uses: docker/build-push-action@v6 with: username: ${{ secrets.GITHUB_ACTOR }} @@ -22,8 +22,8 @@ jobs: tags: | ghcr.io/libremfg/packml-simulator:${{ github.ref_name }} ghcr.io/libremfg/packml-simulator:latest - - name: Publish to GitHub Container Registry - if: ${{ github.github_ref_type}} == 'branch' + - name: Publish to GitHub Container Registry on Branch + if: ${{ github.ref_type == 'branch' }} uses: docker/build-push-action@v6 with: username: ${{ secrets.GITHUB_ACTOR }} From 2f51c7a0abec0c3d936c888fd823f992d459453a Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 11 Oct 2024 10:26:29 -0400 Subject: [PATCH 04/15] Fix github action --- .github/workflows/main.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d5a63c5..e59e3a9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -14,23 +14,26 @@ jobs: steps: - uses: actions/checkout@master - name: Publish to GitHub Container Registry - if: ${{ github.github_ref_type}} == 'tag' + if: ${{ github.ref_type == 'tag' }} uses: docker/build-push-action@v6 with: username: ${{ secrets.GITHUB_ACTOR }} password: ${{ secrets.GITHUB_TOKEN }} + push: true tags: | ghcr.io/libremfg/packml-simulator:${{ github.ref_name }} ghcr.io/libremfg/packml-simulator:latest - - name: Publish to GitHub Container Registry - if: ${{ github.github_ref_type}} == 'branch' + - name: Publish to GitHub Container Registry on Branch + if: ${{ github.ref_type == 'branch' }} uses: docker/build-push-action@v6 with: username: ${{ secrets.GITHUB_ACTOR }} password: ${{ secrets.GITHUB_TOKEN }} + push: true tags: | ghcr.io/libremfg/packml-simulator:test - name: Publish to DockerHub Registry + if: ${{ github.ref_type == 'tag' }} uses: elgohr/Publish-Docker-Github-Action@v5 with: name: spruiktec/packml-simulator From 29afcbb4a0fa22e9f77d09952b457b6ec23c7ffa Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 11 Oct 2024 10:32:08 -0400 Subject: [PATCH 05/15] Add login step --- .github/workflows/main.yml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e59e3a9..44348c2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,12 +13,16 @@ jobs: packages: write steps: - uses: actions/checkout@master + - name: Login to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ secrets.GITHUB_ACTOR }} + password: ${{ secrets.GITHUB_TOKEN }} - name: Publish to GitHub Container Registry if: ${{ github.ref_type == 'tag' }} uses: docker/build-push-action@v6 with: - username: ${{ secrets.GITHUB_ACTOR }} - password: ${{ secrets.GITHUB_TOKEN }} push: true tags: | ghcr.io/libremfg/packml-simulator:${{ github.ref_name }} @@ -27,8 +31,6 @@ jobs: if: ${{ github.ref_type == 'branch' }} uses: docker/build-push-action@v6 with: - username: ${{ secrets.GITHUB_ACTOR }} - password: ${{ secrets.GITHUB_TOKEN }} push: true tags: | ghcr.io/libremfg/packml-simulator:test From b6f2e36835eada7268c942a0c0fc1bc75400376f Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 11 Oct 2024 10:33:16 -0400 Subject: [PATCH 06/15] Fix username env variable --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 44348c2..9de31f5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,7 +17,7 @@ jobs: uses: docker/login-action@v3 with: registry: ghcr.io - username: ${{ secrets.GITHUB_ACTOR }} + username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Publish to GitHub Container Registry if: ${{ github.ref_type == 'tag' }} From a833775fc3ce88ea900495a379463ff7b465d45a Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 11 Oct 2024 10:52:47 -0400 Subject: [PATCH 07/15] Change to ghcr.io container registry --- README.md | 10 +++++----- chart/values.yaml | 2 +- docker-compose.yml | 6 +++--- package.json | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bf48c29..5b76b24 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Start your container with environmental variables. #### Basic MQTT Structure ```shell -$ docker run -it -e SITE=Site -e AREA=Area -e LINE=Line -e MQTT_URL=mqtt://broker.hivemq.com -m 30m libremfg/packml-simulator +$ docker run -it -e SITE=Site -e AREA=Area -e LINE=Line -e MQTT_URL=mqtt://broker.hivemq.com -m 30m ghcr.io/libremfg/packml-simulator 2020-06-22T03:13:49.301Z | info: Initializing 2020-06-22T03:13:49.817Z | info: Connected to mqtt://broker.hivemq.com:1883 2020-06-22T03:13:49.819Z | info: Site/Area/Line/Status/UnitModeCurrent : Production @@ -33,7 +33,7 @@ Once up and running, use an MQTT client to publish to .../Command/Reset and .../ #### SparkPlugB ```shell -$ docker run -it -e CLIENT_TYPE=sparkplugb -e SITE=Site -e AREA=Area -e LINE=Line -e MQTT_URL=mqtt://broker.hivemq.com -m 30m libremfg/packml-simulator +$ docker run -it -e CLIENT_TYPE=sparkplugb -e SITE=Site -e AREA=Area -e LINE=Line -e MQTT_URL=mqtt://broker.hivemq.com -m 30m ghcr.io/libremfg/packml-simulator 2020-06-22T03:13:49.301Z | info: Initializing 2020-06-22T03:13:49.817Z | info: Connected to mqtt://broker.hivemq.com:1883 2020-06-22T03:13:49.819Z | info: Site/Area/Line/Status/UnitModeCurrent : Production @@ -263,7 +263,7 @@ version: "2.4" services: greenville-packaging-line1: - image: libremfg/packml-simulator + image: ghcr.io/libremfg/packml-simulator environment: SITE: Greenville AREA: Packaging @@ -271,7 +271,7 @@ services: mem_limit: 30MB greenville-packaging-line2: - image: libremfg/packml-simulator + image: ghcr.io/libremfg/packml-simulator environment: SITE: Greenville AREA: Packaging @@ -279,7 +279,7 @@ services: mem_limit: 30MB greenville-cnc-line1: - image: libremfg/packml-simulator + image: ghcr.io/libremfg/packml-simulator environment: SITE: Greenville AREA: CNC diff --git a/chart/values.yaml b/chart/values.yaml index 4dfec33..84cc754 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -1,6 +1,6 @@ replicaCount: 3 image: - repository: libremfg/packml-simulator + repository: ghcr.io/libremfg/packml-simulator pullPolicy: IfNotPresent config: diff --git a/docker-compose.yml b/docker-compose.yml index 1e4f65f..a58de18 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: "2.4" services: bathurst-packaging-line1: - image: libremfg/packml-simulator + image: ghcr.io/libremfg/packml-simulator environment: SITE: Bathurst AREA: Packaging @@ -13,7 +13,7 @@ services: mem_limit: 30MB bathurst-packaging-line2: - image: libremfg/packml-simulator + image: ghcr.io/libremfg/packml-simulator environment: SITE: Bathurst AREA: Packaging @@ -24,7 +24,7 @@ services: mem_limit: 30MB greenville-cnc-line1: - image: libremfg/packml-simulator + image: ghcr.io/libremfg/packml-simulator environment: SITE: Greenville AREA: CNC diff --git a/package.json b/package.json index 32d6233..581994c 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "test": "echo \"Error: no test specified\" && exit 1", "style": "standard", "quality": "sonar-scanner", - "build": "docker build . -t libremfg/packml-simulator" + "build": "docker build . -t ghcr.io/libremfg/packml-simulator" }, "author": "Libre Technologies Inc", "license": "MIT", From 04da3687cac2008014d9c2da404b481f3a51ed5b Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 11 Oct 2024 10:55:57 -0400 Subject: [PATCH 08/15] Change to only run on tags but keep comments when testing actions on a branch --- .github/workflows/main.yml | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9de31f5..3ceaa40 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,8 +3,9 @@ on: push: tags: - v* - branches: # Temporary to test the action - - chore/change-container-build-and-push + # # Temporary to test the action + # branches: + # - my/current-branch jobs: build: runs-on: ubuntu-latest @@ -20,22 +21,23 @@ jobs: username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Publish to GitHub Container Registry - if: ${{ github.ref_type == 'tag' }} + if: ${{ github.ref_type == 'tag' }} # Only useful when testing actions on a branch uses: docker/build-push-action@v6 with: push: true tags: | ghcr.io/libremfg/packml-simulator:${{ github.ref_name }} ghcr.io/libremfg/packml-simulator:latest - - name: Publish to GitHub Container Registry on Branch - if: ${{ github.ref_type == 'branch' }} - uses: docker/build-push-action@v6 - with: - push: true - tags: | - ghcr.io/libremfg/packml-simulator:test + # # Uncomment when testing actions on a branch + # - name: Publish to GitHub Container Registry on Branch + # if: ${{ github.ref_type == 'branch' }} + # uses: docker/build-push-action@v6 + # with: + # push: true + # tags: | + # ghcr.io/libremfg/packml-simulator:test - name: Publish to DockerHub Registry - if: ${{ github.ref_type == 'tag' }} + if: ${{ github.ref_type == 'tag' }} # Only useful when testing actions on a branch uses: elgohr/Publish-Docker-Github-Action@v5 with: name: spruiktec/packml-simulator From ccd34a6f18be670576484c1a260da01ab81524f8 Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 11 Oct 2024 11:31:40 -0400 Subject: [PATCH 09/15] Fix host reference on shutdown --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index fecd4a5..cf6d6c7 100644 --- a/src/index.js +++ b/src/index.js @@ -121,7 +121,7 @@ switch (global.config.CLIENT_TYPE) { } client.on('connect', (packet) => { - logger.info(`Connected to ${client.options().href || global.config.MQTT_URL}:${client.options().port}`) + logger.info(`Connected to ${client.options().href || client.options().host || global.config.MQTT_URL}:${client.options().port}`) state.observe('onEnterState', (lifecycle) => { const stateCurrent = helper.titleCase(lifecycle.to) logger.debug(`Entering State ${stateCurrent}`) @@ -139,7 +139,7 @@ client.on('connect', (packet) => { }) client.on('close', () => { - logger.info(`Disconnected from ${client.options().href || globalConfig.MQTT_URL}:${client.options().port}`) + logger.info(`Disconnected from ${client.options().href || client.globalConfig.MQTT_URL}:${client.options().port}`) }) // Handle PackML Commands From 27302df069489e36cb11a2804ba9634822c0c673 Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 11 Oct 2024 11:34:19 -0400 Subject: [PATCH 10/15] Bump version to v2.0.6 --- Dockerfile | 2 +- README.md | 6 ++++++ chart/Chart.yaml | 2 +- package.json | 2 +- sonar-project.properties | 2 +- src/clients/sparkplug.js | 2 +- 6 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5303eb8..ec7ef94 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ LABEL org.opencontainers.image.authors="tom@rhize.com" LABEL org.opencontainers.image.source="https://github.com/libremfg/PackML-MQTT-Simulator" LABEL org.opencontainers.image.url="https://www.libremfg.com/" LABEL org.opencontainers.image.vendor="Libre Technologies Inc" -LABEL org.opencontainers.image.version="2.0.5" +LABEL org.opencontainers.image.version="2.0.6" WORKDIR /machine diff --git a/README.md b/README.md index 5b76b24..8a231e6 100644 --- a/README.md +++ b/README.md @@ -299,6 +299,12 @@ For any issue, there are fundamentally three ways an individual can contribute: ## Changelog +- 2.0.6 + - Fix host reference on shutdown + - Change to GitHub Container Registry + - Update CI/CD to build to GitHub Container Registry + - Bump Revision + - 2.0.5 - Bump Revision - Change base docker image from node:12-alpine to node:20-alpine diff --git a/chart/Chart.yaml b/chart/Chart.yaml index 0c0ff57..55e1266 100644 --- a/chart/Chart.yaml +++ b/chart/Chart.yaml @@ -3,4 +3,4 @@ name: packml-sim description: PackML Machine Simulator type: application version: 0.0.2 -appVersion: 2.0.5 +appVersion: 2.0.6 diff --git a/package.json b/package.json index 581994c..16acb61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "packaging-machine-language-mqtt-simulator", - "version": "2.0.5", + "version": "2.0.6", "description": "Manufacturing line simulator interfaced using PackML over MQTT.", "keywords": [ "packml", diff --git a/sonar-project.properties b/sonar-project.properties index 87d1418..8502663 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,6 +1,6 @@ sonar.projectKey=PackagingMachineLanguageSimulator sonar.projectName=Packaging Machine Language Simulator -sonar.projectVersion=2.0.5 +sonar.projectVersion=2.0.6 sonar.language=js sonar.sources=src sonar.sourceEncoding=UTF-8 diff --git a/src/clients/sparkplug.js b/src/clients/sparkplug.js index 35a3413..7bf38b6 100644 --- a/src/clients/sparkplug.js +++ b/src/clients/sparkplug.js @@ -118,7 +118,7 @@ class Client extends events.EventEmitter { { name: "Properties/OS Version", timestamp: timestamp, - value: "2.0.5", + value: "2.0.6", type: "String" }, { From 47117a364222cb382d5e615deaba45d4e60dab04 Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Thu, 24 Oct 2024 16:15:34 -0400 Subject: [PATCH 11/15] Fix Product constructor error message typo --- src/packml-tags.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/packml-tags.js b/src/packml-tags.js index 6d2b287..9bbc5b9 100644 --- a/src/packml-tags.js +++ b/src/packml-tags.js @@ -63,7 +63,7 @@ function Ingredient(index, productIndex) { function Product(index) { if (index === undefined || index === null) { - throw TypeError('Must construct a Prodct with an index') + throw TypeError('Must construct a Product with an index') } return { _index: index, From b1e10469627f5458420f3c1f1c34dfbbfd57d409 Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 25 Oct 2024 08:48:44 -0400 Subject: [PATCH 12/15] Fix packml command reference and call arguments --- src/index.js | 11 +++++----- src/packml-commands.js | 47 +++++++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/index.js b/src/index.js index cf6d6c7..047eee4 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ const logging = require('./logging') const packmlModel = require('./packml-model') const packmlTags = require('./packml-tags') +const packmlCommands = require('./packml-commands') const simulation = require('./simulation') const helper = require('./helper') const mqtt = require('./clients/mqtt'); @@ -145,15 +146,15 @@ client.on('close', () => { // Handle PackML Commands client.on('message', (topic, message) => { if (topic.match(stateCommandTopic)) { - stateCommand(topic, message, state) + packmlCommands.stateCommand(logger, topic, message, state, stateCommandTopic) } else if (topic.match(modeCommandTopic)) { - modeCommand(topic, message, mode) + packmlCommands.modeCommand(logger, message, mode) } else if (topic.match(machineSpeedCommandTopic)) { - machineSpeedCommand(topic, message, tags) + packmlCommands.machineSpeedCommand(logger, topic, message, tags) } else if (topic.match(packmlParameters)) { - parameterCommand(topic, message, tags) + packmlCommands.parameterCommand(logger, topic, message, tags, packmlParameters, changed) } else if (topic.match(packmlProducts)) { - productCommand(topic, message, tags) + packmlCommands.productCommand(logger, topic, message, tags, packmlProducts, changed) } else { logger.debug(`No handle defined for ${topic}`) } diff --git a/src/packml-commands.js b/src/packml-commands.js index a793cff..a8f949b 100644 --- a/src/packml-commands.js +++ b/src/packml-commands.js @@ -1,8 +1,13 @@ #!/usr/bin/env node 'use strict' +// Imports +const packmlModel = require('./packml-model') +const packmlTags = require('./packml-tags') +const helper = require('./helper') + // handle state commands -function stateCommand(topic, message, state) { +exports.stateCommand = (logger, topic, message, state, stateCommandTopic) => { // State Commands const command = topic.match(stateCommandTopic)[1] try { @@ -18,7 +23,7 @@ function stateCommand(topic, message, state) { } // handle mode commands -function modeCommand(message, mode) { +exports.modeCommand = (logger, message, mode) => { if (isNaN(message)) { message = message.toLowerCase() } else { @@ -27,12 +32,12 @@ function modeCommand(message, mode) { if (packmlModel.isUnitMode(message)) { mode.goto(message) } else { - logger.error('Cannot change to unknown UnitMode') + logger.error(`Cannot change to unknown UnitMode: ${message}`) } } // handle machine speed commands -function machineSpeedCommand(topic, message, tags) { +exports.machineSpeedCommand = (logger, topic, message, tags) => { if (Number.isNaN(message)) { logger.error(`Bad request: ${topic} Must be an number`) return @@ -46,7 +51,7 @@ function machineSpeedCommand(topic, message, tags) { } // handle parameter commands -function parameterCommand(topic, message, tags) { +exports.parameterCommand = (logger, topic, message, tags, packmlParameters, changed) => { // Parameters const bits = topic.match(packmlParameters) const index = parseInt(bits[1]) @@ -81,25 +86,25 @@ function parameterCommand(topic, message, tags) { } // handle product commands -function productCommand(topic, message, tags) { +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(index, topic, message, tags) + productCommandForProduct(logger, index, topic, message, tags, changed) } else if (bits.length === 5) { const nextIndex = bits[3] if (bits[0].indexOf('Ingredient')) { - productCommandForIngredient(nextIndex, message, tags) + productCommandForIngredient(nextIndex, bits, message, tags, index) } else { - productCommandForProductParameter(nextIndex, bits, topic, message, tags) + productCommandForProductParameter(logger, nextIndex, bits, topic, message, tags, changed) } } else if (bits.length === 7) { - productCommandForIngredientParameter(bits, topic, message, tags) + productCommandForIngredientParameter(logger, bits, topic, message, tags, changed) } } -function productCommandForProduct(index, topic, message, tags) { +function productCommandForProduct(logger, index, topic, message, tags, changed) { while (tags.status.product.length <= index) { tags.status.product.push(new Proxy(new packmlTags.Product(tags.status.product.length), { set (target, prop, value) { @@ -117,17 +122,17 @@ function productCommandForProduct(index, topic, message, tags) { tags.status.product[index].productId = message } -function productCommandForIngredient(nextIndex, message, tags) { +function productCommandForIngredient(nextIndex, bits, message, tags, index) { while (tags.status.product.length <= index) { - tags.status.product.push(new packmlTags.Product()) + tags.status.product.push(new packmlTags.Product(tags.status.product.length)) } while (tags.status.product[index].ingredient.length <= nextIndex) { - tags.status.product[index].ingredient.push(new packmlTags.Ingredient()) + tags.status.product[index].ingredient.push(new packmlTags.Ingredient(tags.status.product[index].ingredient.length)) } tags.status.product[index].ingredient[nextIndex][helper.camelCase(bits[4])] = parseInt(message) } -function productCommandForProductParameter(nextIndex, bits, topic, message, tags) { +function productCommandForProductParameter(logger, nextIndex, bits, topic, message, tags, changed) { if (bits[4] === 'ID') { message = parseInt(message) if (isNaN(message)) { @@ -142,7 +147,7 @@ function productCommandForProductParameter(nextIndex, bits, topic, message, tags } } while (tags.status.product.length <= index) { - tags.status.product.push(new packmlTags.Product()) + 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), { @@ -155,7 +160,7 @@ function productCommandForProductParameter(nextIndex, bits, topic, message, tags tags.status.product[index].processParameter[nextIndex][helper.camelCase(bits[4])] = message } -function productCommandForIngredientParameter(bits, topic, message, tags) { +function productCommandForIngredientParameter(logger, bits, topic, message, tags, changed) { const ingredientIndex = parseInt(bits[2]) const parameterIndex = parseInt(bits[5]) if (bits[6] === 'ID') { @@ -172,18 +177,18 @@ function productCommandForIngredientParameter(bits, topic, message, tags) { } } while (tags.status.product.length <= index) { - tags.status.product.push(new packmlTags.Product()) + 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.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) } }) } - while (tags.status.product[index].ingredient[ingredientIndex].parameter <= parameterIndex) { - tags.status.product[index].ingredient[ingredientIndex].parameter.push(new packmlTags.Parameter()) + 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[parameterIndex][helper.camelCase(bits[6])] = message } \ No newline at end of file From b0fcde9618f3228e8d7fb65a164b22148c804c24 Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 25 Oct 2024 14:03:33 -0400 Subject: [PATCH 13/15] Fix packml product commands to align with README --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 047eee4..17f143f 100644 --- a/src/index.js +++ b/src/index.js @@ -47,7 +47,7 @@ const stateCommandTopic = new RegExp(String.raw`^${global.config.topicPrefix}\/C const modeCommandTopic = new RegExp(String.raw`^${global.config.topicPrefix}\/Command\/(UnitMode)$`) const machineSpeedCommandTopic = new RegExp(String.raw`^${global.config.topicPrefix}\/Command\/MachSpeed$`) const packmlParameters = new RegExp(String.raw`^${global.config.topicPrefix}\/Command\/Parameter\/(\d*)\/(ID|Name|Unit|Value)$`) -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)))$`) +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)))$`) // PackML State Model let state = new packmlModel.StateMachine() From e38c824adcf0204c0d8b818caf373b1cb52d645a Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 25 Oct 2024 14:04:37 -0400 Subject: [PATCH 14/15] Fix product proxy reflecting value change --- src/packml-commands.js | 91 ++++++++++++++++++++++++------------------ src/packml-tags.js | 7 ++-- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/packml-commands.js b/src/packml-commands.js index a8f949b..528feaf 100644 --- a/src/packml-commands.js +++ b/src/packml-commands.js @@ -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)) { @@ -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) @@ -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 } \ No newline at end of file diff --git a/src/packml-tags.js b/src/packml-tags.js index 9bbc5b9..48ba83d 100644 --- a/src/packml-tags.js +++ b/src/packml-tags.js @@ -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: '', @@ -67,8 +68,8 @@ function Product(index) { } return { _index: index, - productId: 0, - parameter: [], + id: 0, + processParameter: [], ingredient: [] } } From 2fb7811dd95d51af65a1d6fba35d331068040486 Mon Sep 17 00:00:00 2001 From: Tom Hollingworth Date: Fri, 25 Oct 2024 14:05:39 -0400 Subject: [PATCH 15/15] Fix mode command raw byte to string conversion --- src/packml-commands.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/packml-commands.js b/src/packml-commands.js index 528feaf..c605d45 100644 --- a/src/packml-commands.js +++ b/src/packml-commands.js @@ -25,7 +25,7 @@ exports.stateCommand = (logger, topic, message, state, stateCommandTopic) => { // handle mode commands exports.modeCommand = (logger, message, mode) => { if (isNaN(message)) { - message = message.toLowerCase() + message = message.toString().toLowerCase() } else { message = packmlModel.getModeTextByModeInt(message).toLowerCase() }