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

description: >
  Service commissioning file for pushing live shop floor data from
  Connectware into a Power BI streaming semantic model (Example)

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

parameters:
  tenantId:
    description: Tenant ID from the push URL of the streaming semantic model
    type: string
    default: 00000000-0000-0000-0000-000000000000

  datasetId:
    description: Dataset ID from the push URL of the streaming semantic model
    type: string
    default: 00000000-0000-0000-0000-000000000000

  pushKey:
    description: Resource key from the push URL (the value of the key query parameter)
    type: string

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

resources:
  # Connection to the Power BI REST API
  powerBiConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: api.powerbi.com
        port: 443

  # Pushes rows into the streaming semantic model
  # The resource key in the URL authenticates the request
  powerBiRowsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref powerBiConnection
      write:
        path: !sub '/beta/${tenantId}/datasets/${datasetId}/rows?key=${pushKey}'

  # Wraps every MQTT message into a rows array and adds a timestamp
  powerBiRowsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/telemetry'
          publish:
            endpoint: !ref powerBiRowsEndpoint
          rules:
            - transform:
                expression: '{ "body": [ $merge([$, { "timestamp": $now() }]) ] }'
