コード例 #1
0
    def __init__(self, driver, device, com, bps, timeout, com2, bps2, bytesize,
                 stopbits, Ret1, Ret2):
        self.device = device
        self.driver = driver
        self.Ret1 = Ret1
        self.Ret2 = Ret2
        #日志
        self.loger = logging.getLogger('test_maoyan.sub.module')  #多模块使用logging
        self.image_path = setting.TEST_ERROR_Image
        # 获取屏幕分辨率
        self.screen_x = self.driver.get_window_size()['width']  # 屏幕宽度--单位是px
        self.screen_y = self.driver.get_window_size()['height']  # 屏幕的高度-单位是px

        #选择设备
        self.ch_device = ChooesDevice(self.device, self.driver)
        self.wifi_connect = WifiConnect(self.driver, self.device, self.loger,
                                        self.screen_x, self.screen_y)

        # 打印串口
        self.port = com
        self.bps = bps
        self.timeout = timeout
        self.start = True

        # global Ret1
        try:
            # 打开串口,并得到串口对象
            self.main_engine = serial.Serial(self.port,
                                             self.bps,
                                             timeout=self.timeout)
            self.main_engine.flushInput()  # 清空缓冲区
            # 判断是否打开成功
            if (self.main_engine.is_open):
                self.Ret1 = True
        except Exception as e:
            print("---异常---:", e)
        # 控制串口
        self.port2 = com2
        self.bps2 = bps2
        self.bytesize = bytesize
        self.stopbits = stopbits
        # global Ret2
        try:
            # 打开串口,并得到串口对象
            self.main_engine2 = serial.Serial(self.port2,
                                              self.bps2,
                                              timeout=self.timeout,
                                              bytesize=self.bytesize,
                                              stopbits=self.stopbits)
            # 判断是否打开成功
            if (self.main_engine2.is_open):
                self.Ret2 = True
                # 初始化继电器类
                self.relay_h = RelayHandle(self.main_engine2)
        except Exception as e:
            print("---异常---:", e)
コード例 #2
0
class H6087MainHandle():
    dataList = ''

    def __init__(self, driver, device, com, bps, timeout, com2, bps2, bytesize,
                 stopbits, Ret1, Ret2):
        self.device = device
        self.driver = driver
        self.Ret1 = Ret1
        self.Ret2 = Ret2
        #日志
        self.loger = logging.getLogger(
            'H6087MainCase.sub.module')  #多模块使用logging
        # 获取屏幕分辨率
        self.screen_x = self.driver.get_window_size()['width']  # 屏幕宽度--单位是px
        self.screen_y = self.driver.get_window_size()['height']  # 屏幕的高度-单位是px
        self.swip = Swip(self.screen_x, self.screen_y, self.driver)
        #选择设备
        self.ch_device = ChooesDevice(self.device, self.driver)
        #打印串口
        self.port = com
        self.bps = bps
        self.timeout = timeout
        self.start = True
        #截图路径
        self.image_path = setting.TEST_ERROR_Image
        # global Ret1
        try:
            # 打开串口,并得到串口对象
            self.main_engine = serial.Serial(self.port,
                                             self.bps,
                                             timeout=self.timeout)
            self.main_engine.flushInput()  # 清空缓冲区
            # 判断是否打开成功
            if (self.main_engine.is_open):
                self.Ret1 = True
        except Exception as e:
            print("---异常---:", e)
        #控制串口
        self.port2 = com2
        self.bps2 = bps2
        self.bytesize = bytesize
        self.stopbits = stopbits
        # global Ret2
        try:
            # 打开串口,并得到串口对象
            self.main_engine2 = serial.Serial(self.port2,
                                              self.bps2,
                                              timeout=self.timeout,
                                              bytesize=self.bytesize,
                                              stopbits=self.stopbits)
            # 判断是否打开成功
            if (self.main_engine2.is_open):
                self.Ret2 = True
                #初始化继电器类
                self.relay_h = RelayHandle(self.main_engine2)
        except Exception as e:
            print("---异常---:", e)

    #设备配网
    def connect_H6087(self, skip_wifi, times):
        if (self.Ret1 and self.Ret2):
            #功能线程运行
            threading.Thread(target=self.add_device,
                             name='add_device',
                             args=(
                                 skip_wifi,
                                 times,
                             )).start()
            #日志线程运行
            threading.Thread(
                target=self.recv,
                name='recvData',
            ).start()
        else:
            print("创建串口失败")

    #模式切换
    def modeChange_H6087(self):
        if (self.Ret1 and self.Ret2):
            #功能线程运行
            threading.Thread(
                target=self.mode_change,
                name='mode_change',
            ).start()
            #日志线程运行
            threading.Thread(
                target=self.recv,
                name='recvData',
            ).start()
        else:
            print("创建串口失败")

    def mode_change(self):
        n = 0
        #进入设备详情页
        try:
            WebDriverWait(
                self.driver, 120, 1
            ).until(lambda x: x.find_element_by_android_uiautomator(
                'new UiSelector().text(\"%s\")' % self.device)).is_displayed()
        except:
            self.loger.info('没有找到该设备,请先添加设备')
        else:
            self.driver.find_element_by_android_uiautomator(
                'new UiSelector().text(\"%s\")' % self.device).click()
        #DIY模式切换
        WebDriverWait(self.driver, 20,
                      1).until(lambda x: x.find_element_by_android_uiautomator(
                          "new UiSelector().text(\"DIY\")")).click()
        #开始DIY模式循环切换
        while True:
            # try:
            #     #DIY-流水
            #     WebDriverWait(self.driver, 10, 1).until(
            #         lambda x: x.find_element_by_android_uiautomator(
            #             "new UiSelector().text(\"流水\")")).click()
            #     self.loger.info('切换流水diy模式')
            #     print('切换流水diy模式')
            #     sleep(3)
            #     # 关机
            #     self.relay_h.once_click()
            #     self.loger.info('物理按键,控制设备关机')
            #     print('物理按键,控制设备关机')
            #     sleep(3)
            #     # 开机
            #     self.relay_h.once_click()
            #     self.loger.info('物理按键,控制设备开机')
            #     print('物理按键,控制设备开机')
            #
            #     # DIY-灿烂
            #     WebDriverWait(self.driver, 10, 1).until(
            #         lambda x: x.find_element_by_android_uiautomator(
            #             "new UiSelector().text(\"灿烂\")")).click()
            #     self.loger.info('切换灿烂diy模式')
            #     print('切换灿烂diy模式')
            #     sleep(3)
            #     # 关机
            #     self.relay_h.once_click()
            #     self.loger.info('物理按键,控制设备关机')
            #     print('物理按键,控制设备关机')
            #     sleep(3)
            #     # 开机
            #     self.relay_h.once_click()
            #     self.loger.info('物理按键,控制设备开机')
            #     print('物理按键,控制设备开机')
            #
            #     # DIY-四季
            #     WebDriverWait(self.driver, 10, 1).until(
            #         lambda x: x.find_element_by_android_uiautomator(
            #             "new UiSelector().text(\"四季\")")).click()
            #     self.loger.info('切换四季diy模式')
            #     print('切换四季diy模式')
            #     sleep(3)
            #     # 关机
            #     self.relay_h.once_click()
            #     self.loger.info('物理按键,控制设备关机')
            #     print('物理按键,控制设备关机')
            #     sleep(3)
            #     # 开机
            #     self.relay_h.once_click()
            #     self.loger.info('物理按键,控制设备开机')
            #     print('物理按键,控制设备开机')
            #
            #     # DIY-摇摆
            #     WebDriverWait(self.driver, 10, 1).until(
            #         lambda x: x.find_element_by_android_uiautomator(
            #             "new UiSelector().text(\"摇摆\")")).click()
            #     self.loger.info('切换摇摆diy模式')
            #     print('切换摇摆diy模式')
            #     sleep(3)
            #     # 关机
            #     self.relay_h.once_click()
            #     self.loger.info('物理按键,控制设备关机')
            #     print('物理按键,控制设备关机')
            #     sleep(3)
            #     # 开机
            #     self.relay_h.once_click()
            #     self.loger.info('物理按键,控制设备开机')
            #     print('物理按键,控制设备开机')
            #
            #     n += 1
            #     self.loger.info('第[%s]循环次切换DIY模式结束' % n)
            #     print('第[%s]循环次切换DIY模式结束' % n)
            # except Exception as e:
            #     self.loger.info(e)
            #     self.loger.info('DIY模式切换失败')
            #     print('DIY模式切换失败')
            try:
                # 音乐模式
                try:
                    WebDriverWait(self.driver, 60, 0.2).until(
                        EC.element_to_be_clickable((By.ID, 'mode_1_icon')))
                except:
                    self.loger.info('蓝牙连接失败')
                    self.screenshot('蓝牙连接失败')
                    self.driver.find_element_by_android_uiautomator(
                        "new UiSelector().text(\"重新连接\")").click()
                else:
                    WebDriverWait(self.driver, 10).until(
                        lambda x: x.find_element_by_id("mode_1_icon")).click()
                    if self.check_ble_status():
                        self.loger.info('切换音乐模式后,蓝牙连接正常')
                        sleep(2)
                        self.loger.info('再次物理点击颜色按键')
                        self.relay_h.once_click()
                    else:
                        self.loger.info('切换音乐模式后,蓝牙断连')
                        self.screenshot('切换音乐模式后,蓝牙断连')
                        WebDriverWait(self.driver, 60, 0.5).until(
                            lambda x: x.find_element_by_android_uiautomator(
                                "new UiSelector().text(\"重新连接\")")).click()
                sleep(3)
                # 颜色模式
                try:
                    WebDriverWait(self.driver, 60, 0.2).until(
                        EC.element_to_be_clickable((By.ID, 'mode_2_icon')))
                except:
                    self.loger.info('蓝牙连接失败')
                    self.screenshot('蓝牙连接失败')
                    self.driver.find_element_by_android_uiautomator(
                        "new UiSelector().text(\"重新连接\")").click()
                else:
                    WebDriverWait(self.driver, 10).until(
                        lambda x: x.find_element_by_id("mode_2_icon")).click()
                    if self.check_ble_status():
                        self.loger.info('切换颜色模式后,蓝牙连接正常')
                        sleep(2)
                        self.loger.info('再次物理点击颜色按键')
                        self.relay_h.once_click()
                    else:
                        self.loger.info('切换颜色模式后,蓝牙断连')
                        self.screenshot('切换颜色模式后,蓝牙断连')
                        WebDriverWait(self.driver, 60, 0.5).until(
                            lambda x: x.find_element_by_android_uiautomator(
                                "new UiSelector().text(\"重新连接\")")).click()
                sleep(3)
                # 场景模式
                try:
                    WebDriverWait(self.driver, 60, 0.2).until(
                        EC.element_to_be_clickable((By.ID, 'mode_3_icon')))
                except:
                    self.loger.info('蓝牙连接失败')
                    self.screenshot('蓝牙连接失败')
                    self.driver.find_element_by_android_uiautomator(
                        "new UiSelector().text(\"重新连接\")").click()
                else:
                    WebDriverWait(self.driver, 10).until(
                        lambda x: x.find_element_by_id("mode_3_icon")).click()
                    if self.check_ble_status():
                        self.loger.info('切换场景模式后,蓝牙连接正常')
                        sleep(2)
                        self.loger.info('再次物理点击颜色按键')
                        self.relay_h.once_click()
                    else:
                        self.loger.info('切换场景模式后,蓝牙断连')
                        self.screenshot('切换场景模式后,蓝牙断连')
                        WebDriverWait(self.driver, 60, 0.5).until(
                            lambda x: x.find_element_by_android_uiautomator(
                                "new UiSelector().text(\"重新连接\")")).click()
                # DIY模式
                sleep(3)
                try:
                    WebDriverWait(self.driver, 60, 0.2).until(
                        EC.element_to_be_clickable((By.ID, 'mode_4_icon')))
                except:
                    self.loger.info('蓝牙连接失败')
                    self.screenshot('蓝牙连接失败')
                    self.driver.find_element_by_android_uiautomator(
                        "new UiSelector().text(\"重新连接\")").click()
                else:
                    WebDriverWait(self.driver, 10).until(
                        lambda x: x.find_element_by_id("mode_4_icon")).click()
                    if self.check_ble_status():
                        self.loger.info('切换DIY模式后,蓝牙连接正常')
                        # self.loger.info('----------------------------------')
                        sleep(2)
                        self.loger.info('再次物理点击颜色按键')
                        self.relay_h.once_click()
                    else:
                        self.loger.info('切换DIY模式后,蓝牙断连')
                        self.screenshot('切换DIY模式后,蓝牙断连')
                        WebDriverWait(self.driver, 60, 0.5).until(
                            lambda x: x.find_element_by_android_uiautomator(
                                "new UiSelector().text(\"重新连接\")")).click()
            except Exception as e:
                self.loger.info(e)
                self.loger.info('模式切换失败')
                print('模式切换失败')

    #检查蓝牙状态
    def check_ble_status(self):
        try:
            WebDriverWait(
                self.driver, 5,
                0.5).until(lambda x: x.find_element_by_android_uiautomator(
                    "new UiSelector().text(\"已连接\")")).is_displayed()
        except:
            return False
        else:
            return True

    #设备配网
    #1、次数  2、是否跳过WiFi设置
    def add_device(self, skip_wifi, times):
        result = [0, 0]
        m = 0
        app_check = False
        com_check = False
        for i in range(times):
            # 选择设备类型
            try:
                self.ch_device.chooes_device()
                m += 1
                # 扫描蓝牙
                while True:
                    try:
                        WebDriverWait(self.driver, 120, 1).until(
                            lambda x: x.find_element_by_android_uiautomator(
                                'new UiSelector().text(\"%s\")' % self.device)
                        ).is_displayed()
                    except:
                        self.loger.info('蓝牙扫描失败,重新扫描')
                        self.screenshot('蓝牙扫描失败')
                        try:
                            WebDriverWait(
                                self.driver, 60,
                                1).until(lambda x: x.
                                         find_element_by_android_uiautomator(
                                             "new UiSelector().text(\"重新扫描\")")
                                         ).is_displayed()
                        except:
                            self.loger.info('重新扫描按钮不可点击')
                        else:
                            self.loger.info('点击重新扫描按钮')
                            self.driver.find_element_by_android_uiautomator(
                                "new UiSelector().text(\"重新扫描\")").click()
                    else:
                        self.loger.info('蓝牙扫描成功')
                        sleep(3)
                        ele = self.driver.find_element_by_android_uiautomator(
                            'new UiSelector().text(\"%s\")' % self.device)
                        sleep(1)
                        ele.click()
                        break
                # 进入配对界面,点击设备开关按键
                try:
                    WebDriverWait(self.driver, 60, 1).until(
                        lambda x: x.find_element_by_android_uiautomator(
                            "new UiSelector().text(\"配对\")")).is_displayed()
                except:
                    self.loger.info('进入蓝牙配对界面失败')
                    # 蓝牙没有连接成功时,重新连接,直到成功
                    while True:
                        try:
                            WebDriverWait(
                                self.driver, 10,
                                1).until(lambda x: x.
                                         find_element_by_android_uiautomator(
                                             'new UiSelector().text(\"%s\")' %
                                             self.device)).is_displayed()
                        except:
                            self.loger.info('连接成功')
                            break
                        else:
                            self.loger.info('重新连接')
                            self.driver.find_element_by_android_uiautomator(
                                'new UiSelector().text(\"%s\")' %
                                self.device).click()
                else:
                    self.loger.info('进入蓝牙配对界面成功')
                result = self.connect_ble_wifi(skip_wifi, result[0], result[1])
            except Exception as e:
                print(e)
                self.loger.info(e)
                continue
        self.start = False
        self.sum = 0
        if skip_wifi:
            self.sum = int(result[1])
        else:
            self.sum = int(result[0])
        print('[%s]' % self.device + '设备配网[%s]次,' % m +
              '成功了[%s]次,' % self.sum + ',配网成功率%s' % (self.sum / m * 100) + '%')
        self.loger.info('[%s]' % self.device + '设备配网[%s]次,' % m +
                        '成功了[%s]次,' % self.sum + ',配网成功率%s' %
                        (self.sum / m * 100) + '%')

    def connect_ble_wifi(self, skip_wifi, n, k):
        result_list = []
        # 等待时间
        wait_time = random.randint(1, 25)  #随机生成一个[1,25]之间的整数,包括1、25
        sleep(wait_time)
        print('等待了%s秒' % wait_time)
        self.loger.info('等待了%s秒' % wait_time)
        self.relay_h.once_click()
        # 蓝牙连接成功,点击完成
        try:
            WebDriverWait(
                self.driver, 80,
                1).until(lambda x: x.find_element_by_android_uiautomator(
                    "new UiSelector().text(\"完成\")")).is_displayed()
        except:
            self.loger.info('蓝牙配对失败')
            self.screenshot('蓝牙配对失败')
            print('蓝牙配对失败')
            while True:
                try:
                    WebDriverWait(self.driver, 80, 1).until(
                        lambda x: x.find_element_by_android_uiautomator(
                            "new UiSelector().text(\"完成\")")).is_displayed()
                except:
                    self.loger.info('再次配对')
                    self.driver.find_element_by_android_uiautomator(
                        'new UiSelector().text(\"%s\")' % self.device).click()
                    sleep(2)
                    self.relay_h.once_click()
                else:
                    self.loger.info('再次配对成功')
                    break
        else:
            # 点击完成
            self.loger.info('蓝牙配对成功')
            print('蓝牙配对成功')
        self.driver.find_element_by_android_uiautomator(
            "new UiSelector().text(\"完成\")").click()
        # 进入WiFi配置界面
        try:
            WebDriverWait(
                self.driver, 120,
                1).until(lambda x: x.find_element_by_android_uiautomator(
                    "new UiSelector().text(\"完成\")")).is_displayed()
        except:
            self.loger.info('wifi设置页面加载失败,跳过WiFi设置')
            print('wifi设置页面加载失败,跳过WiFi设置')
            self.screenshot('wifi设置页面加载失败')
            # 跳过WiFi设置
            self.skip_wifiSetting()
            #进入设备详情页
            # self.enter_gateway_detial()
            # 删除设备
            self.delete_lempDevice()
        else:
            # 是否跳过WiFi设置
            if skip_wifi:
                self.loger.info('跳过WiFi设置')
                print('跳过WiFi设置')
                # 跳过WiFi设置
                self.skip_wifiSetting()
                k += 1
            else:
                # 判断设备是否添加成功
                self.driver.find_element_by_android_uiautomator(
                    "new UiSelector().text(\"完成\")").click()
                try:
                    WebDriverWait(self.driver, 180, 0.1).until(
                        EC.presence_of_element_located(
                            (By.XPATH, '//*[contains(@text,"连接成功")]')))
                except:
                    print('WiFi连接失败')
                    self.loger.info('WiFi连接失败')
                    self.screenshot('WiFi连接失败')
                    # continue
                    # wifi连接失败时点击跳过
                    self.skip_wifiSetting()
                else:
                    n += 1
                    print('WiFi连接成功')
                    self.loger.info('WiFi连接成功')
                    print('第[%s]次' % n + '配网成功')
                    self.loger.info('第[%s]次' % n + '配网成功')
                    sleep(1)
                    # 点击确定
                    # self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"确定\")").click()
            result_list.clear()
            result_list.append(n)
            result_list.append(k)
            # 删除设备
            self.delete_lempDevice()
            return result_list

    # 删除设备
    def delete_lempDevice(self):
        # btn_settting = self.driver.find_element_by_id("btn_setting")
        try:
            WebDriverWait(
                self.driver,
                10).until(lambda x: x.find_element_by_id("btn_setting"))
        except:
            print('没有设置按钮')
            self.loger.info('没有设置按钮')
        else:
            sleep(1)
            self.driver.find_element_by_id("btn_setting").click()
        sleep(2)
        # 向下滑动
        self.swip.swipe_up(600)
        self.driver.find_element_by_android_uiautomator(
            "new UiSelector().text(\"删除设备\")").click()
        sleep(1)
        yes = self.driver.find_element_by_android_uiautomator(
            "new UiSelector().text(\"是\")")
        yes.click()
        sleep(1)
        print('删除设备')
        self.loger.info('删除设备')
        # 刷新首页
        self.swip.swipe_down(600)

    #跳过WiFi设置
    def skip_wifiSetting(self):
        sleep(1)
        self.driver.find_element_by_android_uiautomator(
            "new UiSelector().text(\"跳过\")").click()
        sleep(1)
        self.driver.find_element_by_android_uiautomator(
            "new UiSelector().text(\"确认\")").click()
        sleep(1)
        # # 点击确定
        # self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"确定\")").click()

    # 监测蓝牙配对日志
    def com_bluetooth_adddevice_log_check(self, dataList):
        start = len(dataList) - 2500
        end = len(dataList)
        str1 = dataList[start:end]
        str2 = str1.replace(" ", "").replace("\r",
                                             "").replace("\n",
                                                         "")  # 去掉空格、回车符、换行符
        strlist = str2.split(':', 50)  # 以冒号进行分割
        # 打印串口日志
        self.loger.info('配网日志:' + str(strlist))
        print('配网日志:' + str(strlist))
        sl = '33b200'
        if str(strlist).find(sl) >= 0:
            self.loger.info('本次配网日志校验成功')
            print('本次配网日志校验成功')
            return True
        else:
            print('本次配网日志校验失败')
            self.loger.info('本次配网日志校验失败')
            return False

    #监测蓝牙控制日志
    def com_bluetooth_normal_log_check(self,
                                       dataList,
                                       mlen,
                                       option_type,
                                       sl=None):
        start = len(dataList) - mlen
        end = len(dataList)
        str1 = dataList[start:end]
        str2 = str1.replace(" ", "").replace("\r",
                                             "").replace("\n",
                                                         "")  # 去掉空格、回车符、换行符
        strlist = str2.split(':', 50)  # 以冒号进行分割
        #打印串口日志
        self.loger.info('本次操作后蓝牙日志:' + str(strlist))
        print('本次操作后蓝牙日志:' + str(strlist))
        if option_type == 'mode_switch':
            for i in range(len(strlist)):
                sl = strlist[i][len(strlist[i]) - 7:len(strlist[i])]
                if sl == 'blesend':
                    print('blesend:' + strlist[i + 1])
                    self.loger.info('蓝牙回复结果:blesend:' + str(strlist[i + 1]))
                    if strlist[i + 1][0] == '3' and strlist[
                            i + 1][4] == '0' and strlist[i + 1][5] == '0':
                        self.loger.info('本次蓝牙下操作成功')
                        print('本次蓝牙下操作成功')
                        return True
                    else:
                        self.loger.info('本次蓝牙下操作失败')
                        print('本次蓝牙下操作失败')
                        return False
        else:
            check = False
            for i in range(len(strlist)):
                if str(strlist[i]).find(sl) >= 0:
                    self.loger.info(strlist[i])
                    print(strlist[i])
                    check = True
                    break
                else:
                    check = False
            return check

    #监测普通WiFi日志
    def com_wifi_normal_log_check(self, dataList, sl, option_type):
        if option_type == 'on_off_switch':
            start = len(dataList) - 3400
        elif option_type == 'leve_switch':
            start = len(dataList) - 1400
        else:
            start = len(dataList) - 2200
        end = len(dataList)
        str1 = dataList[start:end]
        str2 = str1.replace(" ", "").replace("\r",
                                             "").replace("\n",
                                                         "")  # 去掉空格、回车符、换行符
        strlist = str2.split(':', 50)  # 以冒号进行分割
        # 打印串口日志
        self.loger.info('本次操作后日志:' + str(strlist))
        for i in range(len(strlist)):
            if strlist[i].find(sl) >= 0:
                print('串口日志:' + strlist[i])
                self.loger.info('串口日志:' + strlist[i])
                return strlist[i]

    # 监测WiFi配网日志
    def com_wifi_adddevice_log_check(self, dataList):
        start = len(dataList) - 8000
        end = len(dataList)
        str1 = dataList[start:end]
        str2 = str1.replace(" ", "").replace("\r",
                                             "").replace("\n",
                                                         "")  # 去掉空格、回车符、换行符
        strlist = str2.split(':', 80)  # 以冒号进行分割
        # 打印串口日志
        self.loger.info('wifi联网日志:' + str(strlist))
        print('wifi联网日志:' + str(strlist))
        sl = 'IOT_BLE_WIFI_CONNECT_SUC'
        if str(strlist).find(sl) >= 0:
            print('本次WiFi连接成功')
            return True
        else:
            print('本次WiFi连接失败')
            return False

    # 接收串口数据
    def recv(self):
        if self.start:
            print("开始接收数据:")
        else:
            print("测试完毕,停止接收数据:")
        while self.start:
            try:
                count = self.main_engine.inWaiting()  # 获取串口缓冲区数据
                if count != 0:
                    data = self.main_engine.read(
                        self.main_engine.in_waiting).decode(
                            "gbk")  # 读出串口数据,数据采用gbk编码
                    # print(time.strftime("%Y-%m-%d %H-%M-%S"), " --- recv --> ")  # 打印一下子
                    time.sleep(0.1)  # 延时0.1秒,免得CPU出问题
                    self.dataList += data
                    data = data.replace("\r", "")
                    self.loger.info(data)
            except Exception as e:
                print("异常报错:", e)

    # 截图
    def screenshot(self, msg):
        # 截图保存
        now = time.strftime("%Y-%m-%d %H-%M-%S")
        image_name = self.image_path + '/' + msg + " " + now + ".png"
        self.driver.save_screenshot(image_name)


#
#
# if __name__ == '__main__':
#     platformVersion = '10'   #安卓系统版本
#     mobileName = 'R58M36MWZ1B'   #手机名称
#     device = 'H7130_C583'  #设备名称
#     times = 100  #测试次数
#     Ret1 = False  #日志打印串口是否创建成功标志
#     Ret2 = False  #控制串口是否创建成功标志
#     com1 = 'com13'
#     bps = 115200
#     timeout = 0.5
#     com2 = "com12"
#     bps2 = 9600
#     timex = 0.5
#     bytesize = 8
#     stopbits = 1
#     ms = Humidifier2(platformVersion,mobileName,device,com1, bps, timeout,com2,bps2,bytesize,stopbits)
#     if(Ret1 and Ret2):
#         #功能线程运行
#         # threading.Thread(target=ms.add_device,args=(times,)).start()
#         # threading.Thread(target=ms.leve_switch, args=(times,)).start()
#         threading.Thread(target=ms.on_off_switch, args=(times,)).start()
#         #日志线程运行
#         threading.Thread(target=ms.recv,).start()
#         # ms.add_device(times)
#     else:
#         print("创建串口失败")
コード例 #3
0
class TestMaoYanHandle(object):
    dataList = ''

    @classmethod
    def __init__(self, driver, device, com, bps, timeout, com2, bps2, bytesize,
                 stopbits, Ret1, Ret2):
        self.device = device
        self.driver = driver
        self.Ret1 = Ret1
        self.Ret2 = Ret2
        #日志
        self.loger = logging.getLogger('test_maoyan.sub.module')  #多模块使用logging
        self.image_path = setting.TEST_ERROR_Image
        # 获取屏幕分辨率
        self.screen_x = self.driver.get_window_size()['width']  # 屏幕宽度--单位是px
        self.screen_y = self.driver.get_window_size()['height']  # 屏幕的高度-单位是px

        #选择设备
        self.ch_device = ChooesDevice(self.device, self.driver)
        self.wifi_connect = WifiConnect(self.driver, self.device, self.loger,
                                        self.screen_x, self.screen_y)

        # 打印串口
        self.port = com
        self.bps = bps
        self.timeout = timeout
        self.start = True

        # global Ret1
        try:
            # 打开串口,并得到串口对象
            self.main_engine = serial.Serial(self.port,
                                             self.bps,
                                             timeout=self.timeout)
            self.main_engine.flushInput()  # 清空缓冲区
            # 判断是否打开成功
            if (self.main_engine.is_open):
                self.Ret1 = True
        except Exception as e:
            print("---异常---:", e)
        # 控制串口
        self.port2 = com2
        self.bps2 = bps2
        self.bytesize = bytesize
        self.stopbits = stopbits
        # global Ret2
        try:
            # 打开串口,并得到串口对象
            self.main_engine2 = serial.Serial(self.port2,
                                              self.bps2,
                                              timeout=self.timeout,
                                              bytesize=self.bytesize,
                                              stopbits=self.stopbits)
            # 判断是否打开成功
            if (self.main_engine2.is_open):
                self.Ret2 = True
                # 初始化继电器类
                self.relay_h = RelayHandle(self.main_engine2)
        except Exception as e:
            print("---异常---:", e)

    #设备配网
    def connect_device(self, skip_wifi, times):
        if (self.Ret1 and self.Ret2):
            self.add_device(skip_wifi, times)
        else:
            print("创建串口失败")

    # 设备配网
    # 1、次数  2、是否跳过WiFi设置
    def add_device(self, skip_wifi, times):
        ble_scan_device = BleScanConnect(self.driver, self.device, self.loger,
                                         self.relay_h)
        # 扫描设备
        result = [0, 0]
        m = 0
        app_check = False
        com_check = False
        for i in range(times):
            try:
                # 选择设备类型
                self.ch_device.chooes_device()
                m += 1
                #扫描蓝牙并连接
                ble_scan_device.ble_scan_connect()
                result = self.wifi_connect.wifi_connect(
                    skip_wifi, result[0], result[1])
            except Exception as e:
                print(e)
                self.loger.info(e)
                continue
        self.start = False
        self.sum = 0
        if skip_wifi:
            self.sum = int(result[1])
        else:
            self.sum = int(result[0])
        print('[%s]' % self.device + '设备配网[%s]次,' % m +
              '成功了[%s]次,' % self.sum + ',配网成功率%s' % (self.sum / m * 100) + '%')
        self.loger.info('[%s]' % self.device + '设备配网[%s]次,' % m +
                        '成功了[%s]次,' % self.sum + ',配网成功率%s' %
                        (self.sum / m * 100) + '%')

    # 截图
    def screenshot(self, msg):
        # 截图保存
        now = time.strftime("%Y-%m-%d %H-%M-%S")
        image_name = self.image_path + '/' + msg + " " + now + ".png"
        self.driver.save_screenshot(image_name)

    # 接收串口数据
    def recv(self):
        if self.start:
            print("开始接收数据:")
        else:
            print("测试完毕,停止接收数据:")
        while self.start:
            try:
                count = self.main_engine.inWaiting()  # 获取串口缓冲区数据
                if count != 0:
                    data = self.main_engine.read(
                        self.main_engine.in_waiting).decode(
                            "gbk")  # 读出串口数据,数据采用gbk编码
                    # print(time.strftime("%Y-%m-%d %H-%M-%S"), " --- recv --> ")  # 打印一下子
                    time.sleep(0.1)  # 延时0.1秒,免得CPU出问题
                    self.dataList += data
            except Exception as e:
                print("异常报错:", e)
コード例 #4
0
ファイル: H6058MainHandle.py プロジェクト: CJH01/PythonAPPUI
class H6058MainHandle():
    dataList = ''
    def __init__(self,driver,device, com, bps, timeout,com2,bps2,bytesize,stopbits,Ret1,Ret2):
        self.device = device
        self.driver = driver
        self.Ret1 = Ret1
        self.Ret2 = Ret2
        #日志
        self.loger = logging.getLogger('H6058MainCase.sub.module')  #多模块使用logging
        # 获取屏幕分辨率
        self.screen_x = self.driver.get_window_size()['width']  # 屏幕宽度--单位是px
        self.screen_y = self.driver.get_window_size()['height']  # 屏幕的高度-单位是px

        #选择设备
        self.ch_device = ChooesDevice(self.device,self.driver)
        self.wifi_connect =  WifiConnect(self.driver, self.device, self.loger,self.screen_x,self.screen_y)
        #打印串口
        self.port = com
        self.bps = bps
        self.timeout = timeout
        self.start = True

        # global Ret1
        try:
            # 打开串口,并得到串口对象
            self.main_engine = serial.Serial(self.port, self.bps, timeout=self.timeout)
            self.main_engine.flushInput()  # 清空缓冲区
            # 判断是否打开成功
            if (self.main_engine.is_open):
                self.Ret1 = True
        except Exception as e:
            print("---异常---:", e)
        #控制串口
        self.port2 = com2
        self.bps2 = bps2
        self.bytesize = bytesize
        self.stopbits =  stopbits
        # global Ret2
        try:
            # 打开串口,并得到串口对象
             self.main_engine2= serial.Serial(self.port2,self.bps2,timeout=self.timeout,bytesize=self.bytesize,stopbits=self.stopbits)
            # 判断是否打开成功
             if (self.main_engine2.is_open):
                self.Ret2 = True
                #初始化继电器类
                self.relay_h = RelayHandle(self.main_engine2)
        except Exception as e:
            print("---异常---:", e)

    #设备配网
    def connect_H6058(self,skip_wifi,times):
        if(self.Ret1 and self.Ret2):
                #功能线程运行
                threading.Thread(target=self.add_device,name= 'add_device', args=(skip_wifi,times,)).start()
                #日志线程运行
                threading.Thread(target=self.recv,name= 'recvData',).start()
        else:
            print("创建串口失败")

    #模式切换
    def modeChange_H6058(self):
        if(self.Ret1 and self.Ret2):
                #功能线程运行
                threading.Thread(target=self.mode_change,name= 'mode_change',).start()
                #日志线程运行
                threading.Thread(target=self.recv,name= 'recvData',).start()
        else:
            print("创建串口失败")

    def mode_change(self):
        n = 0
        #进入设备详情页
        try:
            WebDriverWait(self.driver, 120, 1).until(
                lambda x: x.find_element_by_android_uiautomator(
                    'new UiSelector().text(\"%s\")' % self.device)).is_displayed()
        except:
            self.loger.info('没有找到该设备,请先添加设备')
        else:
            self.driver.find_element_by_android_uiautomator('new UiSelector().text(\"%s\")' % self.device).click()
        #DIY模式切换
        WebDriverWait(self.driver, 20, 1).until(
            lambda x: x.find_element_by_android_uiautomator(
                "new UiSelector().text(\"DIY\")")).click()
        #开始DIY模式循环切换
        while True:
            try:
                #DIY-流水
                WebDriverWait(self.driver, 10, 1).until(
                    lambda x: x.find_element_by_android_uiautomator(
                        "new UiSelector().text(\"流水\")")).click()
                self.loger.info('切换流水diy模式')
                print('切换流水diy模式')
                sleep(3)
                # 关机
                self.relay_h.once_click()
                self.loger.info('物理按键,控制设备关机')
                print('物理按键,控制设备关机')
                sleep(3)
                # 开机
                self.relay_h.once_click()
                self.loger.info('物理按键,控制设备开机')
                print('物理按键,控制设备开机')

                # DIY-灿烂
                WebDriverWait(self.driver, 10, 1).until(
                    lambda x: x.find_element_by_android_uiautomator(
                        "new UiSelector().text(\"灿烂\")")).click()
                self.loger.info('切换灿烂diy模式')
                print('切换灿烂diy模式')
                sleep(3)
                # 关机
                self.relay_h.once_click()
                self.loger.info('物理按键,控制设备关机')
                print('物理按键,控制设备关机')
                sleep(3)
                # 开机
                self.relay_h.once_click()
                self.loger.info('物理按键,控制设备开机')
                print('物理按键,控制设备开机')

                # DIY-四季
                WebDriverWait(self.driver, 10, 1).until(
                    lambda x: x.find_element_by_android_uiautomator(
                        "new UiSelector().text(\"四季\")")).click()
                self.loger.info('切换四季diy模式')
                print('切换四季diy模式')
                sleep(3)
                # 关机
                self.relay_h.once_click()
                self.loger.info('物理按键,控制设备关机')
                print('物理按键,控制设备关机')
                sleep(3)
                # 开机
                self.relay_h.once_click()
                self.loger.info('物理按键,控制设备开机')
                print('物理按键,控制设备开机')

                # DIY-摇摆
                WebDriverWait(self.driver, 10, 1).until(
                    lambda x: x.find_element_by_android_uiautomator(
                        "new UiSelector().text(\"摇摆\")")).click()
                self.loger.info('切换摇摆diy模式')
                print('切换摇摆diy模式')
                sleep(3)
                # 关机
                self.relay_h.once_click()
                self.loger.info('物理按键,控制设备关机')
                print('物理按键,控制设备关机')
                sleep(3)
                # 开机
                self.relay_h.once_click()
                self.loger.info('物理按键,控制设备开机')
                print('物理按键,控制设备开机')

                n += 1
                self.loger.info('第[%s]循环次切换DIY模式结束' % n)
                print('第[%s]循环次切换DIY模式结束' % n)
            except Exception as e:
                self.loger.info(e)
                self.loger.info('DIY模式切换失败')
                print('DIY模式切换失败')

    #设备配网
    #1、次数  2、是否跳过WiFi设置
    def add_device(self,skip_wifi,times):
        # 扫描设备
        ble_scan_device = BleScanConnect(self.driver, self.device, self.loger,self.relay_h)
        result = [0,0]
        m = 0
        app_check = False
        com_check = False
        for i in range(times):
            try:
                # 选择设备类型
                self.ch_device.chooes_device()
                m += 1
                #扫描蓝牙并连接
                ble_scan_device.ble_scan_connect()
                result = self.wifi_connect.wifi_connect(skip_wifi,result[0],result[1])
            except Exception as e:
                print(e)
                self.loger.info(e)
                continue
        self.start = False
        self.sum = 0
        if skip_wifi:
            self.sum = int(result[1])
        else:
            self.sum = int(result[0])
        print('[%s]' % self.device + '设备配网[%s]次,' % m + '成功了[%s]次,' % self.sum + ',配网成功率%s' % (self.sum / m * 100) + '%')
        self.loger.info('[%s]' % self.device + '设备配网[%s]次,' % m + '成功了[%s]次,' % self.sum + ',配网成功率%s' % (self.sum / m * 100) + '%')

    # 监测蓝牙配对日志
    def com_bluetooth_adddevice_log_check(self, dataList):
        start = len(dataList) - 2500
        end = len(dataList)
        str1 = dataList[start:end]
        str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "")  # 去掉空格、回车符、换行符
        strlist = str2.split(':', 50)  # 以冒号进行分割
        # 打印串口日志
        self.loger.info('配网日志:' + str(strlist))
        print('配网日志:' + str(strlist))
        sl = '33b200'
        if str(strlist).find(sl) >= 0:
            self.loger.info('本次配网日志校验成功')
            print('本次配网日志校验成功')
            return True
        else:
            print('本次配网日志校验失败')
            self.loger.info('本次配网日志校验失败')
            return False

    #监测蓝牙控制日志
    def com_bluetooth_normal_log_check(self,dataList,mlen,option_type,sl=None):
        start = len(dataList) - mlen
        end = len(dataList)
        str1 = dataList[start:end]
        str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "") # 去掉空格、回车符、换行符
        strlist = str2.split(':', 50)  # 以冒号进行分割
        #打印串口日志
        self.loger.info('本次操作后蓝牙日志:'+str(strlist))
        print('本次操作后蓝牙日志:'+str(strlist))
        if option_type == 'mode_switch':
            for i in range(len(strlist)):
                sl = strlist[i][len(strlist[i]) - 7:len(strlist[i])]
                if sl == 'blesend':
                    print('blesend:' + strlist[i + 1])
                    self.loger.info('蓝牙回复结果:blesend:' + str(strlist[i + 1]))
                    if  strlist[i+1][0] == '3' and strlist[i+1][4] == '0' and strlist[i+1][5] == '0':
                        self.loger.info('本次蓝牙下操作成功')
                        print('本次蓝牙下操作成功')
                        return True
                    else:
                        self.loger.info('本次蓝牙下操作失败')
                        print('本次蓝牙下操作失败')
                        return False
        else :
            check = False
            for i in range(len(strlist)):
                if str(strlist[i]).find(sl) >= 0:
                    self.loger.info(strlist[i])
                    print(strlist[i])
                    check = True
                    break
                else:
                    check = False
            return check


    #监测普通WiFi日志
    def com_wifi_normal_log_check(self, dataList, sl, option_type):
        if option_type == 'on_off_switch':
            start = len(dataList) - 3400
        elif option_type == 'leve_switch':
            start = len(dataList) - 1400
        else:
            start = len(dataList) - 2200
        end = len(dataList)
        str1 = dataList[start:end]
        str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "")  # 去掉空格、回车符、换行符
        strlist = str2.split(':', 50)  # 以冒号进行分割
        # 打印串口日志
        self.loger.info('本次操作后日志:' + str(strlist))
        for i in range(len(strlist)):
            if strlist[i].find(sl) >= 0:
                print('串口日志:' + strlist[i])
                self.loger.info('串口日志:' + strlist[i])
                return strlist[i]

    # 监测WiFi配网日志
    def com_wifi_adddevice_log_check(self, dataList):
        start = len(dataList) - 8000
        end = len(dataList)
        str1 = dataList[start:end]
        str2 = str1.replace(" ", "").replace("\r", "").replace("\n", "")  # 去掉空格、回车符、换行符
        strlist = str2.split(':', 80)  # 以冒号进行分割
        # 打印串口日志
        self.loger.info('wifi联网日志:' + str(strlist))
        print('wifi联网日志:' + str(strlist))
        sl = 'IOT_BLE_WIFI_CONNECT_SUC'
        if str(strlist).find(sl) >= 0:
            print('本次WiFi连接成功')
            return True
        else:
            print('本次WiFi连接失败')
            return False

    # 接收串口数据
    def recv(self):
        if self.start:
            print("开始接收数据:")
        else:
            print("测试完毕,停止接收数据:")
        while self.start:
            try:
                count = self.main_engine.inWaiting()  # 获取串口缓冲区数据
                if count != 0:
                    data = self.main_engine.read(self.main_engine.in_waiting).decode("gbk")  # 读出串口数据,数据采用gbk编码
                    # print(time.strftime("%Y-%m-%d %H-%M-%S"), " --- recv --> ")  # 打印一下子
                    time.sleep(0.1)  # 延时0.1秒,免得CPU出问题
                    self.dataList += data
                    data = data.replace("\r", "")
                    self.loger.info(data)
            except Exception as e:
                print("异常报错:", e)