---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Connecting a SINUMERIK CNC Control (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file that reads machine, spindle, and tool data from
  a Siemens SINUMERIK CNC control with the SINUMERIK connector and maps it
  into an ISA-95 style MQTT topic hierarchy (Example)

metadata:
  name: SINUMERIK CNC Control Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  sinumerikHost:
    description: Hostname or IP address of the SINUMERIK OPC UA server
    type: string
    default: 192.168.1.100

  sinumerikPort:
    description: Port of the SINUMERIK OPC UA server
    type: integer
    default: 4840

  sinumerikUsername:
    description: Username of the SINUMERIK OPC UA user
    type: string
    default: ''

  sinumerikPassword:
    description: Password of the SINUMERIK OPC UA user
    type: string
    default: ''

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

resources:
  # Connection to the SINUMERIK "Access MyMachine / OPC UA" interface
  sinumerikConnection:
    type: Cybus::Connection
    properties:
      protocol: Sinumerik
      targetState: connected
      connection:
        host: !ref sinumerikHost
        port: !ref sinumerikPort
        username: !ref sinumerikUsername
        password: !ref sinumerikPassword

  # Static machine information: NC type, NC version, HMI language
  machineInfo:
    type: Cybus::Endpoint
    properties:
      protocol: Sinumerik
      connection: !ref sinumerikConnection
      subscribe:
        type: poll
        method: getMachineInfo
        pollInterval: 60000

  # Machine state: program name, program status, power-on state, runtime
  machineState:
    type: Cybus::Endpoint
    properties:
      protocol: Sinumerik
      connection: !ref sinumerikConnection
      subscribe:
        type: poll
        method: getMachineState
        pollInterval: 5000

  # Actual and commanded spindle speed
  spindleSpeed:
    type: Cybus::Endpoint
    properties:
      protocol: Sinumerik
      connection: !ref sinumerikConnection
      subscribe:
        type: poll
        method: getSpindleSpeed
        pollInterval: 2000

  # Actual and commanded constant cutting rate
  spindleFeed:
    type: Cybus::Endpoint
    properties:
      protocol: Sinumerik
      connection: !ref sinumerikConnection
      subscribe:
        type: poll
        method: getSpindleFeed
        pollInterval: 2000

  # Tool numbers in all magazine places (0 = no tool loaded)
  toolsInMagazines:
    type: Cybus::Endpoint
    properties:
      protocol: Sinumerik
      connection: !ref sinumerikConnection
      subscribe:
        type: poll
        method: getToolsInMagazines
        pollInterval: 10000

  # On-demand tool read with the request/response pattern:
  # tool = getTool(toolNo)
  getTool:
    type: Cybus::Endpoint
    properties:
      protocol: Sinumerik
      connection: !ref sinumerikConnection
      read:
        method: getTool

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref machineInfo
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/machine/info'
        - subscribe:
            endpoint: !ref machineState
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/machine/state'
        - subscribe:
            endpoint: !ref spindleSpeed
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/spindle/speed'
        - subscribe:
            endpoint: !ref spindleFeed
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/spindle/feed'
        - subscribe:
            endpoint: !ref toolsInMagazines
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/tools/magazines'
