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

description: >
  Service commissioning file for the integration between Connectware and
  Cumulocity IoT using SmartREST static templates over MQTT (Example)

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

parameters:
  tenantDomain:
    description: Domain of your Cumulocity IoT tenant, without scheme
    type: string
    default: mytenant.cumulocity.com

  tenantId:
    description: ID of your Cumulocity IoT tenant
    type: string
    default: t76543210

  deviceUsername:
    description: Username of the device credentials, without the tenant ID prefix
    type: string
    default: device_press-01

  devicePassword:
    description: Password of the device credentials
    type: string

  deviceId:
    description: Device identifier, used as the MQTT client ID (no colons allowed)
    type: string
    default: press-01

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

resources:
  # Connection to the Cumulocity IoT Core MQTT endpoint
  cumulocityConnection:
    type: Cybus::Connection
    properties:
      protocol: Mqtt
      connection:
        host: !ref tenantDomain
        port: 8883
        scheme: mqtts
        username: !sub '${tenantId}/${deviceUsername}'
        password: !ref devicePassword
        clientId: !ref deviceId
        keepalive: 60

  # Publishes SmartREST static templates to Cumulocity IoT
  upstreamEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Mqtt
      connection: !ref cumulocityConnection
      write:
        topic: s/us

  # Direct channel for any static template, for example device creation (100)
  upstreamMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/cumulocity/upstream'
          publish:
            endpoint: !ref upstreamEndpoint

  # Sends temperature values as temperature measurements (template 211)
  temperatureMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/temperature'
          publish:
            endpoint: !ref upstreamEndpoint
          rules:
            - transform:
                expression: '"211," & $string(value)'

  # Sends self-describing JSON payloads as measurements (template 200)
  measurementMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/measurement'
          publish:
            endpoint: !ref upstreamEndpoint
          rules:
            - transform:
                expression: '"200," & fragment & "," & series & "," & $string(value) & "," & unit'

  # Receives operations from Cumulocity IoT
  operationsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Mqtt
      connection: !ref cumulocityConnection
      subscribe:
        topic: s/ds

  operationsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref operationsEndpoint
          publish:
            topic: !sub '${topicRoot}/cumulocity/operations'

  # Receives SmartREST error messages from Cumulocity IoT
  errorEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Mqtt
      connection: !ref cumulocityConnection
      subscribe:
        topic: s/e

  errorMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref errorEndpoint
          publish:
            topic: !sub '${topicRoot}/cumulocity/errors'
