---
# ----------------------------------------------------------------------------
# Service Commissioning File
# ----------------------------------------------------------------------------
# Copyright: Cybus GmbH
# Contact: support@cybus.io
# ----------------------------------------------------------------------------
# HiveMQ Integration (Example)
# ----------------------------------------------------------------------------

description: >
  Service commissioning file for the bidirectional integration between
  Connectware and a HiveMQ broker (Example)

metadata:
  name: HiveMQ Integration
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  hivemqHost:
    description: Hostname of the HiveMQ broker or HiveMQ Cloud cluster URL
    type: string
    default: a1b2c3d4e5f6.s1.eu.hivemq.cloud

  hivemqUsername:
    description: Username of the MQTT credential created for Connectware
    type: string
    default: connectware

  hivemqPassword:
    description: Password of the MQTT credential
    type: string

  clientId:
    description: MQTT client ID, must be unique on the broker
    type: string
    default: connectware-shopfloor

  topicRoot:
    description: Root of the MQTT topic hierarchy on the Connectware side
    type: string
    default: enterprise

resources:
  # TLS connection to the HiveMQ broker. For a self-hosted broker without a
  # TLS listener, set scheme to mqtt and port to 1883 (not recommended for
  # production).
  hivemqConnection:
    type: Cybus::Connection
    properties:
      protocol: Mqtt
      connection:
        host: !ref hivemqHost
        port: 8883
        scheme: mqtts
        username: !ref hivemqUsername
        password: !ref hivemqPassword
        clientId: !ref clientId
        keepalive: 60

  # Bridges shop floor telemetry from the ISA-95 topic hierarchy to the
  # uns/ prefix on the HiveMQ broker
  telemetryMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/telemetry'
            qos: 1
          publish:
            connection: !ref hivemqConnection
            topic: uns/$site/$area/$line/$cell/telemetry
            qos: 1

  # Receives commands that enterprise applications publish to the HiveMQ
  # broker on the cmd/shopfloor/commands topic
  commandEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Mqtt
      connection: !ref hivemqConnection
      subscribe:
        topic: cmd/shopfloor/commands
        qos: 1

  commandMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref commandEndpoint
          publish:
            topic: !sub '${topicRoot}/backend/hivemq/commands'
