# SPDX-License-Identifier: MIT

name: energy_meter_gateway__modbus_mqtt_opcua
description: Polls iEM3000 frequency via Modbus TCP and mirrors it to MQTT and a read-only OPC UA node.

meta_parameters:
  meter_host:
    type: str
    default: 127.0.0.1
  meter_port:
    type: int
    default: 5023
  meter_unit_id:
    type: int
    default: 1
  mqtt_broker_host:
    type: str
    default: mosquitto-server
  mqtt_broker_port:
    type: int
    default: 1883
  mqtt_topic_prefix:
    type: str
    default: spx/examples/energy-gateway
  opcua_endpoint:
    type: str
    default: opc.tcp://0.0.0.0:4846/spx/energy-gateway

attributes:
  frequency_hz: -1.0

communication:
  - modbus_master:
      poll_interval: 0.2
      timeout: 0.25
      max_retries: 0
      max_failures: 100
      bindings:
        - name: frequency_hz
          host: "$param(meter_host)"
          port: "$param(meter_port)"
          slave_id: "$param(meter_unit_id)"
          area: i_r
          address: 3110
          length: 2
          codec: float
          bit_order: Big Endian
          direction: inbound
          attribute: "$in(frequency_hz)"
  - mqtt:
      broker: "$param(mqtt_broker_host)"
      port: "$param(mqtt_broker_port)"
      topic_prefix: "$param(mqtt_topic_prefix)"
      publish_interval: 0.2
      default_qos: 1
      default_retain: false
      bindings:
        - name: frequency_hz
          attribute: "$in(frequency_hz)"
          topic: telemetry/frequency_hz
          direction: publish
          payload_codec: json
  - opcua_server:
      endpoint: "$param(opcua_endpoint)"
      namespace_uri: urn:spx:examples:energy-gateway
      poll_interval: 0.2
      security:
        policy: NoSecurity
        user_tokens:
          - type: anonymous
      sections:
        - name: energy_gateway
          browse_path: [EnergyGateway]
          bindings:
            - name: frequency_hz
              direction: outbound
              read_attribute: "#attr(frequency_hz)"
              node:
                browse_path: [FrequencyHz]
                data_type: Double
