---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Migrating from Node-RED (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file migrated from the Node-RED flow
  "Line 1 - Filling Machine" (Example)

metadata:
  name: Node-RED Migration Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  plcHost:
    description: Host of the modbus-client node "Filling Machine PLC"
    type: string
    default: 192.168.1.100

  plcPort:
    description: Port of the modbus-client node "Filling Machine PLC"
    type: integer
    default: 502

resources:
  # Configuration node "Filling Machine PLC" (modbus-client)
  fillingMachineConnection:
    type: Cybus::Connection
    properties:
      protocol: Modbus
      targetState: connected
      connection:
        host: !ref plcHost
        port: !ref plcPort

  # Node "Read fill level": HoldingRegister, adr 0, rate 1 s
  # The downstream function node only uses the first register.
  fillLevel:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref fillingMachineConnection
      subscribe:
        fc: 3
        address: 0
        length: 1
        dataType: int16BE
        interval: 1000

  # Function node "Scale to liters" became a JSONata transform rule:
  # msg.payload = { fillLevel: msg.payload[0] / 10 }
  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref fillLevel
          publish:
            topic: factory/line1/filling/fill-level
          rules:
            - transform:
                expression: |
                  {
                    "timestamp": timestamp,
                    "fillLevel": value / 10
                  }
