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

description: >
  Service commissioning file migrated from the Neuron southbound node
  modbus-tcp-1, group group-1 (Example)

metadata:
  name: EMQX Neuron Migration Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  plcHost:
    description: Host of the southbound node modbus-tcp-1
    type: string
    default: 192.168.1.100

  plcPort:
    description: Port of the southbound node modbus-tcp-1
    type: integer
    default: 502

definitions:
  # Topics mirror the Neuron structure <node>/<group>/<tag>
  MQTT_TOPIC_PREFIX: modbus-tcp-1/group-1

resources:
  # Southbound node "modbus-tcp-1" (Modbus TCP)
  modbusTcp1Connection:
    type: Cybus::Connection
    properties:
      protocol: Modbus
      targetState: connected
      connection:
        host: !ref plcHost
        port: !ref plcPort

  # Tag "temperature": 1!400001, INT16, read
  temperature:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref modbusTcp1Connection
      subscribe:
        fc: 3
        address: 0
        length: 1
        dataType: int16BE
        interval: 2000

  # Tag "setpoint": 1!400002, UINT16, read + write (attribute 3)
  setpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref modbusTcp1Connection
      subscribe:
        fc: 3
        address: 1
        length: 1
        dataType: uint16BE
        interval: 2000

  setpointWrite:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref modbusTcp1Connection
      write:
        fc: 6
        address: 1
        length: 1
        dataType: uint16BE

  # Tag "flow_rate": 1!400010#LL, FLOAT, read + subscribe (attribute 5)
  # Endianness suffix #LL translated to floatLE: verify against live values
  flowRate:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref modbusTcp1Connection
      subscribe:
        fc: 3
        address: 9
        length: 2
        dataType: floatLE
        interval: 2000

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref temperature
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/temperature'
        - subscribe:
            endpoint: !ref setpoint
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/setpoint'
        - subscribe:
            topic: !sub '${MQTT_TOPIC_PREFIX}/setpoint/set'
          publish:
            endpoint: !ref setpointWrite
        # Subscribe attribute: only publish on change of value
        - subscribe:
            endpoint: !ref flowRate
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/flow-rate'
          rules:
            - cov:
                key: value
