---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Connecting MTConnect Machines via SHDR (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file that reads machine data from an MTConnect
  adapter via SHDR and maps it into an ISA-95 style MQTT topic hierarchy
  (Example)

metadata:
  name: MTConnect SHDR Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  adapterHost:
    description: Hostname or IP address of the MTConnect adapter
    type: string
    default: 192.168.1.100

  adapterPort:
    description: TCP port of the MTConnect adapter
    type: integer
    default: 7878

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

resources:
  # Connection to the MTConnect adapter (SHDR over TCP)
  shdrConnection:
    type: Cybus::Connection
    properties:
      protocol: Shdr
      targetState: connected
      connection:
        host: !ref adapterHost
        port: !ref adapterPort
        connectionStrategy:
          initialDelay: 1000
          maxDelay: 30000
          incrementFactor: 2

  # The data item keys below follow the conventions of the MTConnect
  # reference adapters. Check the data items of your adapter and adjust
  # the keys accordingly.
  availability:
    type: Cybus::Endpoint
    properties:
      protocol: Shdr
      connection: !ref shdrConnection
      subscribe:
        key: avail
        type: event

  execution:
    type: Cybus::Endpoint
    properties:
      protocol: Shdr
      connection: !ref shdrConnection
      subscribe:
        key: execution
        type: event

  partCount:
    type: Cybus::Endpoint
    properties:
      protocol: Shdr
      connection: !ref shdrConnection
      subscribe:
        key: part_count
        type: event

  xAxisPosition:
    type: Cybus::Endpoint
    properties:
      protocol: Shdr
      connection: !ref shdrConnection
      subscribe:
        key: Xact
        type: sample

  temperatureCondition:
    type: Cybus::Endpoint
    properties:
      protocol: Shdr
      connection: !ref shdrConnection
      subscribe:
        key: temp_cond
        type: condition

  # Subscribes to the complete unparsed SHDR stream. Useful to discover
  # the data item keys of an adapter. Remove it once you know the keys.
  raw:
    type: Cybus::Endpoint
    properties:
      protocol: Shdr
      connection: !ref shdrConnection
      subscribe:
        key: raw
        type: sample

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref availability
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/availability'
        - subscribe:
            endpoint: !ref execution
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/execution'
        - subscribe:
            endpoint: !ref partCount
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/part-count'
        - subscribe:
            endpoint: !ref xAxisPosition
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/axes/x/position'
        - subscribe:
            endpoint: !ref temperatureCondition
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/conditions/temperature'
        - subscribe:
            endpoint: !ref raw
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/raw'
