---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Connecting WERMA Signal Towers (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file that reads machine states from WERMA signal
  towers with the Werma WIN Ethernet connector and maps them into an ISA-95
  style MQTT topic hierarchy (Example)

metadata:
  name: WERMA Signal Towers Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  wermaHost:
    description: Hostname or IP address of the Werma WIN Ethernet master
    type: string
    default: 192.168.1.100

  wermaPort:
    description: Port of the Werma WIN Ethernet master
    type: integer
    default: 15200

  wermaPassword:
    description: Password of the Werma WIN Ethernet master
    type: string
    default: b1b0

  millingMac:
    description: MAC address of the transmitter on the milling machine tower
    type: string
    default: 00BCDD

  latheMac:
    description: MAC address of the transmitter on the lathe tower
    type: string
    default: 00BCDE

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

resources:
  # Connection to the Werma WIN Ethernet master
  wermaConnection:
    type: Cybus::Connection
    properties:
      protocol: Werma
      targetState: connected
      connection:
        host: !ref wermaHost
        port: !ref wermaPort
        password: !ref wermaPassword
        connectionStrategy:
          initialDelay: 1000
          maxDelay: 10000
          incrementFactor: 2

  # All data points of the milling machine tower: tiers t1-t4 and RSSI
  millingTowerState:
    type: Cybus::Endpoint
    properties:
      protocol: Werma
      connection: !ref wermaConnection
      subscribe:
        mac: !ref millingMac
        field: all

  # Red tier of the milling machine tower (tier 1 in this example)
  millingRedTier:
    type: Cybus::Endpoint
    properties:
      protocol: Werma
      connection: !ref wermaConnection
      subscribe:
        mac: !ref millingMac
        field: t1

  # Wireless signal strength of the milling machine transmitter
  millingRssi:
    type: Cybus::Endpoint
    properties:
      protocol: Werma
      connection: !ref wermaConnection
      subscribe:
        mac: !ref millingMac
        field: rssi

  # All data points of the lathe tower
  latheTowerState:
    type: Cybus::Endpoint
    properties:
      protocol: Werma
      connection: !ref wermaConnection
      subscribe:
        mac: !ref latheMac
        field: all

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref millingTowerState
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/milling-01/tower/state'
        - subscribe:
            endpoint: !ref millingRedTier
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/milling-01/tower/red'
        - subscribe:
            endpoint: !ref millingRssi
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/milling-01/tower/rssi'
        - subscribe:
            endpoint: !ref latheTowerState
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/lathe-01/tower/state'
        # Machine-stop event: only forward when the red tier switches to 'on'
        - subscribe:
            endpoint: !ref millingRedTier
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/milling-01/events/machine-stop'
          rules:
            - cov:
                key: value
            - filter:
                expression: value = 'on'
