def check_serve(self): DBG('check_serve') if self.check010(self.data.s1_array, 10) is True: DBG('check_serve 1') # 查找3s内beaconArray有没有在附近 time.sleep(3) DBG('check_serve 2') if len(self.data.beacons_array) == 0: DBG('check_serve 3') # 设置use状态 dic = {'type': int(IntMessage.use), 'pyload': ''} self._signal.emit(json.dumps(dic)) else: DBG('check_use_or_serve 4') # 设置serve状态,并且获取最近的beacon pl = self.data.get_nearest_beacon(5) if len(pl) > 0: return self._signal.emit( json.dumps({ 'type': int(IntMessage.serve), 'pyload': pl })) else: return self._signal.emit( json.dumps({ 'type': int(IntMessage.use), 'pyload': '' }))
def check_serve_or_clean(self): DBG('check_serve_or_clean') pl = self.data.get_nearest_beacon(10) if len(pl) > 0: DBG("server") time.sleep(1) self.check_serve_or_clean() else: return self._signal.emit( json.dumps({ 'type': int(IntMessage.clean), 'pyload': '' }))
def s1_event_comming(self, statu): self.s1_array.append({'s': statu, 't': time.time()}) # DBG("s1_event_comming:") if self.get_checking() is False: pre_statu = self.get_room_pre_statu() if pre_statu == IntMessage.serve: # 收到s1灭掉信号 if statu == 0: # 3s内s2有亮的情况 DBG('checking_serve_or_clean s1') # 收到s2亮信号 else: self.set_checking() self.ck_statu = ThreadCheckStatus.checking_serve_or_clean DBG('checking_serve_or_clean set_checking s1')
def run(self): # DBG("run!") while True: if g_check_queue.empty(): time.sleep(1) # DBG("CheckingThread") self.lock.acquire() if self.data.get_checking() is True: if self.data.ck_statu == ThreadCheckStatus.checking_use_or_serve: self.check_use_or_serve() elif self.data.ck_statu == ThreadCheckStatus.checking_serve: self.check_serve() elif self.data.ck_statu == ThreadCheckStatus.checking_serve_or_clean: self.check_serve_or_clean() # elif self.data.ck_statu == ThreadCheckStatus.checking_use_or_clean: # self.check_use_or_clean() else: DBG("get_checking == TRUE 5") self.data.reset_checking() self.lock.release() else: data = g_check_queue.get() # DBG(data) self._signal.emit(data)
def show_dialog(self, msgtxt, detailtxt): msg = QMessageBox() msg.setIcon(QMessageBox.Information) msg.setText(msgtxt) msg.setWindowTitle("Alert") msg.setDetailedText(detailtxt) msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel) DBG("value of pressed message box button:")
def check010(arr, ts): is_find = False st = 0 ctime = time.time() - ts idx = len(arr) if idx == 1: dt = arr[0] if dt['s'] == 1: st = 3 elif idx == 2: while idx > 0: idx = idx - 1 dt = arr[idx] if dt['t'] > ctime: if st == 0: if dt['s'] == 0: st = 1 else: st = 3 break elif st == 1: if dt['s'] == 1: st = 3 break else: break else: while idx > 0: idx = idx - 1 dt = arr[idx] DBG(str(dt) + str(idx)) if dt['t'] > ctime: if st == 0: if dt['s'] == 0: st = 1 else: st = 3 break elif st == 1: if dt['s'] == 1: st = 3 else: st = 1 break elif st == 2: if dt['s'] == 0: st = 3 break else: break if st == 3: is_find = True return is_find
def check_use_or_serve(self): # READY状态下判断S2亮 use or Serve: # 前10s判断s1有010状态变更 DBG('check_use_or_serve') if self.check010(self.data.s1_array, 10) is True: DBG('check_use_or_serve 1') # 查找3s内beaconArray有没有在附近 DBG('check_use_or_serve 2') if len(self.data.beacons_array) == 0: DBG('check_use_or_serve 3') # 设置use状态 dic = {'type': int(IntMessage.use), 'pyload': ''} self._signal.emit(json.dumps(dic)) else: DBG('check_use_or_serve 4') # 设置serve状态,并且获取最近的beacon pl = self.data.get_nearest_beacon(5) DBG(str(pl) + str(len(pl))) if len(pl) > 0: return self._signal.emit( json.dumps({ 'type': int(IntMessage.serve), 'pyload': pl })) else: return self._signal.emit( json.dumps({ 'type': int(IntMessage.use), 'pyload': '' }))
def set_room_statu(self, dic): msg = IntMessage(dic['type'], dic['pyload']) DBG("set_room_statu:" + str(msg.get_type())) self.data.room_array.append(msg.get_type()) if msg.get_type() == IntMessage.ready: self._ui.statuLeftBtn.setText("IS READY") self._ui.statuLeftBtn.setStyleSheet( "QPushButton {background-color:rgb(33, 255, 6);color: white; border: none;font-size:24px;}" ) self._ui.statuRightBtn.setText("Please Come In") elif msg.get_type() == IntMessage.use: DBG("lai la!!!!!!") self._ui.statuLeftBtn.setText("IN USE") self._ui.statuLeftBtn.setStyleSheet( "QPushButton {background-color: yellow;color: white; border: none;font-size:24px;}" ) self._ui.statuRightBtn.setText("Now Serve") elif msg.get_type() == IntMessage.serve: pl = msg.get_payload() info = ('UUID:' + str(pl['UUID']) + 'MAJ:' + str(pl['MAJOR']) + 'MIN:' + str(pl['MINOR'])) self._ui.becon_info_label.setText(info) self._ui.statuLeftBtn.setText("IN SERVICE") self._ui.statuLeftBtn.setStyleSheet( "QPushButton {background-color: blue;color: white; border: none;font-size:24px;}" ) self._ui.statuRightBtn.setText("Now Serve") elif msg.get_type() == IntMessage.clean: self._ui.statuLeftBtn.setText("NEED CLEAN") self._ui.statuLeftBtn.setStyleSheet( "QPushButton {background-color: gray;color: white; border: none;font-size:24px;}" ) self._ui.statuRightBtn.setText("Do not enter")
def detctThreadWork(self): # CPU informatiom DBG("detctThreadWork!") CPU_temp = self.getCPUtemperature() CPU_usage = self.getCPUuse() # RAM information # Output is in kb, here I convert it in Mb for readability RAM_stats = self.getRAMinfo() RAM_total = round(int(RAM_stats[0]) / 1000, 1) RAM_used = round(int(RAM_stats[1]) / 1000, 1) RAM_free = round(int(RAM_stats[2]) / 1000, 1) # Disk information DISK_stats = self.getDiskSpace() DISK_total = DISK_stats[0] DISK_used = DISK_stats[1] DISK_perc = DISK_stats[3] info = 'CPU Temperature = ' + CPU_temp info = info + '\nCPU Use = ' + CPU_usage info = info + '\nRAM Total = ' + str(RAM_total) + ' MB' info = info + '\nRAM Used = ' + str(RAM_used) + ' MB' info = info + '\nRAM Free = ' + str(RAM_free) + ' MB' info = info + '\nDISK Total Space = ' + str(DISK_total) + 'B' info = info + '\nDISK Used Space = ' + str(DISK_used) + 'B' info = info + '\nDISK Used Percentage = ' + str(DISK_perc) # print('') # # print('CPU Temperature = ' + CPU_temp) # print('CPU Use = ' + CPU_usage) # print('') # print('RAM Total = ' + str(RAM_total) + ' MB') # print('RAM Used = ' + str(RAM_used) + ' MB') # print('RAM Free = ' + str(RAM_free) + ' MB') # print('') # print('DISK Total Space = ' + str(DISK_total) + 'B') # print('DISK Used Space = ' + str(DISK_used) + 'B') # print('DISK Used Percentage = ' + str(DISK_perc)) msg = IntMessage(IntMessage.GETSYSINFO, {'pyload': info}) self._commCallback(msg)
def gui_comm(self, msg): if not isinstance(msg, IntMessage): raise Exception("Message has to be an IntMessage") msg_type = msg.get_type() # start different beacon standards if msg_type is IntMessage.START_SCAN_BLE: self._scannerble.scan() elif msg_type is IntMessage.STOP_SCAN_BLE: DBG('Stopped BLE Scan') self._scannerble.stop_scan() elif msg_type is IntMessage.GETSYSINFO: self._sysInfo.detct() elif msg_type is IntMessage.STOPGETSYSINFO: self._sysInfo.cleanup() elif msg_type is IntMessage.QUITAPP: self._sysInfo.cleanup() self._scannerble.stop_scan() self._detect.cleanup()
def s2_event_comming(self, statu): self.s2_array.append({'s': statu, 't': time.time()}) if self.get_checking() is False: pre_statu = self.get_room_pre_statu() # DBG("s2_event_comming:" + str(statu) + '---room Statu:' + str(pre_statu)) if pre_statu == IntMessage.ready: # 收到s2灭掉信号 if statu == 0: self.dummy_function() # 收到s2亮信号 else: DBG('check') if self.get_checking() is False: DBG('check1') # if self.get_pre_s2_lighton() == 0: DBG('check2') self.set_checking() DBG('check3') self.ck_statu = ThreadCheckStatus.checking_use_or_serve elif pre_statu == IntMessage.use: # 收到s2灭掉信号 if statu == 0: # 3s内s2有亮的情况 DBG('use checking_serve') # 收到s2亮信号 else: self.set_checking() self.ck_statu = ThreadCheckStatus.checking_serve elif pre_statu == IntMessage.serve: # 收到s2灭掉信号 if statu == 0: # 3s内s2有亮的情况 DBG('checking_serve_or_clean s2') # 收到s2亮信号 else: self.set_checking() self.ck_statu = ThreadCheckStatus.checking_serve_or_clean DBG('checking_serve_or_clean set_checking s2') elif pre_statu == IntMessage.clean: self.dummy_function()
def save_to_config(self): DBG("save_to_config:") self.show_dialog( "Saved current values!", "The current values of all Beacons have been saved." "They will be available on the next application start.")
def get_system_info(msg): DBG('get_system_info') dic = {'type': int(msg.get_type()), 'pyload': str(msg.get_payload())} g_data_queue.put(json.dumps(dic))
def dummy_fun(msg): DBG(msg)
def detct(self): DBG("detct!") self._repeatThread.start()
def staturightbutton_clicked(self): pre_statu = self.data.get_room_pre_statu() DBG('---room Statu:' + str(pre_statu)) # if pre_statu == IntMessage.clean: self.set_room_statu({'type': int(IntMessage.ready), 'pyload': ''})
def bcscanstop_clicked(self): msg = IntMessage(IntMessage.STOP_SCAN_BLE) self._commcallback(msg) DBG("Stop Button clicked")
def bcscanstart_clicked(self): msg = IntMessage(IntMessage.START_SCAN_BLE) self._commcallback(msg) DBG("Scan Button clicked")
def saveEvent1(): DBG("Someone is closing to No 1 !")
def saved_values(): DBG("saved_values:")
def test_for_beacon(self, payload): data = bytearray.fromhex(payload) # EddystoneBeacon if len(data) >= 20 and data[19] == 0xaa and data[ 20] == 0xfe: # check if an Eddystone was scanned serviceDataLength = data[21] frameType = data[25] if frameType == 0x10: DBG( "Eddystone-URL" ) # scanned an Eddystone-URL frame, no further handling here elif frameType == 0x00: DBG("Eddystone-UID") # read TX value an convert to signed integer DBG("TX: {}".format( int.from_bytes(data[26], byteorder='big', signed=True))) # read Namespace and Instance ID as HEX values DBG("Namespace: {}".format(payload[(27 * 3):(37 * 3)])) DBG("Instance: {}".format(payload[(37 * 3):(43 * 3)])) # send scanned data to controller msg = IntMessage( IntMessage.SCANNED_EDDYSTONEUID, { 'NID': payload[(27 * 3):(37 * 3)], 'IID': payload[(37 * 3):(43 * 3)], 'TX': data[26] - 256 }) self._comm(msg) elif frameType == 0x20: DBG( "Eddystone-TLM" ) # scanned an Eddystone-TLM frame, no further handling here else: DBG("Unknown Eddystone frame type: {}".format(frameType)) # iBeacon elif len(data) >= 20 and data[17] == 0x1a and data[ 18] == 0xff: # check if an iBeacon was scanned # DBG("iBeacon") # read UUID as HEX from scanned data # DBG("UUID: {}".format(payload[(23 * 3):(39 * 3)])) # read Major and Minor and convert to a unsigned integer # DBG("Major: {}".format(int.from_bytes(data[39:41], byteorder='big', signed=False))) # DBG("Minor: {}".format(int.from_bytes(data[41:43], byteorder='big', signed=False))) # read TX value an convert to signed integer # DBG("TX: {}".format(int.from_bytes(data[43:44], byteorder='big', signed=True))) # send scanned data to controller msg = IntMessage( IntMessage.SCANNED_IBEACON, { 'UUID': payload[(23 * 3):(39 * 3)], 'MAJOR': int.from_bytes(data[39:41], byteorder='big', signed=False), 'MINOR': int.from_bytes(data[41:43], byteorder='big', signed=False), 'TX': int.from_bytes(data[43:44], byteorder='big', signed=True), 'RSSI': int.from_bytes(data[44:45], byteorder='big', signed=True), 'time': time.time() }) self._comm(msg) # AltBeacon elif len(data) >= 20 and data[17] == 0x1b and data[ 18] == 0xff: # check if an AltBeacon was scanned DBG("Altbeacon") # read BeaconID as HEX from scanned data DBG("Beacon ID: {}".format(payload[(23 * 3):(43 * 3)])) # read RSSI value an convert to signed integer DBG("RSSI: {}".format( int.from_bytes(data[43:44], byteorder='big', signed=True))) # read MFG as HEX from scanned data DBG("MFG RSV: {}".format(payload[(44 * 3):(45 * 3)])) # send scanned data to controller msg = IntMessage( IntMessage.SCANNED_ALTBEACON, { 'BID': payload[(23 * 3):(43 * 3)], 'RSSI': int.from_bytes(data[43:44], byteorder='big', signed=True), 'MFG': payload[(44 * 3):(45 * 3)] }) self._comm(msg) # Unknown BLE advertisement else: # DBG("Unknown BLE Adv") pass
def saveEvent2(): DBG("Someone is closing to No 2 !")
def stopgetsysbutton_clicked(self): DBG('stopgetsysbutton_clicked') msg = IntMessage(IntMessage.STOPGETSYSINFO) self._commcallback(msg)