import os import glob import pandas as pd import shutil from pandas.io.formats import excel from time import sleep from threading import Lock from leahi_dialin import FP from leahi_dialin.common.fp_defs import * from leahi_dialin import DD from leahi_dialin.dd.modules.valves import DDValves, DDValveNames excel.ExcelFormatter.header_style = None write_mutex = Lock() DEFAULT_LOG_DIR = "/tmp/FP_logs/" def fp_combine_logs(logs_path = DEFAULT_LOG_DIR, output_path = "/home/denali/Desktop/"): date_time_obj = pd.Timestamp.strftime(pd.Timestamp.now(),'%Y_%m_%d_%H_%M') writer = pd.ExcelWriter('{}FP_Dianogistics_{}.xlsx'.format(output_path,date_time_obj, engine='openpyxl')) log_files = [f for f in glob.glob( logs_path + "*.log")] if len(log_files) != 0: for file in log_files: df = pd.read_csv(file) print("Adding {}".format(file)) df.to_excel( writer, header = False,index=False, sheet_name=os.path.split(file)[1], float_format = "%0.5f") writer.close() else: print("no files found.") def fp_log_setup(logs_path = DEFAULT_LOG_DIR): if os.path.exists(logs_path) and os.path.isdir(logs_path): shutil.rmtree(logs_path) os.mkdir(DEFAULT_LOG_DIR) def fp_float_controller(fp:FP, interval: float = 0.5): """ Method to handle the float controllers of the permeate tank. Checks every half second. Used in situations where the IOFP is not actively controlling the tank and water is flowing. Constraints: - Expected to be logged into the FP before call. :param fp: FP class :param interval: sleep time interval :return: none """ # only manage if we know that the pump is running while True: if fp.levels.p25_level == FPFloaterLevels.LEVEL_STATE_HIGH.value: if fp.valves.m4_valv != FPValveStates.VALVE_STATE_CLOSED.value: fp.valves.cmd_valve_override(FPValveNames.M4_VALV.value, FPValveStates.VALVE_STATE_CLOSED.value) if fp.levels.p25_level == FPFloaterLevels.LEVEL_STATE_LOW.value: if fp.valves.m4_valv != FPValveStates.VALVE_STATE_OPEN.value: fp.valves.cmd_valve_override(FPValveNames.M4_VALV.value, FPValveStates.VALVE_STATE_OPEN.value) sleep(interval) def fp_pressure_recorder(fp:FP, interval: float = 1.0, log_path : str = "{}FP_test_pressures.log".format(DEFAULT_LOG_DIR)): while True: write_mutex.acquire() with open(log_path, "a") as f: # collect data and log it press = "{}, ".format(FPPressureSensorNames.M1_PRES.name) + '{}'.format(fp.pressures.fp_pressures[FPPressureSensorNames.M1_PRES.name]) + \ ", {}, ".format(FPPressureSensorNames.M3_PRES.name) + '{}'.format(fp.pressures.fp_pressures[FPPressureSensorNames.M3_PRES.name]) + \ ", {}, ".format(FPPressureSensorNames.P8_PRES.name) + '{}'.format(fp.pressures.fp_pressures[FPPressureSensorNames.P8_PRES.name]) + \ ", {}, ".format(FPPressureSensorNames.P13_PRES.name) + '{}'.format(fp.pressures.fp_pressures[FPPressureSensorNames.P13_PRES.name]) + \ ", {}, ".format(FPPressureSensorNames.P17_PRES.name) + '{}'.format(fp.pressures.fp_pressures[FPPressureSensorNames.P17_PRES.name]) + \ ", {}, ".format(FPPressureSensorNames.X1_PRES.name) + '{}'.format(fp.pressures.fp_pressures[FPPressureSensorNames.X1_PRES.name]) + \ ", {}, ".format(FPPressureSensorNames.X2_PRES.name) + '{}'.format(fp.pressures.fp_pressures[FPPressureSensorNames.X2_PRES.name]) + \ ", {}, ".format(FPPressureSensorNames.X3_PRES.name) + '{}'.format(fp.pressures.fp_pressures[FPPressureSensorNames.X3_PRES.name]) + \ ", {} ,".format(FPPressureSensorNames.X4_PRES.name) + '{}'.format(fp.pressures.fp_pressures[FPPressureSensorNames.X4_PRES.name]) + \ ", {}\n".format(fp.pressures.fp_pressure_timestamp) f.write(press) f.close() write_mutex.release() sleep(interval) def fp_temperature_recorder(fp:FP, interval: float = 1.0, log_path : str = "{}FP_test_temperatures.log".format(DEFAULT_LOG_DIR)): while True: write_mutex.acquire() with open(log_path, "a") as f: # collect data and log it temp = "{}, ".format(FPTemperaturesNames.P10_TEMP.name) + \ '{},'.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.P10_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.P19_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.P19_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.M1_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.M1_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.M3_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.M3_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.P8_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.P8_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.P13_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.P13_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.X1_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.X1_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.X2_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.X2_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.X3_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.X3_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.X4_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.X4_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.P7_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.P7_TEMP.name]) + \ "{}, ".format(FPTemperaturesNames.P16_TEMP.name) + \ '{}, '.format(fp.temperatures.fp_temperatures[FPTemperaturesNames.P16_TEMP.name]) + \ ", {}\n".format(fp.temperatures.fp_temperature_sensors_timestamp) f.write(temp) f.close() write_mutex.release() sleep(interval) def fp_level_recorder(fp:FP, interval: float = 1.0, log_path : str = "{}FP_test_float.log".format(DEFAULT_LOG_DIR)): while True: write_mutex.acquire() with open(log_path, "a") as f: # collect data and log it level = "{}, ".format(FPFloaterLevelSensorNames.P25_LEVEL.name) + \ '{}'.format(fp.levels.fp_levels[FPFloaterLevelSensorNames.P25_LEVEL.name]) + \ ", {}\n".format(fp.levels.fp_levels_timestamp) f.write(level) f.close() write_mutex.release() sleep(interval) def fp_flow_recorder(fp:FP, interval: float = 1.0, log_path : str = "{}FP_test_flow.log".format(DEFAULT_LOG_DIR) ): while True: write_mutex.acquire() with open(log_path, "a") as f: # collect data and log it flows = "{}, ".format(FPFlowSensorNames.P7_FLOW.name) + '{}'.format(fp.flows.fp_flows[FPFlowSensorNames.P7_FLOW.name]["FLOW"]) + \ ", {}, ".format(FPFlowSensorNames.P16_FLOW.name) + '{}'.format(fp.flows.fp_flows[FPFlowSensorNames.P16_FLOW.name]['FLOW']) + \ ", {}\n".format(fp.flows.fp_flow_timestamp) f.write(flows) f.close() write_mutex.release() sleep(interval) def fp_conductivity_recorder(fp:FP, interval: float = 1.0, log_path : str = "{}FP_test_conductivity.log".format(DEFAULT_LOG_DIR) ): while True: write_mutex.acquire() with open(log_path, "a") as f: # collect data and log it cond = "{}, ".format(FPConductivitySensorsEnum.P9_COND.name) + '{}'.format(fp.conductivity.fp_conductivity['P9_COND']) + \ ", {}, ".format(FPConductivitySensorsEnum.P18_COND.name) + '{}'.format(fp.conductivity.fp_conductivity['P18_COND']) + \ ", {}\n".format(fp.conductivity.fp_conductivity_timestamp) f.write(cond) f.close() write_mutex.release() sleep(interval) def fp_valve_recorder(fp:FP, dd:DD, interval: float = 1.0, log_path : str = "{}FP_test_valve.log".format(DEFAULT_LOG_DIR) ): while True: write_mutex.acquire() with open(log_path, "a") as f: # collect data and log it ddvalve = dd.valves.d3_valv.get("state", None) list_of_states = fp.valves.get_valve_states() valves = "{}, ".format(FPValveNames.M4_VALV.name) + '{}'.format(list_of_states[0]) + \ ", {}, ".format(FPValveNames.P39_VALV.name) + '{}'.format(list_of_states[1]) + \ ", {}, ".format(FPValveNames.P6_VALV.name) + '{}'.format(list_of_states[2]) + \ ", {}, ".format(FPValveNames.P11_VALV.name) + '{}'.format(list_of_states[3]) + \ ", {}, ".format(FPValveNames.P33_VALV.name) + '{}'.format(list_of_states[4]) + \ ", {}, ".format(FPValveNames.P34_VALV.name) + '{}'.format(list_of_states[5]) + \ ", {}, ".format(FPValveNames.P37_VALV.name) + '{}'.format(list_of_states[6]) + \ ", {}, ".format(FPValveNames.M7_VALV.name) + '{}'.format(list_of_states[7]) + \ ", {}, ".format(FPValveNames.P20_VALV.name) + '{}'.format(list_of_states[8]) + \ ", {}, ".format(DDValveNames.D3_VALV.name) + '{}'.format(ddvalve) + \ ", {}\n".format(fp.valves.fp_valves_states_timestamp) f.write(valves) f.close() write_mutex.release() sleep(interval) def fp_ro_pump_recorder(fp:FP, interval: float = 1.0, log_path: str = "{}FP_test_ro_pump.log".format(DEFAULT_LOG_DIR) ): while True: write_mutex.acquire() with open(log_path, "a") as f: # collect data and log it ro = "{}, ".format(FPPumpAttributes.STATE.name) + '{}'.format(fp.ro_pump.ro_pump['P12_PUMP'][FPPumpAttributes.STATE.name]) + \ ", {}, ".format(FPPumpAttributes.DUTY_CYCLE.name) + '{}'.format(fp.ro_pump.ro_pump['P12_PUMP'][FPPumpAttributes.DUTY_CYCLE.name]) + \ ", {}, ".format(FPPumpAttributes.FB_DUTY_CYCLE.name) + '{}'.format(fp.ro_pump.ro_pump['P12_PUMP'][FPPumpAttributes.FB_DUTY_CYCLE.name]) + \ ", {}, ".format('Ctl_sig_out') + '{}'.format(fp.ro_pump.ro_pump['P12_PUMP'][FPPumpAttributes.SPEED.name]) + \ ", {}, ".format('Ctl_sig_err') + '{}'.format(fp.ro_pump.ro_pump['P12_PUMP'][FPPumpAttributes.TARGET_PRES.name]) + \ ", {}, ".format(FPPumpAttributes.TARGET_FLOW.name) + '{}'.format(fp.ro_pump.ro_pump['P12_PUMP'][FPPumpAttributes.TARGET_FLOW.name]) + \ ", {}, ".format('Kp') + '{}'.format(fp.ro_pump.ro_pump['P12_PUMP'][FPPumpAttributes.TARGET_DUTY_CYCLE.name]) + \ ", {}, ".format('Ki') + '{}'.format(fp.ro_pump.ro_pump['P12_PUMP'][FPPumpAttributes.DUTY_CYCLE_PCT.name]) + \ ", {}, ".format('errsum') + '{}'.format(fp.ro_pump.ro_pump['P12_PUMP'][FPPumpAttributes.FB_DUTY_CYCLE_PCT.name]) + \ ", {}\n".format(fp.ro_pump.ro_pump_timestamp) f.write(ro) f.close() write_mutex.release() sleep(interval) def fp_boost_pump_recorder(fp:FP, interval: float = 1.0, log_path : str = "{}FP_test_boost_pump.log".format(DEFAULT_LOG_DIR) ): while True: write_mutex.acquire() with open(log_path, "a") as f: # collect data and log it boost = "{}, ".format(FPPumpAttributes.STATE.name) + '{}'.format(fp.boost_pump.boost_pump['P40_PUMP'][FPPumpAttributes.STATE.name]) + \ ", {}, ".format(FPPumpAttributes.DUTY_CYCLE.name) + '{}'.format(fp.boost_pump.boost_pump['P40_PUMP'][FPPumpAttributes.DUTY_CYCLE.name]) + \ ", {}, ".format(FPPumpAttributes.FB_DUTY_CYCLE.name) + '{}'.format(fp.boost_pump.boost_pump['P40_PUMP'][FPPumpAttributes.FB_DUTY_CYCLE.name]) + \ ", {}, ".format(FPPumpAttributes.SPEED.name) + '{}'.format(fp.boost_pump.boost_pump['P40_PUMP'][FPPumpAttributes.SPEED.name]) + \ ", {}, ".format(FPPumpAttributes.TARGET_PRES.name) + '{}'.format(fp.boost_pump.boost_pump['P40_PUMP'][FPPumpAttributes.TARGET_PRES.name]) + \ ", {}, ".format(FPPumpAttributes.TARGET_FLOW.name) + '{}'.format(fp.boost_pump.boost_pump['P40_PUMP'][FPPumpAttributes.TARGET_FLOW.name]) + \ ", {}, ".format(FPPumpAttributes.TARGET_DUTY_CYCLE.name) + '{}'.format(fp.boost_pump.boost_pump['P40_PUMP'][FPPumpAttributes.TARGET_DUTY_CYCLE.name]) + \ ", {}, ".format(FPPumpAttributes.DUTY_CYCLE_PCT.name) + '{}'.format(fp.boost_pump.boost_pump['P40_PUMP'][FPPumpAttributes.DUTY_CYCLE_PCT.name]) + \ ", {}, ".format(FPPumpAttributes.FB_DUTY_CYCLE_PCT.name) + '{}'.format(fp.boost_pump.boost_pump['P40_PUMP'][FPPumpAttributes.FB_DUTY_CYCLE_PCT.name]) + \ ", {}\n".format(fp.boost_pump.boost_pump_timestamp) f.write(boost) f.close() write_mutex.release() sleep(interval) def fp_op_mode_recorder(fp:FP, interval: float = 1.0, log_path: str = "{}FP_test_op_mode.log".format(DEFAULT_LOG_DIR) ): while True: write_mutex.acquire() with open(log_path, "a") as f: # collect data and log it ro = "op_mode, " + '{}'.format(fp.fp_operation_mode) + \ ", sub_mode, " + '{}'.format(fp.fp_operation_sub_mode) + \ ", {}\n".format(fp.fp_op_mode_timestamp) f.write(ro) f.close() write_mutex.release() sleep(interval)