Object logic management

The Rightech IoT Cloud platform has the ability to set flexible logic for the behavior of an object in the system. The logic is specified in the notation of finite state machines in the graphical interface of the platform.

The main operations that can be reproduced with automation scripts using the API include:

  1. Getting a list of automation scripts.
  2. Getting information about one automation script.
  3. Creating a new automation script.
  4. Automation script editing.
  5. Deleting an automation script.
  6. Creating a new logic container.
  7. Deleting a logic container.
  8. Getting a list of object logic containers.
  9. Running an automation script in a container.
  10. Stopping logic execution in a container.
  11. Sending an event to a container.

Getting a list of automation scripts

A list of predefined automation scripts available to the user can be obtained through the request GET /api/v1/automatons. The response from the webserver will give an array of scripts in json format.

Request

GET /api/v1/automatons HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

Response

HTTP/1.1 200 OK

[
    {
        "_id": "5d8a4f2fd0025e0012fbdbf2",
        "name": "Automat 01",
        "models": [
            "5d8a3319d0025e0012fba0fa"
        ],
        "model": "5d8a3319d0025e0012fba0fa"
    },
    {
        "_id": "5d8a4f6cd0025e0012fbdc75",
        "name": "Automat 02",
        "models": [
            "5d8a1ef2d0025e0012fb76c6"
        ],
        "model": "5d8a1ef2d0025e0012fb76c6"
    }
]

Getting information about one automation script

In order to obtain information about one script, it is necessary to specify its identification number assigned by the system. The response from the server will give the configuration of the entity and the result of the execution of the request.

Request

GET /api/v1/automatons/:id HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

Response

HTTP/1.1 200 OK

{
    "_id": "5d8a4f6cd0025e0012fbdc75",
    "name": "Automat 02",
    "models": [
        "5d8a1ef2d0025e0012fb76c6"
    ],
    "model": "5d8a1ef2d0025e0012fb76c6",
    "success": true
}

Creating a new automation script

To create a new automation script in the request body, you must specify all the fields and their values, which are mandatory for any script. In response from the server, the generated configuration of the automation script, and the result of the request will be received.

Request

POST /api/v1/automatons HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

BODY

Response

HTTP/1.1 200 OK

BODY

Automation script editing

Using the platform API, some fields of scripts can be changed and edited, for example, such as the name, description, model, etc. To add new states and build transitions between them, it is better to use the graphical interface of the platform.

Request

PATCH /api/v1/automatons/:id HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

{
  "description": "new description"
}

Response

HTTP/1.1 200 OK

{
    "_id": "5d8a4f6cd0025e0012fbdc75",
    "name": "Automat 02",
    "models": [
        "5d8a1ef2d0025e0012fb76c6"
    ],
    "model": "5d8a1ef2d0025e0012fb76c6",
    "data": "{"states":{"0":{"data":{"id":"0","type":"init","width":20,"height":20},"position":{"left":80,"top":100}},"1":{"data":{"id":"1","type":"final","width":20,"height":20},"position":{"left":120,"top":440}},"2":{"data":{"id":"2","name":"New condition","description":"","onenter":[],"onexit":[],"type":"state","width":280,"height":180},"position":{"left":400,"top":220}}},"transitions":{"0":{"version":3,"data":{"id":"0","name":"Logic initialization","description":"Event that occurs when the logic model interpretation is started","event":"init","owner":{"id":"0","point":{"location":"right","id":0}},"target":{"id":"2","point":{"location":"top","id":1}}},"block":{"key":"cb-0","data":{"connectionKey":"0","type":"connectionBlock"},"layout":{"height":6,"width":11,"position":{"x":16,"y":2}}},"drawMode":"rou","points":[{"x":140,"y":100},{"x":420,"y":100},{"x":420,"y":200}]},"1":{"version":3,"data":{"id":"1","event":null,"owner":{"id":"2","point":{"location":"left","id":1}},"target":{"id":"1","point":{"location":"top","id":0}}},"block":{"key":"cb-1","data":{"connectionKey":"1","type":"connectionBlock"},"layout":{"height":3,"width":8,"position":{"x":2,"y":11}}},"drawMode":"rou","points":[{"x":380,"y":240},{"x":120,"y":240},{"x":120,"y":420}]}}}",
    "owner": "5d8a18d5d0025e0012fb6a34",
    "group": "5d8a18d4d0025e0012fb6a31",
    "time": 1569345388888,
    "_at": 1569513576483,
    "description": "new description",
    "success": true
}

Deleting an automation script

To delete the script, you must send an HTTP request DELETE /api/v1/objects/:id с with its identifier. The response will receive the configuration of the automation script and the result of the request.

Request

DELETE /api/v1/automatons/:id HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

Response

HTTP/1.1 200 OK

{
    "_id": "5d8a52f6d0025e0012fbe47c",
    "name": "Scenario 03",
    "description": "Automation scenario for microcontroller",
    "target": "5d8a25c1d0025e0012fb84b3",
    "model": "5d8a1ef2d0025e0012fb76c6",
    "owner": "5d8a18d5d0025e0012fb6a34",
    "group": "5d8a18d4d0025e0012fb6a31",
    "time": 1569346294975,
    "_at": 1569346568864,
    "data": {
        ... /* machine automation */
    },
    "success": true
}

Creating a new logic container

After deciding whether to run the logic script on a separate object, you must first create a new container. A container is an isolated environment necessary for interpreting a particular script.

ParameterTypeDescription
idObjectIdObject ID is required
container_idstringThe container identifier is optional, will be generated if it is missing

Request

POST /api/v1/objects/:id/containers/:container_id HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

Response

HTTP/1.1 200 OK

{
    "container" : "NEW_CONTAINER_ID"
    "success"   : true
}

Deleting a logic container

To delete a previously created container, you must send a request:

ParameterTypeDescription
idObjectIdObject ID is required
container_idstringObject ID is required

Request

DELETE /api/v1/objects/:id/containers/:container_id HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

Response

HTTP/1.1 200 OK

{
  "success": true
}

Getting a list of object logic containers

Any time, it is possible to request a list of containers launched for an object with an indication of the execution status.

Request

GET /api/v1/objects/:id/containers HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

Response

HTTP/1.1 200 OK

{
  "object_id": {
    "container-1": true,
    "container-2": false,
    "container-3": true,
  },
  "success": true
}

Running an automation script in a container

After selecting an automation script and creating a container for it, it becomes possible to run the script directly. It should be kept in mind that when forming a request in its body, you must specify the identification number of the script generated by the system.

ParameterTypeDescription
idObjectIdObject ID is required
container_idstringObject ID is required
automatonObjectIdAutomation script ID is required

Request

POST /api/v1/objects/:id/containers/:container_id/start HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

{
    "automaton": "58238a935baa56173b24f0e4",
}

Response

HTTP/1.1 200 OK

{
    "success": true
}

Stopping logic execution in a container

Script execution is stopped using a request

POST /api/v1/objects/:id/containers/:container_id/stop 

In this case, the container in which the logic was previously launched remains.

ParameterTypeDescription
idObjectIdObject ID is required
container_idstringContainer ID is required

Request

POST /api/v1/objects/:id/containers/:container_id/stop HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

Response

HTTP/1.1 200 OK

{
    "success": true
}

Sending an event to a container

An event can be sent to the logic container manually by sending the event identifier specified in the script in the request body. Sending the event for all containers of the object is carried out through the requestPOST /api/v1/objects/:id/emit. Prefixes containers/:container_id are added when sending an event for a specific container.

ParameterTypeRequest
idObjectIdObject ID is required
container_idstringContainer ID is optional
eventstring Event ID is required

Request

POST /api/v1/objects/:id/containers/:container_id/emit HTTP/1.1
Content-Type: application/json
Autorizatoin: Bearer {token}

{
    "event": "custom-user-event",
}

Response

HTTP/1.1 200 OK

{
    "success": true
}

We use cookies to improve your experience. Let’s accept it and move on.