Merge pull request #1422 from shakemid/fix_switchbotmeter_multi

Fix switchbotmeter_multi
This commit is contained in:
Kenyon Ralph 2024-03-29 15:04:39 -07:00 committed by GitHub
commit 2e8f4da189
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 43 additions and 29 deletions

View File

@ -75,7 +75,7 @@ graph_args --base 1000
EOF EOF
for deviceid in ${deviceids} for deviceid in ${deviceids}
do do
echo ${deviceid}.label ${deviceid} echo "${deviceid}.label" "${deviceid}"
done done
cat <<EOF cat <<EOF
@ -88,7 +88,7 @@ graph_args --base 1000 --lower-limit 0 --upper-limit 100
EOF EOF
for deviceid in ${deviceids} for deviceid in ${deviceids}
do do
echo ${deviceid}.label ${deviceid} echo "${deviceid}.label" "${deviceid}"
done done
cat <<EOF cat <<EOF
@ -101,26 +101,32 @@ graph_args --base 1000 --lower-limit 0 --upper-limit 100
EOF EOF
for deviceid in ${deviceids} for deviceid in ${deviceids}
do do
echo ${deviceid}.label ${deviceid} echo "${deviceid}.label" "${deviceid}"
done done
} }
fetch() { fetch() {
local deviceid func val local deviceid func ref
for deviceid in ${deviceids} for deviceid in ${deviceids}
do do
declare $( do_fetch ${deviceid} ) do_fetch "${deviceid}" &
done
wait
for deviceid in ${deviceids}
do
local padding=1 $( cat "${MUNIN_STATEFILE}_${deviceid}" )
done done
for func in temperature humidity battery for func in temperature humidity battery
do do
echo multigraph ${pluginname}_${func} echo multigraph "${pluginname}_${func}"
for deviceid in ${deviceids} for deviceid in ${deviceids}
do do
val=${func}_${deviceid} ref=${func}_${deviceid}
echo ${deviceid}.value ${!val} echo "${deviceid}.value" "${!ref:-U}"
done done
done done
} }
@ -139,39 +145,47 @@ do_fetch() {
time_modified=0 time_modified=0
fi fi
if [ $(( time_now - time_modified )) -le "${interval}" ]; then if [ $(( time_now - time_modified )) -lt "${interval}" ]; then
# read from cache # do nothing
cat "${MUNIN_STATEFILE}_${deviceid}" return
else
# update cache
fetch_api ${deviceid} | tee "${MUNIN_STATEFILE}_${deviceid}"
fi fi
else
fetch_api ${deviceid}
fi fi
fetch_api "${deviceid}" > "${MUNIN_STATEFILE}_${deviceid}"
} }
fetch_api() { fetch_api() {
local deviceid temperature humidity battery t sign nonce response statusCode local deviceid temperature humidity battery t sign nonce http_response http_status api_response api_status
deviceid=$1 deviceid=$1
t=$( date +'%s000' ) t=$( date +'%s%3N' )
nonce=$( openssl rand -base64 32 ) nonce=$( openssl rand -base64 32 )
sign=$( echo -n ${token}${t}${nonce} | openssl dgst -sha256 -hmac ${secret} -binary | base64 ) sign=$( echo -n "${token}${t}${nonce}" | openssl dgst -sha256 -hmac "${secret}" -binary | openssl base64 )
response=$( curl -s -H "Content-Type:application/json;charset=utf8" -H "Authorization:${token}" -H "t:${t}" -H "nonce:${nonce}" -H "sign:${sign}" "https://api.switch-bot.com/v1.1/devices/${deviceid}/status" ) http_response=$( curl -s --fail --retry 3 \
-w '\n%{http_code}\n' \
-H "Content-Type:application/json;charset=utf8" \
-H "Authorization:${token}" -H "t:${t}" -H "nonce:${nonce}" -H "sign:${sign}" \
"https://api.switch-bot.com/v1.1/devices/${deviceid}/status" )
statusCode=$( echo "${response}" | jq '.statusCode' ) http_status=$( echo "${http_response}" | tail -n 1 )
if [ "${statusCode}" -ne 100 ]; then if [ "${http_status}" != "200" ]; then
echo Error with statusCode = "${statusCode}" 1>&2 echo Error with HTTP status = "${http_status}" 1>&2
exit 1 return 1
fi fi
temperature=$( echo "${response}" | jq '.body.temperature' ) api_response=$( echo "${http_response}" | sed '$d' )
humidity=$( echo "${response}" | jq '.body.humidity' ) api_status=$( echo "${api_response}" | jq '.statusCode' )
battery=$( echo "${response}" | jq '.body.battery' ) if [ "${api_status}" != "100" ]; then
echo Error with API status = "${api_status}" 1>&2
return 1
fi
echo temperature_${deviceid}=${temperature} humidity_${deviceid}=${humidity} battery_${deviceid}=${battery} temperature=$( echo "${api_response}" | jq '.body.temperature' )
humidity=$( echo "${api_response}" | jq '.body.humidity' )
battery=$( echo "${api_response}" | jq '.body.battery' )
echo "temperature_${deviceid}=${temperature}" "humidity_${deviceid}=${humidity}" "battery_${deviceid}=${battery}"
} }
# Main # Main