[
    {
        "id": "d4e8c2f1.2b173",
        "type": "tab",
        "label": "Bily primotop D1 Control",
        "disabled": false,
        "info": ""
    },
    {
        "id": "a1b2c3d4.e5f6g7",
        "type": "inject",
        "z": "d4e8c2f1.2b173",
        "name": "Get Token",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 100,
        "wires": [
            [
                "b2c3d4e5.f6g7h8"
            ]
        ]
    },
    {
        "id": "b2c3d4e5.f6g7h8",
        "type": "function",
        "z": "d4e8c2f1.2b173",
        "name": "Prepare Token Request",
        "func": "const crypto = require('crypto-js');\n\nconst ACCESS_ID = \"xmjrf7s594ek7ngm9sm3\";\nconst ACCESS_SECRET = \"ba95b549499349a688397f84cbb096fa\";\n\nconst timestamp = Date.now().toString();\nconst method = 'GET';\nconst url = '/v1.0/token?grant_type=1';\nconst body = '';\n\nconst stringToSign = method + '\\n' + body + '\\n' + '' + '\\n' + url;\nconst signStr = ACCESS_ID + timestamp + stringToSign;\nconst signature = crypto.HmacSHA256(signStr, ACCESS_SECRET).toString();\n\nmsg.headers = {\n    'client_id': ACCESS_ID,\n    't': timestamp,\n    'sign_method': 'HMAC-SHA256',\n    'sign': signature,\n    'Content-Type': 'application/json'\n};\n\nmsg.url = 'https://openapi.tuyaeu.com/v1.0/token?grant_type=1';\nmsg.method = 'GET';\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 320,
        "y": 100,
        "wires": [
            [
                "c3d4e5f6.g7h8i9"
            ]
        ]
    },
    {
        "id": "c3d4e5f6.g7h8i9",
        "type": "http request",
        "z": "d4e8c2f1.2b173",
        "name": "Get Token",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 540,
        "y": 100,
        "wires": [
            [
                "d4e5f6g7.h8i9j0"
            ]
        ]
    },
    {
        "id": "d4e5f6g7.h8i9j0",
        "type": "function",
        "z": "d4e8c2f1.2b173",
        "name": "Store Token",
        "func": "if (msg.payload && msg.payload.success && msg.payload.result) {\n    flow.set('tuya_token', msg.payload.result.access_token);\n    msg.payload = \"Token získán pro Bily primotop: \" + msg.payload.result.access_token;\n} else {\n    msg.payload = \"Chyba při získání tokenu: \" + JSON.stringify(msg.payload);\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 720,
        "y": 100,
        "wires": [
            [
                "e5f6g7h8.i9j0k1"
            ]
        ]
    },
    {
        "id": "e5f6g7h8.i9j0k1",
        "type": "debug",
        "z": "d4e8c2f1.2b173",
        "name": "Token Debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 920,
        "y": 100,
        "wires": []
    },
    {
        "id": "f6g7h8i9.j0k1l2",
        "type": "inject",
        "z": "d4e8c2f1.2b173",
        "name": "Primotop ON",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "command",
                "v": "true",
                "vt": "bool"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 200,
        "wires": [
            [
                "g7h8i9j0.k1l2m3"
            ]
        ]
    },
    {
        "id": "a1b2c3d4.e5f6g8",
        "type": "inject",
        "z": "d4e8c2f1.2b173",
        "name": "Primotop OFF",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "command",
                "v": "false",
                "vt": "bool"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 240,
        "wires": [
            [
                "g7h8i9j0.k1l2m3"
            ]
        ]
    },
    {
        "id": "g7h8i9j0.k1l2m3",
        "type": "function",
        "z": "d4e8c2f1.2b173",
        "name": "Prepare Primotop Command",
        "func": "const crypto = require('crypto-js');\n\nconst ACCESS_ID = \"xmjrf7s594ek7ngm9sm3\";\nconst ACCESS_SECRET = \"ba95b549499349a688397f84cbb096fa\";\nconst DEVICE_ID = \"ebafafd9cd55a9bc02994x\";\n\nconst token = flow.get('tuya_token');\nif (!token) {\n    msg.payload = \"Chyba: Token není k dispozici. Nejdříve získej token.\";\n    return msg;\n}\n\nconst timestamp = Date.now().toString();\nconst method = 'POST';\nconst url = `/v1.0/iot-03/devices/${DEVICE_ID}/commands`;\nconst body = JSON.stringify({\n    \"commands\": [\n        {\n            \"code\": \"switch_1\",\n            \"value\": msg.command\n        }\n    ]\n});\n\nconst stringToSign = method + '\\n' + body + '\\n' + '' + '\\n' + url;\nconst signStr = ACCESS_ID + token + timestamp + stringToSign;\nconst signature = crypto.HmacSHA256(signStr, ACCESS_SECRET).toString();\n\nmsg.headers = {\n    'client_id': ACCESS_ID,\n    'access_token': token,\n    't': timestamp,\n    'sign_method': 'HMAC-SHA256',\n    'sign': signature,\n    'Content-Type': 'application/json'\n};\n\nmsg.url = `https://openapi.tuyaeu.com/v1.0/iot-03/devices/${DEVICE_ID}/commands`;\nmsg.method = 'POST';\nmsg.payload = body;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 380,
        "y": 220,
        "wires": [
            [
                "h8i9j0k1.l2m3n4"
            ]
        ]
    },
    {
        "id": "h8i9j0k1.l2m3n4",
        "type": "http request",
        "z": "d4e8c2f1.2b173",
        "name": "Send Command",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 620,
        "y": 220,
        "wires": [
            [
                "i9j0k1l2.m3n4o5"
            ]
        ]
    },
    {
        "id": "i9j0k1l2.m3n4o5",
        "type": "debug",
        "z": "d4e8c2f1.2b173",
        "name": "Primotop Command Debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 830,
        "y": 220,
        "wires": []
    },
    {
        "id": "j0k1l2m3.n4o5p6",
        "type": "inject",
        "z": "d4e8c2f1.2b173",
        "name": "Get Primotop Status",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 140,
        "y": 320,
        "wires": [
            [
                "k1l2m3n4.o5p6q7"
            ]
        ]
    },
    {
        "id": "k1l2m3n4.o5p6q7",
        "type": "function",
        "z": "d4e8c2f1.2b173",
        "name": "Prepare Status Request",
        "func": "const crypto = require('crypto-js');\n\nconst ACCESS_ID = \"xmjrf7s594ek7ngm9sm3\";\nconst ACCESS_SECRET = \"ba95b549499349a688397f84cbb096fa\";\nconst DEVICE_ID = \"ebafafd9cd55a9bc02994x\";\n\nconst token = flow.get('tuya_token');\nif (!token) {\n    msg.payload = \"Chyba: Token není k dispozici. Nejdříve získej token.\";\n    return msg;\n}\n\nconst timestamp = Date.now().toString();\nconst method = 'GET';\nconst url = `/v1.0/iot-03/devices/${DEVICE_ID}/status`;\nconst body = '';\n\nconst stringToSign = method + '\\n' + body + '\\n' + '' + '\\n' + url;\nconst signStr = ACCESS_ID + token + timestamp + stringToSign;\nconst signature = crypto.HmacSHA256(signStr, ACCESS_SECRET).toString();\n\nmsg.headers = {\n    'client_id': ACCESS_ID,\n    'access_token': token,\n    't': timestamp,\n    'sign_method': 'HMAC-SHA256',\n    'sign': signature,\n    'Content-Type': 'application/json'\n};\n\nmsg.url = `https://openapi.tuyaeu.com/v1.0/iot-03/devices/${DEVICE_ID}/status`;\nmsg.method = 'GET';\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 350,
        "y": 320,
        "wires": [
            [
                "l2m3n4o5.p6q7r8"
            ]
        ]
    },
    {
        "id": "l2m3n4o5.p6q7r8",
        "type": "http request",
        "z": "d4e8c2f1.2b173",
        "name": "Get Status",
        "method": "use",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 580,
        "y": 320,
        "wires": [
            [
                "m3n4o5p6.q7r8s9"
            ]
        ]
    },
    {
        "id": "m3n4o5p6.q7r8s9",
        "type": "debug",
        "z": "d4e8c2f1.2b173",
        "name": "Primotop Status Debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 790,
        "y": 320,
        "wires": []
    }
]