---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Connecting an ifm IO-Link Master (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file that reads IO-Link process data from an ifm
  IO-Link master through the ifm IoT Core JSON-over-HTTP interface and maps
  it into an ISA-95 style MQTT topic hierarchy (Example)

metadata:
  name: ifm IO-Link Master Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  ifmHost:
    description: Hostname or IP address of the IoT port of the ifm IO-Link master
    type: string
    default: 192.168.1.100

  ifmPort:
    description: HTTP port of the ifm IoT Core interface
    type: integer
    default: 80

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

resources:
  # Connection to the ifm IoT Core (JSON over HTTP)
  ifmConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      targetState: connected
      connection:
        scheme: http
        host: !ref ifmHost
        port: !ref ifmPort
        # Probe an IoT Core address that always answers, so that the
        # connection state reflects the availability of the interface.
        probeMethod: GET
        probePath: /devicetag/applicationtag/getdata
        connectionStrategy:
          initialDelay: 1000
          maxDelay: 30000
          incrementFactor: 2

  # Process input data (pdin) of the IO-Link device on port X01.
  # In this example, an ifm TN2531 temperature sensor is connected to X01.
  # The transform rule decodes the hexadecimal string according to the
  # IODD of the sensor: bits 2 to 15 carry the temperature value with a
  # resolution of 0.1 degrees Celsius.
  temperaturePort1:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref ifmConnection
      subscribe:
        path: /iolinkmaster/port[1]/iolinkdevice/pdin/getdata
        interval: 1000
      rules:
        - transform:
            expression: |
              {
                "timestamp": timestamp,
                "value": $floor($number("0x" & value.data.value) / 4) / 10
              }

  # Process input data (pdin) of the IO-Link device on port X02, published
  # as the raw hexadecimal string. Decode it with a transform rule according
  # to the IODD of the connected sensor.
  rawProcessDataPort2:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref ifmConnection
      subscribe:
        path: /iolinkmaster/port[2]/iolinkdevice/pdin/getdata
        interval: 1000
      rules:
        - transform:
            expression: |
              {
                "timestamp": timestamp,
                "value": value.data.value
              }

  # Application tag (name) of the IO-Link master, read on demand by
  # publishing an empty JSON object to the /req topic of this endpoint.
  applicationTag:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref ifmConnection
      topic: device-info/application-tag
      read:
        path: /devicetag/applicationtag/getdata

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref temperaturePort1
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/port-1/temperature'
        - subscribe:
            endpoint: !ref rawProcessDataPort2
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/port-2/process-data-raw'
