Last active
October 8, 2022 15:03
-
-
Save hrz6976/69ee0b5f5bb638e7f87ffe0ec0763d1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Capture: http://www.cpcelc.pku.edu.cn/phone/socket.php?apistr=XXXXX | |
OPERID="XXXXXXXXXXXXXXXXXXXXX" | |
# Reading Log | |
READING_LOG="/tmp/eletribot.log" | |
LOW_THRESHOLD=10 | |
# Serverchan sendkey https://sct.ftqq.com/ | |
SCKEY="YYYYYYYYYYYYYYYYYYYYYYY" | |
get_reading() { | |
PAYLOAD_STR=$(printf 'apistr={"operType":"BGRJ2018_CXDBSY_BY","operFlag":300023,"OperID":"%s"}' "$OPERID") | |
FLOAT_VAL=$(curl -Gs http://www.cpcelc.pku.edu.cn/phone/socket.php \ | |
--data-urlencode "$PAYLOAD_STR" \ | |
| grep -oE "([0-9]{1,3}\.)[0-9]{1,3}" \ | |
| head -n 1) | |
if [ -z "$FLOAT_VAL" ]; then | |
echo "Failed to get reading" | |
exit 1 | |
fi | |
printf "%.0f" "$FLOAT_VAL" | |
} | |
get_date_iso() { | |
date -u +"%Y-%m-%dT%H:%M:%SZ" | |
} | |
send_sc_message() { | |
if [ -z "$1" ] || [ -z "$2" ]; then | |
echo "Missing message or title: message=$1 title=$2" | |
return 1 | |
fi | |
curl -Gs "https://sc.ftqq.com/$SCKEY.send" \ | |
-X POST \ | |
-H 'Content-Type: application/x-www-form-urlencoded' \ | |
--data-urlencode "$(printf 'title=%s' "$1")" \ | |
--data-urlencode "$(printf 'desp=%s' "$2")" | |
} | |
READING=$(get_reading) | |
echo "Current reading: $READING" | |
echo "$(get_date_iso) $READING" >> "$READING_LOG" | |
if [ -z "$READING" ]; then | |
echo "Failed to get reading" | |
exit 1 | |
fi | |
TITLE=$(printf '电量剩余%s度' "$READING") | |
if [ -f "$READING_LOG" ]; then | |
MESSAGE="最近三次读数:" | |
MESSAGE="$MESSAGE $(printf '%s\n' "$(tail -n 3 "$READING_LOG")")" | |
else | |
touch "$READING_LOG" | |
MESSAGE="没有近期记录" | |
fi | |
if [ "$READING" -lt "$LOW_THRESHOLD" ]; then | |
TITLE="【尽快充电】$TITLE" | |
MESSAGE="购电:http://www.cpcelc.pku.edu.cn/phone/mainpage.php | |
$MESSAGE" | |
fi | |
echo "$TITLE" "$MESSAGE" | |
send_sc_message "$TITLE" "$MESSAGE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment