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

description: >
  Service commissioning file for the bidirectional integration between
  Connectware and the AVEVA PI System via the PI Web API (Example)

metadata:
  name: AVEVA PI System Integration
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  piWebApiHost:
    description: Hostname of the PI Web API server
    type: string
    default: pi-server.example.com

  piUsername:
    description: Username for basic authentication against the PI Web API
    type: string

  piPassword:
    description: Password for basic authentication against the PI Web API
    type: string

  piPointWebId:
    description: WebId of the PI Point to write to and read from
    type: string
    default: F1DP000000000000000000000000000000000000

  pollInterval:
    description: Polling interval for reading the latest recorded value, in milliseconds
    type: integer
    default: 5000

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

resources:
  # Connection to the PI Web API using basic authentication
  piWebApiConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !ref piWebApiHost
        port: 443
        prefix: /piwebapi
        auth:
          username: !ref piUsername
          password: !ref piPassword
        # If the PI Web API server uses a self-signed certificate, add the
        # root CA certificate as Base64-encoded PEM file content:
        # caCert: <BASE64_ENCODED_PEM>

  # Writes one value per message to the PI Point
  valueWriteEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref piWebApiConnection
      write:
        path: !sub '/streams/${piPointWebId}/value'

  valueWriteMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/process-values'
          publish:
            endpoint: !ref valueWriteEndpoint
          rules:
            - transform:
                expression: '{ "body": { "Timestamp": $fromMillis(timestamp), "Value": value } }'

  # Writes multiple recorded values per message to the PI Point
  recordedWriteEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref piWebApiConnection
      write:
        path: !sub '/streams/${piPointWebId}/recorded'

  recordedWriteMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/process-values-batch'
          publish:
            endpoint: !ref recordedWriteEndpoint
          rules:
            - transform:
                expression: '{ "body": [ $.{ "Timestamp": $fromMillis(timestamp), "Value": value } ] }'

  # Polls the latest recorded value of the PI Point
  latestValueEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref piWebApiConnection
      subscribe:
        path: !sub '/streams/${piPointWebId}/end'
        interval: !ref pollInterval

  latestValueMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref latestValueEndpoint
          publish:
            topic: !sub '${topicRoot}/pi/latest-value'
