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

description: >
  Service commissioning file for the bidirectional integration between
  Connectware and Odoo (Example)

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

parameters:
  odooHost:
    description: Host of your Odoo instance, without the scheme
    type: string
    default: mycompany.odoo.com

  odooDatabase:
    description: Name of the Odoo database
    type: string
    default: mycompany

  odooUserId:
    description: Numeric ID of the Odoo user that the integration acts as
    type: integer

  odooApiKey:
    description: API key of the Odoo user
    type: string

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

resources:
  # Connection to the Odoo external API. Authentication happens per request
  # inside the JSON-RPC envelope, so the connection carries no credentials.
  odooConnection:
    type: Cybus::Connection
    properties:
      protocol: Http
      connection:
        scheme: https
        host: !ref odooHost
        port: 443

  # Single write endpoint for all JSON-RPC calls
  odooJsonRpcEndpoint:
    type: Cybus::Endpoint
    properties:
      protocol: Http
      connection: !ref odooConnection
      write:
        path: /jsonrpc

  # Reads open manufacturing orders whenever a message arrives on the
  # trigger topic. The JSON-RPC id 1 marks the responses on the /res topic.
  readManufacturingOrdersMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/odoo/manufacturing-orders/read'
          publish:
            endpoint: !ref odooJsonRpcEndpoint
          rules:
            - transform:
                expression: !sub |
                  {
                    "body": {
                      "jsonrpc": "2.0",
                      "method": "call",
                      "id": 1,
                      "params": {
                        "service": "object",
                        "method": "execute_kw",
                        "args": [
                          "${odooDatabase}",
                          ${odooUserId},
                          "${odooApiKey}",
                          "mrp.production",
                          "search_read",
                          [[["state", "in", ["confirmed", "progress"]]]],
                          {
                            "fields": ["name", "state", "product_qty", "qty_producing", "date_start"],
                            "limit": 50
                          }
                        ]
                      }
                    }
                  }

  # Logs shop floor events as notes on Odoo manufacturing orders.
  # The JSON-RPC id 2 marks the responses on the /res topic.
  logNoteMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            topic: !sub '${topicRoot}/+site/+area/+line/+cell/production-events'
          publish:
            endpoint: !ref odooJsonRpcEndpoint
          rules:
            - transform:
                expression: !sub |
                  {
                    "body": {
                      "jsonrpc": "2.0",
                      "method": "call",
                      "id": 2,
                      "params": {
                        "service": "object",
                        "method": "execute_kw",
                        "args": [
                          "${odooDatabase}",
                          ${odooUserId},
                          "${odooApiKey}",
                          "mrp.production",
                          "message_post",
                          [[orderId]],
                          { "body": note }
                        ]
                      }
                    }
                  }

  # HTTP route that Odoo automation rules can call
  odooWebhookServer:
    type: Cybus::Server::Http
    properties:
      basePath: /odoo

  odooWebhookRoute:
    type: Cybus::Node::Http
    properties:
      parent: !ref odooWebhookServer
      method: POST
      route: /events

  webhookMapping:
    type: Cybus::Mapping
    properties:
      mappings:
        - subscribe:
            endpoint: !ref odooWebhookRoute
          publish:
            topic: !sub '${topicRoot}/odoo/events'
