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

description: >
  Service commissioning file for the integration between Connectware and the
  Tulip frontline operations platform (Example)

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

parameters:
  tulipHost:
    description: Host name of your Tulip instance
    type: string
    default: example.tulip.co

  apiKey:
    description: API key of the Tulip API token, used as the basic auth username
    type: string

  apiSecret:
    description: Secret of the Tulip API token, used as the basic auth password
    type: string

  machineId:
    description: ID of the Tulip machine that receives the temperature values
    type: string

  temperatureAttributeId:
    description: ID of the machine attribute that receives the temperature values
    type: string

  qualityTableId:
    description: ID of the Tulip Table that stores quality events
    type: string

  workOrdersTableId:
    description: ID of the Tulip Table that contains work orders
    type: string

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

resources:
  # Connection to the Tulip API using an API token (HTTP basic authentication)
  tulipConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !ref tulipHost
        port: 443
        # For account-scoped API tokens on multi-workspace instances,
        # include the workspace in the prefix, for example /api/v3/w/DEFAULT
        prefix: /api/v3
        auth:
          username: !ref apiKey
          password: !ref apiSecret

  # Reports machine attribute values to Tulip
  machineAttributesEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref tulipConnection
      write:
        path: /attributes/report

  machineAttributesMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/machine-attributes'
          publish:
            endpoint: !ref machineAttributesEndpoint
          rules:
            - transform:
                expression: '{ "body": $ }'

  # Builds the Machine Attributes API payload from a plain temperature value.
  # Add one mapping like this per machine and attribute pair.
  temperatureReportMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/hamburg/assembly/line-1/press-01/temperature'
          publish:
            endpoint: !ref machineAttributesEndpoint
          rules:
            - transform:
                expression: !sub '{ "body": { "attributes": [ { "machineId": "${machineId}", "attributeId": "${temperatureAttributeId}", "value": temperature } ] } }'

  # Creates one Tulip Table record per quality event
  qualityEventsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref tulipConnection
      write:
        path: !sub '/tables/${qualityTableId}/records'

  qualityEventsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/quality-events'
          publish:
            endpoint: !ref qualityEventsEndpoint
          rules:
            - transform:
                expression: '{ "body": $ }'

  # Polls the work orders table and publishes the records to MQTT
  workOrdersEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref tulipConnection
      subscribe:
        path: !sub '/tables/${workOrdersTableId}/records'
        interval: 10000
        query:
          limit: '100'

  workOrdersMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref workOrdersEndpoint
          publish:
            topic: !sub '${topicRoot}/tulip/work-orders'
