---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Connecting an Allen-Bradley ControlLogix PLC (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file that reads tags from an Allen-Bradley
  ControlLogix PLC over EtherNet/IP and maps them into an ISA-95 style
  MQTT topic hierarchy (Example)

metadata:
  name: Allen-Bradley ControlLogix Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  controllogixHost:
    description: Hostname or IP address of the EtherNet/IP port of the controller
    type: string
    default: 192.168.1.100

  controllogixSlot:
    description: Chassis slot number of the controller (0 for CompactLogix)
    type: integer
    default: 0

definitions:
  # ISA-95 style topic prefix: <enterprise>/<site>/<area>/<line>/<work-cell>
  MQTT_TOPIC_PREFIX: enterprise/hamburg/packaging/line-1/palletizer-01

resources:
  # Connection to the controller over EtherNet/IP (CIP explicit messaging,
  # TCP port 44818). The slot property routes the connection to the
  # controller in the chassis.
  controllogixConnection:
    type: Cybus::Connection
    properties:
      protocol: EthernetIp
      targetState: connected
      connection:
        host: !ref controllogixHost
        slot: !ref controllogixSlot
        # Scan the subscribed tags for changes every 500 ms. Connectware
        # publishes a value only when it has changed.
        scanRate: 500
        connectionStrategy:
          initialDelay: 1000
          maxDelay: 30000
          incrementFactor: 2

  # Controller-scoped tags: no programName, the tag name alone is enough.
  # MachineState is a DINT holding the state code of the machine.
  machineState:
    type: Cybus::Endpoint
    properties:
      protocol: EthernetIp
      connection: !ref controllogixConnection
      subscribe:
        tagName: MachineState

  # GoodPartsCount is a DINT counting the parts produced without defects.
  goodPartsCount:
    type: Cybus::Endpoint
    properties:
      protocol: EthernetIp
      connection: !ref controllogixConnection
      subscribe:
        tagName: GoodPartsCount

  # ZoneTemperature is a REAL holding the temperature of the sealing zone.
  zoneTemperature:
    type: Cybus::Endpoint
    properties:
      protocol: EthernetIp
      connection: !ref controllogixConnection
      subscribe:
        tagName: ZoneTemperature

  # Program-scoped tag: CycleTime is a REAL that is local to the program
  # "Palletizing", so the endpoint sets programName in addition to tagName.
  cycleTime:
    type: Cybus::Endpoint
    properties:
      protocol: EthernetIp
      connection: !ref controllogixConnection
      subscribe:
        tagName: CycleTime
        programName: Palletizing

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref machineState
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/machine/state'
        - subscribe:
            endpoint: !ref goodPartsCount
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/counters/good-parts'
        - subscribe:
            endpoint: !ref zoneTemperature
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/temperature/sealing-zone'
        - subscribe:
            endpoint: !ref cycleTime
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/cycle-time'
