From bcc945137ce2118fa1a45320b90903be37ff4364 Mon Sep 17 00:00:00 2001 From: Gilles Grandou Date: Thu, 21 Apr 2022 09:32:57 +0200 Subject: [PATCH] tools --- .gitignore | 2 ++ eval_script | 13 ++++++++++ exec_script | 7 ++++++ inspect_script | 3 +++ put_script | 59 +++++++++++++++++++++++++++++++++++++++++++++ reboot | 5 ++++ shelly.conf.example | 17 +++++++++++++ shelly_log | 10 ++++++++ upload_script | 5 ++++ 9 files changed, 121 insertions(+) create mode 100644 .gitignore create mode 100755 eval_script create mode 100755 exec_script create mode 100755 inspect_script create mode 100755 put_script create mode 100755 reboot create mode 100644 shelly.conf.example create mode 100755 shelly_log create mode 100755 upload_script diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7cd1fd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +shelly.conf +*.log diff --git a/eval_script b/eval_script new file mode 100755 index 0000000..055113c --- /dev/null +++ b/eval_script @@ -0,0 +1,13 @@ +#!/bin/bash -e + +source $(dirname $0)/shelly.conf + +id=1 + +curl -s -X POST \ + -d '{"id":1, "method":"Script.Eval", "params":{"id":'$SCRIPT_ID', "code":"'$@'"}}' \ + $SHELLY_RPC > .inspect.json + +cat .inspect.json | jq .result.result -r | jq . + +rm -f .inspect.json diff --git a/exec_script b/exec_script new file mode 100755 index 0000000..7374174 --- /dev/null +++ b/exec_script @@ -0,0 +1,7 @@ +#!/bin/bash + +source $(dirname $0)/shelly.conf + +curl -s "$SHELLY_RPC/Script.Stop?id=$SCRIPT_ID" | jq . +curl -s "$SHELLY_RPC/Script.Start?id=$SCRIPT_ID" | jq . + diff --git a/inspect_script b/inspect_script new file mode 100755 index 0000000..1c1c132 --- /dev/null +++ b/inspect_script @@ -0,0 +1,3 @@ +#!/bin/bash -e + +./eval_script 'JSON.stringify(status)' diff --git a/put_script b/put_script new file mode 100755 index 0000000..52e6924 --- /dev/null +++ b/put_script @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +# Copyright 2021 Allterco Robotics EOOD +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import json +import sys +from argparse import ArgumentParser + +import requests + +parser = ArgumentParser() +parser.add_argument("host", help="IP address or hostname of the Shelly device") +parser.add_argument("id", help="ID of the script being uploaded") +parser.add_argument("file", help="Local file containing the script code to upload") + +SYMBOLS_IN_CHUNK = 1024 + + +def put_chunk(host, id_, data, append=True): + url = f"http://{host}/rpc/Script.PutCode" + req = {"id": id_, "code": data, "append": append} + req_data = json.dumps(req, ensure_ascii=False) + res = requests.post(url, data=req_data.encode("utf-8"), timeout=2) + print(res.json()) + + +def main(): + args = parser.parse_args() + with open( + args.file, + mode="r", + encoding="utf-8", + ) as f: + code = f.read() + + pos = 0 + append = False + print(f"total {len(code)} bytes") + while pos < len(code): + chunk = code[pos : pos + SYMBOLS_IN_CHUNK] + put_chunk(args.host, args.id, chunk, append) + pos += len(chunk) + append = True + + +if __name__ == "__main__": + main() diff --git a/reboot b/reboot new file mode 100755 index 0000000..bdd046b --- /dev/null +++ b/reboot @@ -0,0 +1,5 @@ +#!/bin/bash + +source $(dirname $0)/shelly.conf + +curl -s $SHELLY_RPC/Shelly.Reboot | jq . diff --git a/shelly.conf.example b/shelly.conf.example new file mode 100644 index 0000000..b112e21 --- /dev/null +++ b/shelly.conf.example @@ -0,0 +1,17 @@ +ROOT_DIR=$(dirname $0) + +# your shelly hostname or IP address +# SHELLY_HOST="192.168.1.23" +# SHELLY_HOST="shelly_pool.local" +SHELLY_HOST="shelly_pool" + +SHELLY_RPC="http://$SHELLY_HOST/rpc" + +# script name and id. +SCRIPT_NAME="pool" +SCRIPT_FILENAME="pool.js" +SCRIPT_ID=1 + +# UDP port on which listen for debug messages +# (to be configured on your shelly device) +UDP_LOG_PORT=6901 diff --git a/shelly_log b/shelly_log new file mode 100755 index 0000000..cd2815a --- /dev/null +++ b/shelly_log @@ -0,0 +1,10 @@ +#!/bin/bash + +source $(dirname $0)/shelly.conf + +LOGFILE=$SHELLY_HOST.log + +while true; do + echo -e "\n======================================================================\n" | tee -a ${LOGFILE} + nc -u -k -l $UDP_LOG_PORT | tee -a ${LOGFILE} +done diff --git a/upload_script b/upload_script new file mode 100755 index 0000000..522d3f1 --- /dev/null +++ b/upload_script @@ -0,0 +1,5 @@ +#!/bin/bash + +source $(dirname $0)/shelly.conf + +$ROOT_DIR/put_script $SHELLY_HOST $SCRIPT_ID $ROOT_DIR/$SCRIPT_FILENAME