class Barrier: """ Implement a barrier, such that threads block until other monitored threads reach a specific location. The barrier can be used multiple times (it is reinitialized after the threads passed). See https://stackoverflow.com/questions/9637374/qt-synchronization-barrier/9639624#9639624 """ def __init__(self, count): self.count = count self.origCount = count self.mutex = QMutex() self.condition = QWaitCondition() def wait(self): """ Wait until all monitored threads called wait. :return: None """ self.mutex.lock() self.count -= 1 if self.count > 0: self.condition.wait(self.mutex) else: self.count = self.origCount self.condition.wakeAll() self.mutex.unlock()
def __init__(self, parent=None): super(FortuneThread, self).__init__(parent) self.quit = False self.hostName = '' self.cond = QWaitCondition() self.mutex = QMutex() self.port = 0
class TestThread(QThread): startRecording = Signal() def __init__(self, config, aruco_tester_widget, acs_control, parent=None): QThread.__init__(self, parent) self.config = config self.aruco_tester_widget = aruco_tester_widget self.image_miner = aruco_tester_widget.image_miner self.acs_control = acs_control self.startRecording.connect(self.aruco_tester_widget.onRecordClicked) self.recordingStopped = QWaitCondition() self.mutex = QMutex() @Slot() def wake(self): self.recordingStopped.wakeAll() pass def run(self): print('starting...') base_dir = os.path.dirname(os.path.realpath(__file__)) config = self.config['config'] angles = self.config['angles'] j = 0 self.image_miner.set_video_capture_property('CAP_PROP_BRIGHTNESS', 50.5/100.) self.image_miner.set_video_capture_property('CAP_PROP_CONTRAST', 50.5/100.) self.image_miner.set_video_capture_property('CAP_PROP_SATURATION', 50.5/100.) prop = 'CAP_PROP_BRIGHTNESS' print(prop) start = config[prop]['start'] end = config[prop]['end'] step = config[prop]['step'] for i in range(start, end+1, step): print(i) self.image_miner.set_video_capture_property(prop, i/100.) series_dir = os.path.join(base_dir, str(prop), str(i), str(self.aruco_tester_widget.use_board)) j += 1 for angle in angles: fileName = os.path.join(series_dir, str(angle)) print(angle) self.acs_control.pa(angle) time.sleep(5) self.mutex.lock() self.startRecording.emit() print('wait for recording to stop') self.recordingStopped.wait(self.mutex) print('saving data to {}'.format(fileName)) self.aruco_tester_widget.broadcaster.saveToFile(fileName) self.mutex.unlock() self.acs_control.pa(0) time.sleep(10)
def __init__(self, config, aruco_tester_widget, acs_control, parent=None): QThread.__init__(self, parent) self.config = config self.aruco_tester_widget = aruco_tester_widget self.image_miner = aruco_tester_widget.image_miner self.acs_control = acs_control self.startRecording.connect(self.aruco_tester_widget.onRecordClicked) self.recordingStopped = QWaitCondition() self.mutex = QMutex()
def __init__(self, parent=None): super(RenderThread, self).__init__(parent) self.mutex = QMutex() self.condition = QWaitCondition() self.centerX = 0.0 self.centerY = 0.0 self.scaleFactor = 0.0 self.resultSize = QSize() self.colormap = [] self.restart = False self.abort = False for i in range(RenderThread.ColormapSize): self.colormap.append(self.rgbFromWaveLength(380.0 + (i * 400.0 / RenderThread.ColormapSize)))
def __init__(self, parent=None): super(FortuneThread, self).__init__(parent) self.quit = False self.hostName = '' self.cond = QWaitCondition() self.mutex = QMutex() self.port = 0
class SignalThread(QThread): def __init__(self, parent=None): QThread.__init__(self, parent) self.waitcond = QWaitCondition() self.mutex = QMutex() self.isstopped = False def trigger(self): """lock first to make sure the QThread is actually waiting for a signal""" self.mutex.lock() self.waitcond.wakeOne() self.mutex.unlock() def stopThread(self): self.mutex.lock() self.isstopped = True self.waitcond.wakeOne() self.mutex.unlock() def run(self): self.mutex.lock() while not self.isstopped: # just wait, and trigger every time we receive a signal self.waitcond.wait(self.mutex) if not self.isstopped: self.emit(SIGNAL("triggerSignal()")) self.mutex.unlock()
def __init__(self, parent=None): super().__init__(parent) self.__mutex = QMutex() self.__laserFileList: list = None self.__rowMargin: int = 0 self.__laserFTPAddress: str = "" self.__laserFTPPort: int = 0 self.__laserFTPRemotePath: str = "" self.__cameraPath: str = "" self.__cameraSendCsv: bool = True self.__localWaitTimeBeforeStart: int = 1 self.__localLoadingPath: str = "" self.__localDownloadingPath: str = "" self.__csvFilename: str = "" self.__errorFilename: str = "" self.__logFilename: str = "" self.__stopRequest: bool = False self.__pauseRequest: bool = False self.__waitCondition = QWaitCondition()
def __init__(self, parent=None): super(RenderThread, self).__init__(parent) self.mutex = QMutex() self.condition = QWaitCondition() self.centerX = 0.0 self.centerY = 0.0 self.scaleFactor = 0.0 self.resultSize = QSize() self.colormap = [] self.restart = False self.abort = False for i in range(RenderThread.ColormapSize): self.colormap.append(self.rgbFromWaveLength(380.0 + (i * 400.0 / RenderThread.ColormapSize)))
def __init__(self, parent=None): QThread.__init__(self, parent) self.waitcond = QWaitCondition() self.mutex = QMutex() self.isstopped = False
class RenderThread(QThread): ColormapSize = 512 renderedImage = Signal(QImage, float) def __init__(self, parent=None): super(RenderThread, self).__init__(parent) self.mutex = QMutex() self.condition = QWaitCondition() self.centerX = 0.0 self.centerY = 0.0 self.scaleFactor = 0.0 self.resultSize = QSize() self.colormap = [] self.restart = False self.abort = False for i in range(RenderThread.ColormapSize): self.colormap.append( self.rgbFromWaveLength(380.0 + (i * 400.0 / RenderThread.ColormapSize))) def stop(self): self.mutex.lock() self.abort = True self.condition.wakeOne() self.mutex.unlock() self.wait(2000) def render(self, centerX, centerY, scaleFactor, resultSize): locker = QMutexLocker(self.mutex) self.centerX = centerX self.centerY = centerY self.scaleFactor = scaleFactor self.resultSize = resultSize if not self.isRunning(): self.start(QThread.LowPriority) else: self.restart = True self.condition.wakeOne() def run(self): while True: self.mutex.lock() resultSize = self.resultSize scaleFactor = self.scaleFactor centerX = self.centerX centerY = self.centerY self.mutex.unlock() halfWidth = resultSize.width() // 2 halfHeight = resultSize.height() // 2 image = QImage(resultSize, QImage.Format_RGB32) NumPasses = 8 curpass = 0 while curpass < NumPasses: MaxIterations = (1 << (2 * curpass + 6)) + 32 Limit = 4 allBlack = True for y in range(-halfHeight, halfHeight): if self.restart: break if self.abort: return ay = 1j * (centerY + (y * scaleFactor)) for x in range(-halfWidth, halfWidth): c0 = centerX + (x * scaleFactor) + ay c = c0 numIterations = 0 while numIterations < MaxIterations: numIterations += 1 c = c * c + c0 if abs(c) >= Limit: break numIterations += 1 c = c * c + c0 if abs(c) >= Limit: break numIterations += 1 c = c * c + c0 if abs(c) >= Limit: break numIterations += 1 c = c * c + c0 if abs(c) >= Limit: break if numIterations < MaxIterations: image.setPixel( x + halfWidth, y + halfHeight, self.colormap[numIterations % RenderThread.ColormapSize]) allBlack = False else: image.setPixel(x + halfWidth, y + halfHeight, qRgb(0, 0, 0)) if allBlack and curpass == 0: curpass = 4 else: if not self.restart: self.renderedImage.emit(image, scaleFactor) curpass += 1 self.mutex.lock() if not self.restart: self.condition.wait(self.mutex) self.restart = False self.mutex.unlock() def rgbFromWaveLength(self, wave): r = 0.0 g = 0.0 b = 0.0 if wave >= 380.0 and wave <= 440.0: r = -1.0 * (wave - 440.0) / (440.0 - 380.0) b = 1.0 elif wave >= 440.0 and wave <= 490.0: g = (wave - 440.0) / (490.0 - 440.0) b = 1.0 elif wave >= 490.0 and wave <= 510.0: g = 1.0 b = -1.0 * (wave - 510.0) / (510.0 - 490.0) elif wave >= 510.0 and wave <= 580.0: r = (wave - 510.0) / (580.0 - 510.0) g = 1.0 elif wave >= 580.0 and wave <= 645.0: r = 1.0 g = -1.0 * (wave - 645.0) / (645.0 - 580.0) elif wave >= 645.0 and wave <= 780.0: r = 1.0 s = 1.0 if wave > 700.0: s = 0.3 + 0.7 * (780.0 - wave) / (780.0 - 700.0) elif wave < 420.0: s = 0.3 + 0.7 * (wave - 380.0) / (420.0 - 380.0) r = pow(r * s, 0.8) g = pow(g * s, 0.8) b = pow(b * s, 0.8) return qRgb(r * 255, g * 255, b * 255)
def __init__(self, count): self.count = count self.origCount = count self.mutex = QMutex() self.condition = QWaitCondition()
class RenderThread(QThread): ColormapSize = 512 renderedImage = Signal(QImage, float) def __init__(self, parent=None): super(RenderThread, self).__init__(parent) self.mutex = QMutex() self.condition = QWaitCondition() self.centerX = 0.0 self.centerY = 0.0 self.scaleFactor = 0.0 self.resultSize = QSize() self.colormap = [] self.restart = False self.abort = False for i in range(RenderThread.ColormapSize): self.colormap.append(self.rgbFromWaveLength(380.0 + (i * 400.0 / RenderThread.ColormapSize))) def stop(self): self.mutex.lock() self.abort = True self.condition.wakeOne() self.mutex.unlock() self.wait(2000) def render(self, centerX, centerY, scaleFactor, resultSize): locker = QMutexLocker(self.mutex) self.centerX = centerX self.centerY = centerY self.scaleFactor = scaleFactor self.resultSize = resultSize if not self.isRunning(): self.start(QThread.LowPriority) else: self.restart = True self.condition.wakeOne() def run(self): while True: self.mutex.lock() resultSize = self.resultSize scaleFactor = self.scaleFactor centerX = self.centerX centerY = self.centerY self.mutex.unlock() halfWidth = resultSize.width() // 2 halfHeight = resultSize.height() // 2 image = QImage(resultSize, QImage.Format_RGB32) NumPasses = 8 curpass = 0 while curpass < NumPasses: MaxIterations = (1 << (2 * curpass + 6)) + 32 Limit = 4 allBlack = True for y in range(-halfHeight, halfHeight): if self.restart: break if self.abort: return ay = 1j * (centerY + (y * scaleFactor)) for x in range(-halfWidth, halfWidth): c0 = centerX + (x * scaleFactor) + ay c = c0 numIterations = 0 while numIterations < MaxIterations: numIterations += 1 c = c*c + c0 if abs(c) >= Limit: break numIterations += 1 c = c*c + c0 if abs(c) >= Limit: break numIterations += 1 c = c*c + c0 if abs(c) >= Limit: break numIterations += 1 c = c*c + c0 if abs(c) >= Limit: break if numIterations < MaxIterations: image.setPixel(x + halfWidth, y + halfHeight, self.colormap[numIterations % RenderThread.ColormapSize]) allBlack = False else: image.setPixel(x + halfWidth, y + halfHeight, qRgb(0, 0, 0)) if allBlack and curpass == 0: curpass = 4 else: if not self.restart: self.renderedImage.emit(image, scaleFactor) curpass += 1 self.mutex.lock() if not self.restart: self.condition.wait(self.mutex) self.restart = False self.mutex.unlock() def rgbFromWaveLength(self, wave): r = 0.0 g = 0.0 b = 0.0 if wave >= 380.0 and wave <= 440.0: r = -1.0 * (wave - 440.0) / (440.0 - 380.0) b = 1.0 elif wave >= 440.0 and wave <= 490.0: g = (wave - 440.0) / (490.0 - 440.0) b = 1.0 elif wave >= 490.0 and wave <= 510.0: g = 1.0 b = -1.0 * (wave - 510.0) / (510.0 - 490.0) elif wave >= 510.0 and wave <= 580.0: r = (wave - 510.0) / (580.0 - 510.0) g = 1.0 elif wave >= 580.0 and wave <= 645.0: r = 1.0 g = -1.0 * (wave - 645.0) / (645.0 - 580.0) elif wave >= 645.0 and wave <= 780.0: r = 1.0 s = 1.0 if wave > 700.0: s = 0.3 + 0.7 * (780.0 - wave) / (780.0 - 700.0) elif wave < 420.0: s = 0.3 + 0.7 * (wave - 380.0) / (420.0 - 380.0) r = pow(r * s, 0.8) g = pow(g * s, 0.8) b = pow(b * s, 0.8) return qRgb(r*255, g*255, b*255)
class FortuneThread(QThread): newFortune = Signal(str) error = Signal(int, str) def __init__(self, parent=None): super(FortuneThread, self).__init__(parent) self.quit = False self.hostName = '' self.cond = QWaitCondition() self.mutex = QMutex() self.port = 0 def __del__(self): self.mutex.lock() self.quit = True self.cond.wakeOne() self.mutex.unlock() self.wait() def requestNewFortune(self, hostname, port): locker = QMutexLocker(self.mutex) self.hostName = hostname self.port = port if not self.isRunning(): self.start() else: self.cond.wakeOne() def run(self): self.mutex.lock() serverName = self.hostName serverPort = self.port self.mutex.unlock() while not self.quit: Timeout = 5 * 1000 socket = QTcpSocket() socket.connectToHost(serverName, serverPort) if not socket.waitForConnected(Timeout): self.error.emit(socket.error(), socket.errorString()) return while socket.bytesAvailable() < 2: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return instr = QDataStream(socket) instr.setVersion(QDataStream.Qt_4_0) blockSize = instr.readUInt16() while socket.bytesAvailable() < blockSize: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return self.mutex.lock() fortune = instr.readQString() self.newFortune.emit(fortune) self.cond.wait(self.mutex) serverName = self.hostName serverPort = self.port self.mutex.unlock()
class AutoAns(QThread): hdszgwslb = 'http://www.hdszgwslb.com' login_url = 'http://www.hdszgwslb.com/index/users/login' logout_url = 'http://www.hdszgwslb.com/index/users/logout' vercode_url = 'http://www.hdszgwslb.com/vercode/vercode.php?a=0.7732044146990473' check_login = '******' computer_url = 'http://www.hdszgwslb.com/index/cglb/gk/?id=780' # 计算机组的ID基本不会变 answers_url = 'http://www.hdszgwslb.com/index/cglb/ajaxdt?passid=%s&id=%s&answer=%s&_=1612179898881' finish_url = 'http://www.hdszgwslb.com/index/cglb/passresult?passid=%s&matchid=%s' pass_url = 'http://www.hdszgwslb.com/index/cglb/gk?id=780&passtype=71&matchid=%s' headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6', 'Accept-Encoding': 'gzip, deflate', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive', 'Host': 'www.hdszgwslb.com', 'Upgrade-Insecure-Requests': '1', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) \ Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56' } joint = '~' post_data = dict() m_question = dict() m_rands = [1.5,5.5] m_err_ans = '' m_working = False m_cond = QWaitCondition() m_mutex = QMutex() vercode_signal = Signal(bytes) def __init__(self): super(AutoAns, self).__init__() self.session = requests.Session() self.load_answer() # 加载题库 def load_answer(self): with open('answers.txt', encoding='utf-8') as f: for line in f: question,answer = line.strip('\n').split(self.joint) if (self.joint in question): print(question) question = self.trim_space(question) if (question in self.m_question): print(question) self.m_question[question] = answer.split(';') # get请求 def get_request(self, url, israndom=0): if (not israndom): times = self.gen_random() self.implicitly_wait(times) result = self.session.get(url, headers=self.headers) if (result.status_code != 200): return None return result # post请求 def post_request(self, url): times = self.gen_random() self.implicitly_wait(times) result = self.session.post(url, headers=self.headers, data=self.post_data) if (result.status_code != 200): return None return result # 打开登陆链接 def open_login(self): if (self.get_request(self.login_url,True)==None): return False return True # 获取验证码图片数据 def get_vercode(self): result = self.get_request(self.vercode_url,True) if (result == None): return False # with open('vercode.jpg','wb') as f: # f.write(result.content) self.vercode_signal.emit(result.content) return True def set_interval(self, min, max): self.m_rands[0] = min self.m_rands[1] = max # 设置随机时间,uniform为浮点随机数 def gen_random(self): return float(format(random.uniform(self.m_rands[0],self.m_rands[1]), '.1f')) # 等待 def implicitly_wait(self, times): tick = 0.1 click = 0 while(True): click += tick if (click >= times): break if (not self.m_working): break time.sleep(tick) # 将BOM头去掉 def filters_bom(self, text): json_text = text if json_text.startswith(u'\ufeff'): json_text = text.encode('utf8')[3:].decode('utf8') return json_text def trim_space(self, title_str): return title_str.replace(' ', '') # 登陆验证 def login(self): result = self.post_request(self.check_login) if (result == None): return False if result.cookies.get_dict() != {}: print("登陆成功!") json_text = self.filters_bom(result.text) statue_dic = json.loads(json_text) if (statue_dic.get('status') == 2): print(statue_dic.get('message')) return False return True def logout(self): self.get_request(self.logout_url, True) self.get_request(self.hdszgwslb, True) def extrac_url(self, list): url_list = [] for li in list: cglb = li.xpath('./@href')[0] url_list.append(self.hdszgwslb+cglb) return url_list def get_level(self, text): all_ans_url = [] dom = etree.HTML(text) yellow_list = dom.xpath('//li[@class="bg_yellow"]/a') if (yellow_list): all_ans_url.extend(self.extrac_url(yellow_list)) brown_list = dom.xpath('//li[@class="bg_brown"]/a') if (brown_list): all_ans_url.extend(self.extrac_url(brown_list)) return all_ans_url def get_ans_total(self, level_url): result = self.get_request(level_url) try: dom = etree.HTML(result.text) prog = dom.xpath('//span[@id="progress"]') count = prog[0].xpath('text()')[0].split('/')[1] except: print(result.text) count = 0 return count def record_title(self, data, ans): ques = data['question'] subject = self.trim_space(ques['title']) opt_list = ques['option'] self.m_question[subject] = [] for opt in opt_list: if (opt[0] in ans): self.m_question[subject].append(opt[1]) with open('answers.txt', 'ab+') as of: str_data = '%s%s%s\n'%(subject, self.joint, ';'.join(self.m_question[subject])) print(str_data) of.write(bytes(str_data,'utf-8')) def filter_except(self, data): if ('answer' in data.keys()): ans_str = data['answer'] # GetLog().info(self.m_err_ans) # GetLog().info('答案:%s'%(ans_str)) # GetLog().info('==============================\n') self.record_title(self.m_err_ans,ans_str) if (data['isright'] == -1): GetLog().info(data) self.set_interval(2.5, 7.5) return 0 if ('question' not in data.keys()): GetLog().info(data) return -1 return 1 def computer_group(self): result = self.get_request(self.computer_url) if (result == None): return False level_list = self.get_level(result.text) if (level_list == None): return False for u in level_list: if (not self.m_working): return -2 # 解析URL获取其中的字段 dest_str = urlparse(u) url_dic = dict(parse_qs(dest_str.query)) # print(url_dic) passid = url_dic['id'][0] matchid = url_dic['matchid'][0] ans_str = '0' total = int(self.get_ans_total(u)) if (total == 0): return -1 print(total) for i in range(total+1): if (not self.m_working): return -2 # 获取第一个题目 new_url = self.answers_url%(passid, matchid, ans_str) # print(new_url) # 尝试两次,如失败记录link退出 result = self.get_request(new_url) if (result == None): result = self.get_request(new_url) if (result == None): GetLog().info(new_url) return 0 json_text = self.filters_bom(result.text) question_dic = json.loads(json_text) ret = self.filter_except(question_dic) if (ret < 0): return -3 elif (ret == 0): continue # GetLog().info(question_dic) ques = question_dic['question'] subject = self.trim_space(ques['title']) # print('题目:%s'%(subject)) # print('选项:%s'%(ques['option'])) # print('类型:%s'%(ques['type'])) # 题库中存在该题 ans_str = '' if (subject in self.m_question.keys()): local_ans = self.m_question[subject] # print(local_ans) for ans in ques['option']: if (ans[1] in local_ans): ans_str += ans[0] else: # 在题库中未找到该题目 GetLog().info('not found anawer!\n\ttitle: %s'%(subject)) for ans in ques['option']: if (ans[1] == ''): continue ans_str += ans[0] break sort_ans = '' if (ques['type'] == '多选题'): for s in sorted(ans_str): sort_ans += s ans_str = sort_ans # print('选择:%s'%(ans_str)) ans_str = quote(ans_str, safe=';/?:@&=+$,', encoding='utf-8') matchid = ques['id'] self.m_err_ans = question_dic # 缓存当前答题的数据,如果答错则记录 print(i) finish_new = self.finish_url%(passid,'0') result = self.get_request(finish_new) print(result.text) if (result==None): return False def set_user_info(self, username, passwd, pin): self.post_data['username'] = username self.post_data['password'] = passwd self.post_data['vercode'] = pin self.m_cond.wakeOne() def wait_cond(self): self.m_mutex.lock() self.m_cond.wait(self.m_mutex) self.m_mutex.unlock() def starts(self): self.m_working = True self.start() def stops(self): self.m_mutex.lock() self.m_working = False self.m_mutex.unlock() def is_run(self): return self.m_working def run(self): setup = Setup.AllSetup while(self.m_working): if (setup & Setup.UserLogin): print('打开登陆页') if (not self.open_login()): continue setup = setup << 1 if (setup & Setup.GetVercode): print('获取验证码') if (not self.get_vercode()): continue self.wait_cond() if (setup & Setup.CheckLogin): print('用户登陆') if (not self.login()): continue setup = setup << 2 if (setup & Setup.ComputerGroup): print('开始答题') code = self.computer_group() if (code == -1): print('今天的闯关时间已用尽,请明天再来...') return if (code == -2): print('停止答题,并退出登录...') if (code == -3): print('有未知的答题集合') return if (code == 0): print('请求错误') self.logout()
class FortuneThread(QThread): newFortune = Signal(str) error = Signal(int, str) def __init__(self, parent=None): super(FortuneThread, self).__init__(parent) self.quit = False self.hostName = '' self.cond = QWaitCondition() self.mutex = QMutex() self.port = 0 def __del__(self): self.mutex.lock() self.quit = True self.cond.wakeOne() self.mutex.unlock() self.wait() def requestNewFortune(self, hostname, port): locker = QMutexLocker(self.mutex) self.hostName = hostname self.port = port if not self.isRunning(): self.start() else: self.cond.wakeOne() def run(self): self.mutex.lock() serverName = self.hostName serverPort = self.port self.mutex.unlock() while not self.quit: Timeout = 5 * 1000 socket = QTcpSocket() socket.connectToHost(serverName, serverPort) if not socket.waitForConnected(Timeout): self.error.emit(socket.error(), socket.errorString()) return while socket.bytesAvailable() < 2: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return instr = QDataStream(socket) instr.setVersion(QDataStream.Qt_4_0) blockSize = instr.readUInt16() while socket.bytesAvailable() < blockSize: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return self.mutex.lock() fortune = instr.readQString() self.newFortune.emit(fortune) self.cond.wait(self.mutex) serverName = self.hostName serverPort = self.port self.mutex.unlock()
class CSVRegenerator(QThread): cleanLocalFolderStepSignal = Signal(int) cleanCameraFolderStepSignal = Signal(int) renameLaserItemsStepSignal = Signal(int) downloadItemsFromLaserStepSignal = Signal(int) cleanLaserFolderStepSignal = Signal(int) csvBuildProcessStepSignal = Signal(int) sendCsvToLaserStepSignal = Signal(int) sendCsvToCameraStepSignal = Signal(int) exitCodeSignal = Signal(int) cvsNewFileEmptySignal = Signal(bool) threadPausedSignal = Signal(bool) def __init__(self, parent=None): super().__init__(parent) self.__mutex = QMutex() self.__laserFileList: list = None self.__rowMargin: int = 0 self.__laserFTPAddress: str = "" self.__laserFTPPort: int = 0 self.__laserFTPRemotePath: str = "" self.__cameraPath: str = "" self.__cameraSendCsv: bool = True self.__localWaitTimeBeforeStart: int = 1 self.__localLoadingPath: str = "" self.__localDownloadingPath: str = "" self.__csvFilename: str = "" self.__errorFilename: str = "" self.__logFilename: str = "" self.__stopRequest: bool = False self.__pauseRequest: bool = False self.__waitCondition = QWaitCondition() def setLaserFileList(self, fileList: list): locker = QMutexLocker(self.__mutex) self.__laserFileList = fileList def getLaserFileList(self): locker = QMutexLocker(self.__mutex) return self.__laserFileList def getRowMargin(self): return self.__rowMargin def setRowMargin(self, rowMargin: int): self.__rowMargin = rowMargin def setLaserConnectionParameters(self, ftpAddress, ftpPort, ftpRemotePath=""): locker = QMutexLocker(self.__mutex) self.__laserFTPAddress = ftpAddress self.__laserFTPPort = ftpPort self.__laserFTPRemotePath = ftpRemotePath def getLaserConnectionParameters(self): locker = QMutexLocker(self.__mutex) return self.__laserFTPAddress, self.__laserFTPPort, self.__laserFTPRemotePath def setCameraConnectionParameters(self, cameraPath: str): locker = QMutexLocker(self.__mutex) self.__cameraPath = cameraPath def getCameraConnectionParameters(self): locker = QMutexLocker(self.__mutex) return self.__cameraPath def getCameraSendCSV(self): locker = QMutexLocker(self.__mutex) return self.__cameraSendCsv def setCameraSendCSV(self, cameraSendCSV: bool): locker = QMutexLocker(self.__mutex) self.__cameraSendCsv = cameraSendCSV def getLocalWaitTimeBeforeStart(self): locker = QMutexLocker(self.__mutex) return self.__localWaitTimeBeforeStart def setLocalWaitTimeBeforeStart(self, waitTime: int): locker = QMutexLocker(self.__mutex) self.__localWaitTimeBeforeStart = waitTime def setLocalLoadingPath(self, path: str): locker = QMutexLocker(self.__mutex) self.__localLoadingPath = path def getLocalLoadingPath(self): locker = QMutexLocker(self.__mutex) return self.__localLoadingPath def setLocalDownloadingPath(self, path: str): locker = QMutexLocker(self.__mutex) self.__localDownloadingPath = path def getLocalDownloadingPath(self): locker = QMutexLocker(self.__mutex) return self.__localDownloadingPath def getCsvFilename(self): locker = QMutexLocker(self.__mutex) return self.__csvFilename def setCsvFilename(self, filename: str): locker = QMutexLocker(self.__mutex) self.__csvFilename = filename def getErrorFilename(self): locker = QMutexLocker(self.__mutex) return self.__errorFilename def setErrorFilename(self, filename: str): locker = QMutexLocker(self.__mutex) self.__errorFilename = filename def getLogFilename(self): locker = QMutexLocker(self.__mutex) return self.__logFilename def setLogFilename(self, filename: str): locker = QMutexLocker(self.__mutex) self.__logFilename = filename def getPauseRequest(self): locker = QMutexLocker(self.__mutex) return self.__pauseRequest def setPauseRequest(self, pause: bool): locker = QMutexLocker(self.__mutex) self.__pauseRequest = pause if not self.__pauseRequest: self.__waitCondition.notify_all() def getStopRequest(self): locker = QMutexLocker(self.__mutex) return self.__stopRequest def setStopRequest(self, stop: bool): locker = QMutexLocker(self.__mutex) self.__stopRequest = stop def connectFtp(self, ftpController) -> bool: isConnected: bool = False try: Logger().info("Connessione al laser in FTP") ftpController.connect() ftpController.login() isConnected = True except ftplib.all_errors as ftpErr: Logger().error("Errore connessione FTP: " + str(ftpErr)) ftpController.close() isConnected = False return isConnected def run(self): Logger().info("Start CSV Regenerator") self.cleanLocalFolderStepSignal.emit(CSVRegeneratorStepStatus.IDLE) self.cleanCameraFolderStepSignal.emit(CSVRegeneratorStepStatus.IDLE) self.renameLaserItemsStepSignal.emit(CSVRegeneratorStepStatus.IDLE) self.downloadItemsFromLaserStepSignal.emit( CSVRegeneratorStepStatus.IDLE) self.cleanLaserFolderStepSignal.emit(CSVRegeneratorStepStatus.IDLE) self.csvBuildProcessStepSignal.emit(CSVRegeneratorStepStatus.IDLE) self.sendCsvToLaserStepSignal.emit(CSVRegeneratorStepStatus.IDLE) self.sendCsvToCameraStepSignal.emit(CSVRegeneratorStepStatus.IDLE) self.cvsNewFileEmptySignal.emit(False) locker = QMutexLocker(self.__mutex) rowMargin = self.__rowMargin laserFileList = self.__laserFileList laserFTPAddress = self.__laserFTPAddress laserFTPPort = self.__laserFTPPort laserFTPRemotePath = self.__laserFTPRemotePath cameraPath = self.__cameraPath cameraSendCSV = self.__cameraSendCsv waitTimeBeforeStart = self.__localWaitTimeBeforeStart localDownloadingPath = self.__localDownloadingPath csvFilename = self.__csvFilename csvExpireFilename = self.__csvFilename + ".expire" errorFilename = self.__errorFilename logFilename = self.__logFilename csvNewFilename = self.__csvFilename + ".new" locker.unlock() Logger().debug("Laser FTP Address: " + laserFTPAddress) Logger().debug("Laser FTP Port: " + str(laserFTPPort)) Logger().debug("Laser FTP Remote Path: " + laserFTPRemotePath) Logger().debug("Camera Path: " + cameraPath) Logger().debug("Camera Send CSV: " + str(cameraSendCSV)) Logger().debug("Local Downloading Path: " + localDownloadingPath) Logger().debug("Csv Filename: " + csvFilename) Logger().debug("Error Filename: " + errorFilename) Logger().debug("Log Filename: " + logFilename) Logger().debug("Csv New Filename: " + csvNewFilename) Logger().info("Aspetto " + str(waitTimeBeforeStart) + " [s] prima di iniziare") self.sleep(waitTimeBeforeStart) Logger().info("Inizio il processo") # path csv e file di errore csvFileLocalPath = localDownloadingPath + "\\" + csvFilename csvNewFileLocalPath = localDownloadingPath + "\\" + csvNewFilename errorFileLocalPath = localDownloadingPath + "\\" + errorFilename localLogPath = localDownloadingPath + "\\log" csvCameraPath = cameraPath + "\\" + csvFilename # variabili locali itemFounded = 0 canContinue = False errorCode = ThreadExitCode.NO_ERROR stepToProcess = CSVRegeneratorStep.CLEAN_LOCAL_FOLDER # variabili connessione FTP ftpController = FTP() ftpController.host = laserFTPAddress ftpController.port = laserFTPPort isFtpConnected = False while stepToProcess != CSVRegeneratorStep.PROCESS_END: # controllo se posso continuare locker = QMutexLocker(self.__mutex) while self.__pauseRequest: self.threadPausedSignal.emit(True) self.__waitCondition.wait(self.__mutex) self.threadPausedSignal.emit(False) stopRequest = self.__stopRequest locker.unlock() if stopRequest: break # stopRequest = self.getStopRequest() # if stopRequest: # break if stepToProcess == CSVRegeneratorStep.CLEAN_LOCAL_FOLDER: Logger().info("Rimozione files nella cartella download locale") self.cleanLocalFolderStepSignal.emit( CSVRegeneratorStepStatus.IN_PROGRESS) try: if os.path.exists(csvFileLocalPath): Logger().info("Rimozione file: " + csvFileLocalPath) os.remove(csvFileLocalPath) if os.path.exists(csvNewFileLocalPath): Logger().info("Rimozione file: " + csvNewFileLocalPath) os.remove(csvNewFileLocalPath) if os.path.exists(errorFileLocalPath): Logger().info("Rimozione file: " + errorFileLocalPath) os.remove(errorFileLocalPath) except OSError as err: if err != errno.ENOENT: Logger().error("Rimozione file fallita, errore: " + str(err)) self.cleanLocalFolderStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) self.sleep(1) continue stepToProcess += 1 self.cleanLocalFolderStepSignal.emit( CSVRegeneratorStepStatus.ENDED_OK) Logger().info( "Rimozione files nella cartella download locale OK") elif stepToProcess == CSVRegeneratorStep.CLEAN_CAMERA_FOLDER: Logger().info("Rimozione file " + csvFilename + " dalla camera") self.cleanCameraFolderStepSignal.emit( CSVRegeneratorStepStatus.IN_PROGRESS) try: if os.path.exists(csvCameraPath): Logger().debug("Rimozione file .csv dalla camera") os.remove(csvCameraPath) except OSError as err: if err != errno.ENOENT: Logger().error("Rimozione file fallita") self.cleanCameraFolderStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) self.sleep(1) continue stepToProcess += 1 self.cleanCameraFolderStepSignal.emit( CSVRegeneratorStepStatus.ENDED_OK) Logger().info("Rimozione file " + csvFilename + " dalla camera OK") elif stepToProcess == CSVRegeneratorStep.RENAME_LASER_ITEMS: Logger().info("Rinomino file " + csvFilename + " in " + csvExpireFilename) self.renameLaserItemsStepSignal.emit( CSVRegeneratorStepStatus.IN_PROGRESS) if not isFtpConnected: isFtpConnected = self.connectFtp(ftpController) if not isFtpConnected: self.renameLaserItemsStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) self.sleep(1) continue try: Logger().info("Spostamento nella cartella FTP: " + laserFTPRemotePath) ftpController.cwd(laserFTPRemotePath) Logger().info("Rinomino il file " + csvFilename + " in " + csvExpireFilename) ftpController.rename(csvFilename, csvExpireFilename) except ftplib.all_errors as ftpErr: Logger().error("Errore connessione FTP: " + str(ftpErr)) self.renameLaserItemsStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) ftpController.close() isFtpConnected = False self.sleep(1) continue stepToProcess += 1 self.renameLaserItemsStepSignal.emit( CSVRegeneratorStepStatus.ENDED_OK) Logger().info("Rinomino file " + csvFilename + " in " + csvExpireFilename + " OK") elif stepToProcess == CSVRegeneratorStep.DOWNLOAD_ITEMS_FROM_LASER: Logger().info("Download file dal laser") self.downloadItemsFromLaserStepSignal.emit( CSVRegeneratorStepStatus.IN_PROGRESS) if not os.path.exists(localLogPath): os.mkdir(localLogPath) if not isFtpConnected: isFtpConnected = self.connectFtp(ftpController) if not isFtpConnected: self.downloadItemsFromLaserStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) self.sleep(1) continue try: ftpController.cwd(laserFTPRemotePath) # recupero il file lista.csv e lo elimino da ftp Logger().info("Download file: " + csvExpireFilename) cmd = "RETR " + csvExpireFilename Logger().debug("Comando: " + cmd) ftpController.retrbinary( cmd, open(csvFileLocalPath, 'wb').write) # recupero il file error.txt e lo elimino da ftp Logger().info("Download file: " + errorFilename) cmd = "RETR " + errorFilename Logger().debug("Comando: " + cmd) ftpController.retrbinary( cmd, open(errorFileLocalPath, 'wb').write) except ftplib.all_errors as ftpErr: Logger().error("Errore download file: " + str(ftpErr)) self.downloadItemsFromLaserStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) ftpController.close() isFtpConnected = False self.sleep(1) continue try: # recupero il file log ts = time.time() logFilenameTS = datetime.datetime.fromtimestamp( ts).strftime('%Y%m%d-%H%M%S') logFilenameTS = localLogPath + "\\" + logFilenameTS + ".log" Logger().info("Download file: " + logFilename) cmd = "RETR " + logFilename Logger().debug("Comando: " + cmd) ftpController.retrbinary(cmd, open(logFilenameTS, 'wb').write) except ftplib.all_errors as ftpErr: Logger().error("Errore download file: " + str(ftpErr)) ftpController.close() isFtpConnected = False stepToProcess += 1 self.downloadItemsFromLaserStepSignal.emit( CSVRegeneratorStepStatus.ENDED_OK) Logger().info("Download file dal laser OK") elif stepToProcess == CSVRegeneratorStep.CLEAN_LASER_FOLDER: Logger().info("Rimozione file cartella laser") self.cleanLaserFolderStepSignal.emit( CSVRegeneratorStepStatus.IN_PROGRESS) if not isFtpConnected: isFtpConnected = self.connectFtp(ftpController) if not isFtpConnected: self.cleanLaserFolderStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) self.sleep(1) continue try: ftpController.cwd(laserFTPRemotePath) Logger().info("Rimozione file: " + csvExpireFilename) ftpController.delete(csvExpireFilename) Logger().info("Rimozione file: " + errorFilename) ftpController.delete(errorFilename) except ftplib.all_errors as ftpErr: Logger().error("Errore rimozione file: " + str(ftpErr)) self.cleanLaserFolderStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) ftpController.close() isFtpConnected = False self.sleep(1) continue stepToProcess += 1 self.cleanLaserFolderStepSignal.emit( CSVRegeneratorStepStatus.ENDED_OK) Logger().info("Rimozione file cartella laser OK") elif stepToProcess == CSVRegeneratorStep.CSV_BUILD_PROCESS: Logger().info("Generazione nuovo file csv") self.csvBuildProcessStepSignal.emit( CSVRegeneratorStepStatus.IN_PROGRESS) # apro il file error in lettura per vedere l'indice dell'ultima stringa stampata fp = None try: Logger().info("Recupero informazioni dal file: " + errorFileLocalPath) fp = open(errorFileLocalPath, encoding="utf-8-sig", newline="\n") lineStr = fp.readline().rstrip("\n") lastPrintedString = fp.readline() Logger().debug("Line: " + str(lineStr)) Logger().debug("LastPrintedString: " + lastPrintedString) lineToSeek = int(lineStr) lineToSeek += rowMargin except OSError: Logger().error("Errore apertura file " + errorFileLocalPath) self.csvBuildProcessStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) fp.close() self.sleep(1) continue except ValueError: Logger().error("Errore conversione indice riga a intero") self.csvBuildProcessStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) fp.close() self.sleep(1) continue fp.close() # creo la lista di stringhe scartando quelle gia' stampate streamFile = None Logger().info("Lettura file locale " + csvFilename + " escludendo le stringhe stampate") try: fp = open(csvFileLocalPath, newline="\r\n", encoding="utf-8") for rowCounter in range(lineToSeek): readLine = fp.readline() if readLine == "": break streamFile = fp.read() except OSError: Logger().error("Errore apertura file: " + csvFileLocalPath) self.csvBuildProcessStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) fp.close() self.sleep(1) continue fp.close() # se file vuoto, allora ho finito, devo far caricare al cliente un nuovo file csv if streamFile == None or len(streamFile) == 0: Logger().info("Lista vuota") self.cvsNewFileEmptySignal.emit(True) stepToProcess = CSVRegeneratorStep.PROCESS_END self.csvBuildProcessStepSignal.emit( CSVRegeneratorStepStatus.ENDED_OK) continue # creo il nuovo file csv con la nuova lista Logger().info("Scrittura file " + csvNewFilename + " senza le stringhe stampate") try: fp = open(csvNewFileLocalPath, "wb") bytes2send = bytearray(streamFile, "utf-8") fp.write(bytes2send) except OSError: Logger().error("Errore apertura file: " + csvNewFileLocalPath) self.csvBuildProcessStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) fp.close() self.sleep(1) continue fp.close() stepToProcess += 1 self.csvBuildProcessStepSignal.emit( CSVRegeneratorStepStatus.ENDED_OK) Logger().info("Generazione nuovo file csv OK") elif stepToProcess == CSVRegeneratorStep.SEND_CSV_TO_LASER: Logger().info("Invio file " + csvNewFilename + " al laser") self.sendCsvToLaserStepSignal.emit( CSVRegeneratorStepStatus.IN_PROGRESS) if not isFtpConnected: isFtpConnected = self.connectFtp(ftpController) if not isFtpConnected: self.sendCsvToLaserStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) self.sleep(1) continue try: ftpController.cwd(laserFTPRemotePath) Logger().info("Upload file: " + csvFilename) cmd = "STOR " + csvFilename Logger().debug("Comando: " + cmd) fp = open(csvNewFileLocalPath, "rb") ftpController.storbinary(cmd, fp) except ftplib.all_errors as ftpErr: Logger().error("Error on FTP:" + str(ftpErr)) self.sendCsvToLaserStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) ftpController.close() isFtpConnected = False self.sleep(1) continue stepToProcess += 1 self.sendCsvToLaserStepSignal.emit( CSVRegeneratorStepStatus.ENDED_OK) Logger().info("Invio file " + csvNewFilename + " al laser OK") elif stepToProcess == CSVRegeneratorStep.SEND_CSV_TO_CAMERA: if cameraSendCSV: Logger().info("Invio nuovo file " + csvFilename + " alla camera") self.sendCsvToCameraStepSignal.emit( CSVRegeneratorStepStatus.IN_PROGRESS) try: shutil.copy(csvNewFileLocalPath, csvCameraPath) except: Logger().error( "Impossibile copiare il nuovo fil csv in camera: " + csvNewFileLocalPath) self.sendCsvToCameraStepSignal.emit( CSVRegeneratorStepStatus.ENDED_KO) self.sleep(1) continue self.sendCsvToCameraStepSignal.emit( CSVRegeneratorStepStatus.ENDED_OK) stepToProcess += 1 Logger().info("Invio nuovo file " + csvFilename + " alla camera OK") else: Logger().info("Invio nuovo file " + csvFilename + " alla camera non richiesto") stepToProcess += 1 self.sendCsvToCameraStepSignal.emit( CSVRegeneratorStepStatus.ENDED_OK) Logger().info("Processo terminato correttamente") if isFtpConnected: ftpController.close() self.exit(0)
class Communicator(QObject): resetHideTimer = Signal() showWindow = Signal() onPlayerReload = Signal() mutex = QMutex() waitCondition = QWaitCondition()