From fbd3ba98d3022e1ccb838c4fa0259212dc1b5fc0 Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sun, 31 Mar 2019 13:13:59 +0200 Subject: [PATCH] Add doall.sh configuration helper --- deploy/README.md | 27 ++++++++++++ deploy/config.csv | 12 ++++++ deploy/doall.sh | 104 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 deploy/README.md create mode 100644 deploy/config.csv create mode 100755 deploy/doall.sh diff --git a/deploy/README.md b/deploy/README.md new file mode 100644 index 0000000..d238e16 --- /dev/null +++ b/deploy/README.md @@ -0,0 +1,27 @@ +# Convenience tool + +This tool reads a CSV and sets all TimeSpecs found. How to use: +1) edit config.csv and edit the last column in the CSV (a TimeSpec in UTC) +2) run ./doall.sh -q get > old.csv +3) diff -u old.csv config.csv +4) run ./doall.sh set + and check the output +5) run ./doall.sh -f set +6) Check in the new config.csv + +## Example + +``` +# Generated on Sun Mar 31 13:03:21 CEST 2019 +# Device,IPv4,Chan,Name,TimeSpec(UTC) +esp8266_808501,192.168.9.7,0,Heli Basking Light,06:45:00-18:30:00 +esp8266_808501,192.168.9.7,1,Heli UV Light,07:00:00-18:15:00 +esp8266_052EE8,192.168.9.12,0,Heli Red/White LED,06:30:00-18:45:00 +esp8266_052FB1,192.168.9.13,0,Heli Blue LED,22:00:00-04:00:00 +esp8266_807AAD,192.168.9.8,0,Sticky Basking Light,06:45:00-18:30:00 +esp8266_807AAD,192.168.9.8,1,Sticky UV Light,07:00:00-18:15:00 +esp8266_202936,192.168.9.10,0,Sticky Red/White LED,06:30:00-18:45:00 +esp8266_053F26,192.168.9.11,0,Sticky Blue LED,22:00:00-04:00:00 +esp8266_807AAD,192.168.9.8,2,Sticky Fogging,17:02:00-17:22:00 +esp8266_807AAD,192.168.9.8,3,Sticky Raining,17:15:00-17:15:15 +``` diff --git a/deploy/config.csv b/deploy/config.csv new file mode 100644 index 0000000..0010d99 --- /dev/null +++ b/deploy/config.csv @@ -0,0 +1,12 @@ +# Generated on Sun Mar 31 13:03:21 CEST 2019 +# Device,IPv4,Chan,Name,TimeSpec(UTC) +esp8266_808501,192.168.9.7,0,Heli Basking Light,06:45:00-18:30:00 +esp8266_808501,192.168.9.7,1,Heli UV Light,07:00:00-18:15:00 +esp8266_052EE8,192.168.9.12,0,Heli Red/White LED,06:30:00-18:45:00 +esp8266_052FB1,192.168.9.13,0,Heli Blue LED,22:00:00-04:00:00 +esp8266_807AAD,192.168.9.8,0,Sticky Basking Light,06:45:00-18:30:00 +esp8266_807AAD,192.168.9.8,1,Sticky UV Light,07:00:00-18:15:00 +esp8266_202936,192.168.9.10,0,Sticky Red/White LED,06:30:00-18:45:00 +esp8266_053F26,192.168.9.11,0,Sticky Blue LED,22:00:00-04:00:00 +esp8266_807AAD,192.168.9.8,2,Sticky Fogging,17:02:00-17:22:00 +esp8266_807AAD,192.168.9.8,3,Sticky Raining,17:15:00-17:15:15 diff --git a/deploy/doall.sh b/deploy/doall.sh new file mode 100755 index 0000000..8e70e82 --- /dev/null +++ b/deploy/doall.sh @@ -0,0 +1,104 @@ +#!/bin/bash + +usage() { + [ -n "$1" ] && { + echo $1 + echo + } + echo "Usage:" + echo "$0 [-csv ] [-f] [-q] get|set" + echo "Using Input CSV file given by -csv argument, either get all channel info, or set all channel info." + echo "If not given, the file config.csv in the working directory is chosen" + echo "Setting -f (force) will call the RPCs, without it, only print what would be sent" + echo "Setting -q (quiet) will only output the CSV and errors" + echo "" + exit -1 +} + +log() { + [ $QUIET -eq 1 ] && return + echo "# $*" +} + + +doget() { + echo "# Generated on $(date)" + echo "# Device,IPv4,Chan,Name,TimeSpec(UTC)" + while IFS=, read -r dev ip chan name timespec + do + [ "${dev:0:1}" == "#" ] && continue + cmd="mos --port ws://$ip/rpc call TimeSpec.Get '{\"idx\": $chan }'" + log "$cmd" + SPEC=$(eval $cmd | awk '/spec/ { print $2 }' | sed -e 's,",,g') + echo "$dev,$ip,$chan,$name,$SPEC" + done < $CONFIG +} + +doset() { +# Device,IPv4,Chan,Name,TimeSpec(UTC) + while IFS=, read -r dev ip chan name timespec + do + [ "${dev:0:1}" == "#" ] && continue + + cmd="mos --port ws://$ip/rpc call TimeSpec.Clear '{\"idx\": $chan }'" + log "$cmd" + [ $FORCE -ne 0 ] && { + eval $cmd + } + cmd="mos --port ws://$ip/rpc call TimeSpec.Add '{\"idx\": $chan, \"spec\": \"$timespec\" }'" + log "$cmd" + [ $FORCE -ne 0 ] && { + eval $cmd + } + done < $CONFIG +} + +VERB="get" +CONFIG="config.csv" +FORCE=0 +QUIET=0 + +while [ $# -ne 0 ]; do + case $1 in + -f) + FORCE=1 + ;; + -q) + QUIET=1 + ;; + -csv) + shift + CONFIG=$1 + ;; + get) + VERB="get" + ;; + set) + VERB="set" + ;; + *) + usage "Unknown commandline flag '$1'" + ;; + esac + shift +done + +[ ! -r $CONFIG ] && { + echo "Cannot open file '$CONFIG'" + exit -2 +} + + +case $VERB in + get) + doget + ;; + set) + doset + ;; + *) + echo "Unknown verb: $VERB" + exit -3; + ;; +esac +