1
- import bottle
2
1
from bottle import *
3
2
4
3
import db
5
4
from subprocess import Popen , PIPE , call
6
- import os , signal , time , json
5
+ from os import path , kill
6
+ from signal import SIGTERM
7
+ import json
7
8
8
- basedir = os . path .dirname (__file__ )
9
+ basedir = path .dirname (__file__ )
9
10
10
11
app = Bottle ()
11
12
12
- outputfile = os . path .join (basedir ,"output.txt" )
13
+ outputfile = path .join (basedir ,"output.txt" )
13
14
14
15
15
16
class RoiData ():
@@ -20,18 +21,17 @@ def __init__(self):
20
21
21
22
@app .route ('/static/<filepath:path>' )
22
23
def server_static (filepath ):
23
- return static_file (filepath , root = os . path .join (basedir ,"static" ))
24
+ return static_file (filepath , root = path .join (basedir ,"static" ))
24
25
25
26
@app .route ('/' )
26
27
def index ():
27
- print (os .path .dirname (__file__ ))
28
28
mid = checkMachineId ()
29
29
_ ,status = checkPid ()
30
30
df = subprocess .Popen (["df" , "./" ], stdout = subprocess .PIPE )
31
31
output = df .communicate ()[0 ]
32
32
device , size , used , available , percent , mountpoint = \
33
33
output .split (b"\n " )[1 ].split ()
34
- return template (os . path .join (basedir + "/views" ,"index.tpl" ), machineId = mid , status = status , freeSpace = percent )
34
+ return template (path .join (basedir + "/views" ,"index.tpl" ), machineId = mid , status = status , freeSpace = percent )
35
35
36
36
@app .route ('/websocket' )
37
37
def handle_websocket ():
@@ -74,9 +74,7 @@ def list_roi():
74
74
@app .post ('/changeMachineId' )
75
75
def changeMachineId ():
76
76
try :
77
- print ("changing name" )
78
77
name = request .json
79
- print (name )
80
78
changeMId (name ['newName' ])
81
79
return (name ['newName' ])
82
80
except :
@@ -89,27 +87,26 @@ def starStop():
89
87
try :
90
88
data = request .json
91
89
t = data ['time' ]
92
- print (t )
93
90
#set time, given in miliseconds from javascript, used in seconds for date
94
91
setTime = call (['date' , '-s' , '@' + str (t )[:- 3 ]])
95
92
except :
96
93
print ("no data" )
97
94
pid , isAlreadyRunning = checkPid ()
98
95
99
96
if isAlreadyRunning :
100
- os . kill (pid ,signal . SIGTERM )
97
+ kill (pid ,SIGTERM )
101
98
else :
102
99
db .writeMask (data )
103
100
#f = open('mask.msk','wb')
104
101
#pickle.dump(data['roi'], f)
105
102
#f.close()
106
- pySolo = Popen (["python2" ,os . path .join (basedir ,"pvg_standalone.py" ),
107
- "-c" , os . path .join (basedir ,"pysolo_video.cfg" ),
103
+ pySolo = Popen (["python2" ,path .join (basedir ,"pvg_standalone.py" ),
104
+ "-c" , path .join (basedir ,"pysolo_video.cfg" ),
108
105
"-i" ,"0" ,
109
106
"-k" , "mask.msk" ,
110
107
"-t" , str (data ['trackingType' ]),
111
108
"-o" , outputfile ,
112
- "--showmask" ,
109
+ "--showmask" ,#useful?
113
110
"--trackonly" ])
114
111
115
112
#pySolo = Popen(["python2", "pvg.py"])# -c pysolo_video.cfg -i 0 -k mask.msk -t 0 -o output.txt", shell=True)
@@ -123,11 +120,12 @@ def state():
123
120
def refresh ():
124
121
pid , isAlreadyRunning = checkPid ()
125
122
if isAlreadyRunning :
126
- #add a call to a function to update snapshot
123
+ #add a call to a function to update snapshot when trackingType
124
+ #for now, do nothing
127
125
pass
128
126
else :
129
- pySolo = call (["python2" ,os . path .join (basedir ,"pvg_standalone.py" ),
130
- "-c" , os . path .join (basedir ,"pysolo_video.cfg" ),
127
+ pySolo = call (["python2" ,path .join (basedir ,"pvg_standalone.py" ),
128
+ "-c" , path .join (basedir ,"pysolo_video.cfg" ),
131
129
"-i" ,"0" ,
132
130
"--snapshot" ,])
133
131
redirect ("/" )
@@ -140,8 +138,8 @@ def downloadData(machineID):
140
138
pid , isAlreadyRunning = checkPid ()
141
139
mid = checkMachineId ()
142
140
if mid == machineID :
143
- #Add a "last downloaded" and "free space available "
144
- return static_file (outputfile , root = '' )
141
+ #TODO: Add a "last downloaded"
142
+ return static_file (outputfile , root = '/' , download = outputfile )
145
143
else :
146
144
redirect ("/" )
147
145
@@ -185,7 +183,7 @@ def poweroff(machineID):
185
183
186
184
def checkPid ():
187
185
proc = Popen (["pgrep" , "-f" ,
188
- "python2 " + os . path .join (basedir ,"pvg_standalone.py" )]
186
+ "python2 " + path .join (basedir ,"pvg_standalone.py" )]
189
187
, stdout = PIPE )
190
188
try :
191
189
pid = int (proc .stdout .readline ())
@@ -197,13 +195,13 @@ def checkPid():
197
195
return pid , started
198
196
199
197
def changeMId (name ):
200
- f = open (os . path .join (basedir ,'machineId' ),'w' )
198
+ f = open (path .join (basedir ,'machineId' ),'w' )
201
199
piId = f .write (name )
202
200
f .close ()
203
201
return True
204
202
205
203
def checkMachineId ():
206
- f = open (os . path .join (basedir ,'machineId' ),'r' )
204
+ f = open (path .join (basedir ,'machineId' ),'r' )
207
205
piId = f .read ().rstrip ()
208
206
f .close ()
209
207
return piId
@@ -217,7 +215,7 @@ def readData():
217
215
lastData = lines [- 1 ]
218
216
splitedData = lastData .split ('\t ' )
219
217
jsonData = json .dumps (splitedData )
220
- print (jsonData )
218
+ # print (jsonData)
221
219
return jsonData
222
220
223
221
"""The main program"""
0 commit comments