-
Notifications
You must be signed in to change notification settings - Fork 0
/
TH300test.py
148 lines (112 loc) · 3.06 KB
/
TH300test.py
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
# -*- coding:utf-8 -*-
#import threading
import serial, time
from flask import Flask, render_template, make_response
from httplib2 import Http
from struct import unpack
#from flask.ext.autoindex import AutoIndex
#import os.path
#import thread
from apscheduler.scheduler import Scheduler
from datetime import datetime
sched = Scheduler()
sched.start()
app = Flask(__name__)
#AutoIndex(app, browse_root = os.path.curdir + '/static')
ser = serial.Serial(
port='/dev/ttyUSB0',
baudrate='38400',
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
)
SMS = Http()
IS = '\x00'
URL = "http://192.168.0.3:9090/sendsms?phone=13013786354&text="
if not ser.isOpen():
ser.open()
def SMS(InnerS):
for i in range(8):
if InnerS[i] == 1:
URL = URL + "警報" + i + "On,"
else:
URL = URL + "警報" + i + "Off,"
SMS.request(URL,'GET')
@app.route('/')
def index():
return render_template("index.htm")
@app.route('/getva')
def returnv():
global TH
#print TH.value
return TH.value
def getv():
ser.flushInput()
ser.write('\x01\x03\x75\x95\x00\x1b\x0f\xe1')
time.sleep(0.04)
if ser.inWaiting() == 59:
TH.value = ser.read(59)
RecordData(TH.value)
else:
getv()
def testtime():
print datetime.now().microsecond
@app.route('/run')
def run():
ser.write('\x01\x06\x9c\x41\x00\x01\x36\x4e')
r = ser.read(8)
return "TH300 is Running ..."
@app.route('/stop')
def stop():
ser.write('\x01\x06\x9c\x41\x00\x00\xf7\x8e')
r = ser.read(8)
return "TH300 stopped......"
def RecordData(r):
da = open('text','a')
da.write(r)
@app.route('/csv')
def ConvertCSV():
csvtxt = open('./TH300/data/record.csv','w')
da = open('text','rb')
csv = ''
records = da.read()
for i in range(len(records)/59):
da.seek(i*59 + 3)
data = unpack('>hhhhhh',da.read(12))
csv = csv + str(float(data[0])/100-100) + ',' + str(float(data[1])/100-100) \
+ ',' + str(float(data[2])/100) + ',' + str(float(data[3])/10) + ',' + str(float(data[4])/10) \
+ ',' + str(float(data[5])/100) + '\n'
#for j in range(6):
# csv = csv + str(data[j]) + ','
#csv = csv + '\n'
csvtxt.write(csv)
return 'converted to csv!!'
@app.route('/data')
def data():
da = open("./TH300/data/record.csv",'r')
response = make_response(da.read())
response.headers['Content-Disposition'] = 'attachment;filename=record.csv'
return response
@app.route('/sms')
def sms():
return "sms"
@app.route('/email')
def email():
return "email"
class TH350:
def __init__(self):
self.IS = 0
url = "http://192.168.0.3:9090/sendsms?phone=13013786354&text="
self.value = 'started'
TH = TH350()
sched.add_interval_job(getv,seconds=1)
#t = threading.Timer(1.0,getv)
#t.start()
'''def thread1():
threading.Timer(1,getv)
#time.sleep(1)
thread1()
'''
if __name__=='__main__':
app.run('0.0.0.0',port=8080,debug=True)
#thread.start_new_thread(getv,(0.2,))