# ----------------------------------------------------------------------------#
# Commissioning File
# ----------------------------------------------------------------------------#
# Copyright: Cybus GmbH (2020)
# Contact: support@cybus.io
# ----------------------------------------------------------------------------#
# Source Interface Definition - Modbus TCP
# ----------------------------------------------------------------------------#

description: |
  Sample commissioning file for Modbus TCP protocol connectivity and data mapping

metadata:

  name: ModBus TCP Protocol Connectivity
  icon: https://www.cybus.io/wp-content/uploads/2019/03/Cybus-logo-Claim-lang.svg
  provider: cybus
  homepage: https://www.cybus.io
  version: 0.0.1

parameters:
  IP_Address:
    type: string
    default: 192.168.10.30

  Port_Number:
    type: number
    default: 502

  initialReconnectDelay:
    type: integer
    default: 1000

  maxReconnectDelay:
    type: integer
    default: 30000

  factorReconnectDelay:
    type: integer
    default: 2

resources:

  modbusConnection:
    type: Cybus::Connection
    properties:
      protocol: Modbus
      targetState: connected
      connection:
        host: !ref IP_Address
        port: !ref Port_Number
        connectionStrategy:
          initialDelay: !ref initialReconnectDelay
          maxDelay: !ref maxReconnectDelay
          incrementFactor: !ref factorReconnectDelay

  readCoil:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref modbusConnection
      subscribe:
        fc: 1
        length: 2
        interval: 1000
        address: 3
        dataType: boolean

  writeCoil:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref modbusConnection
      write:
        fc: 5
        length: 2
        address: 3
        dataType: boolean

  readRegister:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref modbusConnection
      subscribe:
        fc: 3
        length: 2
        interval: 1000
        address: 1
        dataType: int16BE

  writeRegister:
    type: Cybus::Endpoint
    properties:
      protocol: Modbus
      connection: !ref modbusConnection
      write:
        fc: 6
        length: 2
        address: 1
        dataType: int16BE

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref readCoil
          publish:
            topic: !sub '${Cybus::MqttRoot}/read/coil'
        - subscribe:
            topic: !sub '${Cybus::MqttRoot}/write/coil'
          publish:
            endpoint: !ref writeCoil
        - subscribe:
            endpoint: !ref readRegister
          publish:
            topic: !sub '${Cybus::MqttRoot}/read/register'
        - subscribe:
            topic: !sub '${Cybus::MqttRoot}/write/register'
          publish:
            endpoint: !ref writeRegister
