---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Connecting Legacy Devices via Kepware KEPServerEX (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file that reads tags from legacy devices through the
  OPC UA server interface of Kepware KEPServerEX with the OPC UA connector
  and maps them into an ISA-95 style MQTT topic hierarchy (Example)

metadata:
  name: Kepware KEPServerEX Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  kepwareHost:
    description: Hostname or IP address of the KEPServerEX machine
    type: string
    default: 192.168.1.100

  kepwarePort:
    description: Port of the KEPServerEX OPC UA server endpoint
    type: integer
    default: 49320

  kepwareUsername:
    description: Username of a KEPServerEX user with access to the project
    type: string
    default: ''

  kepwarePassword:
    description: Password of the KEPServerEX user
    type: string
    default: ''

definitions:
  # ISA-95 style topic prefix: <enterprise>/<site>/<area>/<line>/<work-cell>
  MQTT_TOPIC_PREFIX: enterprise/hamburg/stamping/line-3/press-01

resources:
  # Connection to the OPC UA server interface of KEPServerEX.
  # The security options match the default endpoint of the server, and
  # username and password are required because anonymous login is
  # disabled by default.
  kepwareConnection:
    type: Cybus::Connection
    properties:
      protocol: Opcua
      targetState: connected
      connection:
        host: !ref kepwareHost
        port: !ref kepwarePort
        username: !ref kepwareUsername
        password: !ref kepwarePassword
        options:
          securityPolicy: Basic256Sha256
          messageSecurityMode: SignAndEncrypt

  # The NodeIds below are project-specific. Replace the namespace index and
  # the <channel>.<device>.<tag> paths with the values from your own
  # KEPServerEX project and verify them with an OPC UA browser.

  # Stroke counter of the press (DWord)
  strokeCounter:
    type: Cybus::Endpoint
    properties:
      protocol: Opcua
      connection: !ref kepwareConnection
      subscribe:
        nodeId: ns=2;s=Stamping.Press01.StrokeCounter
        samplingInterval: 1000
        publishInterval: 1000

  # Machine running flag (Boolean)
  machineRunning:
    type: Cybus::Endpoint
    properties:
      protocol: Opcua
      connection: !ref kepwareConnection
      subscribe:
        nodeId: ns=2;s=Stamping.Press01.MachineRunning
        samplingInterval: 1000
        publishInterval: 1000

  # Hydraulic oil temperature (Float)
  oilTemperature:
    type: Cybus::Endpoint
    properties:
      protocol: Opcua
      connection: !ref kepwareConnection
      subscribe:
        nodeId: ns=2;s=Stamping.Press01.OilTemperature
        samplingInterval: 2000
        publishInterval: 2000

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref strokeCounter
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/stroke-counter'
        - subscribe:
            endpoint: !ref machineRunning
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/machine/running'
        - subscribe:
            endpoint: !ref oilTemperature
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/oil-temperature'
