-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryTest.py
More file actions
390 lines (345 loc) · 13.1 KB
/
FactoryTest.py
File metadata and controls
390 lines (345 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from functools import partial
from tkinter import *
import Utils
from ConfigOptions import ConfigOptions
from ReadCpuInfoThread import ReadCpuInfoThread
from Utils import logger as logging
from constant import const
class GUITest:
"""
工厂测试工具
"""
def __init__(self, config=None):
self.autoTest = 0
self.grids = []
self.testCaseButtons = {}
self.configOptions = config
self.testCase = []
self.testCaseEnable = {}
self.currentIndex = 0
self.currentItem = ''
self.window = None
self.messageLabel = NONE
self.toplevel = None
self.successButton = None
self.failButton = None
self.readCpuInfoThread = ReadCpuInfoThread()
self.readCpuInfoThread.setDaemon(True)
self.readCpuInfoThread.set_flag(False)
self.readCpuInfoThread.start()
def init_config(self):
"""
从配置文件中加载相关配置
"""
testCaseStatus = self.configOptions.config[const.TEST_CASE]
self.testCase = self.configOptions.config[const.TEST_CASE_LIST][const.TEST_CASE_LIST].split(' ')
logging.debug('testCase {}'.format(self.testCase))
for index, key in enumerate(testCaseStatus):
logging.debug('testCase {} {}'.format(key, testCaseStatus[key]))
val = IntVar()
val.set(testCaseStatus[key])
self.testCaseEnable[key] = val
if index < len(self.grids):
self.grids[index].config(state=DISABLED if testCaseStatus[key] == "0" else NORMAL)
logging.debug('testCaseEnable {}'.format(self.testCaseEnable))
logging.debug('auto test {}'.format(self.autoTest))
def start(self):
"""
启动测试主界面
"""
self.window = Tk(className='Factory test')
self.window.resizable(0, 0)
self.init_config()
self.create_grid()
self.create_menu()
Utils.resize_window(self.window)
self.window.mainloop()
def create_grid(self):
"""
根据测试项生成测试界面
"""
for index, item in enumerate(self.testCase[:-1]):
button = Button(
self.window,
text=item,
font='Helvetica 16 bold',
width=16,
height=4,
state='normal',
highlightbackground='#3E4149',
command=partial(self.on_item_click_listener, index, item),
)
button.grid(row=index // 2, column=index % 2, sticky=W, padx=5, pady=5)
self.grids.append(button)
def create_menu(self):
"""
测试界面菜单部分
"""
menu = Menu(self.window)
self.window.config(menu=menu)
# menu file
fileMenu = Menu(menu, tearoff=False)
fileMenu.add_command(label='Reset config', command='')
fileMenu.add_separator()
fileMenu.add_command(label='Exit', command=self.window.quit)
menu.add_cascade(label='File', menu=fileMenu)
# menu test case
testCaseMenu = Menu(menu, tearoff=False)
for index, item in enumerate(self.testCase):
if index == len(self.testCase) - 1:
testCaseMenu.add_separator()
testCaseMenu.add_checkbutton(
label=item,
onvalue=1,
offvalue=0,
variable=self.testCaseEnable[item],
command=partial(self.set_test_case_enable, index, item)
)
menu.add_cascade(label=const.TEST_CASE, menu=testCaseMenu)
# Auto Test
# menu.add_command(label='Auto test', command=partial(self.start_auto_test))
# Agin test
menu.add_command(label='Aging test', command=partial(self.test_aging, const.AGING))
def set_test_case_enable(self, index, item):
"""
设置测试项是否可用
"""
value = str(self.testCaseEnable[item].get())
self.configOptions.config[const.TEST_CASE][item] = value
self.configOptions.save_config()
self.grids[index].config(state=DISABLED if value == "0" else NORMAL)
def start_auto_test(self):
"""
启动自动测试
"""
def test_aging(self, title):
"""
启动老化测试,并展示 CPU 相关信息
stressapptest -M 1200 -s 300000
memtester 1G 5
"""
os.system('stressapptest -M 1200 -s 300000 &')
os.system('memtester 1G 5 &')
if self.toplevel:
self.toplevel.destroy()
self.toplevel = None
self.toplevel = Toplevel()
self.toplevel.title(title)
self.messageLabel = Label(self.toplevel, width=40, height=20)
self.messageLabel.pack()
self.readCpuInfoThread.set_flag(True)
self.readCpuInfoThread.set_ui(self.messageLabel)
self.toplevel.protocol('WM_DELETE_WINDOW', self.stop_read_cpu_info_thread)
self.toplevel.update()
Utils.resize_window(self.toplevel)
def stop_read_cpu_info_thread(self):
"""
停止读取 CPU 相关信息
"""
self.readCpuInfoThread.set_flag(False)
# 查找 stressapptest 相关进程并关闭
os.system("pidof stressapptest | xargs kill -9 &")
# 查找 memtester 相关进程并关闭
os.system("pidof memtester | xargs kill -9 &")
self.toplevel.destroy()
def on_test_case_click_listener(self, item):
"""
点击测试项目执行相关的命令
"""
logging.debug('test case name {}'.format(item))
try:
path = self.configOptions.config[const.TEST_CASE_PATH][item]
except KeyError as _:
path = ''
logging.debug('test case path {}'.format(path))
result, message = Utils.run_shell_command(Utils.commands(item, path))
switchAutoCheck = {
const.WIFI: message.strip(),
const.BLUETOOTH: 'hci' in message,
const.USB_NETWORK: message.strip(),
const.NFC: 'success' in message,
const.HUMIDITY: message.strip(),
const.TEMPERATURE: message.strip(),
const.PRESSURE: message.strip(),
const.MAX44009: message.strip(),
const.VL53L1X: message.strip(),
const.XM132: message.strip(),
}
if item in switchAutoCheck.keys():
logging.debug('result is {} auto check is {}'.format(result, switchAutoCheck.get(item, False)))
color = const.COLOR_SUCCESS if (result and switchAutoCheck.get(item, False)) else const.COLOR_ERROR
self.testCaseButtons[item].config(bg=color)
def test_wifi_bt(self):
"""
显示 WIIF 和 Bluetooth 以及 4G 5G 测试模块
"""
logging.debug('test wifi bt')
self.create_test_case_button(const.WIFI, 0, 2)
self.create_test_case_button(const.BLUETOOTH, 1, 2)
self.create_test_case_button(const.USB_NETWORK, 2, 2)
def test_rtc(self):
"""
显示 RTC 测试模块,RTC 测试直接判断相关节点是否存在即可
"""
logging.debug('test rtc')
try:
rtcPath = self.configOptions.config[const.TEST_CASE_PATH][const.RTC]
except KeyError as _:
rtcPath = '/dev/rtc1'
self.on_result_click_listener(os.path.exists(rtcPath))
def test_relay(self):
"""
显示继电器测试模块,首先判断是否有对应的 GPIO 口如果没有就创建
:return:
"""
logging.debug('test relay')
try:
relay54 = self.configOptions.config[const.TEST_CASE_PATH][const.RELAY54]
except KeyError as _:
relay54 = "54"
try:
relay55 = self.configOptions.config[const.TEST_CASE_PATH][const.RELAY55]
except KeyError as _:
relay55 = "55"
Utils.check_and_create_gpio(relay54)
Utils.check_and_create_gpio(relay55)
self.create_test_case_button(const.RELAY54, 0, 2)
self.create_test_case_button(const.RELAY55, 1, 2)
def test_sound(self):
"""
测试声卡是否正常,分为录音和放音功能
"""
logging.debug('test sound')
self.create_test_case_button(const.SOUND_RECORD, 0, 2)
self.create_test_case_button(const.SOUND_PLAY, 1, 2)
def test_nfc(self):
"""
测试 NFC 模块是否正常
"""
logging.debug('test nfc')
self.create_test_case_button(const.NFC, 0, 2)
def test_camera(self):
"""
测试摄像头是否正常
"""
logging.debug('test_camera')
self.create_test_case_button(const.CAMERA, 0, 2)
def test_rgb(self):
"""
测试 RGB 灯是否正常,首先判断节点是否存在,如果不存在就创建对应的节点
"""
logging.debug('test_rgb')
# try:
# ledR = self.configOptions.config[const.TEST_CASE_PATH][const.LED_R]
# except KeyError as _:
# ledR = "33"
# try:
# ledG = self.configOptions.config[const.TEST_CASE_PATH][const.LED_G]
# except KeyError as _:
# ledG = "45"
# try:
# ledB = self.configOptions.config[const.TEST_CASE_PATH][const.LED_B]
# except KeyError as _:
# ledB = "32"
# Utils.check_and_create_gpio(ledR)
# Utils.check_and_create_gpio(ledG)
# Utils.check_and_create_gpio(ledB)
self.create_test_case_button(const.LED_R, 0, 2)
self.create_test_case_button(const.LED_G, 1, 2)
self.create_test_case_button(const.LED_B, 2, 2)
def test_sensor(self):
"""
测试 Sensor 是否正常
"""
logging.debug('test_sensor')
self.create_test_case_button(const.VL53L1X, 0, 1)
self.create_test_case_button(const.XM132, 1, 1)
self.create_test_case_button(const.MAX44009, 2, 1)
self.create_test_case_button(const.HUMIDITY, 0, 2)
self.create_test_case_button(const.TEMPERATURE, 1, 2)
self.create_test_case_button(const.PRESSURE, 2, 2)
def on_result_click_listener(self, result):
"""
根据测试结果改变相关界面并保存结果到测试日志
:param result: 测试结果
"""
logging.debug('{} test {}'.format(self.currentItem, result))
color = const.COLOR_SUCCESS if result else const.COLOR_ERROR
resultMessage = const.SUCCESS if result else const.ERROR
self.grids[self.currentIndex].config(bg=color)
self.configOptions.save_log('{} test {}\n'.format(self.currentItem, resultMessage))
self.toplevel.destroy()
self.toplevel = None
def create_test_case_button(self, text, row, column):
button = Button(
self.toplevel,
text=text,
font="Helvetica 10 bold",
width=8,
height=2,
state="normal",
highlightbackground="#3E4149",
command=partial(self.on_test_case_click_listener, text)
)
button.grid(row=row, column=column, padx=10, pady=10)
self.testCaseButtons[text] = button
def on_item_click_listener(self, index, item):
"""
点击屏幕上的条目时触发的事件
"""
logging.debug('on item click {} {}'.format(index, item))
self.currentIndex = index
self.currentItem = item
if self.toplevel:
self.toplevel.destroy()
self.toplevel = Toplevel()
self.toplevel.title(item)
# Python字典中还可以包括函数或Lambda表达式
# 使用字典形式实现其他的语言 switch 效果避免if else 嵌套
switch = {
const.WIFI_BT: self.test_wifi_bt,
const.RTC: self.test_rtc,
const.RELAY: self.test_relay,
const.SOUND: self.test_sound,
const.NFC: self.test_nfc,
const.CAMERA: self.test_camera,
const.RGB: self.test_rgb,
const.SENSOR: self.test_sensor,
}
self.successButton = Button(
self.toplevel,
text=const.SUCCESS,
font="Helvetica 10 bold",
width=8,
height=2,
state="normal",
highlightbackground="#3E4149",
command=partial(self.on_result_click_listener, True)
)
self.successButton.grid(row=3, column=0, padx=10, pady=10)
self.failButton = Button(
self.toplevel,
text=const.ERROR,
font="Helvetica 10 bold",
width=8,
height=2,
state="normal",
highlightbackground="#3E4149",
command=partial(self.on_result_click_listener, False)
)
self.failButton.grid(row=3, column=3, padx=10, pady=10)
method = switch.get(item)
if method:
method()
if self.toplevel:
self.toplevel.update()
Utils.resize_window(self.toplevel)
def main():
Utils.check_and_authorization()
gui = GUITest(ConfigOptions())
gui.start()
if __name__ == '__main__':
main()