def __init__(self): super(Ui_MainWindow, self).__init__() self.ui = QMainWindow() self.setupUi(self.ui) self.initDatas() self.connect_fun() self.Tool = Util()
def add(self, layer, *args, **kwargs): # Init kwargs kwargs["apply_bias"] = kwargs.get("apply_bias", True) kwargs["position"] = kwargs.get("position", len(self._layers) + 1) self._w_stds.append(Util.get_and_pop(kwargs, "std", 0.1)) self._b_inits.append(Util.get_and_pop(kwargs, "init", 0.1)) if Util.get_and_pop(kwargs, "pop_last_init", False): self._w_stds.pop() self._b_inits.pop() if isinstance(layer, str): # noinspection PyTypeChecker self._add_layer(layer, *args, **kwargs) else: if not isinstance(layer, Layer): raise BuildLayerError( "Invalid Layer provided (should be subclass of Layer)") if not self._layers: if isinstance(layer, SubLayer): raise BuildLayerError( "Invalid Layer provided (first layer should not be subclass of SubLayer)" ) if len(layer.shape) != 2: raise BuildLayerError( "Invalid input Layer provided (shape should be {}, {} found)" .format(2, len(layer.shape))) self._layers, self._current_dimension = [layer], layer.shape[1] self._update_layer_information(layer) if isinstance(layer, ConvLayer): self._add_params(layer.shape, layer.n_channels) else: self._add_params(layer.shape) else: if len(layer.shape) > 2: raise BuildLayerError( "Invalid Layer provided (shape should be {}, {} found)" .format(2, len(layer.shape))) if len(layer.shape) == 2: _current, _next = layer.shape if isinstance(layer, SubLayer): if _next != self._current_dimension: raise BuildLayerError( "Invalid SubLayer provided (shape[1] should be {}, {} found)" .format(self._current_dimension, _next)) elif not isinstance( layer, ConvLayer) and _current != self._current_dimension: raise BuildLayerError( "Invalid Layer provided (shape[0] should be {}, {} found)" .format(self._current_dimension, _current)) self._add_layer(layer, _current, _next) elif len(layer.shape) == 1: _next = layer.shape[0] layer.shape = (self._current_dimension, _next) self._add_layer(layer, self._current_dimension, _next) else: raise LayerError( "Invalid Layer provided (invalid shape '{}' found)". format(layer.shape))
def get_Config_matrix(): app.logger.info("get_Config_matrix") config_data = config.energy_iot_DB_RW[TABLE.CONFIG_TABLE].find() Config_matrix = {} for d in config_data: key = d["device_type"] + "#" + d["sensor_type"] if Config_matrix.get(key, None) is None: Config_matrix[key] = [Util().toJson(d)] else: Config_matrix[key].append(Util().toJson(d)) #app.logger.info(Config_matrix) return Config_matrix
def __new__(mcs, *args, **kwargs): name, bases, attr = args[:3] timing = Timing() for _name, _value in attr.items(): if "__" in _name or "timing" in _name or "estimate" in _name: continue if Util.callable(_value): attr[_name] = timing.timeit(level=2)(_value) return type(name, bases, attr)
def __new__(mcs, *args, **kwargs): name, bases, attr = args[:3] timing = Timing() for _name, _value in attr.items(): if "__" in _name or "timing" in _name or "evaluate" in _name: continue if Util.callable(_value): attr[_name] = timing.timeit(level=2)(_value) return type(name, bases, attr)
def __new__(mcs, *args, **kwargs): name, bases, attr = args[:3] timing = Timing() for _name, _value in attr.items(): if "__" in _name or "timing" in _name or "evaluate" in _name: continue if Util.callable(_value): attr[_name] = timing.timeit(level=2)(_value) def show_timing_log(self, level=2): getattr(self, name + "Timing").show_timing_log(level) attr["show_timing_log"] = show_timing_log return tyep(name,bases, attr)
def __init__(self): print " Got here log _server constructor " ''' Description, Constructor method for the class, log_server. All attributes related to log_server will be initialized and caller will have to make sure that all required arguments are passed in during object initialization. ''' #super(log_server,self).__init__() #Util.__init__(self) self.UtilObj = Util() self.SocketObj = self.UtilObj.SocketObj
def load(self, path=None, pb="Frozen.pb"): if path is None: path = os.path.join("Models", "Cache") try: with open(os.path.join(path, "Model.nn"), "rb") as file: _dic = pickle.load(file) for key, value in _dic["structures"].items(): setattr(self, key, value) for name, param in zip(self._layer_names, self._layer_params): self.add(name, *param) for key, value in _dic["params"].items(): setattr(self, key, value) except Exception as err: raise BuildNetworkError("Failed to load Network ({}), structure initialized".format(err)) with open(os.path.join(path, "IO.txt"), "r") as file: self._entry = file.readline().strip()[9:] self._output = file.readline().strip()[9:] Util.load_frozen_graph(os.path.join(path, pb), True, self._entry, self._output) print() print("=" * 30) print("Model restored") print("=" * 30)
def __new__(mcs, *args, **kwargs): name, bases, attr = args[:3] timing = Timing() for _name, _value in attr.items(): if "__" in _name or "timing" in _name or "evaluate" in _name: continue if Util.callable(_value): attr[_name] = timing.timeit(level=2)(_value) def show_timing_log(self, level=2): getattr(self, name + "Timing").show_timing_log(level) attr["show_timing_log"] = show_timing_log return type(name, bases, attr)
def __init__(self): ''' Description Constructor method which will be called during the creation of the object. Will take in all possible class attributes for performing object specific intialization. Method Arguments, TBD Returns, Returns None. ''' self.UtilObj = Util() self.SocketObj = self.UtilObj.SocketObj
def __init__(self): self.UtilObj = Util() self.SocketObj = self.UtilObj.SocketObj
def __init__(self, h_params: HParams): self.h_params = h_params self.util = Util(h_params) self.data_log = dict() self.preprocessed_folder_name = "Preprocessed"
#사용자 정의 클래스 from Util.Util import Util from Layer.MultiClassNetWork import * #외부 프레임워크 import tensorflow as tf from sklearn.model_selection import train_test_split from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, SimpleRNN, LSTM from tensorflow.keras.preprocessing import sequence #TensorFlow model 이용, CNN from tensorflow.keras import layers from tensorflow.keras.layers import Dense import sys import matplotlib.pyplot as plt util = Util() df = util.get_file( "C:/Users/sunng/PycharmProjects/South_Sea_SST_ML/Raw_data.csv") #print(df) #print(df.keys()) ########################################################################### #Index(['YEAR', 'MONTH', 'KS_SST', 'EQ_SOI', 'UWND', 'SouthSea', # 'PAC_IND_SST_DIFF', 'EAST_PACIFIC', 'INDIA', 'KS_Anomaly', 'SouthSEA', # 'EastSea'], # dtype='object') #YEAR 연도 #MONTH 월
def __init__(self): self.UtilObj = Util()
class Master(Util): def __init__(self): self.UtilObj = Util() def InitializeMaster(self): while (True): #self.clone_IMT_Repo() t = time.time() print('starting time is:', t) self.SlaveType() self.SlaveOps() time.sleep(5) self.collectSlaveInfo() print('Ending time is:', time.time()) print('Difference is:', time.time() - t) print("ipmac_fail was:", ipmac_fail) print( "-----------------------Program Ended----------------------..........................Thank You..........................." ) self.UtilObj.WaitForXMinutes(1) def clone_IMT_Repo(self): print("Cloning IMT Repo locally ...") os.system("git clone git@gitlab-chennai:IMT/IMT.git /opt/IMT") def SlaveType(self): logger.info( "Finding Slave type <Windows/Linux/VMware> IP's using nmap") print("Finding Slave type <Windows/Linux/VMware> IP's using nmap") Run_nmap = Find_IP_Nmap.nmap_fun() ipmac_fail.extend(Find_IP_Nmap.ipmac_fail) ipmac_work_win.extend(Find_IP_Nmap.ipmac_work_win) ipmac_work_livm.extend(Find_IP_Nmap.ipmac_work_livm) #os_list = ['Windows','windows','Linux','linux','VMware','Host seems down'] #for mac in ipmac_all: # p = subprocess.Popen(['nmap', '-F','-O','--osscan-guess',mac],stdout=subprocess.PIPE,stderr=subprocess.PIPE) # out_data = str( p.communicate() ) # out_type = out_data.split("TCP/IP fingerprint:")[0] #slave_type = "Not Working or Powered Off" #for os_type in os_list: # if(re.search(os_type, out_type)): # slave_type = os_type #logger.info("%s Slave OS is %s "%(mac,slave_type)) #print ("%s Slave OS is %s "%(mac,slave_type)) #if (slave_type != "Not Working or Powered Off"): # if (slave_type != 'Host seems down'): # if ((slave_type == "Windows") or (slave_type == "windows")): # ipmac_work_win.append(mac) # else: # ipmac_work_livm.append(mac) #else: # ipmac_fail.append(mac) #else: # ipmac_fail.append(mac) def SlaveOps(self): #print ("slaveOps ip's are:",ipmac_work_livm) print("List before remove ip:", ipmac_work_livm) for i in ipmac_work_livm: if i in exception_li: ipmac_work_livm.remove(i) print("List after remove ip:", ipmac_work_livm) for mac in ipmac_work_livm: try: logger.info("Probing started for the slave %s" % mac) print("Probing started for the slave %s" % mac) check = self.find_os_start_slave(mac) print("check is %s" % check) if (check == "invalid"): ipmac_work_livm.remove(mac) else: print("Login Completed with %s" % check) ipmac_dic[mac] = check #return check #self.collectSlaveInfo(user) except: logger.info( "Exception Occurred during file_copy/search/process_running/firewall_disable : Exiting from the current Execution of %s" % mac) print( "Exception Occurred during file_copy/search/process_running/firewall_disable : Exiting from the current Execution of %s" % mac) print(ipmac_dic) def find_os_start_slave(self, slave_ip): FabricObj = Fabric() logonuser = "" logonuser = FabricObj.connect_host(slave_ip) #logger.info("Connected to the host %s with %s"%slave_ip,logonuser) print("Connected to the host %s with %s" % (slave_ip, logonuser)) if (logonuser != "invalid"): unix_variant = self.getLinuxVariant(FabricObj) logger.info("Check Whether Slave agent is running or not") print("Check Whether Slave agent is running or not") status = self.IsSlaveRunning(FabricObj, unix_variant) else: print("Slave Agent can't be Started : Invalid User") if (status): logger.info( "Slave Agent is already Running on the Remote Host %s" % slave_ip) print("Slave Agent is already Running on the Remote Host %s" % slave_ip) else: logger.info("Slave Agent is not Running on the Remote Host %s" % slave_ip) print("Slave Agent is not Running on the remote Host %s" % slave_ip) if ((logonuser == "ubuntu") or (logonuser == "sesqa")): status = self.CheckIfSlaveAgentPresentUb(FabricObj, logonuser) if (status != True): logger.info("Pushing Slave Agent into the Machine") print("Pushing ubuntu Slave Agent into the Machine ") self.PushSlaveAgentUb(FabricObj, unix_variant, slave_ip, logonuser) logger.info( "Successfully Pushed Slave Agent into the Machine ") print("Successfully Pushed Slave Agent into the Machine") firewall_status = self.StopFirewall( FabricObj, slave_ip, logonuser) status = self.StartSlaveAgentUb(FabricObj, logonuser) else: status = self.CheckIfSlaveAgentPresent(FabricObj) if (status != True): logger.info("Pushing Slave Agent into the Machine") print("Pushing Slave Agent into the Machine") self.PushSlaveAgent(FabricObj, unix_variant, slave_ip) logger.info( "Successfully Pushed Slave Agent into the Machine") print("Successfully Pushed Slave Agent into the Machine") firewall_status = self.StopFirewall( FabricObj, slave_ip, logonuser) status = self.StartSlaveAgent(FabricObj) print("Status after Staring the Machine IP %s is %s" % (slave_ip, status)) if (status == True): logger.info("Started Slave Successfully in the Machine %s" % slave_ip) print("Started Slave Successfully in the Machine %s" % slave_ip) return (logonuser) def getLinuxVariant(self, FabricObj): os_variant = FabricObj.execute("uname -s", path='.', ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) if (os_variant != None): return os_variant def IsSlaveRunning(self, FabricObj, unix_variant): cmd_string = "" if (unix_variant == "VMkernel"): cmd_string = "ps -c | grep \"Slave.py\" | grep -v grep" else: cmd_string = "ps -eaf | grep \"Slave.py\" | grep -v grep" status = FabricObj.execute(cmd_string, path='.', ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) ret_status = re.search('Slave.py', status) if (ret_status != None): return True return False def CheckIfSlaveAgentPresent(self, FabricObj): status = FabricObj.execute("ls /opt | grep \"^IMT$\"", path='.', ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) if (re.search('IMT', status)): return True else: return False def CheckIfSlaveAgentPresentUb(self, FabricObj, user): status = FabricObj.execute("ls /home/%s | grep \"^IMT$\"" % (user), path='.', ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) if (re.search('IMT', status)): print "IMT slave agent was present" return True else: print "IMT slave agent was not present" return False def PushSlaveAgent(self, FabricObj, unix_variant, hostname): status = FabricObj.execute("mkdir /opt/IMT", ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) logger.info("Created directory /opt/IMT") print("Created directory /opt/IMT") status = FabricObj.execute( "mkdir /opt/IMT/IMT_Automation", ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, ) logger.info("Created directory /opt/IMT/IMT_Automation") print("Created directory /opt/IMT/IMT_Automation") status = FabricObj.execute( "mkdir /opt/IMT/IMT_Automation/Slave", ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, ) status = FabricObj.execute( "mkdir /opt/IMT/IMT_Automation/Util", ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, ) self.ftp_client = FileTransfer(hostname) self.ftp_client.push_slave() logger.info("Successfully Pushed Slave Agent into the Slave Host") print("Successfully Pushed Slave Agent into the Slave Host") return True def PushSlaveAgentUb(self, FabricObj, unix_variant, hostname, user): status = FabricObj.execute("mkdir /home/%s/IMT" % (user), ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) logger.info("Created directory /home/%s/IMT" % (user)) print("Created directory /home/%s/IMT" % (user)) status = FabricObj.execute( "mkdir /home/%s/IMT/IMT_Automation" % (user), ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, ) logger.info("Created directory /home/%s/IMT/IMT_Automation" % (user)) print("Created directory /home/%s/IMT/IMT_Automation" % (user)) status = FabricObj.execute( "mkdir /home/%s/IMT/IMT_Automation/Slave" % (user), ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, ) status = FabricObj.execute( "mkdir /home/%s/IMT/IMT_Automation/Util" % (user), ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, ) self.ftp_client = FileTransferUb(hostname, user) self.ftp_client.push_slave(user) logger.info("Successfully Pushed Slave Agent into the Slave Host") print("Successfully Pushed Slave Agent into the Slave Host") return True def StopFirewall(self, FabricObj, slave_ip, user): vm_check = FabricObj.execute("vmware -v", path='.', ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) vm_status = re.search('VMware', vm_check) if (vm_status == None): fire_status = FabricObj.execute("cat /etc/issue", path='.', ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) if (re.search('SUSE', fire_status)): fire_status = FabricObj.execute("rcSuSEfirewall2 stop", path='.', ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) logger.info("Firewall Disabled for this SUSE Machine") print("Firewall Disabled for this SUSE Machine") elif (re.search('Ubuntu', fire_status)): fire_status = FabricObj.firewal(slave_ip, user) logger.info("Firewall Disabled for this Ubuntu Machine") print("Firewall Disabled for this Ubuntu Machine") else: try: fire_status = FabricObj.execute("systemctl stop firewalld", path='.', ignore_err=False, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) except: #FabricObj.execute("cd") fire_status = FabricObj.execute("service iptables stop", path='.', ignore_err=True, hide_opt=None, out_stream=None, err_stream=None, disconnect=False) logger.info("Firewall Disabled for this RHEL/CentOs Machine") print("Firewall Disabled for this RHEL/CentOs Machine") else: FabricObj.execute("esxcli network firewall unload") logger.info("Firewall Disabled for this VMware Machine") print("Firewall Disabled for this VMware Machine") def StartSlaveAgent(self, FabricObj): logger.info("Executing Slave Agent") print("Executing slave agent") status = FabricObj.execute( "(nohup python Slave.py > /dev/null < /dev/null &)&", path='/opt/IMT/IMT_Automation/Slave/', ignore_err=True, hide_opt=1, out_stream=None, err_stream=None, disconnect=True) logger.info("Started Slave Agent") print("Started Slave Agent") return status def StartSlaveAgentUb(self, FabricObj, user): logger.info("Executing Slave Agent") print("Executing Slave Agent") status = FabricObj.execute( "(nohup python Slave.py > /dev/null < /dev/null &)&", path='/home/%s/IMT/IMT_Automation/Slave/' % (user), ignore_err=True, hide_opt=1, out_stream=None, err_stream=None, disconnect=True) logger.info("Started Slave Agent") print("Started Slave Agent") return status def collectSlaveInfo(self): port_num = PORT_NUM logger.info("collectSlaveInfo :: Function Started") print("collectSlaveInfo :: Function Started") print("Have got these many Machines %s" % ipmac_work_livm) try: #for mac in ipmac_work_livm: for mac in ipmac_dic: try: logger.info( "Monitor Log Server Started for Master-Slave Communication %s and user is %s" % (mac, ipmac_dic[mac])) print( "Monitor Log Server Started for Master-Slave Communication %s and user is %s" % (mac, ipmac_dic[mac])) self.handle_slave(port_num, mac, ipmac_dic[mac]) except: logger.info( "Exception Occurred during Master-Slave Communication : Exiting from the current Execution of %s" % mac) print( "Exception Occurred during Master-Slave Communication : Exiting from the current Execution of %s" % mac) port_num = port_num + 1 except: logger.info( "Exception Occurred during Consolidated Log File Generation : Exiting from the current Execution of %s" % mac) print( "Exception Occurred during Consolidated Log File Generation : Exiting from the current Execution of %s" % mac) self.consolidateLogGenerate() port_num = PORT_NUM def handle_slave(self, port_num, slave_ip, user): logger.info("Creating socket to connect to the Slave %s" % slave_ip) print("Creating socket to connect to the Slave %s" % slave_ip) local_socket = self.UtilObj.SocketObj.CreateSocket(port_num, whoami="master") logger.info("Created local socket on the port number,%s" % port_num) print("Created local socket on the port number %s" % port_num) if (local_socket == None): logger.info("Socket Creation Failure") print("Socket Creation Failure") return -1 else: logger.info("Socket Creation Successful") print("Socket Creation Successful") logger.info( "About to send socket message : prepare_%s to the Slave %s" % (user, slave_ip)) print( "About to send socket message : \"prepare_%s\" to the Slave %s" % (user, slave_ip)) msg = "prepare_" + user print("message is:", msg) local_socket.connect((slave_ip, 40197)) logger.info("Connected to the Slave Socket %s" % slave_ip) print("Connected to the Slave Socket %s" % slave_ip) no_of_bytes = local_socket.send(msg) print("Have had sent " + str(no_of_bytes) + " bytes to the Master") while (True): logger.info("Waiting for Slave Response") print("Waiting for Slave Response") data = local_socket.recv(1024) logger.info("Received Message from Slave IP") print( "Received message from the Slave IP %s and the Message is : %s" % (slave_ip, data)) slave_msg = "windows_root_ubuntu" if (re.search(data, slave_msg)): local_socket.send("done") logger.info("Breaking Communication from Slave %s" % slave_ip) print("Breaking Communication from Slave %s" % slave_ip) break elif not data: local_socket.send("prepare_" + user) continue else: reply = "prepare_" + user continue if (re.search(data, "ubuntu")): self.ftp_client = FileTransferUb(slave_ip, user) status = self.ftp_client.get_log(slave_ip, user) else: self.ftp_client = FileTransfer(slave_ip) status = self.ftp_client.get_log(slave_ip) self.UtilObj.SocketObj.DestroySocket(local_socket) if (status): logger.info( "____________________------------------------Slave Log File Download Completed-------------------------____________________" ) print( "____________________------------------------Slave Log File Download Completed-------------------------____________________" ) return 0 else: logger.info( "____________________------------------------Slave Log File Download Failed-------------------------____________________" ) print( "____________________------------------------Slave Log File Download Failed-------------------------____________________" ) return -1 def consolidateLogGenerate(self): path = "/opt/repo" list_dir = [] list_dir = os.listdir(path) header1 = "" header = [ "IP", "No_of_PCI_Devices", "NICs", "Ram_size", "OS_Version", "Processor_no_of_cores", "Remarks" ] for i in header: header1 = header1 + "," + i header1 = header1[1:] filename = "Consolidated_Log_File_Report" logfile = '%s-%s.csv' % ( filename, datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")) path1 = "/opt/" f2 = open("%s%s" % (path1, logfile), "w+") f2.write(header1) f2.write("\n") for file in list_dir: print "name of the file is ", file if file.endswith(".csv"): print "opened the file", file f = open(path + "/" + file) lines = f.readlines() f2.write(lines[1]) f.close() #os.system("rm -rf %s/%s"%(path,file)) #os.system("rm -rf %s/*"%("/opt/repo")) os.system("rm -rf /opt/repo/*") time.sleep(2) #print ("Not Working IP Mac's are : "+str(ipmac_fail)) for mac in ipmac_fail: footer1 = "" footer = [ mac, "N/A", "N/A", "N/A", "N/A", "N/A", "Unable to login with credentials" ] for j in footer: footer1 = footer1 + "," + j footer1 = footer1[1:] f2.write(footer1) f2.write("\n") logger.info( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Successfully Generated the Consolidated Slave Log Report!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) print( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Successfully Generated the Consolidated Slave Log Report!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" ) f2.close()
#사용자 정의 클래스 from Util.Util import Util from Layer.MultiClassNetWork import * #외부 프레임워크 import tensorflow as tf from sklearn.model_selection import train_test_split import numpy as np import matplotlib.pyplot as plt import sys util = Util() df = util.get_file(Path+"/Raw_data.csv") #print(df) #print(df.keys()) ########################################################################### #Index(['YEAR', 'MONTH', 'KS_SST', 'EQ_SOI', 'UWND', 'SouthSea', # 'PAC_IND_SST_DIFF', 'EAST_PACIFIC', 'INDIA', 'KS_Anomaly', 'SouthSEA', # 'EastSea'], # dtype='object') #YEAR 연도 #MONTH 월 # KS_SST 대한해협 수온 # EQ_SOI 인도양과 동태평양의 기압차이 # UWND 동서방향 바람의 세기
class DeviceControl(Ui_MainWindow): def __init__(self): super(Ui_MainWindow, self).__init__() self.ui = QMainWindow() self.setupUi(self.ui) self.initDatas() self.connect_fun() self.Tool = Util() # widge.show() def initDatas(self): self._serial = serial.Serial() self.is_find_comm = True self.is_port_open = False self.is_debug = False self.is_debug_feedback = True self.is_send_debug_data = False self.debug_send_time = time.time() self.debug_recive_data = "" self.debug_send_data = "" self.debug_buffer = None self.current_address = [] self.current_device = "" self.address_num = "" self.is_send_hex = True self.is_recive_hex = True self.radbtnRcvHex.setChecked(True) self.radbtnSenHex.setChecked(True) self.recstr = '' # 串口接收字符串 self.rec_data_cnt = 0 # 数据接收计数 self.send_data_cnt = 0 # 数据发送是计数 self.timer = QtCore.QTimer() self.comms_add_data() # self.is_cancel_debug = False def comms_add_data(self): self.comSerialPort.addItems(self.set_port_list()) self.comBaudRate.addItems(Config.BAUDRATES) self.comCheckBit.addItems(Config.SERIAL_CHECKBIT_ARRAY) self.comDataBit.addItems(Config.SERIAL_DATABIT_ARRAY) self.comStopBit.addItems(Config.SERIAL_STOPBIT_ARRAY) self.comChooseDevice.addItems(Command.DEVICES) self.comBaudRate.setCurrentText("9600") def set_port_list(self): comms = [] port_list = list(serial.tools.list_ports.comports()) for port in port_list: comms.append(port[0]) if not comms: self.is_find_comm = False comms = ['none'] return comms def connect_fun(self): self.btnOpenSerialPort.clicked.connect(self.open_port) self.btnSend.clicked.connect(self.open_send) self.btnInputAdr.clicked.connect(self.click_input_adr) self.checkBoxDebug.clicked.connect(self.click_debug_device) self.radbtnRcvHex.clicked.connect(self.click_recive_hex) self.radbtnRcvASCII.clicked.connect(self.click_recive_str) self.timer.timeout.connect(self.recive_data) def open_port(self): if not self.is_port_open: if not self.is_find_comm: self.textBrowser.setText("串口没插入或重启软件!") return com_num = self.comSerialPort.currentText() baud_rate = self.comBaudRate.currentText() data_bit = Config.SERIAL_DATABIT_ARRAY[ self.comDataBit.currentIndex()] stop_bit = Config.SERIAL_STOPBIT_ARRAY[ self.comStopBit.currentIndex()] check_bit = Config.SERIAL_CHECKBIT_ARRAY[ self.comCheckBit.currentIndex()] try: self._serial = serial.Serial() self._serial.port = com_num self._serial.baudrate = int(baud_rate) self._serial.stopbits = int(stop_bit) self._serial.bytesize = float(data_bit) self._serial.parity = check_bit self._serial.open() except (OSError, serial.SerialException): self._serial.close() self.timer.stop() self.is_port_open = False self.show_warning("端口警告!", "端口被占用或者不存在") if self._serial.is_open: self.open_comm_status() print("open!") else: self.close_comm_status() self._serial.close() def open_comm_status(self): self.comCheckBit.setEnabled(False) self.comSerialPort.setEnabled(False) self.comBaudRate.setEnabled(False) self.comStopBit.setEnabled(False) self.comDataBit.setEnabled(False) self.is_port_open = True self.btnOpenSerialPort.setText("关闭") self.timer.start(20) if self.is_debug or self.checkBoxDebug.isChecked(): self.is_debug = True self.radbtnRcvHex.setChecked(True) self.radbtnSenASCII.setChecked(True) self.radbtnRcvASCII.setEnabled(False) self.radbtnSenHex.setEnabled(False) self.checkBoxRcvTime.setChecked(False) self.checkBoxRcvTime.setEnabled(False) self.checkBoxSenAT.setChecked(False) self.checkBoxSenAT.setEnabled(False) self.checkBoxSenTime.setChecked(False) self.checkBoxSenTime.setEnabled(False) self.textEdit.setEnabled(False) def close_comm_status(self): self.comCheckBit.setEnabled(True) self.comSerialPort.setEnabled(True) self.comBaudRate.setEnabled(True) self.comStopBit.setEnabled(True) self.comDataBit.setEnabled(True) self.is_port_open = False self.timer.stop() self.btnOpenSerialPort.setText("打开") if self.is_debug: self.is_debug = False self.radbtnRcvASCII.setEnabled(True) self.radbtnSenASCII.setEnabled(True) self.checkBoxRcvTime.setEnabled(True) self.checkBoxSenAT.setEnabled(True) self.radbtnSenHex.setEnabled(True) self.checkBoxSenTime.setEnabled(True) self.textEdit.setEnabled(True) def open_send(self): if self.is_port_open: if self.is_debug: print("debug modle") if self.debug_send_data.strip(): self.textBrowserDebug.append(self.debug_send_data) self.textEdit.setText("") send = threading.Thread(target=self.get_debug_data) send.start() else: self.textBrowser.setText("点击发送!") else: self.show_warning("提示", "当前串口没有打开") def recive_data(self): if self.cleanDatas.is_clean: self.textBrowser.clear() self.cleanDatas.is_clean = False if self.saveDatas.is_save: self.saveDatas.is_save = False try: self.SaveDatas() except: print("close") if self.is_port_open: try: bytesToRead = self._serial.inWaiting() except: print("read serial port wrong") time.sleep(1) return if bytesToRead > 0: self.recstr = self._serial.read(bytesToRead) if self.current_device and self.is_debug: origin = Command.CMD_DIR[self.current_device] len_origin = ((origin[4] & 0xFF) << 8) | (origin[5] & 0xFF) if self.debug_buffer: bytesToRead = bytesToRead + len(self.debug_buffer) if bytesToRead < 2 * len_origin + 5: self.debug_buffer = self.recstr print("mistake...") return # self.recdatacnt += bytesToRead if self.is_debug and self.debug_buffer: self.recstr = self.debug_buffer + self.recstr self.debug_buffer = None print(self.recstr) # self.recstr = self.debug_buffer print("window_refresh") self.window_refresh() else: if self.is_debug and bytesToRead == 0 and self.current_address and self.is_debug_feedback == False and self.is_send_debug_data: if time.time() - self.debug_send_time > 3: print("=========chaoshi=======") print(time.time()) print(time.time() - self.debug_send_time) print(self.current_address) self.is_debug_feedback = True self.is_send_debug_data = False self.address_num = self.current_address[0] self.debug_send_time = time.time() self.current_address.remove(self.current_address[0]) self.debug_recive_data = "【接收】" + self.current_device + " " + str( self.address_num) + "设置超时" self.textBrowserDebug.append(self.debug_recive_data) self.debug_recive_data = "" print("chao shi") def window_refresh(self): if self.is_recive_hex: print("refresh-text") print(self.recstr) print(self.Tool.hex_show(self.recstr)) array = self.Tool.hex_show(self.recstr) self.address_num = self.recstr[0] self.textBrowser.append(array) print("show text") try: if self.is_debug: crc16 = CRC16() array = self.Tool.byte_to_hexarray(self.recstr) origin = Command.CMD_DIR[self.current_device] print(origin) print(array) len_origin = ((origin[4] & 0xFF) << 8) | (origin[5] & 0xFF) print(len_origin) self.is_set_success = True if len(array) < 5: print("if1") self.is_set_success = False elif len(array) != len_origin * 2 + 5: print("if2") self.is_set_success = False elif (array[0] & 0xFF) != int(self.current_address[0]): print(array[0]) print(self.current_address[0]) print("if3") self.is_set_success = False elif array[1] != origin[1]: print("if4") self.is_set_success = False elif not crc16.check_crc16(array): print("if5") self.is_set_success = False if self.is_set_success: print("chenggong") self.debug_recive_data = "【接收】" + self.current_device + "-" + str( self.current_address[0]) + " 设置成功" self.textBrowserDebug.append(self.debug_recive_data) self.current_address.remove(self.current_address[0]) self.debug_recive_data = "" time.sleep(1) else: print("shibai--mei chao shi") if self.current_address and self.is_send_debug_data: print("in") print(self.current_address[0]) self.debug_recive_data = "【接收】" + self.current_device + str( self.current_address[0]) + " 设置失败" print(self.debug_recive_data) self.textBrowserDebug.append( self.debug_recive_data) print("chenggong--mei chao shi3") self.current_address.remove( self.current_address[0]) self.debug_recive_data = "" print("chenggong--mei chao shi5") time.sleep(1) self.is_send_debug_data = False self.is_debug_feedback = True self.recstr = None except: print("except") if self.textBrowser.toPlainText().__len__() > 100000: self.textBrowser.clear() else: try: print("no hex recive") print(self.recstr) print("sss") print(self.recstr.decode("utf-8")) print("bbb") self.textBrowser.append(self.recstr.decode("utf-8")) if self.textBrowser.toPlainText().__len__() > 200000: self.textBrowser.clear() except: print("ex") def SaveDatas(self): print('save-press') filename = QFileDialog.getSaveFileName( None, 'Save File', '.', "Text file(*.txt);;All file(*.*)") fname = open(filename[0], 'w') print('save-press1:' + str(filename)) try: fname.write(self.textBrowser.toPlainText()) except: print('exception press') fname.close() def click_recive_hex(self): self.is_recive_hex = True def click_recive_str(self): self.is_recive_hex = False def click_input_adr(self): if not self.is_port_open: self.show_warning("提示", "当前串口没有打开") return if not self.is_debug: if self.checkBoxDebug.isChecked(): self.click_debug_device() if self.is_debug: str = Tool.parse_address(self.textEditDeviceIp.text()) if str: print(str) self.current_address = str.split(",") self.current_device = self.comChooseDevice.currentText() print(self.current_device) print(self.current_address) self.debug_send_data = "【发送】设备类型:" + self.current_device + ",调试地址:" + str self.textEdit.setText("设备类型:" + self.current_device + ",调试地址:" + str) else: self.show_warning("提示", "请先选中'调试'选框") def click_debug_device(self): if self.is_port_open: if self.checkBoxDebug.isChecked(): self.is_debug = True self.radbtnRcvHex.setChecked(True) self.radbtnSenASCII.setChecked(True) self.radbtnRcvASCII.setEnabled(False) self.radbtnSenHex.setEnabled(False) self.checkBoxRcvTime.setChecked(False) self.checkBoxRcvTime.setEnabled(False) self.checkBoxSenAT.setChecked(False) self.checkBoxSenAT.setEnabled(False) self.checkBoxSenTime.setChecked(False) self.checkBoxSenTime.setEnabled(False) self.textEdit.setEnabled(False) else: self.is_debug = False self.radbtnRcvASCII.setEnabled(True) self.radbtnSenASCII.setEnabled(True) self.checkBoxRcvTime.setEnabled(True) self.checkBoxSenAT.setEnabled(True) self.radbtnSenHex.setEnabled(True) self.checkBoxSenTime.setEnabled(True) self.textEdit.setEnabled(True) def get_debug_data(self): print("get_debug_data") print(self.debug_send_data) self.debug_send_data = "" if not self.current_address: print("no address") print(self.current_address) return while self.current_address: if self.is_debug_feedback: try: print(self.current_address) data = Command.get_device_cmd( self.current_address[0], Command.CMD_DIR[self.current_device]) print("x2") print(data) self.is_send_debug_data = True self.is_debug_feedback = False self.send_data(bytes(data)) print("send success") except: print("except bu zhi chi") self.is_debug_feedback = False self.is_send_debug_data = True self.debug_send_time = time.time() # else: # def send_data(self, sdata): try: print("send data - write") if self.is_debug: self.debug_send_time = time.time() + 1 print(self.debug_send_time) self._serial.write(sdata) time.sleep(1) except: self.show_warning('出错', "写入出错") def parse_address(self, address): adr = address.strip() print("ddddddd33") print(adr) is_adr = True if not adr: is_adr = False QMessageBox.warning(None, '错误', "请输入地址", QMessageBox.Ok) else: res = adr.replace(" ", "").split(',') print(res) p1 = re.compile('^[0-9]*$') for num in res: number = p1.match(num) if not number: self.show_warning('错误', "非法地址参数:" + num) is_adr = False break if num and int(num) > 254: self.show_warning('错误', "非法地址参数:" + num + ",仅支持0-254") is_adr = False break if is_adr: while '' in res: res.remove('') strr = "" i = 0 print("ddddddddd3") res = list(set(res)) for m in res: if (i == 0): strr = strr + m i = 2 else: strr = strr + "," + m result = strr.strip() if result: return result else: # QMessageBox.warning(None, '错误', "请输入正确地址", QMessageBox.Ok) self.show_warning('错误', "请输入正确地址") def show_warning(self, title, message): QMessageBox.warning(None, title, message + "。 ", QMessageBox.Ok) # if __name__ == "__main__": # app = QApplication(sys.argv) # mainWindow = QMainWindow() # ui = CommControl(mainWindow) # # mainWindow.show() # sys.exit(app.exec_()) # pass