From 6f887552768f8b6aaf746b43307a920d6c833871 Mon Sep 17 00:00:00 2001 From: shakemid Date: Sun, 24 Mar 2024 19:30:59 +0900 Subject: [PATCH] fix switchbotmeter_multi for handling HTTP errors --- plugins/sensors/switchbotmeter_multi | 53 +++++++++++++++++----------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/plugins/sensors/switchbotmeter_multi b/plugins/sensors/switchbotmeter_multi index 33d9395c..559bbbcf 100755 --- a/plugins/sensors/switchbotmeter_multi +++ b/plugins/sensors/switchbotmeter_multi @@ -75,7 +75,7 @@ graph_args --base 1000 EOF for deviceid in ${deviceids} do - echo ${deviceid}.label ${deviceid} + echo "${deviceid}.label" "${deviceid}" done cat <&2 - exit 1 + http_status=$( echo "${http_response}" | tail -n 1 ) + if [ "${http_status}" != "200" ]; then + echo Error with HTTP status = "${http_status}" 1>&2 + return 1 fi - temperature=$( echo "${response}" | jq '.body.temperature' ) - humidity=$( echo "${response}" | jq '.body.humidity' ) - battery=$( echo "${response}" | jq '.body.battery' ) + api_response=$( echo "${http_response}" | sed '$d' ) + api_status=$( echo "${api_response}" | jq '.statusCode' ) + 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