Создать операцию связи

Создаёт операцию в связи с заданным ID.

Запрос

HTTP Запрос

POST /node/api/links/:id/operations

Права

linkPermissions & manage-links & manage-operations

Параметры пути

Параметр Тип Описание

id

String
обязательный

ID связи.

Параметры тела запроса

Этот запрос использует модель Операция (кроме поля id).

Поле parameters отличается для каждого типа и подтипа операции.

Тело запроса

{
    "name": "Name of the link operation", "description": "The description of the link operation",
    "type": 1,  // The type of the operation. 1 is an MQTT operation, 2 is program/script operation
    "popupResult": false
    "parameters": {
       // Type-specific parameters
    }
}

Параметры, специфичные для операции:

Параметры MQTT
{
    "name": "MQTT",
    ...
    "type": 1,
        "parameters": {
            "topic": "MQTT Topic",
            "message": "MQTT Message Text"
        }
}
Параметры скрипта из файловой системы
{
    "name": "Filesystem Script",
    ...
    "type": 2,
    "parameters": {
        "type": "fileSystem",
        "path": "/script_name.sh",
        "args": [
            "Hello",
            "World"
        ]
    }
}
Параметры скрипта из репозитория
{
    "name": "Repository Script",
    ...
    "type": 2,
    "parameters": {
        "type": "scriptReference",
        "ref": "5ecfc1e06d9341263ceaaeb3"   // Script ID
    }
}
Параметры текстового скрипта
{
    "name": "Plain Text Script",
    ...
    "type": 2,
    "parameters": {
        "type": "scriptText",
        "text": "echo 'Hello World!'"
    }
}

Ответ

Возвращает созданную операцию (включая её id).

Примеры

MQTT operation

Этот пример создаёт операцию связи, которая отправляет MQTT сообщение.

Запрос

  • Bash

  • JavaScript

  • NodeJS

  • Python

login=<...>
password=<...>
saymon_hostname=<...>
link_id=<...>
url=https://$saymon_hostname/node/api/links/$link_id/operations

curl -X POST $url -u $login:$password \
    -H "Content-Type: application/json" \
    -d @- <<EOF
    {
        "name": "MQTT Operation",
"description": "This is an the operation that sends an MQTT message",
        "type": 1,
        "popupResult": false
        "parameters": {
            "topic": "MQTT Topic",
            "message": "MQTT Message Text"
        }
    }
EOF
let login = <...>
let password = <...>
let saymonHostname = <...>
let linkId = <...>
let path = "/node/api/links/" + linkId + "/operations";
let auth = "Basic " + btoa(login + ":" + password);

let headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", auth);

let data = JSON.stringify({
        "name": "MQTT Operation",
"description": "This is an the operation that sends an MQTT message",
        "type": 1,
        "popupResult": false
        "parameters": {
            "topic": "MQTT Topic",
            "message": "MQTT Message Text"
        }
    });



let requestOptions = {
    method: "POST",
    headers: headers,
    body: data
};

fetch(saymonHostname + path, requestOptions)
    .then(response => response.text())
    .then(result => console.log(result))
    .catch(error => console.log("error", error));
const http = require("http");

let login = <...>
let password = <...>
let saymonHostname = <...>
let linkId = <...>
let path = "/node/api/links/" + linkId + "/operations";
let auth = "Basic " + Buffer.from(login + ":" + password).toString("base64");

let options = {
    "method": "POST",
    "hostname": saymonHostname,
    "headers": {
        "Authorization": auth,
        "Content-Type": "application/json"
    },
    "path": path
};

let req = http.request(options, function (res) {
    let chunks = [];

    res.on("data", function (chunk) {
        chunks.push(chunk);
    });

    res.on("end", function (chunk) {
        let body = Buffer.concat(chunks);
        console.log(body.toString());
    });

    res.on("error", function (error) {
        console.error(error);
    });
});

let data = JSON.stringify({
        "name": "MQTT Operation",
"description": "This is an the operation that sends an MQTT message",
        "type": 1,
        "popupResult": false
        "parameters": {
            "topic": "MQTT Topic",
            "message": "MQTT Message Text"
        }
    });

req.write(data);
req.end();
import requests

login = <...>
password = <...>
saymon_hostname = <...>
link_id = <...>
url = "https://" + saymon_hostname + "/node/api/links/" + \
    link_id + "/operations"

body = {
        "name": "MQTT Operation",
"description": "This is an the operation that sends an MQTT message",
        "type": 1,
        "popupResult": False,
        "parameters": {
            "topic": "MQTT Topic",
            "message": "MQTT Message Text"
        }
    }

response = requests.request("POST", url, json=body, auth=(login, password))
print(response.text)

Ответ

    {
        "name": "MQTT",
        "parameters": {
            "topic": "MQTT Topic",
            "message": "MQTT Message Text"
        },
        "description": "MQTT Description",
        "type": 1,
        "popupResult": false,
        "id": "62d9331aac866e4fcc1f21c0"
    }

Операция скрипта файловой системы

Этот пример создаёт операцию связи, которая вызывает скрипт из файловой системы. В этом примере операция вызывает скрипт в файле /script_name.sh с двумя аргументами – Hello и World.

Запрос

  • Bash

  • JavaScript

  • NodeJS

  • Python

login=<...>
password=<...>
saymon_hostname=<...>
link_id=<...>
url=https://$saymon_hostname/node/api/links/$link_id/operations

curl -X POST $url -u $login:$password \
    -H "Content-Type: application/json" \
    -d @- <<EOF
    {
        "name": "Filesystem Script",
        "description": "Filesystem script description",
        "type": 2,
        "popupResult": false,
        "parameters": {
            "path": "/script_name.sh",
            "type": "fileSystem",
            "args": [
                "Hello",
                "World"
            ]
        }
    }
EOF
let login = <...>
let password = <...>
let saymonHostname = <...>
let linkId = <...>
let path = "/node/api/links/" + linkId + "/operations";
let auth = "Basic " + btoa(login + ":" + password);

let headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", auth);

let data = JSON.stringify({
        "name": "Filesystem Script",
        "description": "Filesystem script description",
        "type": 2,
        "popupResult": false,
        "parameters": {
            "path": "/script_name.sh",
            "type": "fileSystem",
            "args": [
                "Hello",
                "World"
            ]
        }
    });

let requestOptions = {
    method: "POST",
    headers: headers,
    body: data
};

fetch(saymonHostname + path, requestOptions)
    .then(response => response.text())
    .then(result => console.log(result))
    .catch(error => console.log("error", error));
const http = require("http");

let login = <...>
let password = <...>
let saymonHostname = <...>
let linkId = <...>
let path = "/node/api/links/" + linkId + "/operations";
let auth = "Basic " + Buffer.from(login + ":" + password).toString("base64");

let options = {
    "method": "POST",
    "hostname": saymonHostname,
    "headers": {
        "Authorization": auth,
        "Content-Type": "application/json"
    },
    "path": path
};

let req = http.request(options, function (res) {
    let chunks = [];

    res.on("data", function (chunk) {
        chunks.push(chunk);
    });

    res.on("end", function (chunk) {
        let body = Buffer.concat(chunks);
        console.log(body.toString());
    });

    res.on("error", function (error) {
        console.error(error);
    });
});

let data = JSON.stringify({
        "name": "Filesystem Script",
        "description": "Filesystem script description",
        "type": 2,
        "popupResult": false,
        "parameters": {
            "path": "/script_name.sh",
            "type": "fileSystem",
            "args": [
                "Hello",
                "World"
            ]
        }
    });

req.write(data);
req.end();
import requests

login = <...>
password = <...>
saymon_hostname = <...>
link_id = <...>
url = "https://" + saymon_hostname + "/node/api/links/" + \
    link_id + "/operations"

body = {
        "name": "Filesystem Script",
        "description": "Filesystem script description",
        "type": 2,
        "popupResult": False,
        "parameters": {
            "path": "/script_name.sh",
            "type": "fileSystem",
            "args": [
                "Hello",
                "World"
            ]
        }
    }

response = requests.request("POST", url, json=body, auth=(login, password))
print(response.text)

Ответ

{
    "name": "Filesystem Script",
    "description": "Filesystem script description",
    "type": 2,
    "popupResult": false,
    "parameters": {
        "path": "/script_name.sh",
        "type": "fileSystem",
        "args": [
            "Hello",
            "World"
        ]
    },
    "id": "62d95c18b238aa0131bd3b64"
}

Операция со скриптом из репозитория

Этот пример создаёт операцию связи, которая вызывает скрипт из репозитория.

Запрос

  • Bash

  • JavaScript

  • NodeJS

  • Python

login=<...>
password=<...>
saymon_hostname=<...>
link_id=<...>
url=https://$saymon_hostname/node/api/links/$link_id/operations

curl -X POST $url -u $login:$password \
    -H "Content-Type: application/json" \
    -d @- <<EOF
{
    "name": "Repository Script",
    "description": "Script from repository",
    "type": 2,
    "popupResult": false,
    "parameters": {
        "ref": "5ecfc1e06d9341263ceaaeb3",
        "type": "scriptReference"
    }
}
EOF
let login = <...>
let password = <...>
let saymonHostname = <...>
let linkId = <...>
let path = "/node/api/links/" + linkId + "/operations";
let auth = "Basic " + btoa(login + ":" + password);

let headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", auth);

let data = JSON.stringify({
    "name": "Repository Script",
    "description": "Script from repository",
    "type": 2,
    "popupResult": false,
    "parameters": {
        "ref": "5ecfc1e06d9341263ceaaeb3",
        "type": "scriptReference"
    }
});



let requestOptions = {
    method: "POST",
    headers: headers,
    body: data
};

fetch(saymonHostname + path, requestOptions)
    .then(response => response.text())
    .then(result => console.log(result))
    .catch(error => console.log("error", error));
const http = require("http");

let login = <...>
let password = <...>
let saymonHostname = <...>
let linkId = <...>
let path = "/node/api/links/" + linkId + "/operations";
let auth = "Basic " + Buffer.from(login + ":" + password).toString("base64");

let options = {
    "method": "POST",
    "hostname": saymonHostname,
    "headers": {
        "Authorization": auth,
        "Content-Type": "application/json"
    },
    "path": path
};

let req = http.request(options, function (res) {
    let chunks = [];

    res.on("data", function (chunk) {
        chunks.push(chunk);
    });

    res.on("end", function (chunk) {
        let body = Buffer.concat(chunks);
        console.log(body.toString());
    });

    res.on("error", function (error) {
        console.error(error);
    });
});

let data = JSON.stringify({
    "name": "Repository Script",
    "description": "Script from repository",
    "type": 2,
    "popupResult": false,
    "parameters": {
        "ref": "5ecfc1e06d9341263ceaaeb3",
        "type": "scriptReference"
    }
});

req.write(data);
req.end();
import requests

login = <...>
password = <...>
saymon_hostname = <...>
link_id = <...>
url = "https://" + saymon_hostname + "/node/api/links/" + \
    link_id + "/operations"

body = {
    "name": "Repository Script",
    "description": "Script from repository",
    "type": 2,
    "popupResult": False,
    "parameters": {
        "ref": "5ecfc1e06d9341263ceaaeb3",
        "type": "scriptReference"
    }
}

response = requests.request("POST", url, json=body, auth=(login, password))
print(response.text)

Ответ

{
    "name": "Repository Script",
    "parameters": {
        "ref": "5ecfc1e06d9341263ceaaeb3",
        "type": "scriptReference"
    },
    "description": "Script from repository",
    "type": 2,
    "popupResult": false,
    "id": "62d9353d60d9d84f03997b22"
}

Операция с текстовым скриптом

Этот пример создаёт операцию связи, которая выполняет Bash-скрипт, который выводит "Hello World!" в stdout.

Запрос

  • Bash

  • JavaScript

  • NodeJS

  • Python

login=<...>
password=<...>
saymon_hostname=<...>
link_id=<...>
url=https://$saymon_hostname/node/api/links/$link_id/operations

curl -X POST $url -u $login:$password \
    -H "Content-Type: application/json" \
    -d @- <<EOF
{
        "name": "Script Text",
        "description": "Prints 'Hello World!'",
        "popupResult": true,
        "type": 2,
        "parameters": {
            "type": "scriptText",
            "text": "echo 'Hello World!'"
        }
    }
EOF
let login = <...>
let password = <...>
let saymonHostname = <...>
let linkId = <...>
let path = "/node/api/links/" + linkId + "/operations";
let auth = "Basic " + btoa(login + ":" + password);

let headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", auth);

let data = JSON.stringify({
        "name": "Script Text",
        "description": "Prints 'Hello World!'",
        "popupResult": true,
        "type": 2,
        "parameters": {
            "type": "scriptText",
            "text": "echo 'Hello World!'"
        }
    });



let requestOptions = {
    method: "POST",
    headers: headers,
    body: data
};

fetch(saymonHostname + path, requestOptions)
    .then(response => response.text())
    .then(result => console.log(result))
    .catch(error => console.log("error", error));
const http = require("http");

let login = <...>
let password = <...>
let saymonHostname = <...>
let linkId = <...>
let path = "/node/api/links/" + linkId + "/operations";
let auth = "Basic " + Buffer.from(login + ":" + password).toString("base64");

let options = {
    "method": "POST",
    "hostname": saymonHostname,
    "headers": {
        "Authorization": auth,
        "Content-Type": "application/json"
    },
    "path": path
};

let req = http.request(options, function (res) {
    let chunks = [];

    res.on("data", function (chunk) {
        chunks.push(chunk);
    });

    res.on("end", function (chunk) {
        let body = Buffer.concat(chunks);
        console.log(body.toString());
    });

    res.on("error", function (error) {
        console.error(error);
    });
});

let data = JSON.stringify({
        "name": "Script Text",
        "description": "Prints 'Hello World!'",
        "popupResult": true,
        "type": 2,
        "parameters": {
            "type": "scriptText",
            "text": "echo 'Hello World!'"
        }
    });

req.write(data);
req.end();
import requests

login = <...>
password = <...>
saymon_hostname = <...>
link_id = <...>
url = "https://" + saymon_hostname + "/node/api/links/" + \
    link_id + "/operations"

body = {
        "name": "Script Text",
        "description": "Prints 'Hello World!'",
        "popupResult": True,
        "type": 2,
        "parameters": {
            "type": "scriptText",
            "text": "echo 'Hello World!'"
        }
    }

response = requests.request("POST", url, json=body, auth=(login, password))
print(response.text)

Ответ

    {
        "name": "Script Text",
        "description": "Prints 'Hello World!'",
        "popupResult": true,
        "type": 2,
        "parameters": {
            "type": "scriptText",
            "text": "echo 'Hello World!'"
        },
        "id": "62d8473456d203149a08007e"
    }