Skip to content
Snippets Groups Projects
vending-machine-dev.rst 18 KiB
Newer Older
.. SPDX-FileCopyrightText: Huawei Inc.
..
.. SPDX-License-Identifier: CC-BY-4.0

Vending Machine Blueprint Applications Interface and Protocol
#############################################################

.. contents::
   :depth: 4

Communication Protocol
**********************

The "Vending Machine" blueprint will take advantage of two applications: a UI
and an "IO Controller". These applications will exchange messages over a
defined interface using a specific protocol. For the scope of this
specification, the communication will happen over plain WebSockets/TCP.

Specification
-------------

In terms of roles, we have a client and a server. The "IO Controller" acts as a
server while the "UI" process, as a client.

As a minimum client/server specification, the applications will exchange
messages as per the following diagram:

.. code-block::

   ┌────────────────┐     selection      ┌─────────────────────┐
   │                ├───────────────────>┤                     │
   │                │                    │                     │
   │                │     deliver        │ IO                  │
   │ UI Application ├───────────────────>┤ Control Application │
   │    (client)    │                    │      (server)       │
   │                │     delivered      │                     │
   │                │◄───────────────────┤                     │
   └────────────────┘                    └─────────────────────┘

Static information can be set in configuration files shared between both
applications (e.g. for items name, a timeout for simulated actions, number of
item slots, etc.).

Server application is made on generic concepts inspired by WoT/WebThings:

- properties: set a *selection* of products
  - selection is a fixed size array and items are identified from indices in
  this array while the values represent the associated item quantity
- actions: request a *deliver* order
  - order also contain the current selection as a parameter
- events: *delivered* event will notify that the *deliver* action was finished
  - event is delivered based on the *addEventSubscription* subscription message

Thoses objects will be used through websockets's messages on default endpoint
(ie: `<ws://localhost:8888/>`).

Client request's payloads are formatted using JSON structures. Below there is
an example for each of the types defined:

Properties
==========

.. code-block:: json

    {
      "messageType": "setProperty",
      "data": {
          "selection": [0, 0, 0, 1]
      }
    }

Actions
=======

.. code-block:: json

    {
      "messageType": "requestAction",
      "data": {
        "deliver": {
          "input": {
            "selection": [0, 1, 0, 0]
          }
        }
      }
    }

Events
======

The client needs to send a subscription message once and listen from server's
event messages:

.. code-block:: json

    {
      "messageType": "addEventSubscription",
      "data": {
        "delivered": {}
      }
    }

.. code-block:: json

    {
      "messageType": "event",
      "data": {
        "delivered": {}
      }
    }

Inter-application message flow
------------------------------

The UI and Control applications will adhere to the message schema defined
above. The message flow is described as it follows:

.. code-block::

   ┌────┐                      ┌────┐
   │    │                      │    │
   │    │                      │    │
   │    │      selection       │    │
   │    ├─────────────────────►│    │
   │    │                      │    │
   │    │      selection       │    │
   │    ├─────────────────────►│    │
   │    │                      │    │
   │    │      selection       │    │
   │    ├─────────────────────►│    │
   │    │                      │    │
   │    │                      │    │
   │    │ [...]                │    │
   │    │                      │    │
   │ UI │ [...]                │ IO │
   │    │                      │    │
   │    │                      │    │
   │    ├─────────────────────►│    │
   │    │                      │    │
   │    │                      │    │
   │    │        deliver       │    │
   │    ├─────────────────────►│    │
   │    │                      │    │
   │    │       delivered      │    │
   │    │◄─────────────────────┤    │
   │    │                      │    │
   │    │                      │    │
   │    │                      │    │
   │    │                      │    │
   │    │                      │    │
   └────┘                      └────┘

Detailed example flow:

Firstly, client is intializing by subscribing for server's future **"delivered" events**:

.. code-block:: json

    {
      "messageType": "addEventSubscription",
      "data": {
        "delivered": {}
      }
    }

Client's application is setting an empty selection on server and then UI will
wait for user inputs:

.. code-block:: json

    {
      "messageType": "setProperty",
      "data": {
        "selection": [0, 0, 0, 0]
      }
    }

User selects one product (one of type "1"):

- UI will be updated accordigly
- The client process makes a request to the server to set **selection
  "property"**

.. code-block:: json

   {
     "messageType": "setProperty",
     "data": {
       "selection": [0, 1, 0, 0]
     }
   }

The IO Controller will turn on the associated LEDs to show another visual
indication.

Then the user decides to add 1 more product of type "3":

.. code-block:: json

   {
     "messageType": "setProperty",
     "data": {
       "selection": [0, 1, 0, 1]
     }
   }

The user confirms the order by pressing the relevant UI element, then a
**"deliver" action"** is sent from client to the server:

.. code-block:: json

   {
     "messageType": "requestAction",
     "data": {
       "deliver": {
         "input": {
           "selection": [0, 1, 0, 1]
         }
       }
     }
   }

The UI application will be blocked until ready or timeout is reached:

- watchdog/timeout timer starts on UI/client
- UI waits for the **delivered** event

Processing is done server-side and **delivered event** is triggered:

.. code-block:: json

   {
     "messageType": "event",
     "data": {
       "delivered": {}
     }
   }

The UI is unblocked and ready for new selection (it should reinitialized to
empty).

If no "delivered" event after a defined timeout, the UI will display an "out of
order" message and show a "reset" button to refresh for the next order.

Software Dependencies Versions
------------------------------

Gururaj Shetty's avatar
Gururaj Shetty committed
Oniro Project supports the following libraries for message encoding/decoding/parsing and
the communication protocol:

* `libwebsockets <https://libwebsockets.org/>`_ 4.0.1

* `cjson <https://github.com/DaveGamble/cJSON/>`_ 1.7.13 (to be upgraded to
Gururaj Shetty's avatar
Gururaj Shetty committed
  1.7.14 for OpenHarmony convergence)
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658

* `json-c <https://github.com/json-c/json-c>`_ 0.13.1

Extra software could be integrated if needed:

* `libmicrohttpd <https://git.gnunet.org/libmicrohttpd.git/tree/src/include/microhttpd.h>`_

For prototyping purposes server can be easily implemented using
`webthings framework <https://webthings.io/>`_.


Message schema
--------------

Selection Message Schema
========================

The schema for the "selection" messages is:

.. code-block:: json

   {
     "$schema": "http://json-schema.org/draft-07/schema",
     "$id": "http://example.com/example.json",
     "type": "object",
     "title": "The root schema",
     "description": "The root schema comprises the entire JSON document.",
     "default": {},
     "examples": [
       {
         "messageType": "setProperty",
         "data": {
           "selection": [
             0,
             1,
             0,
             0
           ]
         }
       }
     ],
     "required": [
       "messageType",
       "data"
     ],
     "properties": {
       "messageType": {
         "$id": "#/properties/messageType",
         "type": "string",
         "title": "The messageType schema",
         "default": "",
         "examples": [
           "setProperty"
         ]
       },
       "data": {
         "$id": "#/properties/data",
         "type": "object",
         "title": "The data schema",
         "default": {},
         "examples": [
           {
             "selection": [
               0,
               1,
               0,
               0
             ]
           }
         ],
         "required": [
           "selection"
         ],
         "properties": {
           "selection": {
             "$id": "#/properties/data/properties/selection",
             "type": "array",
             "title": "The selection schema",
             "default": [],
             "examples": [
               [
                 0,
                 1
               ]
             ],
             "additionalItems": true,
             "items": {
               "$id": "#/properties/data/properties/selection/items",
               "anyOf": [
                 {
                   "$id": "#/properties/data/properties/selection/items/anyOf/0",
                   "type": "integer",
                   "title": "The first anyOf schema",
                   "default": 0,
                   "examples": [
                     0,
                     1
                   ]
                 }
               ]
             }
           }
         },
         "additionalProperties": true
       }
     },
     "additionalProperties": true
   }

Deliver Message Schema
======================

The schema for the "deliver" messages is:

.. code-block:: json

   {
     "$schema": "http://json-schema.org/draft-07/schema",
     "$id": "http://example.com/example.json",
     "type": "object",
     "title": "The root schema",
     "description": "The root schema comprises the entire JSON document.",
     "default": {},
     "examples": [
       {
         "messageType": "requestAction",
         "data": {
           "deliver": {
             "input": {
               "selection": [
                 0,
                 1,
                 0,
                 0
               ]
             }
           }
         }
       }
     ],
     "required": [
       "messageType",
       "data"
     ],
     "properties": {
       "messageType": {
         "$id": "#/properties/messageType",
         "type": "string",
         "title": "The messageType schema",
         "default": "",
         "examples": [
           "requestAction"
         ]
       },
       "data": {
         "$id": "#/properties/data",
         "type": "object",
         "title": "The data schema",
         "default": {},
         "examples": [
           {
             "deliver": {
               "input": {
                 "selection": [
                   0,
                   1,
                   0,
                   0
                 ]
               }
             }
           }
         ],
         "required": [
           "deliver"
         ],
         "properties": {
           "deliver": {
             "$id": "#/properties/data/properties/deliver",
             "type": "object",
             "title": "The deliver schema",
             "default": {},
             "examples": [
               {
                 "input": {
                   "selection": [
                     0,
                     1,
                     0,
                     0
                   ]
                 }
               }
             ],
             "required": [
               "input"
             ],
             "properties": {
               "input": {
                 "$id": "#/properties/data/properties/deliver/properties/input",
                 "type": "object",
                 "title": "The input schema",
                 "default": {},
                 "examples": [
                   {
                     "selection": [
                       0,
                       1,
                       0,
                       0
                     ]
                   }
                 ],
                 "required": [
                   "selection"
                 ],
                 "properties": {
                   "selection": {
                     "$id": "#/properties/data/properties/deliver/properties/input/properties/selection",
                     "type": "array",
                     "title": "The selection schema",
                     "default": [],
                     "examples": [
                       [
                         0,
                         1
                       ]
                     ],
                     "additionalItems": true,
                     "items": {
                       "$id": "#/properties/data/properties/deliver/properties/input/properties/selection/items",
                       "anyOf": [
                         {
                           "$id": "#/properties/data/properties/deliver/properties/input/properties/selection/items/anyOf/0",
                           "type": "integer",
                           "title": "The first anyOf schema",
                           "default": 0,
                           "examples": [
                             0,
                             1
                           ]
                         }
                       ]
                     }
                   }
                 },
                 "additionalProperties": true
               }
             },
             "additionalProperties": true
           }
         },
         "additionalProperties": true
       }
     },
     "additionalProperties": true
   }

Delivered Message Schema
========================

The schema for the "delivered" messages is:

.. code-block:: json

   {
     "$schema": "http://json-schema.org/draft-07/schema",
      "type": "object",
      "title": "The root schema",
      "description": "The root schema comprises the entire JSON document.",
      "default": {},
      "examples": [
        {
          "messageType": "event",
          "data": {
            "delivered": {}
          }
        }
      ],
      "required": [
        "messageType",
        "data"
      ],
      "properties": {
        "messageType": {
          "$id": "#/properties/messageType",
          "type": "string",
          "title": "The messageType schema",
          "description": "An explanation about the purpose of this instance.",
          "default": "",
          "examples": [
            "event"
          ]
        },
        "data": {
          "$id": "#/properties/data",
          "type": "object",
          "title": "The data schema",
          "description": "An explanation about the purpose of this instance.",
          "default": {},
          "examples": [
            {
              "delivered": {}
            }
          ],
          "required": [
            "delivered"
          ],
          "properties": {
            "delivered": {
              "$id": "#/properties/data/properties/delivered",
              "type": "object",
              "title": "The delivered schema",
              "description": "An explanation about the purpose of this instance.",
              "default": {},
              "examples": [
                {}
              ],
              "required": [],
              "additionalProperties": true
            }
          },
          "additionalProperties": true
        }
      },
      "additionalProperties": true
  }

Previous event will be notified if the client sends a subscription message:

.. code-block:: json

   {
     "$schema": "http://json-schema.org/draft-07/schema",
     "$id": "http://example.com/example.json",
     "type": "object",
     "title": "The root schema",
     "description": "The root schema comprises the entire JSON document.",
     "default": {},
     "examples": [
       {
         "messageType": "addEventSubscription",
         "data": {
           "delivered": {}
         }
       }
     ],
     "required": [
       "messageType",
       "data"
     ],
     "properties": {
       "messageType": {
         "$id": "#/properties/messageType",
         "type": "string",
         "title": "The messageType schema",
         "description": "An explanation about the purpose of this instance.",
         "default": "",
         "examples": [
           "addEventSubscription"
         ]
       },
       "data": {
         "$id": "#/properties/data",
         "type": "object",
         "title": "The data schema",
         "description": "An explanation about the purpose of this instance.",
         "default": {},
         "examples": [
           {
             "delivered": {}
           }
         ],
         "required": [
           "delivered"
         ],
         "properties": {
           "delivered": {
             "$id": "#/properties/data/properties/delivered",
             "type": "object",
             "title": "The delivered schema",
             "description": "An explanation about the purpose of this instance.",
             "default": {},
             "examples": [
               {}
             ],
             "required": [],
             "additionalProperties": true
           }
         },
         "additionalProperties": true
       }
     },
     "additionalProperties": true
   }

Current assumptions
-------------------

* Both of the applications (server/client, "UI"/"IO Controller" are running on
  the same, Linux-based target.
* The quantity of a selection is maximum "1". This means that the selection
  array can contain values of 0 or 1.
* The availability from the perspective of the "IO Controller" is infinite.