---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Connecting a Schneider Modicon M580 PLC (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file that reads located variables from a Schneider
  Electric Modicon M580 PLC over Modbus/TCP and maps them into an ISA-95
  style MQTT topic hierarchy (Example)

metadata:
  name: Schneider Modicon M580 Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  m580Host:
    description: Hostname or IP address of the Modicon M580
    type: string
    default: 192.168.1.100

  m580Port:
    description: Modbus/TCP port of the Modicon M580
    type: integer
    default: 502

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

resources:
  # Connection to the Modbus/TCP server of the M580 CPU
  m580Connection:
    type: Cybus::Connection
    properties:
      protocol: Modbus
      targetState: connected
      connection:
        host: !ref m580Host
        port: !ref m580Port
        connectionStrategy:
          initialDelay: 1000
          maxDelay: 30000
          incrementFactor: 2

  # The %MW addresses below are project-specific examples. Replace them with
  # the located variables of your own Control Expert project. The located
  # variable %MWn is read at Modbus address n with function code 3.

  # INT at %MW100: one register, decoded as int16BE.
  machineState:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref m580Connection
      subscribe:
        fc: 3
        address: 100
        length: 1
        dataType: int16BE
        interval: 1000

  # DINT at %MW102 and %MW103: two registers. int32BE expects the most
  # significant word at the lower address. Verify against a known value;
  # if the words arrive swapped, exchange them in the PLC application.
  partsCounter:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref m580Connection
      subscribe:
        fc: 3
        address: 102
        length: 2
        dataType: int32BE
        interval: 2000

  # REAL at %MW104 and %MW105: two registers. floatBEWS matches the default
  # word order of located variables in Control Expert (least significant
  # word first). If values look implausible, use floatBE instead.
  coolantTemperature:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref m580Connection
      subscribe:
        fc: 3
        address: 104
        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 partsCounter
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/counter/parts'
        - subscribe:
            endpoint: !ref coolantTemperature
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/temperature/coolant'
