From e926ad45029504e849fa101059462399e728d5f1 Mon Sep 17 00:00:00 2001 From: Motea Marius Date: Thu, 21 Dec 2023 16:04:36 +0200 Subject: [PATCH] Ensure the actions are executed in order --- BridgeEmulator/functions/rules.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/BridgeEmulator/functions/rules.py b/BridgeEmulator/functions/rules.py index 1df391607..136afdd10 100755 --- a/BridgeEmulator/functions/rules.py +++ b/BridgeEmulator/functions/rules.py @@ -1,9 +1,9 @@ import logManager import configManager -import json + from datetime import datetime from threading import Thread -from time import sleep, strftime +from time import sleep import requests logging = logManager.logger.get_logger(__name__) @@ -55,7 +55,7 @@ def checkRuleConditions(rule, device, current_time, ignore_ddx=False): if not bridgeConfig[url_pices[1]][url_pices[2]].dxState[url_pices[4]] == current_time: return [False, 0] else: - ddx = int(condition["value"][2:4]) * 3600 + int(condition["value"][5:7]) * 60 + int(condition["value"][8:10]) + ddx = int(condition["value"][2:4]) * 3600 + int(condition["value"][5:7]) * 60 + int(condition["value"][-2:]) ddx_sensor = url_pices except Exception as e: logging.debug("rule " + rule.name + " failed, reason:" + str(e)) @@ -84,13 +84,17 @@ def ddxRecheck(rule, device, current_time, ddx_delay, ddx_sensor): elif action["method"] == "PUT": requests.put("http://localhost/api/local" + action["address"], json=action["body"], timeout=5) -def threadRequest(method, address, data): - if method == "POST": - requests.post(address, json=data, timeout=5) - elif method == "PUT": - requests.put(address, json=data, timeout=5) - - +def threadActions(actionsToExecute): + sleep(0.2) + for action in actionsToExecute: + urlPrefix = "http://localhost/api/local" + if action["address"].startswith("http"): + urlPrefix = "" + if action["method"] == "POST": + requests.post( urlPrefix + action["address"], json=action["body"], timeout=5) + elif action["method"] == "PUT": + requests.put( urlPrefix + action["address"], json=action["body"], timeout=5) + def rulesProcessor(device, current_time): logging.debug("Processing rules for " + device.name) @@ -109,8 +113,5 @@ def rulesProcessor(device, current_time): else: #if ddx rule logging.info("ddx rule " + rule.id_v1 + ", name: " + rule.name + " will be re validated after " + str(rule_result[1]) + " seconds") Thread(target=ddxRecheck, args=[rule, device, current_time, rule_result[1], rule_result[2]]).start() - for action in actionsToExecute: - urlPrefix = "http://localhost/api/local" - if action["address"].startswith("http"): - urlPrefix = "" - Thread(target=threadRequest, args=[action["method"], urlPrefix + action["address"], action["body"]]).start() + + Thread(target=threadActions, args=[actionsToExecute]).start()