def btDongTu_click(self): #chong doi phim if (self.btClickTimer): return else: self.buttonTimer() #chon dong tat ca cac tu o ca 2 ben server.tuTraiPhai = 'A' server.dongMoTuFunction(0, 0)
def btNext_click(self): #cho luong thuc hien cong viec dong tu lan dau self.ui = Ui_SV_mainDisplay() self.ui.setupUi() self.ui.show() if server.isFullSceen: self.ui.showFullScreen() #dong form này lại---dong tu lan dau dong ca 2 ban server.tuTraiPhai = 'A' # dong ca 2 ben tu server.dongMoTuFunction(0, 0) self.dtLanDau.close()
def btMo_clicked(self): if len(self.data) > 0: if self.data[self.indexNow][5] == '1': firstName = 'Left_' else: firstName = 'Right_' numTu = int(self.data[self.indexNow][4]) if (firstName[0] == 'L' and numTu <= server.numClientLeft) or ( firstName[0] == 'R' and numTu <= server.numClientRight): nameTu = firstName + str(numTu) if server.isWaiting == 2: server.dongMoTuFunction(2, nameTu) elif server.isWaiting == 0: server.dongMoTuFunction(1, nameTu) else: self.label.setText('Giá không tồn tại')
def btNext_click(self): linkFileMP3 = server.folderMP3 + "007.mp3" server.playmp3(linkFileMP3) #cho luong thuc hien cong viec dong tu lan dau self.window = QtWidgets.QMainWindow() self.ui = Ui_SV_mainDisplay() self.ui.setupUi(self.window) self.window.show() if server.isFullSceen: self.window.showFullScreen() #dong form này lại---dong tu lan dau dong ca 2 ban server.tuTraiPhai = 'A' # dong ca 2 ben tu server.dongMoTuFunction(0, 0) self.SVChonClient.close()
def mouseReleaseEvent(self, ev): self.xE = ev.pos().x() self.yE = ev.pos().y() difX = self.xE - self.xS difY = self.yE - self.yS if difX > self.lengthMouse: print("swipe right do nothing") elif difX < -self.lengthMouse: print("swipe left") elif difY > self.lengthMouse: print("swipe down - dong toan bo gia") server.tuTraiPhai = 'A' server.dongMoTuFunction(0, 0) elif difY < -self.lengthMouse: print('swipr Up - thuc hien thong gio') server.serverMain.thongGioFuction()
def btMoTu_click(self): #chong doi phim if (self.btClickTimer): return else: self.buttonTimer() maxTu = server.numClientLeft + server.numClientRight + 1 dialogKey = Ui_Dialog(maxTu) value = dialogKey.exec_() if value: nameTu = server.serverMain.getNameOfTu(int(value)) if nameTu[0] == 'H': return server.tuTraiPhai = nameTu[0] if server.isWaiting == 2: server.dongMoTuFunction(2, nameTu) elif server.isWaiting == 0: server.dongMoTuFunction(1, nameTu)
def setEvent(self): server.statusVanHanh = 41 # da bat form bao duong linkFile = server.folderMP3 + "017.mp3" server.playmp3(linkFile) print("close tu left start") #lb tu dong: 1-7 self.listlb = [] self.listlb.append(self.lbTempIn) self.listlb.append(self.lbTempOut) self.listlb.append(self.lbCoichip) self.listlb.append(self.lbQuatGio) self.listlb.append(self.lbDenThap) self.listlb.append(self.lbCBChayTrong) self.listlb.append(self.lbCBChayNgoai) self.listSoundServer = [ "030", "031", "035", "036", "037", "038", "039" ] self.listSound = [ "030", "031", "032", "033", "034", "035", "038", "039", "040", "040", "040", "041", "042", "043" ] self.listdata2Ar = [ b'\x01', b'\x02', b'\x06', b'\x07', b'\x08', b'\x09', b'\x0a' ] self.caseBD = 0 server.caseBD = 0 self.flagFirst = True #dong tat ca cac tu truoc khi bao duong server.tuTraiPhai = 'A' server.dongMoTuFunction(0, 1) for lb in self.listlb: lb.hide() self.btThoat.clicked.connect(self.btExit_click) self.ctimer = QTimer(self.frame) self.ctimer.timeout.connect(self.checkTimer) self.ctimer.start(400) self.Tu = server.numClientLeft self.statusTu = 2 # 2 ben trai 1 ben phai 0 master if self.Tu == 0: #ko co tu ben trai self.Tu = server.numClientRight self.statusTu = 1 # 2 ben trai 1 ben phai 0 master
def showTemp(self): #try: _date = strftime("%H:%M:%S %A,%d/%m/%Y") minitue = int(strftime("%M")) sec = int(strftime("%S")) if minitue % 10 == 0: if self.saveLog: self.saveLog = False server.saveNhietDoDoAm() else: self.saveLog = True if sec % 1 == 0: server.serverMain.sendTempOut() #neu dang trong che do bao duong ko lam gi luon return luon #Kiem tra den thap sent to Arduino to status of server if server.isWaiting == 2 or server.isWaiting == 3 or server.isWaiting == 1: #Dung khan cap bao den vang uart.DataCamBien[4] = 0x32 uart.sentCambien() elif server.isWaiting == 0: # hoat dong binh thuong bao den xanh uart.DataCamBien[4] = 0x31 uart.sentCambien() if server.caseBD > 0: return self.lbHumiInValue.setText(str(uart.dataReceved.humiIn)) self.lbTempInValue.setText(str(uart.dataReceved.tempIn)) self.lbTempOutValue.setText(str(uart.dataReceved.tempOut)) self.lbHumiOutValue.setText(str(uart.dataReceved.humiOut)) tuActive = server.tuActive if tuActive[0] == 'L' or tuActive[ 0] == 'R': #hien thi tu nao dang mo va thong bao nguoi vao ra if server.dataReceivedSer[tuActive].statusMotor == 2: self.lbstatusMotor.setText('Giá ' + str(server.converNameTu(tuActive)) + " Mở") self.lbSoNguoi.setText( str(server.dataReceivedSer[tuActive].numPersonIn)) elif server.dataReceivedSer[tuActive].statusMotor == 4: self.lbstatusMotor.setText("Đóng") self.lbSoNguoi.setText("") self.lbKhoangCach.setText( str(server.dataReceivedSer[tuActive].distanceSen_1)) else: #neu ko co tu nao dang mo thi setText bang Dong or Dung self.lbKhoangCach.setText("") self.lbSoNguoi.setText("") self.lbWarning.setText("") self.lbstatusMotor.setText("Đóng") if server.isWaiting == 2: # dung khan cap self.lbstatusMotor.setText("Dừng") #thong bao co nguoi vao tu tuHumen, isHumenIn, numHumen = server.serverMain.checkHumen() if isHumenIn: #co nguoi vao tu self.linkFile = server.folderMP3 + "009.mp3" server.playmp3(self.linkFile) self.lbSoNguoi.setText(str(numHumen)) self.lbstatusMotor.setText(tuHumen) #check su co xem có sự cố cháy ko nếu có sự cố cháy thì chỉ hiện sự cố cháy thôi rồi return server.serverMain.checkSuCo(False) if server.statusSuCo == 0: # khong co su co thi reset form self.isShowSuCoForm = False if server.arraySuCo[1] != '0' or server.arraySuCo[ 2] != '0': # su co co chay if self.isShowSuCoForm == False: if server.arraySuCo[1] != '0': #phat hien chay trong tu --> mo tu bi chay self.linkFile = server.folderMP3 + "010.mp3" server.playmp3(self.linkFile) if server.arraySuCo[1][ 0] == 'M': # mo 2 tu gan master nhat Left_1 Right_1 server.tuTraiPhai = 'A' server.dongMoTuFunction(4, 'All_1') elif server.arraySuCo[1][0] == 'L' or server.arraySuCo[1][ 0] == 'R': server.tuTraiPhai = server.arraySuCo[1][0] server.dongMoTuFunction(3, server.arraySuCo[1]) else: #phat hien chay ngoai tu -> dong tat ca cac tu self.linkFile = server.folderMP3 + "011.mp3" server.playmp3(self.linkFile) server.tuTraiPhai = 'A' server.dongMoTuFunction(0, 1) self.isShowSuCoForm = True self.callSuCoForm() return #dang van hanh ma co vat can thi hien form su co canh bao vat can if server.isWaiting == 3 and (server.statusSuCo == 1 or server.statusSuCo == 4): if self.isShowSuCoForm == False: self.isShowSuCoForm = True self.callSuCoForm() return if server.isWaiting != 3 or server.statusSuCo != 5: self.isShowFormKhoaDC = False if server.isWaiting == 3 and server.statusSuCo == 5: if self.isShowFormKhoaDC == False: self.isShowFormKhoaDC = True self.callFormKhoaDC() return #yeu cau dong mo tu va khong co nguoi trong tu if server.isWaiting == 0 or server.isWaiting == 2: self.isShowWaitForm = False elif server.isWaiting == 1: if self.isShowWaitForm == False: self.isShowWaitForm = True self.callWaitingForm() '''phan nay kiem tra thong gio timer thuc thi thong gio''' #check thong gio co bat hay khong if server.numThongGio >= 1: server.numThongGio = -2 self.btThongGio_click() if server.executeThongGio == 0: # thong gio dang khong thuc thi dateH = int(strftime("%H")) dateM = int(strftime("%M")) timeThongGio = uart.dataAllJsonConfig["timeThongGio"] timeSplit = timeThongGio.split(':') if dateH == int(timeSplit[0]) and dateM == int( timeSplit[1] ) and timeSplit[3] == '1': # co kich hoat ham autoThongGio print("Mo thong gio tu dong") self.isPressThongGio = False #thuc hien tu dong self.btThongGio_click() # thuc hien chuc nang mo thong gio server.countThongGio = int(timeSplit[2]) * 60 elif server.executeThongGio == 1: #che do thong gio dang mo server.countThongGio -= 1 #tang so giay thong gio dang mo len 1 if server.countThongGio <= 0 and self.isPressThongGio == False: #chi tu dong tat khi tu dong bat, khi bat bang tay ko tu tat thong gio self.btThongGio_click( ) # neu het thoi gian thong gio tat che do thong gio if server.numThongGio == -3: self.btThongGio.setText("Tắt\nThông gió") elif server.numThongGio == 0: self.isPressThongGio = True self.btThongGio.setText("Thông gió") '''phan kiem tra PC --> master bat tat dieu hoa''' if server.statusVanHanh == 0x34: #DieuKhienDieuHoa self.callDieuHoaForm() elif server.statusVanHanh == 40: # bat che do bao duong self.callBaoDuongForm()
def run(self): self.soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #self.soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.soc.bind((IPSERVER, PORT)) self.soc.listen(5) self.logger = logging.getLogger("ServerThreadMain") self.isLeft = True self.conn = None self.csdl = [] self.isConnectPC = False print("socket is running") #self.logger.debug("socket is running - server bat dau hoat dong") while server.isListenCL: self.isConnectPC = False self.conn, addr = self.soc.accept() ip, port = str(addr[0]), str(addr[1]) print('Accepting connection from ', ip, ':', port) self.isConnectPC = True while server.isListenCL: try: data = self.conn.recv(3) dt = self.conn.recv(2) print('pc:', data, dt) if len(data) == 3 and len(dt) == 2 and data[0] == 0xff: traiOrPhai = '0' if data[1] == 1: # chon tu ben trai: nameTu = "Left_" traiOrPhai = 'L' elif data[1] == 2: nameTu = "Right_" traiOrPhai = 'R' elif data[1] == 0xff: nameTu = 'All_' traiOrPhai = 'A' else: print("recive Frame tu server bi sai form", data, dt) continue numberTu = int( data[2]) # neu numberTu = 255 tat ca cac tu nameTu = nameTu + str(numberTu) #Left_2 Right_3 if dt[0] == 0xAA: #nhom chuc nang co ban dong, mo, dung, khoa dien tu if dt[1] == 0x01: server.tuTraiPhai = traiOrPhai server.dongMoTuFunction(0, 0) elif dt[1] == 0x02 and numberTu < 255 and numberTu > 0 and traiOrPhai != 'A': #mo tu nao do if server.isWaiting == 2: server.dongMoTuFunction(2, nameTu) elif server.isWaiting == 0: server.dongMoTuFunction(1, nameTu) elif dt[1] == 0x03: # dung khan cap deo can quan tam nhung thang kia la cai me gi server.serverMain.sentStop2AllClient() elif dt[1] == 0x04: #khoa dien tu khoa dong co self.khoaDC(nameTu, numberTu, b'\x00') elif dt[1] == 0x05: self.khoaDC(nameTu, numberTu, b'\x01') elif dt[0] == 0xbb and (dt[1] == 0x01 or dt[1] == 0x02 or dt[1] == 0x03): #bao an toan print("Pc bao an toan-nhom chuc nang su co") elif dt[0] == 0xcc: #nhom chuc nang bao duong print('pc gui lenh bao duong he thong') if dt[1] == 0x01: #bat dau qua trinh bao duong server.statusVanHanh = 40 elif dt[1] == 0x02: # tiep tuc bao duong server.caseBD += 1 elif dt[1] == 0x03: #ket thuc qua trinh bao duong server.statusVanHanh = 42 elif dt[0] == 0xdd: # nhom chuc nang dieu khien dieu hoa value = self.conn.recv(1)[0] server.statusVanHanh = value #0x30-0x35 0x10 - 0x1f print('PC van Hanh dieu hoa ', server.statusVanHanh) elif dt[0] == 0xee: # nhom chuc nang phu tro if dt[1] == 0x30: #bat den chieu sang self.batTatDen(nameTu, numberTu, b'\xdd') elif dt[1] == 0x31: # tat den chieu sang self.batTatDen(nameTu, numberTu, b'\xdf') elif dt[1] == 0x32: # gui lai thong tin so tu tren moi day print("sent to PC numTu ") header = b'\xff\x01\x01\xee\x32\x32' trai = struct.pack('B', server.numClientLeft) phai = struct.pack('B', server.numClientRight) header = header + trai + phai self.conn.send(header) elif dt[1] == 0x33 and server.executeThongGio == 0: # bat quat thong gio neu thong gio chua thuc thi server.serverMain.thongGioFuction() elif dt[1] == 0x34 and server.executeThongGio == 1: # tat quat thong gio neu thong gio dang thuc thi server.serverMain.thongGioFuction() elif dt[1] == 0x35: #cai dat thoi gian tu Pc ddate = self.conn.recv(6) self.configTime(ddate) self.conn.send( data + dt + ddate + b'\x32' ) # phan hoi lai PC cai dat thanh cong elif dt[1] >= 0x01 and dt[ 1] <= 0x0A: # dieu chinh cuong do am thanh ung voi 10 muc do pass elif dt[1] >= 0x0B and dt[ 1] <= 0x0F: # 5 loai giong noi pass elif dt[1] >= 0x11 and dt[ 1] <= 0x1A: # 10 muc dieu chinh do sang man hinh pass elif dt[0] == 0xff: # nhom chuc nang cai dat value = self.conn.recv(1)[0] if dt[1] == 0x01: # khoang cach mo tu self.saveConfig(nameTu, 255, 'KhoangCach', value) elif dt[1] == 0x02: # chieu quay mo tu self.saveConfig(nameTu, 255, 'ChieuQuayDC', value) elif dt[1] == 0x03: # toc do dong co self.saveConfig(nameTu, numberTu, 'TocDoDC', value) elif dt[1] == 0x04: # khoang cach giam toc self.saveConfig(nameTu, 255, 'KCGiamToc', value) elif dt[1] == 0x05: # luc chong ket self.saveConfig(nameTu, numberTu, 'LucCK', value) elif dt[1] == 0x06: # tat cam bien pass elif dt[0] == 0xab and dt[ 1] == 0xab: #update thong tin len PC self.sendPCInfor(nameTu, numberTu) elif len( data ) == 3 and data[0] == 0x23 and data[1] == 0x23 and data[ 2] == 0x23: #truyen nhan du lieu tra cuu thong tin tu #dt 2byte luc nay la ID master hien tai chua can dung toi header = self.conn.recv( 7) # theo header la doc 7 byte tiep theo if len(header) == 7: # neu nhan du 7 byte maLenh = int(header[0]) lengthAfter = int(header[4] << 24) + int( header[3] << 16) + int(header[2] << 8) + int( header[1]) print('maLenh=', maLenh, ' lengthAfter=', lengthAfter) #doc du so byte gui toi self.csdl = [] dtByte = b'' while (len(dtByte) < lengthAfter): dtByte += self.conn.recv(1024) dataString = dtByte.decode( 'utf-8') #decode('ISO-8859-1') kq = dataString.split('$$$') for dt in kq: online = dt.split('&&&') if (len(online) == 8): self.csdl.append(online) print('phan tich xong data nhan duoc') elif not data or not dt: print('data', data, ' dt=', dt) self.conn.close() print("Client masterPC exit or connection error") break except Exception as e: print("Exception masterPC : ", str(e)) self.conn.close() break
def btTiepTuc_click(self): #khi kiem tra xong het 1 tu if self.caseBD == 14 and self.Tu > 0: self.caseBD = 4 server.caseBD = 4 self.Tu -= 1 if self.Tu == 0: if self.statusTu == 2: self.statusTu -= 1 self.Tu = server.numClientRight #kiem tra xong tu ben trai chuyen sang tu ben phai elif self.statusTu == 1: self.statusTu -= 1 self.caseBD = 2 server.caseBD = 2 self.caseBD += 1 server.caseBD = self.caseBD if self.Tu == 0 and self.caseBD >= 8: linkFile = server.folderMP3 + "050.mp3" server.playmp3(linkFile) return linkFile = server.folderMP3 + self.listSound[self.caseBD - 1] + ".mp3" #truong hop kiem tra tự động if self.caseBD == 1 or self.caseBD == 2: # 2 case kiem tra tự động cho cac client va sever self.listlb[self.caseBD - 1].show() data = struct.pack('B', self.caseBD) for i in range(1, server.numClientLeft + 1): nameTu = "Left_" + str(i) server.serverMain.sendMes2Client(nameTu, b'\xbd\xbd' + data) for i in range(1, server.numClientRight + 1): nameTu = "Right_" + str(i) server.serverMain.sendMes2Client(nameTu, b'\xbd\xbd' + data) uart.caseBDIsACK = 0 uart.sentDataRandom(b'\xbd\xbd', b'\x1f\x00\x00\x00\x00\x00\x00\x00') time.sleep(0.5) uart.sentDataRandom(b'\xbd\xbd', data + b'\x00\x00\x00\x00\x00\x00\x00') if self.caseBD == 3 or self.caseBD == 4 and self.Tu > 0: data = struct.pack('B', self.caseBD) for i in range(1, server.numClientLeft + 1): nameTu = "Left_" + str(i) server.serverMain.sendMes2Client(nameTu, b'\xbd\xbd' + data) for i in range(1, server.numClientRight + 1): nameTu = "Right_" + str(i) server.serverMain.sendMes2Client(nameTu, b'\xbd\xbd' + data) if self.caseBD == 5 and self.Tu > 0: #sent mo tu ngoai cung if self.statusTu == 2: firstNameTu = "Left_" else: firstNameTu = "Right_" nameTu = firstNameTu + str(self.Tu) server.tuTraiPhai = nameTu[0] server.dongMoTuFunction(3, nameTu) numInLoop = 0 while server.dataReceivedSer[nameTu].statusMotor != 2: numInLoop += 1 time.sleep(0.1) if numInLoop > 100: print("Tu eo biet da duoc mo hay chua time out mo tu") break pass print(" tu da mo xong") server.playmp3(linkFile) time.sleep(2) data = struct.pack('B', self.caseBD) server.serverMain.sendMes2Client(nameTu, b'\xbd\xbd' + data) return if self.caseBD >= 5 and self.Tu > 0: data = struct.pack('B', self.caseBD) if self.statusTu == 2: firstNameTu = "Left_" else: firstNameTu = "Right_" nameTu = firstNameTu + str(self.Tu) server.serverMain.sendMes2Client(nameTu, b'\xbd\xbd' + data) if self.Tu == 0: #check cac test khac co pass hay ko if self.caseBD == 2 or self.caseBD == 3 or self.caseBD == 7 and uart.caseBDIsACK == 1: self.listlb[self.caseBD - 2].setStyleSheet("color:#FF0000") linkFile = server.folderMP3 + self.listSoundServer[self.caseBD - 1] + ".mp3" if self.listSoundServer[self.caseBD - 1] == '037': server.playmp3(linkFile) time.sleep(2) self.listlb[self.caseBD - 1].show() uart.caseBDIsACK = 0 uart.sentDataRandom(b'\xbd\xbd', b'\x1f\x00\x00\x00\x00\x00\x00\x00') time.sleep(0.5) uart.sentDataRandom( b'\xbd\xbd', self.listdata2Ar[self.caseBD - 1] + b'\x00\x00\x00\x00\x00\x00\x00') server.playmp3(linkFile)