induction_config.py
9.4 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
# codinf:utf-8
from flask import request
from app import app
import json
import time
import re
import timeit
import pexpect
import os
import csv
import logging
from config import Config
from werkzeug.utils import secure_filename
from app.utils.serial_communication import SerialCommunication
from serial_config import ReadConfig
basepath = os.path.dirname(__file__)
ip_config = {'post':'wait'}
try:
uploads_path = basepath + Config.STATE_PATH
with open(uploads_path + 'ipconfig.csv','r') as f:
reader = csv.reader(f)
current = [row for row in reader]
ip_config['ip'] = current[0][0]
ip_config['cid'] = current[0][1]
logging.warning("ip&cid...:{}".format(ip_config))
except Exception as e:
logging.warning("ip,cid未配置")
@app.route('/getstate',methods=['POST'])
def getstate():
current_state = read_state()
state_info = []
version = read_version()
ser = SerialCommunication()
serial_state=ser.check_serial()
# serial_cf = ReadConfig()
addrs = read_addrs()
serial_num = read_com()
ad_serial = "串口号:{},地址:{}".format(serial_num,addrs)
c_dict = {
'state':current_state,
'msg':'未进行测试动作',
'ipconfig':ip_config,
'version':version,
'serial_state':serial_state,
'ad_serial':ad_serial
}
state_info.append(c_dict)
return json.dumps(state_info)
@app.route('/taillog',methods=['POST'])
def taillog():
a=os.popen("tail -n 20 /prog/smartshelf/logs/smart.log").readlines()
s = '<br>'
p = s.join(a)
poststate = ip_config['post']
jsondata = {'msg':p,'poststate':poststate}
return json.dumps(jsondata)
@app.route('/updateip',methods=['POST'])
def updateip():
data = request.get_json()
confirm_ip(data)
logging.warning("ip&cid:{}".format(ip_config))
ip_info = []
ip_info.append(ip_config)
return json.dumps(ip_info)
def confirm_state(state):
uploads_path = basepath + Config.STATE_PATH
with open(uploads_path + "state.txt","w") as f:
f.write(state)
def read_state():
uploads_path = basepath + Config.STATE_PATH
with open(uploads_path + "state.txt","r") as f:
state=f.readline()
return state
def read_version():
uploads_path = basepath + Config.STATE_PATH
with open(uploads_path + "version.txt","r") as f:
s_version=f.readline()
return s_version
def confirm_ip(data):
uploads_path = basepath + Config.STATE_PATH
sipconfig = read_ip()
with open(uploads_path + 'ipconfig.csv','w') as f:
if data['ip'] and not data['cid']:
data['cid'] = sipconfig[0][1]
elif data['cid'] and not data['ip']:
data['ip'] = sipconfig[0][0]
fieldnames = ['ip', 'cid']
writer = csv.DictWriter(f,fieldnames=fieldnames)
writer.writerow(data)
ip_config['ip'] = data['ip']
ip_config['cid'] = data['cid']
confirm_autostart()
def confirm_autostart():
# server_add = ip_config['ip'] + '/accshelf/' + ip_config['cid']
result = re.findall(r"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", ip_config['ip'])
if result:
ip = result[0]
server_add = "http://{}/neotel".format(ip)
else:
server_add = "127.0.0.1:5000"
line1 = "Comment = star chrome and enter the website for \"{}\"".format(server_add)
line2 = "Exec = chromium-browser -kiosk \"{}\"".format(server_add)
strs=["[Desktop Entry]","Name = Chrome Autostar","Type = Application"]
strs.append(line1)
strs.append(line2)
filename = "/home/pi/.config/autostart/auto.desktop"
if os.path.isfile(filename):
os.remove(filename)
os.mknod(filename)
cmd = "chmod 777 " + filename
run = pexpect.spawn('su -c "%s" root' %cmd)
time.sleep(0.5)
run.sendline('xxx')
time.sleep(0.1)
with open("/home/pi/.config/autostart/auto.desktop","a") as f:
for i in strs:
f.write("{}\n".format(i))
f.close()
else:
os.mknod(filename)
cmd = "chmod 777 " + filename
run = pexpect.spawn('su -c "%s" root' %cmd)
time.sleep(0.5)
run.sendline('xxx')
time.sleep(0.1)
with open("/home/pi/.config/autostart/auto.desktop","a") as f:
for i in strs:
f.write("{}\n".format(i))
f.close()
logging.warning("自启动脚本更新成功")
def read_ip():
uploads_path = basepath + Config.STATE_PATH
with open(uploads_path + 'ipconfig.csv','r') as f:
reader = csv.reader(f)
sipconfig = [row for row in reader]
return sipconfig
# 上传配置文件
@app.route('/upload/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
# check if the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file:
basepath = os.path.dirname(__file__)
uploads_path = Config.UPLOAD_FOLDER
# print (uploads_path)
# print (Config.UPLOAD_FOLDER)
filename = secure_filename(file.filename)
file.save(os.path.join(uploads_path, filename))
# reload_config()
return '{"filename":"%s"}' % filename
return True
# 更新串口以及地址配置
@app.route('/update_serial', methods=['POST'])
def update_serial():
uploads_path = basepath + Config.STATE_PATH
# serialconfig = read_serial()
data = request.get_json()
if data.get('serial_num','') and not data.get('addrs',''):
confirm_com(data['serial_num'])
elif data.get('addrs','') and not data.get('serial_num',''):
confirm_addrs(data['addrs'])
elif data.get('addrs','') and data.get('serial_num',''):
confirm_com(data['serial_num'])
confirm_addrs(data['addrs'])
serial_num = read_com()
addrs = read_addrs()
ad_serial = "串口号:{},地址:{}".format(serial_num,addrs)
return json.dumps({"addr":ad_serial})
def read_com():
uploads_path = basepath + Config.STATE_PATH
with open(uploads_path + "serialcom.txt","r") as f:
serialcom=f.readline()
return serialcom
def read_addrs():
uploads_path = basepath + Config.STATE_PATH
with open(uploads_path + "addrs.txt","r") as f:
addrs=f.readline()
return addrs
def confirm_com(state):
uploads_path = basepath + Config.STATE_PATH
with open(uploads_path + "serialcom.txt","w") as f:
comnum="/dev/ttyUSB{}".format(state)
f.write(comnum)
f.close()
def confirm_addrs(state):
uploads_path = basepath + Config.STATE_PATH
with open(uploads_path + "addrs.txt","w") as f:
f.write(state)
f.close()
# def update_serial():
# data = request.get_json()
# serial_num = data.get('serial_num','')
# addrs = data.get('addrs','')
# serial_cf = ReadConfig()
# if serial_num:
# serial_num = "/dev/ttyUSB{}".format(serial_num)
# serial_cf.update_serial("default_com",serial_num)
# print (serial_num)
# if addrs:
# print (addrs)
# serial_cf.update_serial("addrs",addrs)
# addrs = serial_cf.get_serial("addrs")
# serial_num = serial_cf.get_serial("default_com")
# ad_serial = "串口号:{},地址:{}".format(serial_num,addrs)
# return json.dumps({"addr":ad_serial})
@app.route('/sendcolor',methods=['POST'])
def sendcolor():
COLORS = Config.COLORS
color_list = list(COLORS.values())
status_list = []
# color_lists = [color_list[i:i+2] for i in range(0,len(color_list),2)]
addrs = read_addrs()
addrs_string=addrs.split('@')
addr_list = filter(None,list(map(remove_a,addrs)))
for saddr in addr_list:
# for dlist in color_lists:
colorset_dict = {"ADDR":saddr,"colorset":color_list}
colorset_command = str(json.dumps(colorset_dict)).replace(" ","")
colorset_command = colorset_command + '\n'
print (colorset_command)
ser = SerialCommunication()
ser.send_data(colorset_command)
status=ser.read_Line()
status_list.append(status)
print(status)
time.sleep(0.5)
logging.warning("颜色集合同步状态:{}".format(status_list))
return json.dumps({"msg":"颜色集合同步状态:{}".format(status_list)})
def remove_a(s):
number = re.findall("\d+",s)
if number:
return number[0]
def get_towerstate():
# tower_state = {
# '1':{'action':False,'status':'off'},
# '2':{'action':False,'status':'off'},
# '3':{'action':True,'status':'on'},
# '4':{'action':False,'status':'off'},
# '5':{'action':False,'status':'off'},
# '6':{'action':True,'status':'on'}
# }
tower_state = {
'1@4':{'action':False,'status':'off'},
'2@5':{'action':False,'status':'off'},
'3@6':{'action':True,'status':'on'}
# '4':{'action':False,'status':'off'},
# '5':{'action':False,'status':'off'},
# '6':{'action':True,'status':'on'}
}
return tower_state