diff --git a/plugins/sensors/switchbotmeter_multi b/plugins/sensors/switchbotmeter_multi index 33d9395c..42c8cecc 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 < "${MUNIN_STATEFILE}_${deviceid}" } 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 - t=$( date +'%s000' ) - nonce=$( openssl rand -base64 32 ) - sign=$( echo -n ${token}${t}${nonce} | openssl dgst -sha256 -hmac ${secret} -binary | base64 ) + t=$( date +'%s%3N' ) + nonce=$( openssl rand -base64 32 ) + 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' ) - if [ "${statusCode}" -ne 100 ]; then - echo Error with statusCode = "${statusCode}" 1>&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