---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Connecting a SIMATIC S7-1500 via S7+ (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file that reads machine data from a Siemens SIMATIC
  S7-1500 PLC with the S7+ connector using symbolic addressing and maps it
  into an ISA-95 style MQTT topic hierarchy (Example)

metadata:
  name: SIMATIC S7-1500 S7+ Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  plcHost:
    description: Hostname or IP address of the SIMATIC S7-1500
    type: string
    default: 192.168.1.100

  plcPort:
    description: TCP port of the SIMATIC S7-1500
    type: integer
    default: 102

definitions:
  # Symbolic paths from the TIA Portal project:
  # <PLC name>.Blocks.<data block name>.<tag name>
  MACHINE_STATE: PLC_1.Blocks.ProductionDB.MachineState
  GOOD_PARTS_COUNTER: PLC_1.Blocks.ProductionDB.GoodPartsCounter
  SPINDLE_TEMPERATURE: PLC_1.Blocks.ProductionDB.SpindleTemperature

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

resources:
  # Connection to the SIMATIC S7-1500 (S7+ with symbolic addressing)
  s71500Connection:
    type: Cybus::Connection
    properties:
      protocol: S7plus
      targetState: connected
      connection:
        host: !ref plcHost
        port: !ref plcPort
        rack: 0
        slot: 1
        ConnType: TCPIPTIA # Loads the TIA symbols and enables symbolic addressing
        connectionTimeout: 30000 # Increase for TIA Portal projects with many symbols
        connectionStrategy:
          initialDelay: 5000
          maxDelay: 30000
          incrementFactor: 2

  # Machine state code (INT tag in the ProductionDB data block)
  machineState:
    type: Cybus::Endpoint
    properties:
      protocol: S7plus
      connection: !ref s71500Connection
      subscribe:
        nodeId: !ref MACHINE_STATE
        dataType: INT
        interval: 1000

  # Good parts counter (DINT tag, changes slowly, longer poll interval)
  goodPartsCounter:
    type: Cybus::Endpoint
    properties:
      protocol: S7plus
      connection: !ref s71500Connection
      subscribe:
        nodeId: !ref GOOD_PARTS_COUNTER
        dataType: DINT
        interval: 2000

  # Spindle temperature (REAL tag)
  spindleTemperature:
    type: Cybus::Endpoint
    properties:
      protocol: S7plus
      connection: !ref s71500Connection
      subscribe:
        nodeId: !ref SPINDLE_TEMPERATURE
        dataType: REAL
        interval: 2000

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref machineState
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/state'
        - subscribe:
            endpoint: !ref goodPartsCounter
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/counter/good-parts'
        - subscribe:
            endpoint: !ref spindleTemperature
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/temperature/spindle'
