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

description: >
  Service commissioning file for exchanging data between Connectware and
  SAP Manufacturing Integration and Intelligence (SAP MII) over HTTP (Example)

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

parameters:
  sapMiiHost:
    description: Hostname of the SAP NetWeaver server that runs SAP MII
    type: string
    default: sap-mii.example.com

  sapMiiPort:
    description: HTTPS port of the SAP NetWeaver instance
    type: integer
    default: 50001

  miiUsername:
    description: Name of the SAP MII service user
    type: string

  miiPassword:
    description: Password of the SAP MII service user
    type: string

  transactionPath:
    description: Full path of the SAP MII transaction to run
    type: string
    default: Default/ShopFloor/ProcessMachineData

  queryTemplatePath:
    description: Full path of the SAP MII query template to run
    type: string
    default: Default/ShopFloor/ProductionOrders

  pollInterval:
    description: Polling interval for the query template in milliseconds
    type: integer
    default: 60000

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

resources:
  # Connection to the SAP MII server using basic authentication
  sapMiiConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !ref sapMiiHost
        port: !ref sapMiiPort
        # The SAP MII servlets are served below the /XMII root path
        prefix: /XMII
        auth:
          username: !ref miiUsername
          password: !ref miiPassword

  # Runs one execution of a SAP MII transaction per message
  transactionRunnerEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref sapMiiConnection
      write:
        path: /Runner
        query:
          Transaction: !ref transactionPath
          # Name of the transaction input property that receives the body
          # of the POST request
          InputParameter: IncomingData
          # Returns all non-XML output properties of the transaction
          OutputParameter: '*'

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

  # Polls a SAP MII query template at a regular interval
  queryPollEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref sapMiiConnection
      subscribe:
        path: /Illuminator
        interval: !ref pollInterval
        query:
          QueryTemplate: !ref queryTemplatePath
          # Content-Type is a query parameter of the Illuminator servlet
          # that selects the output format, not an HTTP header
          Content-Type: text/json

  queryPollMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref queryPollEndpoint
          publish:
            topic: !sub '${topicRoot}/sap-mii/production-orders'

  # Runs the query template on demand via the /req topic of the endpoint,
  # for example with dynamic query template parameters
  queryReadEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref sapMiiConnection
      read:
        path: /Illuminator
        query:
          QueryTemplate: !ref queryTemplatePath
          Content-Type: text/json
