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

description: >
  Service commissioning file for using Connectware as the MQTT broker
  between VDA 5050 compliant AGVs and a master control system (Example)

metadata:
  name: VDA 5050 AGV Integration
  provider: cybus
  homepage: https://www.cybus.io
  version: 1.0.0

parameters:
  manufacturer:
    description: Manufacturer of the AGV as used in the VDA 5050 topic structure
    type: string
    default: KIT

  serialNumber:
    description: Serial number of the AGV as used in the VDA 5050 topic structure
    type: string
    default: '0001'

  agvPassword:
    description: Password for the MQTT user of the AGV
    type: string

  masterControlPassword:
    description: Password for the MQTT user of the master control system
    type: string

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

resources:
  # Permissions for one AGV: subscribe to its command topics,
  # publish on its report topics
  agvRole:
    type: Cybus::Role
    properties:
      permissions:
        - resource: !sub 'uagv/v2/${manufacturer}/${serialNumber}/order'
          operation: read
          context: mqtt
        - resource: !sub 'uagv/v2/${manufacturer}/${serialNumber}/instantActions'
          operation: read
          context: mqtt
        - resource: !sub 'uagv/v2/${manufacturer}/${serialNumber}/state'
          operation: write
          context: mqtt
        - resource: !sub 'uagv/v2/${manufacturer}/${serialNumber}/visualization'
          operation: write
          context: mqtt
        - resource: !sub 'uagv/v2/${manufacturer}/${serialNumber}/connection'
          operation: write
          context: mqtt
        - resource: !sub 'uagv/v2/${manufacturer}/${serialNumber}/factsheet'
          operation: write
          context: mqtt

  # MQTT user for the AGV. The username is prefixed with the service ID,
  # for example vda5050.agv_kit_0001
  agv_kit_0001:
    type: Cybus::User
    properties:
      password: !ref agvPassword
      roles:
        - !ref agvRole

  # Permissions for the master control: publish commands to any AGV,
  # subscribe to everything the AGVs report
  masterControlRole:
    type: Cybus::Role
    properties:
      permissions:
        - resource: uagv/v2/+/+/order
          operation: write
          context: mqtt
        - resource: uagv/v2/+/+/instantActions
          operation: write
          context: mqtt
        - resource: uagv/v2/#
          operation: read
          context: mqtt

  # MQTT user for the master control system
  master_control:
    type: Cybus::User
    properties:
      password: !ref masterControlPassword
      roles:
        - !ref masterControlRole

  # Extracts battery, position, and connection state of every vehicle
  # into the UNS topic hierarchy
  unsMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: 'uagv/v2/+manufacturer/+serial/state'
          publish:
            topic: !sub '${topicRoot}/hamburg/intralogistics/agv-fleet/$manufacturer/$serial/battery'
          rules:
            - transform:
                expression: >
                  {
                    "batteryCharge": batteryState.batteryCharge,
                    "charging": batteryState.charging,
                    "timestamp": timestamp
                  }
        - subscribe:
            topic: 'uagv/v2/+manufacturer/+serial/state'
          publish:
            topic: !sub '${topicRoot}/hamburg/intralogistics/agv-fleet/$manufacturer/$serial/position'
          rules:
            - transform:
                expression: >
                  {
                    "x": agvPosition.x,
                    "y": agvPosition.y,
                    "theta": agvPosition.theta,
                    "mapId": agvPosition.mapId,
                    "timestamp": timestamp
                  }
        - subscribe:
            topic: 'uagv/v2/+manufacturer/+serial/connection'
          publish:
            topic: !sub '${topicRoot}/hamburg/intralogistics/agv-fleet/$manufacturer/$serial/connection-state'
            retain: true
          rules:
            - transform:
                expression: >
                  {
                    "connectionState": connectionState,
                    "timestamp": timestamp
                  }
