This commit is contained in:
Gilles Grandou 2022-04-21 09:32:57 +02:00
parent a09fd25462
commit bcc945137c
9 changed files with 121 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
shelly.conf
*.log

13
eval_script Executable file
View File

@ -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

7
exec_script Executable file
View File

@ -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 .

3
inspect_script Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash -e
./eval_script 'JSON.stringify(status)'

59
put_script Executable file
View File

@ -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()

5
reboot Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
source $(dirname $0)/shelly.conf
curl -s $SHELLY_RPC/Shelly.Reboot | jq .

17
shelly.conf.example Normal file
View File

@ -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

10
shelly_log Executable file
View File

@ -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

5
upload_script Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
source $(dirname $0)/shelly.conf
$ROOT_DIR/put_script $SHELLY_HOST $SCRIPT_ID $ROOT_DIR/$SCRIPT_FILENAME