from socket import * import threading import time import app import main import datetime import user_socket import sql import pythoncom class work_server(threading.Thread): def __init__(self, threadID, name, counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter self.user_var = app.user_var self.opc_value = app.opc_value self.sqlclient = sql.sqlclient self.exit_stat = False self.tcpserverParam = user_socket.tcpserverParam self.plc_conn_timeout = 0 self.order_change_timeout = 0 def air_data_split(self, value): air_value = value.split('\t') idx = 0 for air in air_value: if air == '(OK)' or air == '(TD)': break else: idx = idx + 1 if idx >= 1 and len(air_value) - idx >= 5: return '<{0}>:{4} {5}:{1}:{2} {3}'.format(air_value[idx-1], air_value[idx], air_value[idx + 1], air_value[idx + 2], air_value[idx + 3], air_value[idx + 4]) else: return '' def get_ateq_product_name(self, value): air_value = value.split('\t') return air_value[0] def opc_data_pro(self): value = self.opc_value.get_Bee() self.user_var.bee_stat = value value = self.opc_value.get_Number_type() self.user_var.number_type = '{0}{1}{2}'.format(chr(value[0]), chr(value[1]), chr(value[2])) value = self.opc_value.get_Batch_y() value = self.opc_value.get_Batch_m() value = self.opc_value.get_Batch_d() value = self.opc_value.get_Air_data_17700() self.user_var.product_17700_name = self.get_ateq_product_name(value) air_value = self.air_data_split(value) if air_value != '': self.user_var.airtightness_17700 = air_value self.user_var.airtightness_17700_str.set(self.user_var.airtightness_17700) value = self.opc_value.get_Air_data_17720() self.user_var.product_17720_name = self.get_ateq_product_name(value) air_value = self.air_data_split(value) if air_value != '': self.user_var.airtightness_17720 = air_value self.user_var.airtightness_17720_str.set(self.user_var.airtightness_17720) value = self.opc_value.get_Ok_count() self.user_var.order_ok_str.set(value) value = self.opc_value.get_Ng_count() self.user_var.order_ng_str.set(value) value = self.opc_value.get_Targe_count() self.user_var.order_targe_str.set(value) value = self.opc_value.get_SNP_count() self.user_var.order_snp_str.set(value) value = self.opc_value.get_Origin_ok() self.user_var.originchkok_stat = value value = self.opc_value.get_Origin_dir() self.user_var.origindir_stat = value value = self.opc_value.get_Upairtight_ok() self.user_var.upairtightok_stat = value value = self.opc_value.get_Upairtight_ng() self.user_var.upairtightng_stat = value value = self.opc_value.get_Downairtight_ok() self.user_var.downairtightok_stat = value value = self.opc_value.get_Downairtight_ng() self.user_var.downairtightng_stat = value value = self.opc_value.get_Upprint_ok() self.user_var.upprintok_stat = value value = self.opc_value.get_Downprint_ok() self.user_var.downprintok_stat = value value = self.opc_value.get_Upgage_s() self.user_var.upgages_stat = value value = self.opc_value.get_Downgage_s() self.user_var.downgages_stat = value value = self.opc_value.get_Upwork_s() self.user_var.upworks_stat = value value = self.opc_value.get_Downwork_s() self.user_var.downworks_stat = value value = self.opc_value.get_Upm_pen() self.user_var.upmpen_stat = value value = self.opc_value.get_Downm_pen() self.user_var.downmpen_stat = value value = self.opc_value.get_Upconnect() self.user_var.upconnect_stat = value value = self.opc_value.get_Downconnect() self.user_var.downconnect_stat = value value = self.opc_value.get_Motororgin_ok() self.user_var.motororiginok_stat = value value = self.opc_value.get_Motor_onup() self.user_var.motoronup_stat = value value = self.opc_value.get_Motor_ondown() self.user_var.motorondown_stat = value value = self.opc_value.get_Manual_print() value = self.opc_value.get_Manual_printup() value = self.opc_value.get_Manual_printdown() value = self.opc_value.get_Manual_printok() # stat = self.opc_value.get_PLC_ok() def check_opc_stat(self): self.opc_value.check_Bee() self.opc_value.check_Product_idx() self.opc_value.check_Product_name() self.opc_value.check_Batch_y() self.opc_value.check_Batch_m() self.opc_value.check_Batch_d() self.opc_value.check_Air_data_17700() self.opc_value.check_Air_data_17720() self.opc_value.check_Ok_count() self.opc_value.check_Ng_count() self.opc_value.check_Targe_count() self.opc_value.check_SNP_count() self.opc_value.check_Origin_ok() self.opc_value.check_Origin_dir() self.opc_value.check_Upairtight_ok() self.opc_value.check_Upairtight_ng() self.opc_value.check_Downairtight_ok() self.opc_value.check_Downairtight_ng() self.opc_value.check_Upprint_ok() self.opc_value.check_Downprint_ok() self.opc_value.check_Upgage_s() self.opc_value.check_Downgage_s() self.opc_value.check_Upwork_s() self.opc_value.check_Downwork_s() self.opc_value.check_Upm_pen() self.opc_value.check_Downm_pen() self.opc_value.check_Upconnect() self.opc_value.check_Downconnect() self.opc_value.check_Motororgin_ok() self.opc_value.check_Motor_onup() self.opc_value.check_Motor_ondown() self.opc_value.check_Manual_print() self.opc_value.check_Manual_printup() self.opc_value.check_Manual_printdown() self.opc_value.check_Manual_printok() self.opc_value.check_PLC_ok() self.opc_value.check_Airdata_stat() self.opc_value.check_Auto_printok() self.opc_value.check_Airtest_ok_r() self.opc_value.check_Airtest_ok_w() self.opc_value.check_Airtest_ng_r() self.opc_value.check_Airtest_ng_w() self.opc_value.check_Start_work() self.opc_value.check_Print_start() def batch_pro(self): value = datetime.datetime.now().month if value >= 1 and value <= 9: m_asc = value + 0x30 elif value >= 10 and value <= 12: m_asc = value + 78 m = self.opc_value.get_Batch_m() if m != m_asc: self.opc_value.set_Batch_m(m_asc) self.user_var.batch_m = '{}'.format(chr(m_asc)) value = datetime.datetime.now().day d = self.opc_value.get_Batch_d() if d != value: self.opc_value.set_Batch_d(value) self.user_var.batch_d = '{0:02d}'.format(value) self.user_var.batch = self.user_var.batch_y + self.user_var.batch_m + self.user_var.batch_d self.user_var.batch_str.set(self.user_var.batch) def product_number_pro(self): self.user_var.number_value = '{0}{1}{2}{3}{4:04d}{5}'.format(self.user_var.number_type, self.user_var.batch_y, self.user_var.batch_m, self.user_var.batch_d, self.user_var.number_count, chr(self.user_var.team_sel + ord('A'))) self.user_var.printf_num_str.set(self.user_var.number_value) self.user_var.serial_num_value = '{0:04d}{1:02d}{2:02d}{3:05d}'.format(datetime.datetime.now().year, datetime.datetime.now().month, datetime.datetime.now().day, self.user_var.serial_num) self.user_var.serial_num_str.set(self.user_var.serial_num_value) def insert_airdata(self): print("Save Data") # self.update_order_tbl(self.user_var.order_num, ok=self.user_var.order_ok, # ng=self.user_var.order_ng, targe=self.user_var.order_targe, # snp=self.user_var.order_snp) self.insert_data_tbl() def update_airdata(self, stat): print("Update Data") self.update_data_tbl(stat) def air_work_pro(self): if self.user_var.opc_conn_stat == "PLC已连接": datastat = self.opc_value.get_Airdata_stat() printok = self.opc_value.get_Auto_printok() airok_r = self.opc_value.get_Airtest_ok_r() airok_w = self.opc_value.get_Airtest_ok_w() airng_r = self.opc_value.get_Airtest_ng_r() airng_w = self.opc_value.get_Airtest_ng_w() startwork = self.opc_value.get_Start_work() if self.user_var.airdata_work_stat == 'working': if airok_r == True and airok_w == True: print("ok to printing") self.user_var.product_ok = True self.insert_airdata() # self.opc_value.set_Airdata_stat(1) self.opc_value.set_Airtest_ok_w(False) self.tcpserverParam.wr_data = self.user_var.number_value self.tcpserverParam.wr_stat = True self.user_var.airdata_work_stat = 'printing' elif airng_r == True and airng_w == True: print("ng to finish") self.user_var.order_ng = self.user_var.order_ng + 1 self.user_var.order_targe = self.user_var.order_targe + 1 self.update_order_tbl(self.user_var.order_num, ng=self.user_var.order_ng, targe=self.user_var.order_targe) self.user_var.product_ok = False self.insert_airdata() self.update_airdata(False) # self.opc_value.set_Airdata_stat(1) self.opc_value.set_Airtest_ng_w(False) self.user_var.airdata_work_stat = 'finish' elif self.user_var.airdata_work_stat == 'printing': if self.tcpserverParam.wr_stat == False: print("ok to printed") self.user_var.airdata_work_stat = 'printed' self.opc_value.set_Print_start(True) elif self.user_var.airdata_work_stat == 'printed': if printok == True: print("ok to finish") self.user_var.order_ok = self.user_var.order_ok + 1 self.user_var.order_targe = self.user_var.order_targe + 1 self.update_order_tbl(self.user_var.order_num, ok=self.user_var.order_ok, targe=self.user_var.order_targe) self.update_airdata(True) self.opc_value.set_Print_start(False) self.opc_value.set_Auto_printok(False) self.user_var.airdata_work_stat = 'finish' elif self.user_var.airdata_work_stat == 'finish': if startwork == False: print("to working") self.opc_value.set_Start_work(True) self.user_var.airdata_work_stat = 'working' self.user_var.serial_num = self.user_var.order_ok + self.user_var.order_ng + 1 self.user_var.number_count = self.user_var.order_ok + 1 self.product_number_pro() self.user_var.serial_num_working = self.user_var.serial_num_value # def database_pro(self): def update_data_tbl(self, status): sqlstr = 'UPDATE product_data_tbl SET ' if status == True: sqlstr = sqlstr + "batch='{0}', testok={1}, data_17700='{2}', data_17720='{3}', end_time={4}, stat={5} WHERE serial = '{6}'".format( self.user_var.number_value, True, self.user_var.airtightness_17700, self.user_var.airtightness_17720, int(time.time()), True, self.user_var.serial_num_working) else: sqlstr = sqlstr + "end_time={0}, data_17700='{1}', data_17720='{2}', stat={3} WHERE serial = '{4}'".format( int(time.time()), self.user_var.airtightness_17700, self.user_var.airtightness_17720, True, self.user_var.serial_num_working) self.sqlclient.sql_action_all(sqlstr) def insert_data_tbl(self): sqlstr = "INSERT INTO product_data_tbl " sqlstr = sqlstr + "(batch, serial, product_name, testok, data_17700, data_17720, team, tester, begin_time, end_time, stat)" sqlstr = sqlstr + " VALUES ('{0}', '{1}', '{2}', {3}, '{4}', '{5}', '{6}', '{7}', {8}, {9}, {10})".format( 'NULL', self.user_var.serial_num_value, self.user_var.product_name, False, 'NULL', 'NULL', str(self.user_var.team), 'tester', int(time.time()), int(time.time()), False ) self.sqlclient.sql_action_all(sqlstr) def update_order_tbl(self, order_num, ok=-1, ng=-1, targe=-1, snp=-1, end_time=-1, stat=False): first = False sqlstr = 'UPDATE order_tbl SET ' if ok >=0: sqlstr = sqlstr + ' ok={0}'.format(ok) first = True if ng >= 0: if first == True: sqlstr = sqlstr + ', ' sqlstr = sqlstr + 'ng={0} '.format(ng) first = True if targe >=0: if first == True: sqlstr = sqlstr + ', ' sqlstr = sqlstr + 'targe={0} '.format(targe) first = True if snp >= 0: if first == True: sqlstr = sqlstr + ', ' sqlstr = sqlstr + 'snp={0} '.format(snp) first = True if end_time >= 0: if first == True: sqlstr = sqlstr + ', ' sqlstr = sqlstr + 'end_time={0} '.format(end_time) first = True if stat == True: if first == True: sqlstr = sqlstr + ', ' sqlstr = sqlstr + 'stat={0} '.format(stat) sqlstr = sqlstr + " WHERE order_num = '{0}'".format(order_num) self.sqlclient.sql_action_all(sqlstr) def insert_order_tbl(self, ok, ng, targe, snp): self.user_var.order_ok = ok self.user_var.order_ng = ng self.user_var.order_targe = targe self.user_var.order_snp = snp sqlstr = "INSERT INTO order_tbl " sqlstr = sqlstr + "(order_num, product_idx, product_name, batch, team, ok, ng, targe, snp, begin_time, stat)" sqlstr = sqlstr + " VALUES ('{0}', {1}, '{2}', '{3}', '{4}', {5}, {6}, {7}, {8}, {9}, {10})".format(self.user_var.order_num, self.user_var.product_idx, self.user_var.product_name, self.user_var.batch, str(self.user_var.team), self.user_var.order_ok, self.user_var.order_ng, self.user_var.order_targe, self.user_var.order_snp, int(time.time()), False) self.sqlclient.sql_action_all(sqlstr) def order_tbl_check(self): sqlstr = 'SELECT id, order_num,product_idx, product_name, batch, team, ok, ng, targe, snp FROM order_tbl WHERE stat=False ORDER by begin_time DESC' results = self.sqlclient.sql_inquire_all(sqlstr) if results != None and len(results) > 0: idx = 0 for row in results: if idx == 0: order_num = row[1] product_idx = row[2] product_name = row[3] batch = row[4] team = row[5] ok = row[6] ng = row[7] targe = row[8] snp = row[9] if self.user_var.batch == batch: self.user_var.order_num = order_num self.user_var.team = team self.user_var.team_sel = ord(self.user_var.team) - ord('A') self.user_var.team_value = self.user_var.team_list[self.user_var.team_sel] self.user_var.order_ok = ok self.user_var.order_ng = ng self.user_var.order_targe = targe self.user_var.order_snp = snp self.user_var.number_count = self.user_var.order_ok + 1 self.user_var.serial_num = self.user_var.order_ok + self.user_var.order_ng + 1 else: self.update_order_tbl(order_num, end_time=int(time.time()) ,stat=True) self.insert_order_tbl(0, 0, 0, 0) else: order_num = row[1] self.update_order_tbl(order_num, stat=True) idx = idx + 1 else: self.insert_order_tbl(0,0,0,0) # def insert_param_tbl(self, type): # sqlstr = "INSERT INTO product_param_tbl " # sqlstr = sqlstr + "(product_idx,product_name,param_type,startuptime,airtime,presstime, testtime, \ # exhaust, maxpress, minpress, setpress, capacity, \ # posthreshold, pnegthreshold)" # if type == 17700: # sqlstr = sqlstr + " VALUES ({0}, '{1}', {2}, '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')".format( # self.user_var.product_idx+1, # self.user_var.product_name, # 17700) # else: # sqlstr = sqlstr + " VALUES ({0}, '{1}', {2}, '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0')".format( # self.user_var.product_idx+1, # self.user_var.product_name, # 17720) # self.sqlclient.sql_action_all(sqlstr) # # def param_tbl_check(self): # sqlstr = "SELECT startuptime,airtime,presstime, testtime, exhaust, maxpress, minpress, setpress, capacity, \ # posthreshold, pnegthreshold FROM product_param_tbl WHERE product_name='{0}' and \ # param_type=17700".format(self.user_var.product_name) # paramresult = self.sqlclient.sql_inquire_all(sqlstr) # if paramresult != None and len(paramresult) > 0: # for paramrow in paramresult: # self.user_var.startdelay_17700_str.set(paramrow[0]) # self.user_var.airtime_17700_str.set(paramrow[1]) # self.user_var.presstime_17700_str.set(paramrow[2]) # self.user_var.testtime_17700_str.set(paramrow[3]) # self.user_var.exhaust_17700_str.set(paramrow[4]) # self.user_var.maxpress_17700_str.set(paramrow[5]) # self.user_var.minpress_17700_str.set(paramrow[6]) # self.user_var.setpress_17700_str.set(paramrow[7]) # self.user_var.capacity_17700_str.set(paramrow[8]) # self.user_var.posthreshold_17700_str.set(paramrow[9]) # self.user_var.pnegthreshold_17700_str.set(paramrow[10]) # else: # self.user_var.startdelay_17700_str.set("0") # self.user_var.airtime_17700_str.set("0") # self.user_var.presstime_17700_str.set("0") # self.user_var.testtime_17700_str.set("0") # self.user_var.exhaust_17700_str.set("0") # self.user_var.maxpress_17700_str.set("0") # self.user_var.minpress_17700_str.set("0") # self.user_var.setpress_17700_str.set("0") # self.user_var.capacity_17700_str.set("0") # self.user_var.posthreshold_17700_str.set("0") # self.user_var.pnegthreshold_17700_str.set("0") # self.insert_param_tbl(17700) # # sqlstr = "SELECT startuptime,airtime,presstime, testtime, exhaust, maxpress, minpress, setpress, capacity, \ # posthreshold, pnegthreshold FROM product_param_tbl WHERE product_name='{0}' and \ # param_type=17720".format(self.user_var.product_name) # paramresult = self.sqlclient.sql_inquire_all(sqlstr) # if paramresult != None and len(paramresult) > 0: # for paramrow in paramresult: # self.user_var.startdelay_17720_str.set(paramrow[0]) # self.user_var.airtime_17720_str.set(paramrow[1]) # self.user_var.presstime_17720_str.set(paramrow[2]) # self.user_var.testtime_17720_str.set(paramrow[3]) # self.user_var.exhaust_17720_str.set(paramrow[4]) # self.user_var.maxpress_17720_str.set(paramrow[5]) # self.user_var.minpress_17720_str.set(paramrow[6]) # self.user_var.setpress_17720_str.set(paramrow[7]) # self.user_var.capacity_17720_str.set(paramrow[8]) # self.user_var.posthreshold_17720_str.set(paramrow[9]) # self.user_var.pnegthreshold_17720_str.set(paramrow[10]) # else: # self.user_var.startdelay_17720_str.set("0") # self.user_var.airtime_17720_str.set("0") # self.user_var.presstime_17720_str.set("0") # self.user_var.testtime_17720_str.set("0") # self.user_var.exhaust_17720_str.set("0") # self.user_var.maxpress_17720_str.set("0") # self.user_var.minpress_17720_str.set("0") # self.user_var.setpress_17720_str.set("0") # self.user_var.capacity_17720_str.set("0") # self.user_var.posthreshold_17720_str.set("0") # self.user_var.pnegthreshold_17720_str.set("0") # self.insert_param_tbl(17720) def product_name_set(self): sqlstr = "SELECT product_idx,product_name,17700_name,17720_name FROM product_tbl WHERE stat=True" paramresult = self.sqlclient.sql_inquire_all(sqlstr) if paramresult != None and len(paramresult) > 0: for paramrow in paramresult: product_idx = paramrow[0] product_name = paramrow[1] name_17700 = paramrow[2] name_17720 = paramrow[3] if product_idx < 14: self.user_var.product_list[product_idx] = product_name self.user_var.product_list_str[product_idx].set(product_name) self.user_var.product_17700_list[product_idx] = name_17700 self.user_var.product_17720_list[product_idx] = name_17720 self.user_var.product_load = False def product_name_check(self): value = self.opc_value.get_Product_idx() self.user_var.opc_product_idx = value value = self.opc_value.get_Product_name() self.user_var.opc_product_name = value if self.user_var.opc_product_idx <= 14: if self.user_var.product_list[self.user_var.opc_product_idx-1] == self.user_var.opc_product_name: self.user_var.product_idx = self.user_var.opc_product_idx-1 self.user_var.product_name = self.user_var.product_list[self.user_var.product_idx] self.user_var.product_17700_name = self.user_var.product_17700_list[self.user_var.product_idx] self.user_var.product_17720_name = self.user_var.product_17720_list[self.user_var.product_idx] self.user_var.product_name_noset = '品番已设置' self.user_var.product_idx_str.set(self.user_var.product_idx + 1) self.user_var.product_str.set(self.user_var.product_name) else: self.user_var.product_idx = 0 self.user_var.product_name = '' self.user_var.product_17700_name = '' self.user_var.product_17720_name = '' self.user_var.product_name_noset = '品番不存在,请设置...' else: self.user_var.product_idx = 0 self.user_var.product_name = '' self.user_var.product_17700_name = '' self.user_var.product_17720_name = '' self.user_var.product_name_noset = '品番编号超过14' def everyday_check(self): if self.order_change_timeout == 0: self.order_change_timeout = 10 self.batch_pro() tt_check = False c_time = time.strftime("%H:%M:%S", time.localtime()) if c_time[0:2] == '00': # 判断截取小时是否为0 if c_time[3:5] == '00': # 判断截取分钟是否为0 if c_time[6:8] == '00': # 判断截取秒是否为0 tt_check = True if tt_check == True: self.update_order_tbl(self.user_var.order_num, end_time=int(time.time()), stat=True) self.user_var.order_count = 1 self.user_var.order_num = '{0}{1}{2}{3:02d}'.format(self.user_var.number_type, datetime.datetime.now().strftime('%Y%m%d%H%M%S'), chr(self.user_var.team_sel + ord('A')), self.user_var.order_count) self.insert_order_tbl(0, 0, 0, 0) self.user_var.serial_num = self.user_var.order_ok + self.user_var.order_ng + 1 self.user_var.number_count = self.user_var.order_ok + 1 print("insert order_tbl everyday!") elif self.user_var.order_update_stat == True: self.user_var.order_update_stat = False self.update_order_tbl(self.user_var.order_num, end_time=int(time.time()), stat=True) self.insert_order_tbl(self.user_var.order_ok, self.user_var.order_ng, self.user_var.order_targe, self.user_var.order_snp) else: self.order_change_timeout = self.order_change_timeout - 1 def plc_conn_check(self): if self.plc_conn_timeout == 0: self.plc_conn_timeout = 20 if self.user_var.workstatstr == True: self.user_var.workstatstr = False self.opc_value.set_PLC_ok(True) else: self.user_var.workstatstr = True self.opc_value.set_PLC_ok(False) else: self.plc_conn_timeout = self.plc_conn_timeout - 1 def exit(self): self.exit_stat = True def run(self): self.product_name_set() while self.user_var.opc_conn_stat != "PLC已连接": if self.exit_stat == True: break time.sleep(1) if self.exit_stat == False: self.batch_pro() self.order_tbl_check() # self.param_tbl_check() while self.exit_stat == False: with main.threadlock: if self.user_var.product_load == True: self.product_name_set() self.product_name_check() self.plc_conn_check() self.opc_data_pro() self.check_opc_stat() self.air_work_pro() self.everyday_check() self.product_number_pro() time.sleep(0.1) print("work进程结束!") self.user_var.pro_exit_stat = True