def deadhead_protection(): valve_pos = {} pump_setting = {} dictionary = {} for key in vars_eng.keys(): if "Pump" in key: pump_setting[key[5:]] = vars_eng[key] if "Valve" in key: valve_pos[key[6:]] = vars_eng[key] #print("PumpSetting: ", pump_setting,"ValveSetting: ", valve_pos) pump_out = {} #print(deadhead) for key in deadhead.keys(): key_temp = key[6:] pump_key = "Pump_" + key_temp #print(key) temp = deadhead[key].deadhead_control(valve_pos[key_temp], pump_setting[key_temp]) #print(temp) if temp != None: pump_out.update(temp) #print("PumpOut: ", pump_out) # pump_deadhead = deadhead.deadhead_func(valve_pos) if pump_out != {}: daq.write_channels(pump_out) ## Turns pump off if valve is Zero
def shed_pid(): #shed_label should be SHED2 or 3 depending which is active pid_output = {} for shed_label in shed_status.keys(): pid_output = shed_status[shed_label].pid_func( float(vars_eng[shed_status[shed_label].pid_control])) if hasattr(pid_output, 'keys'): #print(pid_output) daq.write_channels(pid_output)
def background_tasks(queue=Queue): print("Background thread started") t_now = datetime.now() t_next = t_now + timedelta(seconds=1) while True: while t_now < t_next: # runs at higher frequency (Event based execution using queue etc.) if not queue.empty(): # process queue task = queue.get() for key in task.keys(): for key2, value in task[key].items( ): #Required for deadhead. if state is "true" then deadhead protection will be ON if "Pump" in key2: key3 = "Valve" + key2[4:] deadhead[key3].state = value if key == "write_channels": print(task) daq.write_channels(task[key]) elif key == "update_shed_request": update_shed_request(task[key]) elif key == "limit_set_request": update_alarm_limit(task[key]) elif key == "PID_change_request": pid_onoff(task[key]) elif key == "SHED_setpoint_request": setpoint_change(task[key]) elif key == "update_timer_state": shed_status["SHED3"].timer_toggle() else: print("Background task error: The task does not exist") t_now = datetime.now() sleep(0.01) t_next = t_next + timedelta( seconds=1) # runs every 1 second (Slower tasks, reading daq etc) t_now = datetime.now() ##### Scheduled tasks for background ### try: try: read_daq() except: print("READ DAQ ERROR in read_daq() function") update_calculated_variables() #print("AFTER CALCULATED VARIABLES") alarm_monitor( ) # Alarm monitor needs to be before update_display_variables() for some reason vars_eng is updated in that function? #print("AFTER ALARM_MONITOR") shed_pid() #print("BEFORE DEADHEAD") deadhead_protection() update_display_variables() except: print("read_daq error. Possible connection issue. Try again")
def update_shed_request(request): # update shed request from webpage input for key, value in request.items(): key_fixed = key[:5] shed_status[key_fixed].change_request(value) shed_status[key_fixed].update_state() #Below required for Deadhead prevention print(shed_status[key_fixed].configs[shed_status[key_fixed].state]) for key2, value2 in shed_status[key_fixed].configs[ shed_status[key_fixed].state].items(): if "Pump" in key2: key3 = "Valve" + key2[4:] deadhead[key3].state = value daq.write_channels(shed_status[key_fixed].new_state_output()) if shed_status["SHED1"].state == shed_status["SHED2"].state == shed_status[ "SHED3"].state == "off": daq.write_channels(all_off)
def background_tasks(queue=Queue): print("Background thread started") t_now = datetime.now() t_next = t_now + timedelta(seconds=1) while True: while t_now < t_next: # runs at higher frequency (Event based execution using queue etc.) if not queue.empty(): # process queue task = queue.get() for key in task.keys(): if key == "write_channels": daq.write_channels(task[key]) t_now = datetime.now() sleep(0.01) t_next = t_next + timedelta( seconds=1) # runs every 1 second (Slower tasks, reading daq etc) t_now = datetime.now() read_daq()
def background_tasks(queue=Queue): print("Background thread started") t_now = datetime.now() t_next = t_now + timedelta(seconds=1) while True: while t_now < t_next: # runs at higher frequency (Event based execution using queue etc.) if not queue.empty(): # process queue task = queue.get() for key in task.keys(): # add if key == "system" if key == "write_channels": if key in daq_channels: daq.write_channels(task[key]) elif "SHED" in key: update_system_variable(task[key]) t_now = datetime.now() sleep(0.01) t_next = t_next + timedelta( seconds=1) # runs every 1 second (Slower tasks, reading daq etc) t_now = datetime.now() read_daq() update_calculated_variables() update_remote_request()
def update_system_variable(task): for key in task.key(): if vars_sys[key][ "request"] == "off" and vars_sys[key]["state"] != "alarm": vars_sys[key]["request"] = "on" vars_sys[key]["state"] = "on" daq.write_channels(vars_sys[key]["state_settings"]["on"]) #print(vars_sys) else: vars_sys[key]["request"] = "off" daq.write_channels(vars_sys[key]["state_settings"]["off"]) if vars_sys["SHED1"]["request"] == vars_sys["SHED2"][ "request"] == vars_sys["SHED3"]["request"]: daq.write_channels(vars_sys["all_off"])
def update_display_variables(): vars_disp = vars_eng for key in vars_eng.keys(): if "Flowmeter" in key: vars_disp[key] = str(round(float(vars_eng[key]), 2)) # + " GPM" if "T_" in key: vars_disp[key] = str(round(float(vars_eng[key]), 1)) # + ' ' + u'\N{DEGREE SIGN}' + "C" #--------------------- Initialize background thread -------------------------------------------------------------------- daq.write_channels(all_off) queue = Queue() background = Thread(target=background_tasks, args=(queue, )) background2 = Thread(target=dataHandler) background.daemon = True background2.daemon = True #-------------------- Start flask app on wsgi server ------------------------------------------------------------------- if __name__ == '__main__': if flowmeter_serial == True: flow_rate_serial = flowRateSerial() #socketio.run(app, host='0.0.0.0') # used for eventlet with socketio background.start() background2.start() #socketio.run(app, host='0.0.0.0', use_reloader=False) #This requires chosing the correct http or https version of socket io in the .js.