def doSomething(): # Write to log file and update Slack hostname = socket.gethostname() logMessage = "SIMULATOR (" + hostname + "): Creating statistics starting @ " + str(time.ctime()) log = Log() log.info(logMessage) notify.info(logMessage) # Gather environment variables AZURE_SB_SERVICE_NAMESPACE = os.getenv('AZURE_SB_SERVICE_NAMESPACE') AZURE_SB_SHARED_ACCESS_KEY_NAME = os.getenv('AZURE_SB_SHARED_ACCESS_KEY_NAME') AZURE_SB_SHARED_ACCESS_KEY = os.getenv('AZURE_SB_SHARED_ACCESS_KEY') # Start loop and write random messages to Azure Service Bus Queue x=0 while True: x=x+1 time.sleep(2) # Write message to SB Queue sb_service = ServiceBusService(service_namespace=AZURE_SB_SERVICE_NAMESPACE,shared_access_key_name=AZURE_SB_SHARED_ACCESS_KEY_NAME,shared_access_key_value=AZURE_SB_SHARED_ACCESS_KEY) msg_text = "Update number: " + str(x) msg = Message(msg_text.encode("utf-8")) # Randomly create data for SB Queue r = random.randint(1, 10) if r == 1: msg.custom_properties={'deviceid':'mydevice' + str(r),'temp':'40.8','pressure':'61.93','humidity':'20','windspeed':str(r) + '.5'} sb_service.send_queue_message('statistics', msg) elif r == 2: msg.custom_properties={'deviceid':'mydevice' + str(r),'temp':'50.8','pressure':'62.83','humidity':'30','windspeed':str(r) + '.5'} sb_service.send_queue_message('statistics', msg) elif r == 3: msg.custom_properties={'deviceid':'mydevice' + str(r),'temp':'60.8','pressure':'63.73','humidity':'40','windspeed':str(r) + '.5'} sb_service.send_queue_message('statistics', msg) elif r == 4: msg.custom_properties={'deviceid':'mydevice' + str(r),'temp':'70.8','pressure':'64.63','humidity':'50','windspeed':str(r) + '.5'} sb_service.send_queue_message('statistics', msg) elif r == 5: msg.custom_properties={'deviceid':'mydevice' + str(r),'temp':'80.8','pressure':'51.93','humidity':'60','windspeed':str(r) + '.5'} sb_service.send_queue_message('statistics', msg) elif r == 6: msg.custom_properties={'deviceid':'mydevice' + str(r),'temp':'90.8','pressure':'55.93','humidity':'70','windspeed':str(r) + '.5'} sb_service.send_queue_message('statistics', msg) elif r == 7: msg.custom_properties={'deviceid':'mydevice' + str(r),'temp':'45.2','pressure':'34.99','humidity':'45','windspeed':str(r) + '.5'} sb_service.send_queue_message('statistics', msg) elif r == 8: msg.custom_properties={'deviceid':'mydevice' + str(r),'temp':'55.2','pressure':'38.99','humidity':'55','windspeed':str(r) + '.5'} sb_service.send_queue_message('statistics', msg) elif r == 9: msg.custom_properties={'deviceid':'mydevice' + str(r),'temp':'65.2','pressure':'36.99','humidity':'65','windspeed':str(r) + '.5'} sb_service.send_queue_message('statistics', msg) else: msg.custom_properties={'deviceid':'mydevice' + str(r),'temp':'75.2','pressure':'71.77','humidity':'38','windspeed':str(r) + '.5'} sb_service.send_queue_message('statistics', msg) log.info("Message #" + str(x) + " posted.")
def printSummary(): table = SummaryTable(config.AZURE_STORAGE_ACCOUNT_NAME, config.AZURE_STORAGE_ACCOUNT_KEY, config.AZURE_STORAGE_SUMMARY_TABLE_NAME) queue_service = getQueueService() summary = "Queue Length is approximately: " + queue_service.getLength() + "\n\n" summary = summary + "Processed events:\n" summary = summary + "Errors: " + str(table.getCount("ERROR")) + "\n" summary = summary + "Warnings: " + str(table.getCount("WARNING")) + "\n" summary = summary + "Infos: " + str(table.getCount("INFO")) + "\n" summary = summary + "Debugs: " + str(table.getCount("DEBUG")) + "\n" summary = summary + "Others: " + str(table.getCount("OTHER")) + "\n" print(summary) notify.info(summary)
def updateSlack(): log = Log() table = SummaryTable(config.AZURE_STORAGE_ACCOUNT_NAME, config.AZURE_STORAGE_ACCOUNT_KEY, config.AZURE_STORAGE_SUMMARY_TABLE_NAME) queue_service = getQueueService() summary = "Processing Status [" + str(time.ctime()) + "]\n\n" summary = summary + "Queue Length is approximately: " + queue_service.getLength() + "\n" summary = summary + "Errors: " + str(table.getCount("ERROR")) + "\n" summary = summary + "Warnings: " + str(table.getCount("WARNING")) + "\n" summary = summary + "Infos: " + str(table.getCount("INFO")) + "\n" summary = summary + "Debugs: " + str(table.getCount("DEBUG")) + "\n" summary = summary + "Others: " + str(table.getCount("OTHER")) + "\n" print(summary) notify.info(summary)
def fullAnalysis(self): hostname = socket.gethostname() msg = hostname + ': Analyzing log event queue' notify.info(msg) while True: events = self.msgQueue.dequeue() if len(events) > 0: for event in events: self.log.info("Dequeued: " + event.message_text) try: self.processEvent(event) self.msgQueue.delete(event) self.log.info("Counted and deleted: " + event.message_text) except: e = sys.exc_info()[0] self.log.error("Could not process: " + event.message_text + " because %s" % e) traceback.print_exc(file=sys.stdout) else: break
def setV(wire, value): notify.debug('wire.setV setting '+str(wire)+' to '+str(value)) if wire in bus.keys(): if type(value) == type([]): V,S,out = bus[wire] if len(value) > S: notify.warn('wire.setV: '+str(len(value))+' bit value '+str(value)+'is to big and it will be truncaded to LSB ['+str(S)+' pin bus '+str(wire)+']') value = value[-S:] if value != V or GLOB.force_same_v_update: #extend MSB to value svalue = [0]*max(S-len(value),0) svalue.extend(value) #write out bus[wire] = svalue,S,out #add task for updating connected devices for dev in out: task.addTask('recalc', dev) else: notify.info('wire.setV: New value is the same, no update. To ommit, set "force_same_v_update = True"') else: notify.error('wire.setV: value ['+str(value)+' '+str(type(value))+'] is not a list [bus '+str(wire)+']') else: notify.error('wire.setV: Wrong wire name '+str(wire))
def updateSlack(): hostname = socket.gethostname() logMessage = "SLACKER (" + hostname + "): Checking queue size and posting to Slack starting @ " + str(time.ctime()) log = Log() log.info(logMessage) notify.info(logMessage) # Gather environment variables AZURE_SB_SERVICE_NAMESPACE = os.getenv('AZURE_SB_SERVICE_NAMESPACE') AZURE_SB_SHARED_ACCESS_KEY_NAME = os.getenv('AZURE_SB_SHARED_ACCESS_KEY_NAME') AZURE_SB_SHARED_ACCESS_KEY = os.getenv('AZURE_SB_SHARED_ACCESS_KEY') # Connect to Azure Service Bus sb_service = ServiceBusService(service_namespace=AZURE_SB_SERVICE_NAMESPACE,shared_access_key_name=AZURE_SB_SHARED_ACCESS_KEY_NAME,shared_access_key_value=AZURE_SB_SHARED_ACCESS_KEY) while True: # Delay for 6 seconds time.sleep(6) sbqueue = sb_service.get_queue('statistics') queuelength = sbqueue.message_count logMessage = "SLACKER: Queue length is approximately: " + str(queuelength) log.info(logMessage) notify.info(logMessage)
def addDevice(dname, dtype, inset, outset): clear = True typein,typeout = logic[dtype]['data'] if len(inset)!=typein: notify.error('devices.addDevice: Asked for '+str(len(inset))+' inputs in inset, when '+dtype+' has only '+str(typein)) clear = False if len(outset)!=typeout: notify.error('devices.addDevice: Asked for '+str(len(outset))+' inputs in outset, when '+dtype+' has only '+str(typeout)) clear = False if dname in devices.keys(): notify.error('devices.addDevice: Device '+dtype+' already exists') clear = False if clear: devices[dname] = (dtype, inset, outset) notify.info('Device '+dname+' of type '+dtype+' added') if GLOB.autoconnect: #connect mentioned input buses wires = set() for w,i in inset: wires.add(w) for w in wires: wire.connect(w, dname) else: notify.error('Can\'t add device '+dname+' of type '+dtype)
def simulate(): log = Log() hostname = socket.gethostname() msgQueue = Queue(account_name=config.AZURE_STORAGE_ACCOUNT_NAME, account_key=config.AZURE_STORAGE_ACCOUNT_KEY, queue_name=config.AZURE_STORAGE_QUEUE_NAME) if int(config.SIMULATION_ACTIONS) > 0: msg = hostname + ': Attempting to simulate ' + str( config.SIMULATION_ACTIONS) + ' actions' log.debug(msg) notify.info(msg) else: msg = hostname + ': Simulating until stopped' log.debug(msg) notify.info(msg) temp = 70 _actions = 0 while int(config.SIMULATION_ACTIONS) == 0 or int( config.SIMULATION_ACTIONS) - _actions > 0: change = random.randint(-1, 1) if temp <= _too_cold: change = 1 elif temp >= _too_hot: change = -1 msgQueue.enqueue("INFO - Change since last reading: " + str(change)) temp = temp + change msgQueue.enqueue("INFO - Current temperature: " + str(temp)) if temp == _just_right: msgQueue.enqueue("INFO - That's perfect") elif temp < _just_right and temp > _too_cold: msgQueue.enqueue('WARNING - Getting a little chilly') elif temp > _just_right and temp < _too_hot: msgQueue.enqueue('WARNING - Getting a touch warm') elif temp <= _too_cold: msgQueue.enqueue('ERROR - Too cold, how did this happen?') elif temp >= _too_hot: msgQueue.enqueue('ERROR - Too hot, how did this happen?') else: msgQueue.enqueue('ERROR - Can' 't tell if it' 's hot or cold') msgQueue.close() _actions = _actions + 1 time.sleep(int(config.SIMULATION_DELAY)) msg = hostname + ": Simulated " + str( config.SIMULATION_ACTIONS) + " actions and added them to the queue" log.debug(msg) notify.info(msg)
def simulate(): log = Log() hostname = socket.gethostname() msgQueue = Queue(account_name = config.AZURE_STORAGE_ACCOUNT_NAME, account_key=config.AZURE_STORAGE_ACCOUNT_KEY, queue_name=config.AZURE_STORAGE_QUEUE_NAME) if int(config.SIMULATION_ACTIONS) > 0: msg = hostname + ': Attempting to simulate ' + str(config.SIMULATION_ACTIONS) + ' actions' log.debug(msg) notify.info(msg) else: msg = hostname + ': Simulating until stopped' log.debug(msg) notify.info(msg) temp = 70; _actions = 0 while int(config.SIMULATION_ACTIONS) == 0 or int(config.SIMULATION_ACTIONS) - _actions > 0: change = random.randint(-1, 1) if temp <= _too_cold: change = 1 elif temp >= _too_hot: change = -1 msgQueue.enqueue("Change since last reading: " + str(change), level="INFO") temp = temp + change msgQueue.enqueue("Current temperature: " + str(temp), level="INFO") if temp == _just_right: msgQueue.enqueue("That's perfect", level="INFO") elif temp < _just_right and temp > _too_cold: msgQueue.enqueue('Getting a little chilly', level="WARNING") elif temp > _just_right and temp < _too_hot: msgQueue.enqueue('Getting a touch warm', level="WARNING") elif temp <= _too_cold: msgQueue.enqueue('Too cold, how did this happen?', level="ERROR") elif temp >= _too_hot: msgQueue.enqueue('Too hot, how did this happen?', level="ERROR") else: msgQueue.enqueue('Can''t tell if it''s hot or cold', level="ERROR") msgQueue.close() _actions = _actions + 1 time.sleep(int(config.SIMULATION_DELAY)) msg = hostname + ": Simulated " + str(config.SIMULATION_ACTIONS) + " actions and added them to the queue" log.debug(msg) notify.info(msg)