Skip to content

Commit

Permalink
Ensure the actions are executed in order
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusmotea authored Dec 21, 2023
1 parent 6b3446c commit e926ad4
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions BridgeEmulator/functions/rules.py
Original file line number Diff line number Diff line change
@@ -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__)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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()

0 comments on commit e926ad4

Please sign in to comment.