-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.py
More file actions
91 lines (76 loc) · 3.17 KB
/
Utils.py
File metadata and controls
91 lines (76 loc) · 3.17 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
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import os
import subprocess
import sys
from constant import const
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s %(levelname)s %(message)s',
datefmt='%a %d %b %Y %H:%M:%S',
filename='/tmp/FactoryTest.log',
filemode='a')
logger = logging.getLogger()
def run_shell_command(command):
"""
运行 shell 命令获取执行结果
:param command: 要执行的命令
:return: 命令运行的输出结果
"""
logging.debug('run command [{}]'.format(command))
# with os.popen(command) as r:
# result = r.read()
result = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
outMessage = str(result.stdout.read(), encoding="utf-8")
errMessage = str(result.stderr.read(), encoding='utf-8')
logging.debug('[{}] result is [{}]'.format(command, errMessage if errMessage else outMessage))
success = not errMessage
message = errMessage if errMessage else outMessage
return success, message
def resize_window(window):
window.update()
curWidth = window.winfo_reqwidth()
curHeight = window.winfo_reqheight()
scnWidth, scnHeight = window.maxsize()
newGeometry = '{}x{}+{}+{}' \
.format(curWidth, curHeight,
(scnWidth - curWidth) // 2,
(scnHeight - curHeight) // 2)
window.geometry(newGeometry)
def check_and_create_gpio(gpio):
gpioPath = '/sys/class/gpio/gpio{}/value'.format(gpio)
createGpioCommand = 'echo {0} > /sys/class/gpio/export; ' \
'echo out > /sys/class/gpio/gpio{0}/direction'.format(gpio)
if not os.path.exists(gpioPath):
run_shell_command(createGpioCommand)
def commands(item, path):
switch = {
const.WIFI: 'busybox ifconfig wlan0',
const.BLUETOOTH: 'hcitool dev',
const.USB_NETWORK: 'busybox ifconfig usb0',
const.RELAY54: 'echo 1 > /sys/class/gpio/gpio{0}/value; sleep 1; echo 0 > /sys/class/gpio/gpio{0}/value',
const.RELAY55: 'echo 1 > /sys/class/gpio/gpio{0}/value; sleep 1; echo 0 > /sys/class/gpio/gpio{0}/value',
const.SOUND_PLAY: 'aplay /tmp/audio.wav',
# + get_real_path('resource/audio.wav'),
const.SOUND_RECORD: 'arecord -Dhw:0,0 -d 10 -f cd -r 44100 -c 2 -t wav /tmp/audio.wav',
# 'arecord -f cd -d 10 /tmp/audio.wav',
const.NFC: get_real_path('bin/test_nfc'),
const.CAMERA: 'cheese',
const.LED_R: '{}',
const.LED_G: '{}',
const.LED_B: '{}',
const.HUMIDITY: 'cat {}',
const.TEMPERATURE: 'cat {}',
const.PRESSURE: 'cat {}',
const.VL53L1X: 'ls {}',
const.XM132: 'ls {}',
const.MAX44009: 'cat {}',
const.CHANGE_PERMISSION: 'chmod +x {}'
}
return switch.get(item, '').format(path)
def get_real_path(path):
basePath = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(basePath, path)
def check_and_authorization():
binPath = '{}/*'.format(get_real_path('bin'))
run_shell_command(commands(const.CHANGE_PERMISSION, binPath))