diff --git a/src/main.py b/src/main.py index 5855a8b..1f3ad2b 100755 --- a/src/main.py +++ b/src/main.py @@ -16,24 +16,32 @@ from threading import Timer send_pre = [5, 0, 1, 14, 0, 2, 0, 7, 1, 8] - -def post_thread(): + + +def post_thread(): global counter global post_time_s - counter+=1 - log.info("Posting in %s s",str(post_time_s - counter + 1)) - if counter == post_time_s : - post_scada(node.post_path,args.production) + counter += 1 + log.info("Posting in %s s", str(post_time_s - counter + 1)) + if counter == post_time_s: + post_scada(node.post_path, args.production) counter = 0 - post_timer = Timer(1.0,post_thread) + post_timer = Timer(1.0, post_thread) post_timer.start() - + def build_argparser(): label = subprocess.check_output(["git", "describe"]).strip() parser = argparse.ArgumentParser(description="To select production code") - parser.add_argument('-p','--production', action='store_true', default=False, help = "Create production code") - parser.add_argument('-v','--version', action='version', version=label.decode("utf-8")) + parser.add_argument('-p', + '--production', + action='store_true', + default=False, + help="Create production code") + parser.add_argument('-v', + '--version', + action='version', + version=label.decode("utf-8")) return parser @@ -90,7 +98,7 @@ def poll_loras(loras): if __name__ == "__main__": - + args = build_argparser().parse_args() log = build_logger() @@ -106,13 +114,13 @@ def poll_loras(loras): f_post_boot(node.loras, node.post_path) counter = 0 post_time_s = node.post_time - post_timer = Timer(1.0,post_thread) + post_timer = Timer(1.0, post_thread) post_timer.start() node.ser = serial.Serial(node.lora_port, timeout=14) wtd_start.stop() except Watchdog: log.error("Reseting script due to wdt boot") - wtd = Watchdog(300) # 5min + wtd = Watchdog(300) # 5min try: while True: poll_loras(node.loras) @@ -129,4 +137,4 @@ def poll_loras(loras): log.error("App Crashed!") log.error("Problems? %s", sys.exc_info()) log.info("Restarting...") - os.execv(sys.executable, ['python'] + sys.argv) + os.execv(sys.executable, ['python'] + sys.argv) diff --git a/src/post_http.py b/src/post_http.py index fabc8b7..d200512 100644 --- a/src/post_http.py +++ b/src/post_http.py @@ -7,20 +7,20 @@ log = logging.getLogger('post') ch = logging.NullHandler() ch.setLevel(logging.DEBUG) -formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +formatter = logging.Formatter( + '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) log.addHandler(ch) + def post_json(file, is_production): headers = {'Content-type': 'application/json'} scada_url = 'https://postman-echo.com/post' if is_production: - scada_url = "https://apimedidores.ciexpro.com/api/item/custom_create/" - log,debug(scada_url) + scada_url = "https://apimedidores.ciexpro.com/api/item/custom_create/" + log, debug(scada_url) try: - r = requests.post(scada_url, - json=file, - headers=headers) + r = requests.post(scada_url, json=file, headers=headers) log.info("Status code is : %s", str(r.status_code)) log.debug(str(r)) return r.status_code @@ -33,7 +33,7 @@ def post_json(file, is_production): def post_scada(post_path, is_production): log.info("Posting to Scada") - success_code =201 + success_code = 201 with open(post_path, 'r+') as post_file: data_dic = json.load(post_file) log.debug("Data to post: %s", str(data_dic))