Update
Near working
This commit is contained in:
parent
876a7da730
commit
6ecbf02c5e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
fup_anemometer.log
|
@ -9,6 +9,7 @@ from pythonosc import udp_client
|
||||
from pythonosc import osc_message_builder
|
||||
import time
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
# import urllib library
|
||||
@ -21,6 +22,9 @@ import json
|
||||
# import argparse
|
||||
import argparse
|
||||
|
||||
# Logging to file
|
||||
import logging
|
||||
|
||||
# Errors codes
|
||||
OSC_no_error = 0
|
||||
OSC_error = 1
|
||||
@ -28,22 +32,29 @@ OSC_meteobridge_auth_error = 10
|
||||
OSC_meteobridge_url_error = 11
|
||||
OSC_meteobridge_http_error = 12
|
||||
OSC_meteobridge_nosensor_error = 20
|
||||
|
||||
OSC_meteobridge_restart = 30
|
||||
|
||||
debug_args_print = True
|
||||
debug_json_print = True
|
||||
debug_urllib_print = True
|
||||
debug_restart_print = True
|
||||
debug_osc = False
|
||||
debug_working_directory = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
# define urllib timeout in seconds
|
||||
url_timeout = 10
|
||||
|
||||
# max error number before self restart
|
||||
# max_error_rst = 3
|
||||
|
||||
prod_flag = False
|
||||
prod_working_directory = "/usr/local/com.fup.anemometre_daemon"
|
||||
|
||||
meteobridge_template = "cgi-bin/template.cgi?template={%22time%22:%22[DD][MM][YYYY][hh][mm][ss]%22,%22speed%22:[wind0wind-act],%22lastspeed%22:[wind0wind-lastval],%22battery%22:[wind0lowbat-act.0],%22wind-age%22:[wind0wind-age],%22signal%22:[wind0signal-act.0]}"
|
||||
|
||||
def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_port):
|
||||
def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_port, errors_restart):
|
||||
|
||||
error_counter = 0
|
||||
|
||||
http_auth_status = False
|
||||
|
||||
@ -51,9 +62,13 @@ def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_po
|
||||
|
||||
meteobridge_sensor_req = meteobridge_url + meteobridge_template
|
||||
|
||||
# Logging
|
||||
logging.basicConfig(filename='fup_anemometer.log', encoding='utf-8', format='%(asctime)s-%(levelname)s:%(message)s', datefmt='%Y/%m/%d %H:%M:%S', level=logging.DEBUG)
|
||||
logging.info("fup_anemometer started")
|
||||
|
||||
while True:
|
||||
if http_auth_status == False:
|
||||
# http log to meteobridge
|
||||
# http log to meteobridge
|
||||
passman = urllib.request.HTTPPasswordMgrWithDefaultRealm()
|
||||
passman.add_password(None, meteobridge_url, meteobridge_user, meteobridge_passwd)
|
||||
authhandler = urllib.request.HTTPBasicAuthHandler(passman)
|
||||
@ -66,6 +81,8 @@ def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_po
|
||||
except urllib.error.HTTPError as e:
|
||||
if debug_urllib_print:
|
||||
print('HTTP Error code: ', e.code)
|
||||
logging.error("Authentification to " + meteobridge_url + " - HTTP Error code: " + str(e.code))
|
||||
error_counter += 1
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_meteobridge_http_error)
|
||||
oscSender.send(msg.build())
|
||||
@ -76,6 +93,8 @@ def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_po
|
||||
except urllib.error.URLError as e:
|
||||
if debug_urllib_print:
|
||||
print('URL Error Reason: ', e.reason)
|
||||
logging.error("Authentification to " + meteobridge_url + " - URL Error Reason: " + str(e.reason))
|
||||
error_counter += 1
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_meteobridge_url_error)
|
||||
oscSender.send(msg.build())
|
||||
@ -84,10 +103,12 @@ def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_po
|
||||
# raise SystemExit(e)
|
||||
|
||||
else:
|
||||
error_counter = 0
|
||||
res_body = res.read()
|
||||
if debug_urllib_print:
|
||||
print(res_body.decode('utf-8'))
|
||||
http_auth_status = True
|
||||
logging.info("HTTP authentification to " + meteobridge_url + " succesful")
|
||||
# Sleep 2 seconds if no errors
|
||||
time.sleep(2)
|
||||
|
||||
@ -99,6 +120,8 @@ def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_po
|
||||
except urllib.error.HTTPError as e:
|
||||
if debug_urllib_print:
|
||||
print('HTTP Error code: ', e.code)
|
||||
logging.error("Get sensor datas from : " + meteobridge_url + " - HTTP Error code: " + str(e.code))
|
||||
error_counter += 1
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_meteobridge_http_error)
|
||||
oscSender.send(msg.build())
|
||||
@ -108,6 +131,8 @@ def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_po
|
||||
except urllib.error.URLError as e:
|
||||
if debug_urllib_print:
|
||||
print('URL Error Reason: ', e.reason)
|
||||
logging.error("Get sensor datas from : " + meteobridge_url + " - URL Error Reason: " + str(e.reason))
|
||||
error_counter += 1
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_meteobridge_url_error)
|
||||
oscSender.send(msg.build())
|
||||
@ -128,6 +153,8 @@ def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_po
|
||||
except json.JSONDecodeError as e:
|
||||
if debug_json_print:
|
||||
print("Invalid JSON syntax:", e)
|
||||
logging.error("Decode JSON from : " + meteobridge_url + " - Invalid JSON syntax : " + str(e))
|
||||
error_counter += 1
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_meteobridge_nosensor_error)
|
||||
oscSender.send(msg.build())
|
||||
@ -135,6 +162,7 @@ def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_po
|
||||
time.sleep(5)
|
||||
|
||||
else:
|
||||
error_counter = 0
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/speed")
|
||||
msg.add_arg(wind_lastspeed)
|
||||
oscSender.send(msg.build())
|
||||
@ -154,8 +182,22 @@ def main(meteobridge_url, meteobridge_user, meteobridge_passwd, max_host, max_po
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_no_error)
|
||||
oscSender.send(msg.build())
|
||||
if debug_osc:
|
||||
logging.debug("OSC datas sent")
|
||||
# Sleep 2 seconds if no errors
|
||||
time.sleep(2)
|
||||
time.sleep(2)
|
||||
# Self restart
|
||||
if error_counter >= int(errors_restart):
|
||||
msg = osc_message_builder.OscMessageBuilder(address = "/env_sensors/anemometer/error")
|
||||
msg.add_arg(OSC_meteobridge_restart)
|
||||
oscSender.send(msg.build())
|
||||
time.sleep(1)
|
||||
if debug_restart_print:
|
||||
print("Current errors count : " + str(error_counter) + " / " + errors_restart + " max errors")
|
||||
print("Too many errors restarting program ...")
|
||||
logging.error("Current errors count : " + str(error_counter) + " / " + errors_restart + " max errors")
|
||||
logging.error("Too many errors restarting program ...")
|
||||
os.execv(sys.executable, ['python'] + sys.argv)
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="FUP anemometer for OSC")
|
||||
@ -164,6 +206,8 @@ if __name__ == "__main__":
|
||||
parser.add_argument('-p', '--passwd-meteobridge',default='meteobridge')
|
||||
parser.add_argument('-mh', '--max-host', default='localhost')
|
||||
parser.add_argument('-mp','--max-port', default=2222)
|
||||
parser.add_argument('-e', '--errors-restart', default=20)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# if debug_args_print is True, print arguments
|
||||
@ -173,5 +217,6 @@ if __name__ == "__main__":
|
||||
print('Meteobridge Password:', args.passwd_meteobridge)
|
||||
print('OSC host:', args.max_host)
|
||||
print('OSC port:', args.max_port)
|
||||
|
||||
main(meteobridge_url=args.url_meteobridge, meteobridge_user=args.user_meteobridge, meteobridge_passwd=args.passwd_meteobridge, max_host=args.max_host, max_port=args.max_port)
|
||||
print('Errors restart:', args.errors_restart)
|
||||
|
||||
main(meteobridge_url=args.url_meteobridge, meteobridge_user=args.user_meteobridge, meteobridge_passwd=args.passwd_meteobridge, max_host=args.max_host, max_port=args.max_port, errors_restart=args.errors_restart)
|
@ -1,14 +0,0 @@
|
||||
2024-06-12 00:56:38,580 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:56:43,585 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:56:48,590 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:56:53,596 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:56:58,601 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:57:03,606 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:57:08,613 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:57:13,620 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:57:18,627 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:57:23,633 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:57:28,640 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:57:33,647 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:57:38,654 - fup_anemo_daemon - INFO - 27.1
|
||||
2024-06-12 00:57:43,660 - fup_anemo_daemon - INFO - 27.1
|
Loading…
Reference in New Issue
Block a user