import threading import time import mysqlhelper class DBOperation(threading.Thread): def __init__(self): threading.Thread.__init__(self) self._space_dict = {1:{},2:{},3:{}} self._command_dict = {1:{},2:{},3:{}} self._db = mysqlhelper.MySqLHelper() self._isClose = False def close(self): self._isClose = True def query_command_in_unit(self, unit): sql = "select * from command_queue WHERE unit=%s" return self._db.selectall(sql, unit) def query_command_all(self): sql = "select * from command_queue" return self._db.selectall(sql) def query_space_in_unit(self,unit): sql = "select * from space WHERE unit=%s" return self._db.selectall(sql,unit) def query_space_all(self): sql = "select * from space" return self._db.selectall(sql) def update_space_status(self,space_id,statu): sql = "update space set statu=%s where id=%s" return self._db.update(sql,(statu,space_id)) def clear_space_data(self,space_id): sql = "update space set car_number=NULL where id=%s" return self._db.update(sql,space_id) def query_vehicle_primary_key(self,car_number): sql = "select primary_key from vehicle where car_number=%s" return self._db.selectall(sql,car_number) def delete_command(self,car_number): sql = "delete from command_queue where car_number=%s" return self._db.delete(sql,car_number) def get_space_dict(self): return self._space_dict def get_command_dict(self): return self._command_dict def run(self): while not self._isClose: for unit in range(1,4): res = self.query_space_in_unit(unit) if len(res) > 0 and self._space_dict[unit] != res: self._space_dict[unit] = res for unit in range(1,4): res = self.query_command_in_unit(unit) if self._command_dict[unit] != res: self._command_dict[unit] = res time.sleep(0.001) # class ParkManage(threading.Thread): # def __init__(self, ip, port, database, user, password, sender): # threading.Thread.__init__(self) # self.conn = None # self.ip = ip # self.port = port # self.database = database # self.user = user # self.password = password # # # self.lock = threading.Lock() # # self.a_unit_park_list = {} # self.b_unit_park_list = {} # self.c_unit_park_list = {} # # self.command_queue_dict = {} # self.a_command_queue_list = {} # self.b_command_queue_list = {} # self.c_command_queue_list = {} # # # self.my_sql_str = queue.Queue() # # # # self.isClose = False # self.g_sender = sender # # self.db_statu_callback = None # self.command_update_callback = None # self.space_update_callback = None # # def setDbStatuCallback(self, callback): # self.db_statu_callback = callback # def setCommandUpdateCallback(self,callback): # self.command_update_callback = callback # # def setSpaceUpdateCallback(self, callback): # self.space_update_callback = callback # def connect(self): # try: # self.conn = pymysql.connect(host=self.ip, port=self.port, database=self.database, charset="utf8", # user=self.user, passwd=self.password) # return True # except: # return False # def putSql(self,SQL): # self.my_sql_str.put(SQL) # # def updatePark(self, dict, statu): # self.lock.acquire() # # cursor = self.conn.cursor() # SQL = "update space set statu=%d where id=%d" % (statu, dict["id"]) # cursor.execute(SQL) # print(SQL) # self.conn.commit() # cursor.close() # self.lock.release() # # def clearPark(self, dict): # self.lock.acquire() # # cursor = self.conn.cursor() # SQL = "update space set car_number=NULL where id=%d" % (dict["id"]) # cursor.execute(SQL) # print(SQL) # self.conn.commit() # cursor.close() # self.lock.release() # # def processDelete(self, dict): # self.lock.acquire() # cursor = self.conn.cursor() # SQL = "delete from command_queue where car_number='%s';" % (dict["car_number"]) # cursor.execute(SQL) # print(SQL) # self.conn.commit() # cursor.close() # self.lock.release() # # def pickUpPark(self, dict): # self.lock.acquire() # # cursor = self.conn.cursor() # SQL = "select primary_key from vehicle where car_number='%s'" % (dict["car_number"]) # cursor.execute(SQL) # self.conn.commit() # results = cursor.fetchall() # cursor.close() # # self.lock.release() # # if len(results) == 1: # key = ''.join(results[0]) # table = msg.pick_table() # table.primary_key = key # self.g_sender.publish("command_ex", "user_command_port", tf.MessageToString(table, as_utf8=True)) # QMessageBox.question(None, '提示', '取车消息发送成功!', # QMessageBox.Ok) # "退出"代表的是弹出框的标题,"你确认退出.."表示弹出框的内容 # # else: # QMessageBox.warning(None, '警告', '查询结果有误,请检查数据库!', # QMessageBox.Ok) # "退出"代表的是弹出框的标题,"你确认退出.."表示弹出框的内容 # def close(self): # self.isClose = True # def run(self): # while self.isClose is not True: # try: # self.conn.ping() # 采用连接对象的ping()函数检测连接状态 # self.db_statu_callback(True) # except: # self.db_statu_callback(False) # self.connect() # time.sleep(0.5) # continue # # 获取一个光标 # cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor) # SQL = "select * from space" # t_a_unit_park_list = [] # t_b_unit_park_list = [] # t_c_unit_park_list = [] # results = cursor.execute(SQL) # # # # 结果数量大于0 # if results > 0: # parkspace = cursor.fetchall() # for space in parkspace: # if space["unit"] == 1: # t_a_unit_park_list.append(space) # if space["unit"] == 2: # t_b_unit_park_list.append(space) # if space["unit"] == 3: # t_c_unit_park_list.append(space) # # if self.a_unit_park_list != t_a_unit_park_list: # self.a_unit_park_list = t_a_unit_park_list # self.space_update_callback(self.a_unit_park_list, 'A') # # if self.b_unit_park_list != t_b_unit_park_list: # self.b_unit_park_list = t_b_unit_park_list # self.space_update_callback(self.b_unit_park_list, 'B') # # if self.c_unit_park_list != t_c_unit_park_list: # self.c_unit_park_list = t_c_unit_park_list # self.space_update_callback(self.c_unit_park_list, 'C') # # SQL = "select * from command_queue" # t_a_command_list = [] # t_b_command_list = [] # t_c_command_list = [] # results = cursor.execute(SQL) # if results > 0: # command_queue = cursor.fetchall() # self.command_queue_dict = command_queue # for command in command_queue: # if command["unit"] == 1: # t_a_command_list.append(command) # if command["unit"] == 2: # t_a_command_list.append(command) # if command["unit"] == 3: # t_a_command_list.append(command) # # if self.a_command_queue_list != t_a_command_list: # self.a_command_queue_list = t_a_command_list # self.command_update_callback(self.a_command_queue_list, 'A') # # elif self.b_command_queue_list != t_b_command_list: # self.b_command_queue_list = t_b_command_list # self.command_update_callback(self.b_command_queue_list, 'B') # # elif self.c_command_queue_list != t_c_command_list: # self.c_command_queue_list = t_c_command_list # self.command_update_callback(self.c_command_queue_list, 'C') # # if self.my_sql_str.qsize() > 0: # SQL = self.my_sql_str.get(False) # cursor.execute(SQL) # self.conn.commit() # # # 关闭光标 # cursor.close() # # # print("------------------------------") # time.sleep(0.001)