Update fup-anemomoter-user.py
Add OSC errors codes.
This commit is contained in:
parent
b53a7b615b
commit
876a7da730
@ -21,10 +21,23 @@ import json
|
||||
# import argparse
|
||||
import argparse
|
||||
|
||||
debug_args = True
|
||||
debug_json = True
|
||||
# Errors codes
|
||||
OSC_no_error = 0
|
||||
OSC_error = 1
|
||||
OSC_meteobridge_auth_error = 10
|
||||
OSC_meteobridge_url_error = 11
|
||||
OSC_meteobridge_http_error = 12
|
||||
OSC_meteobridge_nosensor_error = 20
|
||||
|
||||
|
||||
debug_args_print = True
|
||||
debug_json_print = True
|
||||
debug_urllib_print = True
|
||||
debug_working_directory = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
# define urllib timeout in seconds
|
||||
url_timeout = 10
|
||||
|
||||
prod_flag = False
|
||||
prod_working_directory = "/usr/local/com.fup.anemometre_daemon"
|
||||
|
||||
@ -48,78 +61,101 @@ def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_po
|
||||
urllib.request.install_opener(opener)
|
||||
|
||||
try:
|
||||
res = urllib.request.urlopen(meteobridge_url)
|
||||
res = urllib.request.urlopen(meteobridge_url, timeout = url_timeout)
|
||||
|
||||
except urllib.error.HTTPError as e:
|
||||
print('HTTP Error code: ', e.code)
|
||||
raise SystemExit(e)
|
||||
if debug_urllib_print:
|
||||
print('HTTP Error code: ', e.code)
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_meteobridge_http_error)
|
||||
oscSender.send(msg.build())
|
||||
# Sleep 5 seconds if error
|
||||
time.sleep(5)
|
||||
# raise SystemExit(e)
|
||||
|
||||
except urllib.error.URLError as e:
|
||||
print('URL Error Reason: ', e.reason)
|
||||
raise SystemExit(e)
|
||||
if debug_urllib_print:
|
||||
print('URL Error Reason: ', e.reason)
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_meteobridge_url_error)
|
||||
oscSender.send(msg.build())
|
||||
# Sleep 5 seconds if error
|
||||
time.sleep(5)
|
||||
# raise SystemExit(e)
|
||||
|
||||
else:
|
||||
res_body = res.read()
|
||||
print(res_body.decode('utf-8'))
|
||||
if debug_urllib_print:
|
||||
print(res_body.decode('utf-8'))
|
||||
http_auth_status = True
|
||||
# Sleep 2 seconds if no errors
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
else:
|
||||
try:
|
||||
response = urllib.request.urlopen(meteobridge_sensor_req)
|
||||
response = urllib.request.urlopen(meteobridge_sensor_req, timeout = url_timeout)
|
||||
|
||||
except urllib.error.HTTPError as e:
|
||||
print('HTTP Error code: ', e.code)
|
||||
if debug_urllib_print:
|
||||
print('HTTP Error code: ', e.code)
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(1)
|
||||
msg.add_arg(OSC_meteobridge_http_error)
|
||||
oscSender.send(msg.build())
|
||||
# Sleep 5 seconds if error
|
||||
time.sleep(5)
|
||||
|
||||
except urllib.error.URLError as e:
|
||||
print('URL Error Reason: ', e.reason)
|
||||
if debug_urllib_print:
|
||||
print('URL Error Reason: ', e.reason)
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(1)
|
||||
msg.add_arg(OSC_meteobridge_url_error)
|
||||
oscSender.send(msg.build())
|
||||
# Sleep 5 seconds if error
|
||||
time.sleep(5)
|
||||
|
||||
# storing the JSON response
|
||||
# from url in data
|
||||
try:
|
||||
data_json = json.loads(response.read())
|
||||
if debug_json == True:
|
||||
print(data_json)
|
||||
wind_speed = float(data_json["speed"])
|
||||
wind_lastspeed = float(data_json["lastspeed"])
|
||||
wind_batt = int(data_json["battery"])
|
||||
wind_age = int(data_json["wind-age"])
|
||||
wind_signal = int(data_json["signal"])
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
print("Invalid JSON syntax:", e)
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(1)
|
||||
oscSender.send(msg.build())
|
||||
# print the json response
|
||||
# print(data_json)
|
||||
else:
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/speed")
|
||||
msg.add_arg(wind_lastspeed)
|
||||
oscSender.send(msg.build())
|
||||
try:
|
||||
data_json = json.loads(response.read())
|
||||
if debug_json_print == True:
|
||||
print(data_json)
|
||||
wind_speed = float(data_json["speed"])
|
||||
wind_lastspeed = float(data_json["lastspeed"])
|
||||
wind_batt = int(data_json["battery"])
|
||||
wind_age = int(data_json["wind-age"])
|
||||
wind_signal = int(data_json["signal"])
|
||||
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/battery")
|
||||
msg.add_arg(wind_batt)
|
||||
oscSender.send(msg.build())
|
||||
except json.JSONDecodeError as e:
|
||||
if debug_json_print:
|
||||
print("Invalid JSON syntax:", e)
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_meteobridge_nosensor_error)
|
||||
oscSender.send(msg.build())
|
||||
# Sleep 5 seconds if error
|
||||
time.sleep(5)
|
||||
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/age")
|
||||
msg.add_arg(wind_age)
|
||||
oscSender.send(msg.build())
|
||||
else:
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/speed")
|
||||
msg.add_arg(wind_lastspeed)
|
||||
oscSender.send(msg.build())
|
||||
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/signal")
|
||||
msg.add_arg(wind_signal)
|
||||
oscSender.send(msg.build())
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/battery")
|
||||
msg.add_arg(wind_batt)
|
||||
oscSender.send(msg.build())
|
||||
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(0)
|
||||
oscSender.send(msg.build())
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/age")
|
||||
msg.add_arg(wind_age)
|
||||
oscSender.send(msg.build())
|
||||
|
||||
time.sleep(1)
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/signal")
|
||||
msg.add_arg(wind_signal)
|
||||
oscSender.send(msg.build())
|
||||
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_no_error)
|
||||
oscSender.send(msg.build())
|
||||
# Sleep 2 seconds if no errors
|
||||
time.sleep(2)
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="FUP anemometer for OSC")
|
||||
@ -130,8 +166,8 @@ if __name__ == "__main__":
|
||||
parser.add_argument('-mp','--max-port', default=2222)
|
||||
args = parser.parse_args()
|
||||
|
||||
# if debug_args is True, print arguments
|
||||
if debug_args:
|
||||
# if debug_args_print is True, print arguments
|
||||
if debug_args_print:
|
||||
print('Meteobridge URL:', args.url_meteobridge)
|
||||
print('Meteobridge User:', args.user_meteobridge)
|
||||
print('Meteobridge Password:', args.passwd_meteobridge)
|
||||
|
Loading…
Reference in New Issue
Block a user