---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# Connecting a Janitza UMG Power Analyzer (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file that reads power quality and energy measurements
  from a Janitza UMG 96RM-E power analyzer over Modbus/TCP and maps them into
  an ISA-95 style MQTT topic hierarchy (Example)

metadata:
  name: Janitza UMG 96RM-E Example
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  umgHost:
    description: Hostname or IP address of the Janitza UMG power analyzer
    type: string
    default: 192.168.1.100

  umgPort:
    description: Modbus/TCP port of the Janitza UMG power analyzer
    type: integer
    default: 502

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

resources:
  # Connection to the Janitza UMG (Modbus/TCP over the Ethernet port)
  umgConnection:
    type: Cybus::Connection
    properties:
      protocol: Modbus
      targetState: connected
      connection:
        host: !ref umgHost
        port: !ref umgPort
        connectionStrategy:
          initialDelay: 1000
          maxDelay: 30000
          incrementFactor: 2

  # Instantaneous measured values: Float format, two registers each,
  # decoded with floatBE (IEEE 754, big-endian register order).
  # Addresses from the UMG 96RM-E Modbus address list, chapter
  # "Frequently required readings". Other UMG models use different
  # addresses; see the Modbus address list of your model on janitza.com.
  voltageL1:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref umgConnection
      subscribe:
        fc: 3
        address: 19000
        length: 2
        dataType: floatBE
        interval: 2000

  currentL1:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref umgConnection
      subscribe:
        fc: 3
        address: 19012
        length: 2
        dataType: floatBE
        interval: 2000

  activePowerTotal:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref umgConnection
      subscribe:
        fc: 3
        address: 19026
        length: 2
        dataType: floatBE
        interval: 2000

  apparentPowerTotal:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref umgConnection
      subscribe:
        fc: 3
        address: 19034
        length: 2
        dataType: floatBE
        interval: 2000

  reactivePowerTotal:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref umgConnection
      subscribe:
        fc: 3
        address: 19042
        length: 2
        dataType: floatBE
        interval: 2000

  cosPhiL1:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref umgConnection
      subscribe:
        fc: 3
        address: 19044
        length: 2
        dataType: floatBE
        interval: 2000

  frequency:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref umgConnection
      subscribe:
        fc: 3
        address: 19050
        length: 2
        dataType: floatBE
        interval: 2000

  # Energy counters: Float format, two registers each. Counters change
  # slowly, so a longer poll interval is sufficient.
  activeEnergyConsumed:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref umgConnection
      subscribe:
        fc: 3
        address: 19068
        length: 2
        dataType: floatBE
        interval: 10000

  activeEnergyDelivered:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref umgConnection
      subscribe:
        fc: 3
        address: 19076
        length: 2
        dataType: floatBE
        interval: 10000

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref voltageL1
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/voltage/l1-n'
        - subscribe:
            endpoint: !ref currentL1
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/current/l1'
        - subscribe:
            endpoint: !ref activePowerTotal
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/power/active'
        - subscribe:
            endpoint: !ref apparentPowerTotal
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/power/apparent'
        - subscribe:
            endpoint: !ref reactivePowerTotal
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/power/reactive'
        - subscribe:
            endpoint: !ref cosPhiL1
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/power-factor/l1'
        - subscribe:
            endpoint: !ref frequency
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/frequency'
        - subscribe:
            endpoint: !ref activeEnergyConsumed
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/energy/active-consumed'
        - subscribe:
            endpoint: !ref activeEnergyDelivered
          publish:
            topic: !sub '${MQTT_TOPIC_PREFIX}/energy/active-delivered'
