def on_models_committed(sender, changes): try: for obj, change in changes: # avoid recursion if isinstance(obj, models.Pwm): L.l.info("Commit change PWM={}".format(obj)) if hasattr(obj, Constant.JSON_PUBLISH_NOTIFY_TRANSPORT): # only send mqtt message once for db saves intended to be distributed if obj.notify_transport_enabled: if hasattr(obj, Constant.JSON_PUBLISH_NOTIFY_DB_COMMIT): if not obj.notified_on_db_commit: obj.notified_on_db_commit = True txt = model_helper.model_row_to_json( obj, operation=change) if txt is None: txt = model_helper.model_row_to_json( obj, operation=change) pass # execute all events directly first, # then broadcast, local events not handled by remote mqtt queue _handle_internal_event(utils.json2obj(txt)) # _process_obj(utils.json2obj(txt)) transport.send_message_json(json=txt) else: pass # send object to rule parser, if connected dispatcher.send(Constant.SIGNAL_DB_CHANGE_FOR_RULES, obj=obj, change=change) except Exception as ex: L.l.exception('Error in DB commit hook, {}'.format(ex))
def handle_local_event_db_post(model, row, last_commit_field_changed_list=None): processed = False L.l.debug('Local DB change sent by model {} row {}'.format(model, row)) if str(models.Parameter) in str(model): # fixme: update app if params are changing to avoid need for restart processed = True # no need to propagate changes to other nodes elif str(models.Module) in str(model): if row.host_name == Constant.HOST_NAME: main.init_module(row.name, row.active) processed = True # propagate changes to all nodes as each must execute db sync or other commands locally # add here tables you are sure are safe to be propagated to all nodes elif str(models.Node) in str(model) or str(models.GpioPin) in str(model) \ or str(models.ZoneCustomRelay) in str(model) \ or str(models.Rule) in str(model) \ or str(models.ZoneThermostat) in str(model)\ or str(models.Pwm) in str(model): # or str(models.Area) in str(model) : txt = model_helper.model_row_to_json(row, operation='update') # execute all events directly first, then broadcast, as local events are not handled by remote mqtt queue obj_json = utils.json2obj(txt) obj_json[Constant. JSON_PUBLISH_FIELDS_CHANGED] = last_commit_field_changed_list # handle_event_mqtt_received(None, None, 'direct-event', obj_json) #_handle_internal_event(obj_json) _process_obj(obj_json) # mqtt_thread_run() transport.send_message_json(json=txt) processed = True
def handle_local_event_db_post(model, row): processed = False L.l.debug('Local DB change sent by model {} row {}'.format(model, row)) if str(models.Parameter) in str(model): # fixme: update app if params are changing to avoid need for restart processed = True # no need to propagate changes to other nodes elif str(models.Module) in str(model): if row.host_name == Constant.HOST_NAME: main.init_module(row.name, row.active) processed = True # propagate changes to all nodes as each must execute db sync or other commands locally # add here tables you are sure are safe to be propagated to all nodes elif str(models.Node) in str(model) or str(models.GpioPin) in str(model) \ or str(models.ZoneCustomRelay) in str(model) \ or str(models.Rule) in str(model): # or str(models.Area) in str(model): txt = model_helper.model_row_to_json(row, operation='update') # execute all events directly first, then broadcast, as local events are not handled by remote mqtt queue handle_event_mqtt_received(None, None, 'direct-event', utils.json2obj(txt)) mqtt_thread_run() if transport.mqtt_io.client_connected: transport.send_message_json(json=txt) processed = True if processed: L.l.debug('Detected {} record change, row={}, trigger executed'.format( model, row)) else: L.l.debug( 'Detected {} record change, row={}, but change processing ignored'. format(model, row))
def _broadcast(record, update, class_name): try: record[Constant.JSON_PUBLISH_SOURCE_HOST] = str(Constant.HOST_NAME) record[Constant.JSON_PUBLISH_TABLE] = class_name record[Constant.JSON_PUBLISH_FIELDS_CHANGED] = list(update.keys()) record['_sent_on'] = utils.get_base_location_now_date() js = utils.safeobj2json(record) transport.send_message_json(json=js) except Exception as ex: L.l.error('Unable to broadcast {} rec={}'.format( class_name, record))
def emit(self, record): if transport.initialised and transport.mqtt_io.client_connected: msg = LogMessage() msg.message = self.format(record) msg.level = record.levelname transport.send_message_json(utils.unsafeobj2json(msg))