def run(): source_lines = SourceLine.select().where(SourceLine.processed == False) rules = Rule.select().where(Rule.active == True).order_by( Rule.application_order) for source_line in source_lines: # First, build up the most inclusive list of matches. matches = {} for rule in rules: matches = rule.find_matches(source_line, matches) # Find the rules which apply. rules = {rule for rule in rules if rule.applies_to(source_line)} # Then, apply them so that the most specific rule takes precedence. output_line_attrs = {} for rule in rules: output_line_attrs = rule.apply(matches, output_line_attrs) # If changes need to be made: if bool(output_line_attrs): # Add to the attributes the source line # which created the output line. output_line_attrs.update(source_line=source_line) # Apply the dictionary of arguments # as keyword arguments to `create`. OutputLine.create(**output_line_attrs) # Mark the source line as processed. source_line.processed = True source_line.save()
def __init__(self, device): super().__init__() self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) self.client = MQTTClient(loop=self.loop) self.name = device.name self.device_id = device.id self.version = 0 self.rules = Rule.select().where(Rule.device_id == self.device_id and Rule.status == 1) logging.info("Device %s loads %d rules" % (self.name, len(self.rules)))
def get_all_rule(): result = [{ "id": r.id, "name": r.name, "description": r.description, "deviceId": r.device_id, "topic": r.topic, "columns": r.columns, "condition": r.condition, "path": r.path, "status": r.status } for r in Rule.select()] return json.dumps(result, ensure_ascii=False)
def run(self) -> None: try: self.loop.run_until_complete(self.connect()) self.loop.run_until_complete(self.subscribe()) while True: current_version = self.version while current_version == self.version: self.loop.run_until_complete(self.handle()) self.rules = Rule.select().where(Rule.device_id == self.device_id and Rule.status == 1) logging.info("Device %s loads %d rules" % (self.name, len(self.rules))) finally: logging.error("Load rule failed: " + self.name) self.loop.close()