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

description: >
  Service commissioning file migrated from the Litmus Edge DeviceHub
  device plc_1 (Modbus TCP) with three tags (Example)

metadata:
  name: Litmus Edge Migration Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  plc1Host:
    description: IP address of the device plc_1 (from the template export)
    type: string
    default: 192.168.1.100

  plc1Port:
    description: Modbus/TCP port of the device plc_1
    type: integer
    default: 502

definitions:
  # Topics mirror the Litmus alias topic structure <deviceName>/<tagName>
  MQTT_TOPIC_PREFIX: plc-1

resources:
  # DeviceHub device "plc_1" (Modbus TCP, station ID 1)
  plc1Connection:
    type: Cybus::Connection
    properties:
      protocol: Modbus
      targetState: connected
      connection:
        host: !ref plc1Host
        port: !ref plc1Port

  # Tag "motor_speed": 400001, int16, polling interval 1 s
  motorSpeed:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref plc1Connection
      subscribe:
        fc: 3
        address: 0
        length: 1
        dataType: int16BE
        interval: 1000

  # Tag "oil_temperature": 400010, float32, polling interval 5 s
  oilTemperature:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref plc1Connection
      subscribe:
        fc: 3
        address: 9
        length: 2
        dataType: floatBE
        interval: 5000

  # Tag "machine_running": 000017, bool, polling interval 1 s
  machineRunning:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref plc1Connection
      subscribe:
        fc: 1
        address: 16
        length: 1
        dataType: boolean
        interval: 1000

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref motorSpeed
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/motor-speed'
        # publishcov was enabled: only publish on change of value
        - subscribe:
            endpoint: !ref oilTemperature
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/oil-temperature'
          rules:
            - cov:
                key: value
        - subscribe:
            endpoint: !ref machineRunning
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/machine-running'
          rules:
            - cov:
                key: value
