description: >
    Temperature & Vibration Visualization for a Machine

metadata:
  name: Condition Monitoring
  icon: https://www.cybus.io/wp-content/uploads/2019/03/Cybus-logo-Claim-lang.svg
  provider: cybus
  homepage: https://www.cybus.io
  version: 0.0.1

definitions:
  SID: !ref Cybus::ServiceId

parameters:
  IP_Address:
    type: string
    default: 172.17.0.1

  Port_Number:
    type: integer
    default: 4840

  Azure_Iot_Hub_Connection_String:
    type: string
    default: some_azure_connection_string

resources:
  # OPC UA simulator container used for the demo
  opcuaServer:
    type: Cybus::Container
    properties:
      image: registry.cybus.io/cybus-factory/opcua-server:1.0.1
      ports:
        - 4840:50000/tcp
      command:
        - "--unsecuretransport"
        - "--autoaccept"
        - "--defaultuser=user"
        - "--defaultpassword=pass"

  opcuaConnection:
    type: Cybus::Connection
    properties:
      protocol: Opcua
      targetState: connected
      connection:
        host: !ref IP_Address
        port: !ref Port_Number
        username: user
        password: pass

  opcuaCurrentServerTime:
    type: Cybus::Endpoint
    properties:
      protocol: Opcua
      connection: !ref opcuaConnection
      subscribe:
        nodeId: i=2258
        samplingInterval: 1000

  opcuaTemperature:
    type: Cybus::Endpoint
    properties:
      protocol: Opcua
      connection: !ref opcuaConnection
      subscribe:
        nodeId: ns=2;s=SpikeData
        samplingInterval: 2000

  opcuaVibration:
    type: Cybus::Endpoint
    properties:
      protocol: Opcua
      connection: !ref opcuaConnection
      subscribe:
        nodeId: ns=2;s=DipData
        samplingInterval: 2000

  mapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref opcuaCurrentServerTime
          publish:
            topic: !sub "${Cybus::MqttRoot}/current-server-time"
        - subscribe:
            endpoint: !ref opcuaTemperature
          rules:
            - transform:
                expression: |
                  {
                    "timestamp": timestamp,
                    "value": (value - 32) * (5/9),
                    "unit": "celsius"
                  }
          publish:
            topic: !sub "${Cybus::MqttRoot}/temperature"
        - subscribe:
            endpoint: !ref opcuaVibration
          publish:
            topic: !sub "${Cybus::MqttRoot}/vibration"

  #----------------------------------------------------------------------------
  # VOLUMES
  #----------------------------------------------------------------------------

  grafanaVolume:
    type: Cybus::Volume

  influxdbVolume:
    type: Cybus::Volume

  #----------------------------------------------------------------------------
  # FRONTENDS
  #----------------------------------------------------------------------------

  # Grafana
  grafanaURL:
    type: Cybus::IngressRoute
    properties:
      container: !ref genericGrafana
      type: http
      slug: grafana
      target:
        path: '/'
        port: 3000

  dashboard:
    type: Cybus::Link
    properties:
      name: Dashboard
      ingressRoute: !ref grafanaURL
      href: ''

  #----------------------------------------------------------------------------
  # Cybus Timeseris & Dashboard service containers
  #----------------------------------------------------------------------------

  influxdbPush:
      type: Cybus::Container
      properties:
        image: registry.cybus.io/cybus-services/influxdb-push:0.0.3
        environment:
          MQTT_HOST: !ref Cybus::MqttHost
          MQTT_USER: !ref Cybus::MqttUser
          MQTT_PORT: !ref Cybus::MqttPort
          MQTT_PASS: !ref Cybus::MqttPassword
          MQTT_ROOT_TOPIC: !sub "${Cybus::MqttRoot}/#"
          INFLUX_HOST: !ref influxdb
          INFLUX_PORT: 8086
          INFLUX_DB: generic
          HTTP_ROOT: /

  influxdb:
    type: Cybus::Container
    properties:
      image: registry.cybus.io/cybus-services/influxdb:1.7.7-alpine
      ports:
        - 8086:8086/tcp
      volumes:
        - !sub "${influxdbVolume}:/var/lib/influxdb"
      environment:
        INFLUXDB_DB: generic

  genericGrafana:
    type: Cybus::Container
    properties:
      image: registry.cybus.io/cybus-services/generic-grafana:1.2.1
      volumes:
        - !sub "${grafanaVolume}:/var/lib/grafana"
      environment:
        GF_SERVER_ROOT_URL: !sub "/services/${SID}/grafana"
        GF_AUTH_ANONYMOUS_ENABLED: true
        INFLUX_DB: generic
        INFLUX_HOST: !ref influxdb
        INFLUX_PORT: 8086

  #----------------------------------------------------------------------------
  # Cybus Azure IoT Hub Connector Service
  #----------------------------------------------------------------------------

  azureIoTConnector:
    type: Cybus::Container
    properties:
      image: registry.cybus.io/cybus-services/azure-iot-connector:0.0.5
      environment:
        LOG_LEVEL: 'info'
        CONNECTOR_CONFIG: !sub  | 
          {
            "general": {
              "name": "Azure Connector"
            },
            "source": {
              "driver": "azure.iot",
              "connection": {
                "connectionString": "${Azure_Iot_Hub_Connection_String}"
              },
              "defaults": {
                "operation": "write"
              }
            },
            "target": {
              "driver": "mqtt",
              "connection": {
                "protocol": "mqtt",
                "host": "${Cybus::MqttHost}",
                "port": ${Cybus::MqttPort},
                "username": "admin",
                "password": "admin"
              },
              "defaults": {
                "operation": "subscribe",
                "topicPrefix": "${Cybus::MqttRoot}"
              }
            },
            "mappings": [
              {
                "source": {
                  "name": "temperature",
                  "type": "telemetry"
                },
                "target": {
                  "topic": "${Cybus::MqttRoot}/temperature"
                }
              },
              {
                "source": {
                  "name": "vibration",
                  "type": "telemetry",
                  "properties": {
                    "default": false,
                    "route": "storage"
                  }
                },
                "target": {
                  "topic": "${Cybus::MqttRoot}/vibration"
                }
              }
            ]
          }
