---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Connecting a WAGO PFC200 Controller (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file that reads PLC variables from a WAGO PFC200
  controller over Modbus/TCP and maps them into an ISA-95 style MQTT topic
  hierarchy (Example)

metadata:
  name: WAGO PFC200 Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  pfc200Host:
    description: Hostname or IP address of the WAGO PFC200
    type: string
    default: 192.168.1.100

  pfc200Port:
    description: Modbus/TCP port of the WAGO PFC200
    type: integer
    default: 502

definitions:
  # ISA-95 style topic prefix: <enterprise>/<site>/<area>/<line>
  MQTT_TOPIC_PREFIX: enterprise/hamburg/packaging/line-2

resources:
  # Connection to the WAGO PFC200 (Modbus/TCP server of the CODESYS runtime)
  pfc200Connection:
    type: Cybus::Connection
    properties:
      protocol: Modbus
      targetState: connected
      connection:
        host: !ref pfc200Host
        port: !ref pfc200Port
        connectionStrategy:
          initialDelay: 1000
          maxDelay: 30000
          incrementFactor: 2

  # The register offsets below are project-specific examples. They come from
  # the Modbus TCP Slave Device I/O Mapping tab of the CODESYS project, where
  # PLC variables are assigned to input registers (read with function code 4).
  machineState:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref pfc200Connection
      subscribe:
        fc: 4
        address: 0
        length: 1
        dataType: uint16BE
        interval: 1000

  lineSpeed:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref pfc200Connection
      subscribe:
        fc: 4
        address: 1
        length: 1
        dataType: int16BE
        interval: 1000

  # REAL values span two registers. The CODESYS runtime on the PFC200
  # typically places the low word first, which matches floatBEWS (big-endian
  # with word swap). If the values look implausible, switch to floatBE.
  motorTemperature:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref pfc200Connection
      subscribe:
        fc: 4
        address: 2
        length: 2
        dataType: floatBEWS
        interval: 2000

  airPressure:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref pfc200Connection
      subscribe:
        fc: 4
        address: 4
        length: 2
        dataType: floatBEWS
        interval: 2000

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref machineState
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/state'
        - subscribe:
            endpoint: !ref lineSpeed
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/line-speed'
        - subscribe:
            endpoint: !ref motorTemperature
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/temperature/motor'
        - subscribe:
            endpoint: !ref airPressure
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/pressure/air'
