def do_insertion_jobs(self, dbcon): while self.insert_message_queue or self.custom_insert_message_queue: #print "Inserting: ", len(self.insert_message_queue), len(self.custom_insert_message_queue) header = self.agent_id message = "" try: if self.insert_message_queue: sender,message = self.insert_message_queue.popleft() self.deserialize_date(message) agent_id = message.get('agentID','') header = agent_id cassandraDB.insert(message['agentID'], message['all_vars'], message['log_vars'], cur_timeLocal=message['cur_timeLocal'], tablename=message['tablename']) if self.custom_insert_message_queue: sender,message = self.custom_insert_message_queue.popleft() self.deserialize_date(message) header = sender cassandraDB.customInsert(message['all_vars'], message['log_vars'], tablename=message['tablename']) self.debugLog(__name__,payload=str(message),header=header,comments="TSD Entry success") except: self.warningLog(__name__,payload=traceback.format_exc(),header=header,comments="Writing to TSD failed") self.warningLog(__name__,payload=str(message),header=header,comments="This was the message:") time.sleep(0.001)
def backupSaveData(self): try: APIObject.getDeviceStatus() cassandraDB.insert(agent_id,APIObject.variables,log_variables) print('Data Pushed to cassandra during backup') except Exception as er: print("ERROR: {} fails to update cassandra database".format(agent_id)) print er
def backupSaveData(self): try: APIObject.getDeviceStatus() cassandraDB.insert(agent_id,APIObject.variables,log_variables) print('Data Pushed to cassandra') except Exception as er: print("ERROR: {} fails to update cassandra database".format(agent_id)) print er
def backupSaveData(self): try: with threadingLock: Plugload.getDeviceStatus() cassandraDB.insert(agent_id,Plugload.variables,log_variables) print('Data Pushed to cassandra as a backup') except Exception as er: print("ERROR: {} fails to update cassandra database".format(agent_id)) print er
def backupSaveData(self): try: with threadingLock: Plugload.getDeviceStatus() cassandraDB.insert(agent_id, Plugload.variables, log_variables) print('Data Pushed to cassandra as a backup') except Exception as er: print("ERROR: {} fails to update cassandra database".format( agent_id)) print er
def updateStatus(self, states=None): if states is not None: print "got state change:", states self.changed_variables = dict() if (self.get_variable('status') != 'ON' if states['status'] == 1 else 'OFF'): self.set_variable('status', 'ON' if states['status'] == 1 else 'OFF') self.changed_variables['status'] = log_variables['status'] if 'power' in states: if (self.get_variable('power') != states['power']): self.changed_variables['power'] = log_variables[ 'power'] self.set_variable('power', states['power']) with threadingLock: try: cassandraDB.insert(agent_id, self.variables, log_variables) print "cassandra success" except Exception as er: print("ERROR: {} fails to update cassandra database". format(agent_id)) print er self.updatePostgresDB() topic = '/agent/ui/' + device_type + '/device_status_response/' + _topic_Agent_UI_tail # now = datetime.utcnow().isoformat(' ') + 'Z' headers = { 'AgentID': agent_id, headers_mod.CONTENT_TYPE: headers_mod.CONTENT_TYPE.JSON, # headers_mod.DATE: now, } if self.get_variable('power') is not None: _data = { 'device_id': agent_id, 'status': self.get_variable('status'), 'power': self.get_variable('power') } else: _data = { 'device_id': agent_id, 'status': self.get_variable('status') } message = json.dumps(_data) message = message.encode(encoding='utf_8') self.publish(topic, headers, message)
def do_insertion_jobs(self): while self.insert_message_queue or self.custom_insert_message_queue: print "Inserting: ", len(self.insert_message_queue), len(self.custom_insert_message_queue) try: if self.insert_message_queue: message = self.insert_message_queue.popleft() self.deserialize_date(message) cassandraDB.insert(message['agentID'], message['all_vars'], message['log_vars'], cur_timeLocal=message['cur_timeLocal'], tablename=message['tablename']) if self.custom_insert_message_queue: message = self.custom_insert_message_queue.popleft() self.deserialize_date(message) cassandraDB.customInsert(message['all_vars'], message['log_vars'], tablename=message['tablename']) except Exception as er: print er print "TSD data entry failure" time.sleep(0.001)
def updateStatus(self,states=None): if states is not None: print "got state change:",states self.changed_variables = dict() if(self.get_variable('status') != 'ON' if states['status']==1 else 'Off'): self.set_variable('status','ON' if states['status']==1 else 'Off') self.changed_variables['status'] = log_variables['status'] if 'power' in states: if(self.get_variable('power') != states['power']): self.changed_variables['power'] = log_variables['power'] self.set_variable('power',states['power']) with threadingLock: try: cassandraDB.insert(agent_id,self.variables,log_variables) print "cassandra success" except Exception as er: print("ERROR: {} fails to update cassandra database".format(agent_id)) print er self.updatePostgresDB() topic = '/agent/ui/'+device_type+'/device_status_response/'+_topic_Agent_UI_tail # now = datetime.utcnow().isoformat(' ') + 'Z' headers = { 'AgentID': agent_id, headers_mod.CONTENT_TYPE: headers_mod.CONTENT_TYPE.JSON, # headers_mod.DATE: now, } if self.get_variable('power') is not None: _data={'device_id':agent_id, 'status':self.get_variable('status'), 'power':self.get_variable('power')} else: _data={'device_id':agent_id, 'status':self.get_variable('status')} message = json.dumps(_data) message = message.encode(encoding='utf_8') self.publish(topic, headers, message)
def deviceMonitorBehavior(self): #step1: get current status of a thermostat, then map keywords and variables to agent knowledge try: Light.getDeviceStatus() except: print("device connection is not successful") self.changed_variables = dict() for v in log_variables: if v in Light.variables: if not v in self.variables or self.variables[ v] != Light.variables[v]: self.variables[v] = Light.variables[v] self.changed_variables[v] = log_variables[v] else: if v not in self.variables: #it won't be in self.variables either (in the first time) self.changed_variables[v] = log_variables[v] self.variables[v] = None try: # Step: Check if any Device is OFFLINE self.cur.execute( "SELECT id FROM " + db_table_active_alert + " WHERE event_trigger_id=%s", ('5', )) if self.cur.rowcount != 0: self.device_offline_detection() # Put scan time in database _time_stamp_last_scanned = datetime.datetime.now() self.cur.execute( "UPDATE " + db_table_lighting + " SET last_scanned_time=%s " "WHERE lighting_id=%s", (_time_stamp_last_scanned, agent_id)) self.con.commit() except Exception as er: print er print("ERROR: {} failed to update last scanned time".format( agent_id)) if len(self.changed_variables) == 0: print 'nothing changed' return self.updateUI() #step4: update PostgresQL (meta-data) database try: self.cur.execute( "UPDATE " + db_table_lighting + " SET status=%s WHERE lighting_id=%s", (self.get_variable('status'), agent_id)) self.con.commit() self.cur.execute( "UPDATE " + db_table_lighting + " SET brightness=%s WHERE lighting_id=%s", (self.get_variable('brightness'), agent_id)) self.con.commit() self.cur.execute( "UPDATE " + db_table_lighting + " SET color=%s WHERE lighting_id=%s", (self.get_variable('hexcolor'), agent_id)) self.con.commit() try: if self.get_variable('status') == "ON": multiple_on_off_status = "" for dummyvar in range( self.get_variable('number_lights')): multiple_on_off_status += "1" self.cur.execute( "UPDATE " + db_table_lighting + " SET multiple_on_off=%s WHERE lighting_id=%s", (multiple_on_off_status, agent_id)) self.con.commit() else: # status is off multiple_on_off_status = "" for dummyvar in range( self.get_variable('number_lights')): multiple_on_off_status += "0" self.cur.execute( "UPDATE " + db_table_lighting + " SET multiple_on_off=%s WHERE lighting_id=%s", (multiple_on_off_status, agent_id)) self.con.commit() except: print("{} this agent has no multiple_on_off_status".format( agent_id)) #TODO check ip_address if self.ip_address != None: psycopg2.extras.register_inet() _ip_address = psycopg2.extras.Inet(self.ip_address) self.cur.execute( "UPDATE " + db_table_lighting + " SET ip_address=%s WHERE lighting_id=%s", (_ip_address, agent_id)) self.con.commit() if self.get_variable('offline_count') >= 3: self.cur.execute( "UPDATE " + db_table_lighting + " SET network_status=%s WHERE lighting_id=%s", ('OFFLINE', agent_id)) self.con.commit() if self.already_offline is False: self.already_offline = True _time_stamp_last_offline = str(datetime.datetime.now()) self.cur.execute( "UPDATE " + db_table_lighting + " SET last_offline_time=%s WHERE lighting_id=%s", (_time_stamp_last_offline, agent_id)) self.con.commit() else: self.already_offline = False self.cur.execute( "UPDATE " + db_table_lighting + " SET network_status=%s WHERE lighting_id=%s", ('ONLINE', agent_id)) self.con.commit() print( "{} updates database name {} during deviceMonitorBehavior successfully" .format(agent_id, db_database)) except: print("ERROR: {} fails to update the database name {}".format( agent_id, db_database)) #step5: update Cassandra (time-series) database try: cassandraDB.insert(agent_id, self.variables, log_variables) print('Data Pushed to cassandra') print "{} success update".format(agent_id) except Exception as er: print("ERROR: {} fails to update cassandra database".format( agent_id)) print er #step6: debug agent knowledge if debug_agent: print("printing agent's knowledge") for k, v in self.variables.items(): print(k, v) print('') if debug_agent: print("printing agentAPImapping's fields") for k, v in agentAPImapping.items(): if k is None: agentAPImapping.update({v: v}) agentAPImapping.pop(k) for k, v in agentAPImapping.items(): print(k, v)
def deviceMonitorBehavior(self): #step1: get current status of a thermostat, then map keywords and variables to agent knowledge try: APIObject.getDeviceStatus() except Exception as er: print("device connection is not successful. error: "+str(er)) self.changed_variables = dict() for v in log_variables: if v in APIObject.variables: if not v in self.variables or self.variables[v] != APIObject.variables[v]: self.variables[v] = APIObject.variables[v] self.changed_variables[v] = log_variables[v] else: if v not in self.variables: #it won't be in self.variables either the first time self.changed_variables[v] = log_variables[v] self.variables[v] = None try: # Step: Check if any Device is OFFLINE self.cur.execute("SELECT id FROM " + db_table_active_alert + " WHERE event_trigger_id=%s", ('5',)) if self.cur.rowcount != 0: self.device_offline_detection() # Update Scan Time _time_stamp_last_scanned = str(datetime.datetime.now()) self.cur.execute("UPDATE "+db_table_rtu+" SET last_scanned_time=%s " "WHERE rtu_id=%s", (_time_stamp_last_scanned, agent_id)) self.con.commit() except Exception as er: print er if len(self.changed_variables) == 0: print 'nothing changed' return topic = '/agent/ui/'+device_type+'/device_status_response/'+_topic_Agent_UI_tail headers = { 'AgentID': agent_id, headers_mod.CONTENT_TYPE: headers_mod.CONTENT_TYPE.JSON, headers_mod.FROM: agent_id, headers_mod.TO: 'ui' } _data = {'outside_temperature': self.get_variable('outside_temperature'), 'supply_temperature': self.get_variable('supply_temperature'), 'return_temperature': self.get_variable('return_temperature'), 'pressure': self.get_variable('pressure'), 'cooling_mode': self.get_variable('cooling_mode'), 'cooling_status': self.get_variable('cooling_status'), 'fan_status': self.get_variable('fan_status'), 'heating': self.get_variable('heating'), 'heat_setpoint': self.get_variable('heat_setpoint'), 'cool_setpoint': self.get_variable('cool_setpoint'), 'outside_damper_position': self.get_variable('outside_damper_position'), 'bypass_damper_position': self.get_variable('bypass_damper_position') } message = json.dumps(_data) message = message.encode(encoding='utf_8') self.publish(topic, headers, message) #step4: update PostgresQL (meta-data) database try: for k, v in agentAPImapping.items(): # check if column exists, then updateDB to corresponding column self.cur.execute("select column_name from information_schema.columns where table_name=%s and column_name=%s", (db_table_rtu, k,)) if bool(self.cur.rowcount): self.updateDB(db_table_rtu, k, db_id_column_name, self.get_variable(k), agent_id) else: pass #TODO check ip_address if self.ip_address != None: psycopg2.extras.register_inet() _ip_address = psycopg2.extras.Inet(self.ip_address) self.cur.execute("UPDATE "+db_table_rtu+" SET ip_address=%s WHERE rtu_id=%s", (_ip_address, agent_id)) self.con.commit() if self.get_variable('offline_count')>=3: self.cur.execute("UPDATE "+db_table_rtu+" SET network_status=%s WHERE rtu_id=%s", ('OFFLINE', agent_id)) self.con.commit() if self.already_offline is False: self.already_offline = True _time_stamp_last_offline = str(datetime.datetime.now()) self.cur.execute("UPDATE "+db_table_rtu+" SET last_offline_time=%s " "WHERE rtu_id=%s", (_time_stamp_last_offline, agent_id)) self.con.commit() else: self.already_offline = False self.cur.execute("UPDATE "+db_table_rtu+" SET network_status=%s WHERE rtu_id=%s", ('ONLINE', agent_id)) self.con.commit() print("{} updates database name {} during deviceMonitorBehavior successfully".format(agent_id, db_database)) except: print("ERROR: {} failed to update database name {}".format(agent_id, db_database)) #step5: update Cassandra (time-series) database try: #log data to cassandra cassandraDB.insert(agent_id,self.variables,log_variables) print('Data Pushed to cassandra') except Exception as er: print("ERROR: {} fails to update cassandra database".format(agent_id)) print er #step6: debug agent knowledge if debug_agent: print("printing agent's knowledge") for k,v in self.variables.items(): print (k,v) print('') if debug_agent: print("printing agentAPImapping's fields") for k, v in agentAPImapping.items(): if k is None: agentAPImapping.update({v: v}) agentAPImapping.pop(k) for k, v in agentAPImapping.items(): print (k, v)
def deviceMonitorBehavior(self): #step1: get current status of a thermostat, then map keywords and variables to agent knowledge try: Light.getDeviceStatus() except: print("device connection is not successful") self.changed_variables = dict() for v in log_variables: if v in Light.variables: if not v in self.variables or self.variables[v] != Light.variables[v]: self.variables[v] = Light.variables[v] self.changed_variables[v] = log_variables[v] else: if v not in self.variables: #it won't be in self.variables either (in the first time) self.changed_variables[v] = log_variables[v] self.variables[v] = None try: # Step: Check if any Device is OFFLINE self.cur.execute("SELECT id FROM " + db_table_active_alert + " WHERE event_trigger_id=%s", ('5',)) if self.cur.rowcount != 0: self.device_offline_detection() # Put scan time in database _time_stamp_last_scanned = datetime.datetime.now() self.cur.execute("UPDATE "+db_table_lighting+" SET last_scanned_time=%s " "WHERE lighting_id=%s", (_time_stamp_last_scanned, agent_id)) self.con.commit() except Exception as er: print er print("ERROR: {} failed to update last scanned time".format(agent_id)) if len(self.changed_variables) == 0: print 'nothing changed' return self.updateUI() #step4: update PostgresQL (meta-data) database try: self.cur.execute("UPDATE "+db_table_lighting+" SET status=%s WHERE lighting_id=%s", (self.get_variable('status'), agent_id)) self.con.commit() self.cur.execute("UPDATE "+db_table_lighting+" SET brightness=%s WHERE lighting_id=%s", (self.get_variable('brightness'), agent_id)) self.con.commit() self.cur.execute("UPDATE "+db_table_lighting+" SET color=%s WHERE lighting_id=%s", (self.get_variable('hexcolor'), agent_id)) self.con.commit() try: if self.get_variable('status') == "ON": multiple_on_off_status = "" for dummyvar in range(self.get_variable('number_lights')): multiple_on_off_status += "1" self.cur.execute("UPDATE "+db_table_lighting+" SET multiple_on_off=%s WHERE lighting_id=%s", (multiple_on_off_status, agent_id)) self.con.commit() else: # status is off multiple_on_off_status = "" for dummyvar in range(self.get_variable('number_lights')): multiple_on_off_status += "0" self.cur.execute("UPDATE "+db_table_lighting+" SET multiple_on_off=%s WHERE lighting_id=%s", (multiple_on_off_status, agent_id)) self.con.commit() except: print("{} this agent has no multiple_on_off_status".format(agent_id)) #TODO check ip_address if self.ip_address != None: psycopg2.extras.register_inet() _ip_address = psycopg2.extras.Inet(self.ip_address) self.cur.execute("UPDATE "+db_table_lighting+" SET ip_address=%s WHERE lighting_id=%s", (_ip_address, agent_id)) self.con.commit() if self.get_variable('offline_count') >= 3: self.cur.execute("UPDATE "+db_table_lighting+" SET network_status=%s WHERE lighting_id=%s", ('OFFLINE', agent_id)) self.con.commit() if self.already_offline is False: self.already_offline = True _time_stamp_last_offline = str(datetime.datetime.now()) self.cur.execute("UPDATE "+db_table_lighting+" SET last_offline_time=%s WHERE lighting_id=%s", (_time_stamp_last_offline, agent_id)) self.con.commit() else: self.already_offline = False self.cur.execute("UPDATE "+db_table_lighting+" SET network_status=%s WHERE lighting_id=%s", ('ONLINE', agent_id)) self.con.commit() print("{} updates database name {} during deviceMonitorBehavior successfully".format(agent_id, db_database)) except: print("ERROR: {} fails to update the database name {}".format(agent_id,db_database)) #step5: update Cassandra (time-series) database try: cassandraDB.insert(agent_id,self.variables,log_variables) print('Data Pushed to cassandra') print "{} success update".format(agent_id) except Exception as er: print("ERROR: {} fails to update cassandra database".format(agent_id)) print er #step6: debug agent knowledge if debug_agent: print("printing agent's knowledge") for k,v in self.variables.items(): print (k,v) print('') if debug_agent: print("printing agentAPImapping's fields") for k, v in agentAPImapping.items(): if k is None: agentAPImapping.update({v: v}) agentAPImapping.pop(k) for k, v in agentAPImapping.items(): print (k, v)
def deviceMonitorBehavior(self): print agent_id #step1: get current status of a thermostat, then map keywords and variables to agent knowledge try: APIObject.getDeviceStatus() except Exception as er: print er print("device connection is not successful") self.changed_variables = dict() for v in log_variables: if v in APIObject.variables: if not v in self.variables or self.variables[v] != APIObject.variables[v]: self.variables[v] = APIObject.variables[v] self.changed_variables[v] = log_variables[v] else: if v not in self.variables: #it won't be in self.variables either the first time self.variables[v] = None self.changed_variables[v] = log_variables[v] try: # Step: Check if any Device is OFFLINE self.cur.execute("SELECT id FROM " + db_table_active_alert + " WHERE event_trigger_id=%s", ('5',)) if self.cur.rowcount != 0: self.device_offline_detection() # Update Scan Time _time_stamp_last_scanned = str(datetime.datetime.now()) self.cur.execute("UPDATE "+db_table_vav+" SET last_scanned_time=%s " "WHERE vav_id=%s", (_time_stamp_last_scanned, agent_id)) self.con.commit() except Exception as er: print er if len(self.changed_variables) == 0: print 'nothing changed' return topic = '/agent/ui/'+device_type+'/device_status_response/'+_topic_Agent_UI_tail headers = { 'AgentID': agent_id, headers_mod.CONTENT_TYPE: headers_mod.CONTENT_TYPE.JSON, headers_mod.FROM: agent_id, headers_mod.TO: 'ui' } _data = {'temperature': self.get_variable('temperature'), 'supply_temperature': self.get_variable('supply_temperature'), 'heat_setpoint': self.get_variable('heat_setpoint'), 'cool_setpoint': self.get_variable('cool_setpoint'), 'flap_override': self.get_variable('flap_override'), 'flap_position': self.get_variable('flap_position') } message = json.dumps(_data) message = message.encode(encoding='utf_8') self.publish(topic, headers, message) #step4: update PostgresQL (meta-data) database try: self.cur.execute("UPDATE "+db_table_vav+" SET temperature=%s WHERE vav_id=%s", (self.get_variable('temperature'), agent_id)) self.con.commit() self.cur.execute("UPDATE "+db_table_vav+" SET supply_temperature=%s WHERE vav_id=%s", (self.get_variable('supply_temperature'), agent_id)) self.con.commit() self.cur.execute("UPDATE "+db_table_vav+" SET heat_setpoint=%s WHERE vav_id=%s", (self.get_variable('heat_setpoint'), agent_id)) self.con.commit() self.cur.execute("UPDATE "+db_table_vav+" SET cool_setpoint=%s WHERE vav_id=%s", (self.get_variable('cool_setpoint'), agent_id)) self.con.commit() self.cur.execute("UPDATE "+db_table_vav+" SET flap_override=%s WHERE vav_id=%s", (self.get_variable('flap_override'), agent_id)) self.con.commit() self.cur.execute("UPDATE "+db_table_vav+" SET flap_position=%s WHERE vav_id=%s", (self.get_variable('flap_position'), agent_id)) self.con.commit() #TODO check ip_address if self.ip_address != None: psycopg2.extras.register_inet() _ip_address = psycopg2.extras.Inet(self.ip_address) self.cur.execute("UPDATE "+db_table_vav+" SET ip_address=%s WHERE vav_id=%s", (_ip_address, agent_id)) self.con.commit() #TODO check nickname #TODO check zone_id #TODO check network_status #TODO check other_parameters #TODO last_scanned_time if self.get_variable('offline_count')>=3: self.cur.execute("UPDATE "+db_table_vav+" SET network_status=%s WHERE vav_id=%s", ('OFFLINE', agent_id)) self.con.commit() _time_stamp_last_offline = str(datetime.datetime.now()) self.cur.execute("UPDATE "+db_table_vav+" SET last_offline_time=%s " "WHERE vav_id=%s", (_time_stamp_last_offline, agent_id)) self.con.commit() else: self.cur.execute("UPDATE "+db_table_vav+" SET network_status=%s WHERE vav_id=%s", ('ONLINE', agent_id)) self.con.commit() print("{} updates database name {} during deviceMonitorBehavior successfully".format(agent_id, db_database)) except: print("ERROR: {} failed to update database name {}".format(agent_id, db_database)) #step5: update Cassandra (time-series) database try: #log data to cassandra cassandraDB.insert(agent_id,self.variables,log_variables) print('Data Pushed to cassandra') print "{} success update database".format(agent_id) except Exception as er: print("ERROR: {} fails to update cassandra database".format(agent_id)) print er #step6: debug agent knowledge if debug_agent: print("printing agent's knowledge") for k,v in self.variables.items(): print (k,v) print('') if debug_agent: print("printing agentAPImapping's fields") for k, v in agentAPImapping.items(): if k is None: agentAPImapping.update({v: v}) agentAPImapping.pop(k) for k, v in agentAPImapping.items(): print (k, v)