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

description: >
  Service commissioning file for posting shop floor notifications from
  Connectware to Slack channels via incoming webhooks (Example)

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

parameters:
  alarmsWebhookPath:
    description: Webhook path for the machine alarms channel, without the host
    type: string

  milestonesWebhookPath:
    description: Webhook path for the production milestones channel, without the host
    type: string

  minimumSeverity:
    description: Lowest alarm severity that is posted to Slack
    type: number
    default: 3

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

resources:
  # Connection to the Slack webhook host, shared by all webhooks
  slackConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: hooks.slack.com
        port: 443

  # Posts machine alarms to the alarms channel
  alarmsEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref slackConnection
      write:
        path: !ref alarmsWebhookPath

  alarmsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/alarms'
          publish:
            endpoint: !ref alarmsEndpoint
          rules:
            # Only alarms at or above the minimum severity reach Slack
            - filter:
                expression: !sub 'severity >= ${minimumSeverity}'
            - transform:
                expression: |
                  {
                    "body": {
                      "text": "Alarm " & code & " on "
                        & $context.vars.site & "/" & $context.vars.line & "/" & $context.vars.cell
                        & ": " & message & " (severity " & $string(severity) & ")"
                    }
                  }

  # Posts production milestones to the milestones channel as Block Kit messages
  milestonesEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref slackConnection
      write:
        path: !ref milestonesWebhookPath

  milestonesMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/milestones'
          publish:
            endpoint: !ref milestonesEndpoint
          rules:
            # The top-level text is the notification fallback for the blocks
            - transform:
                expression: |
                  {
                    "body": {
                      "text": "Production milestone: " & milestone,
                      "blocks": [
                        {
                          "type": "section",
                          "text": {
                            "type": "mrkdwn",
                            "text": "*Production milestone:* " & milestone
                          },
                          "fields": [
                            { "type": "mrkdwn", "text": "*Site*\n" & $context.vars.site },
                            { "type": "mrkdwn", "text": "*Area*\n" & $context.vars.area },
                            { "type": "mrkdwn", "text": "*Line*\n" & $context.vars.line },
                            { "type": "mrkdwn", "text": "*Cell*\n" & $context.vars.cell }
                          ]
                        }
                      ]
                    }
                  }
