[
    {
        "id": "de0fec8edc92fade",
        "type": "subflow",
        "name": "Reolink-API",
        "info": "",
        "category": "",
        "in": [],
        "out": [
            {
                "x": 1180,
                "y": 260,
                "wires": [
                    {
                        "id": "c24a2fcb379ad8c6",
                        "port": 0
                    }
                ]
            },
            {
                "x": 1170,
                "y": 320,
                "wires": [
                    {
                        "id": "d68352d37badcfa5",
                        "port": 0
                    }
                ]
            },
            {
                "x": 1170,
                "y": 380,
                "wires": [
                    {
                        "id": "6b14177cc0beb135",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [
            {
                "name": "IP",
                "type": "str",
                "value": "",
                "ui": {
                    "type": "input",
                    "opts": {
                        "types": [
                            "str"
                        ]
                    }
                }
            },
            {
                "name": "Nutzer",
                "type": "str",
                "value": "admin",
                "ui": {
                    "type": "input",
                    "opts": {
                        "types": [
                            "str"
                        ]
                    }
                }
            },
            {
                "name": "Passwort",
                "type": "cred"
            },
            {
                "name": "Pfad",
                "type": "str",
                "value": "DATEN/Kameras/",
                "ui": {
                    "type": "input",
                    "opts": {
                        "types": [
                            "str"
                        ]
                    }
                }
            },
            {
                "name": "KameraName",
                "type": "str",
                "value": "EindeutigerName",
                "ui": {
                    "type": "input",
                    "opts": {
                        "types": [
                            "str"
                        ]
                    }
                }
            },
            {
                "name": "VideosSpeichern",
                "type": "bool",
                "value": "false",
                "ui": {
                    "type": "checkbox"
                }
            },
            {
                "name": "VideosLoeschen",
                "type": "num",
                "value": "28",
                "ui": {
                    "type": "input",
                    "opts": {
                        "types": [
                            "num"
                        ]
                    }
                }
            },
            {
                "name": "Menschenerkennung",
                "type": "bool",
                "value": "true",
                "ui": {
                    "type": "checkbox"
                }
            },
            {
                "name": "Autoerkennung",
                "type": "bool",
                "value": "true",
                "ui": {
                    "type": "checkbox"
                }
            },
            {
                "name": "BilderSpeichern",
                "type": "bool",
                "value": "false",
                "ui": {
                    "type": "checkbox"
                }
            },
            {
                "name": "BilderLoeschen",
                "type": "num",
                "value": "365",
                "ui": {
                    "type": "input",
                    "opts": {
                        "types": [
                            "num"
                        ]
                    }
                }
            }
        ],
        "meta": {},
        "credentials": {
            "Passwort": ""
        },
        "color": "#A6BBCF",
        "outputLabels": [
            "MenschErkannt",
            "AutoErkannt",
            "BildMitTopic"
        ],
        "icon": "font-awesome/fa-video-camera"
    },
    {
        "id": "111b8582f01265d1",
        "type": "http request",
        "z": "de0fec8edc92fade",
        "name": "",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "body",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 690,
        "y": 160,
        "wires": [
            [
                "c3bfb0d86d4612d8"
            ]
        ]
    },
    {
        "id": "7a83e4d7e7a202d4",
        "type": "http request",
        "z": "de0fec8edc92fade",
        "name": "",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 450,
        "y": 240,
        "wires": [
            [
                "c2fb2a9071ace044",
                "2f7ea4d7b63bea9d"
            ]
        ]
    },
    {
        "id": "1e362aa67d95653d",
        "type": "inject",
        "z": "de0fec8edc92fade",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "url",
                "v": "\"http://\" & $env('IP') & \"/cgi-bin/api.cgi?cmd=GetAiState&token=\" & $flowContext(\"reolink_token\",\"RAM\")",
                "vt": "jsonata"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{}",
        "payloadType": "json",
        "x": 110,
        "y": 240,
        "wires": [
            [
                "4332ee1ffd7c8e2f"
            ]
        ]
    },
    {
        "id": "c2fb2a9071ace044",
        "type": "switch",
        "z": "de0fec8edc92fade",
        "name": "Mensch",
        "property": "payload[0].value.people.alarm_state",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 620,
        "y": 220,
        "wires": [
            [
                "c24a2fcb379ad8c6"
            ]
        ]
    },
    {
        "id": "f9d79b3934079a46",
        "type": "http request",
        "z": "de0fec8edc92fade",
        "name": "ReolinkAuth",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 270,
        "y": 60,
        "wires": [
            [
                "6bb0ab55e7f76913"
            ]
        ]
    },
    {
        "id": "6bb0ab55e7f76913",
        "type": "change",
        "z": "de0fec8edc92fade",
        "name": "SetToken",
        "rules": [
            {
                "t": "set",
                "p": "reolink_token",
                "pt": "flow",
                "to": "payload[0].value.Token.name",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 440,
        "y": 60,
        "wires": [
            []
        ],
        "icon": "font-awesome/fa-lock"
    },
    {
        "id": "9eb4562adb639365",
        "type": "inject",
        "z": "de0fec8edc92fade",
        "name": "50m",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "url",
                "v": "\"http://\" & $env('IP') & \"/cgi-bin/api.cgi?cmd=Login\"",
                "vt": "jsonata"
            }
        ],
        "repeat": "3000",
        "crontab": "",
        "once": true,
        "onceDelay": "1",
        "topic": "",
        "payload": "[{\"cmd\":\"Login\",\"action\":0,\"param\":{\"User\":{\"userName\":$env('Nutzer'),\"password\":$env('Passwort')}}}]",
        "payloadType": "jsonata",
        "x": 110,
        "y": 60,
        "wires": [
            [
                "f9d79b3934079a46"
            ]
        ]
    },
    {
        "id": "beaf3057d84dd126",
        "type": "change",
        "z": "de0fec8edc92fade",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "url",
                "pt": "msg",
                "to": "\"http://\" & $env('IP') & \"/cgi-bin/api.cgi?cmd=Search&token=\" & $flowContext(\"reolink_token\",\"RAM\")",
                "tot": "jsonata"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "[\t   {\t       \"cmd\":\"Search\",\t       \"action\":1,\t       \"param\":{\t           \"Search\":{\t               \"channel\":0,\t               \"onlyStatus\":0,\t               \"streamType\":\"main\",\t               \"StartTime\":$eval($fromMillis($millis(),'{\"year\":[Y1],\"mon\":[M1],\"day\":[D1],\"hour\":0,\"min\":0,\"sec\":0}')),\t               \"EndTime\":$eval($fromMillis($millis()+24*60*60*1000,'{\"year\":[Y1],\"mon\":[M1],\"day\":[D1],\"hour\":23,\"min\":59,\"sec\":59}'))\t           }\t       }\t   }\t]",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 490,
        "y": 160,
        "wires": [
            [
                "111b8582f01265d1"
            ]
        ]
    },
    {
        "id": "4332ee1ffd7c8e2f",
        "type": "switch",
        "z": "de0fec8edc92fade",
        "name": "Erkennung",
        "property": "$env('Menschenerkennung') or $env('Autoerkennung')",
        "propertyType": "jsonata",
        "rules": [
            {
                "t": "true"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 270,
        "y": 240,
        "wires": [
            [
                "7a83e4d7e7a202d4"
            ]
        ]
    },
    {
        "id": "f4f97672ec4c5d6b",
        "type": "delay",
        "z": "de0fec8edc92fade",
        "name": "30ps",
        "pauseType": "rate",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "2",
        "rateUnits": "minute",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": true,
        "allowrate": false,
        "outputs": 1,
        "x": 970,
        "y": 380,
        "wires": [
            [
                "b186fa0481f49aed"
            ]
        ]
    },
    {
        "id": "b186fa0481f49aed",
        "type": "function",
        "z": "de0fec8edc92fade",
        "name": "TimeString",
        "func": "const path = env.get(\"Pfad\")+env.get(\"KameraName\")+\"/Bilder/\";\n\nvar now = new Date((new Date().getTime() - 10000));\nvar timeString = \"\"+now.getFullYear()+\"-\"+(now.getMonth()+1 < 10 ? '0' : '')+(now.getMonth()+1)+\"-\"+(now.getDate() < 10 ? '0' : '')+now.getDate() +\"_\"+ now.getHours()+\"-\"+((now.getMinutes() < 10 ? '0' : '') + now.getMinutes())+\"-\"+((now.getSeconds() < 10 ? '0' : '') + now.getSeconds());\nvar cmd = \"mkdir -pp \"+path+\"; curl \\\"http://\"+env.get(\"IP\")+\"/cgi-bin/api.cgi?cmd=Snap&channel=0&token=\"+flow.get(\"reolink_token\",\"RAM\")+\"&width=800&height=600\\\" --output \"+path+timeString+\".jpg\";\nvar file = path+timeString+\".jpg\";\nreturn {payload: cmd, sideload : file, topic: msg.topic};",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 670,
        "y": 440,
        "wires": [
            [
                "b5ca294a8ad02ff6"
            ]
        ]
    },
    {
        "id": "b5ca294a8ad02ff6",
        "type": "exec",
        "z": "de0fec8edc92fade",
        "command": "",
        "addpay": "payload",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "",
        "x": 830,
        "y": 440,
        "wires": [
            [
                "6b14177cc0beb135"
            ],
            [],
            []
        ]
    },
    {
        "id": "c3bfb0d86d4612d8",
        "type": "function",
        "z": "de0fec8edc92fade",
        "name": "GetVideos",
        "func": "const path = env.get(\"Pfad\")+env.get(\"KameraName\")+\"/Videos/\";\nvar msg2 = {payload: \"mkdir -pp \"+path+\"; \"};\nvar i = 0;\nif (typeof msg.payload[0].value.SearchResult.File !== 'undefined') {\n    var array = msg.payload[0].value.SearchResult.File;\n    for(var video in array){\n        var filename = array[video].name.substring(28, 43);\n        msg2.payload += \"wget -nc \\\"http://\"+env.get(\"IP\")+\"/cgi-bin/api.cgi?cmd=Download&token=\"+flow.get(\"reolink_token\",\"RAM\")+\"&source=\"+array[video].name+\"&output=\"+filename+\".mp4\\\" -O \"+path+filename+\".mp4; \";\n    }\n    return msg2;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 870,
        "y": 160,
        "wires": [
            [
                "07c11d6995151eb2"
            ]
        ]
    },
    {
        "id": "a1a4353cb13e5d2e",
        "type": "inject",
        "z": "de0fec8edc92fade",
        "name": "1m",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "60",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 110,
        "y": 160,
        "wires": [
            [
                "13a20409b6e3122d"
            ]
        ]
    },
    {
        "id": "13a20409b6e3122d",
        "type": "switch",
        "z": "de0fec8edc92fade",
        "name": "VideosSpeichern",
        "property": "VideosSpeichern",
        "propertyType": "env",
        "rules": [
            {
                "t": "true"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 290,
        "y": 160,
        "wires": [
            [
                "beaf3057d84dd126"
            ],
            []
        ]
    },
    {
        "id": "07c11d6995151eb2",
        "type": "switch",
        "z": "de0fec8edc92fade",
        "name": "",
        "property": "downloading",
        "propertyType": "flow",
        "rules": [
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 1030,
        "y": 160,
        "wires": [
            [
                "7156368d7ca5ae7e"
            ]
        ]
    },
    {
        "id": "2485186a52589f6a",
        "type": "complete",
        "z": "de0fec8edc92fade",
        "name": "",
        "scope": [
            "62a0ef7a4ff7c0b3"
        ],
        "uncaught": false,
        "x": 1010,
        "y": 200,
        "wires": [
            [
                "99ea6d543c579273"
            ]
        ]
    },
    {
        "id": "7156368d7ca5ae7e",
        "type": "change",
        "z": "de0fec8edc92fade",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "downloading",
                "pt": "flow",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1230,
        "y": 160,
        "wires": [
            [
                "ea7fe73eaee89064"
            ]
        ]
    },
    {
        "id": "99ea6d543c579273",
        "type": "change",
        "z": "de0fec8edc92fade",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "downloading",
                "pt": "flow",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1230,
        "y": 200,
        "wires": [
            []
        ]
    },
    {
        "id": "ea7fe73eaee89064",
        "type": "exec",
        "z": "de0fec8edc92fade",
        "command": "",
        "addpay": "payload",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "",
        "x": 1430,
        "y": 160,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "6b14177cc0beb135",
        "type": "change",
        "z": "de0fec8edc92fade",
        "name": "sideload",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "sideload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 980,
        "y": 440,
        "wires": [
            []
        ]
    },
    {
        "id": "e745d1dbbe76d47e",
        "type": "inject",
        "z": "de0fec8edc92fade",
        "name": "1d",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "00 04 * * *",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 630,
        "y": 80,
        "wires": [
            [
                "bda5bbc746359063",
                "f623694b845d15bd"
            ]
        ]
    },
    {
        "id": "bda5bbc746359063",
        "type": "change",
        "z": "de0fec8edc92fade",
        "name": "VideosLöschen",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "\"find \" & $env('Pfad') & $env('KameraName') & \"/Videos/* -mtime +\" & $env('VideosLoeschen') & \" -exec rm {} \\\\; ; find \" & $env('Pfad') & $env('KameraName') & \"/Videos/* -empty -type d -delete\"",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 780,
        "y": 60,
        "wires": [
            [
                "3939d2a2f966e77d"
            ]
        ]
    },
    {
        "id": "3939d2a2f966e77d",
        "type": "exec",
        "z": "de0fec8edc92fade",
        "command": "",
        "addpay": "payload",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "",
        "x": 950,
        "y": 80,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "f623694b845d15bd",
        "type": "change",
        "z": "de0fec8edc92fade",
        "name": "BilderLöschen",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "\"find \" & $env('Pfad') & $env('KameraName') & \"/Bilder/* -mtime +\" & $env('BilderLoeschen') & \" -exec rm {} \\\\; ; find \" & $env('Pfad') & $env('KameraName') & \"/Bilder/* -empty -type d -delete\"",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 780,
        "y": 100,
        "wires": [
            [
                "3939d2a2f966e77d"
            ]
        ]
    },
    {
        "id": "2f7ea4d7b63bea9d",
        "type": "switch",
        "z": "de0fec8edc92fade",
        "name": "Auto",
        "property": "payload[0].value.vehicle.alarm_state",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 610,
        "y": 260,
        "wires": [
            [
                "d68352d37badcfa5"
            ]
        ]
    },
    {
        "id": "049eb72f9626afd1",
        "type": "switch",
        "z": "de0fec8edc92fade",
        "name": "BilderSpeichern",
        "property": "BilderSpeichern",
        "propertyType": "env",
        "rules": [
            {
                "t": "true"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 820,
        "y": 380,
        "wires": [
            [
                "f4f97672ec4c5d6b"
            ]
        ]
    },
    {
        "id": "d68352d37badcfa5",
        "type": "change",
        "z": "de0fec8edc92fade",
        "name": "Auto",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Auto",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 730,
        "y": 260,
        "wires": [
            [
                "049eb72f9626afd1"
            ]
        ]
    },
    {
        "id": "c24a2fcb379ad8c6",
        "type": "change",
        "z": "de0fec8edc92fade",
        "name": "Mensch",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "Mensch",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 760,
        "y": 220,
        "wires": [
            [
                "049eb72f9626afd1"
            ]
        ]
    },
    {
        "id": "2c397098c65262ae",
        "type": "change",
        "z": "de0fec8edc92fade",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "downloading",
                "pt": "flow",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 215,
        "y": 120,
        "wires": [
            []
        ],
        "l": false
    },
    {
        "id": "b7efafdb9a1eafc8",
        "type": "inject",
        "z": "de0fec8edc92fade",
        "name": "Init",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 110,
        "y": 120,
        "wires": [
            [
                "2c397098c65262ae"
            ]
        ]
    },
    {
        "id": "03c1564e127a18a0",
        "type": "subflow:de0fec8edc92fade",
        "z": "3a0a18d1862dd8ba",
        "name": "",
        "env": [
            {
                "name": "Passwort",
                "type": "cred"
            }
        ],
        "x": 610,
        "y": 2560,
        "wires": [
            [],
            [],
            []
        ]
    }
]