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

description: >
  Service commissioning file migrated from the KEPServerEX project
  PlantFloorProject, channel ModbusTCP, device PLC1 (Example)

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

parameters:
  plc1Host:
    description: IP address of the device PLC1 (from servermain.DEVICE_ID_STRING)
    type: string
    default: 192.168.1.100

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

definitions:
  # Topics mirror the KEPServerEX hierarchy <channel>/<device>/<tag>
  MQTT_TOPIC_PREFIX: modbustcp/plc1

resources:
  # KEPServerEX channel "ModbusTCP", device "PLC1"
  plc1Connection:
    type: Cybus::Connection
    properties:
      protocol: Modbus
      targetState: connected
      connection:
        host: !ref plc1Host
        port: !ref plc1Port

  # Tag "MotorSpeed": 400001, Word, read/write, 100 ms
  motorSpeed:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref plc1Connection
      subscribe:
        fc: 3
        address: 0
        length: 1
        dataType: uint16BE
        interval: 100

  motorSpeedWrite:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref plc1Connection
      write:
        fc: 6
        address: 0
        length: 1
        dataType: uint16BE

  # Tag "Temperature": 400002, Float, read only, 1000 ms
  temperature:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref plc1Connection
      subscribe:
        fc: 3
        address: 1
        length: 2
        dataType: floatBE
        interval: 1000

  # Tag "RunningFlag": 000001, Boolean, read only, 100 ms
  runningFlag:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref plc1Connection
      subscribe:
        fc: 1
        address: 0
        length: 1
        dataType: boolean
        interval: 100

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref motorSpeed
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/motor-speed'
        - subscribe:
            topic: !sub '${MQTT_TOPIC_PREFIX}/motor-speed/set'
          publish:
            endpoint: !ref motorSpeedWrite
        - subscribe:
            endpoint: !ref temperature
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/temperature'
        - subscribe:
            endpoint: !ref runningFlag
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/running-flag'
