-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresending.py
More file actions
90 lines (83 loc) · 3.16 KB
/
resending.py
File metadata and controls
90 lines (83 loc) · 3.16 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import os
from subprocess import Popen, PIPE, STDOUT
import signal
import fnmatch
import const
def GPIO_setup():
GPIO.setmode(GPIO.BOARD)
SEND = 16
GPIO.setup(const.SEND,GPIO.OUT)
#GPIO.cleanup()
def list_files(path, ext):
# returns a list of names (with extension, without full path) of all files
# in folder path
files = []
for name in os.listdir(path):
if (os.path.isfile(os.path.join(path, name)) and fnmatch.fnmatch(name, ext)):
files.append(name)
return files
try:
GPIO_setup()
timenow = time.strftime("%Y-%m-%d %H:%M:%S")
print(timenow)
print("Resending started")
#rename .tx_ files to .txt (if found)
try:
filelist = list_files(const.PATH_SEND,"*.tx_")
for f in filelist:
print("os.rename("+const.PATH_SEND + f+", "+const.PATH_SEND + f[0:32] + ".txt"+")")
os.rename(const.PATH_SEND + f, const.PATH_SEND + f[0:32] + ".txt")
except:
print("Rename error")
#main loop
while True:
if (const.internet_on()):
try:
filelist = list_files(const.PATH_SEND,"*.txt")
for f in filelist:
tf = open(os.path.join(const.PATH_SEND, f),'r')
fileinfo = tf.read()
tf.close()
# filename = f[0:32] + ".ts"
filename = f[0:32]
# check file creation
if (not os.path.isfile(os.path.join(const.PATH_SEND, filename))):
break
GPIO.output(const.SEND, True)
cmd = ("curl --connect-timeout 15 --max-time 300 --silent " +
"-F id='" + str(const.getserial()) + "' " +
"-F date='" + fileinfo + ".mp4' " +
"-F filename='" + filename + "' " +
"-F filedata=@" + const.PATH_SEND + filename + " " + const.URL_DATA + "")
print(cmd)
try:
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE,stderr=STDOUT, close_fds=True)
result = p.stdout.read()
print("Result: " + result)
if result == "Success":
print("Remove "+filename)
os.remove(os.path.join(const.PATH_SEND, filename + ".mp4"))
os.remove(os.path.join(const.PATH_SEND, filename + ".txt"))
#except:
# print("Curl error")
except Exception, e:
print("Curl error: "+ str(e))
GPIO.output(const.SEND, False)
except KeyboardInterrupt:
print("Ctrl-C pressed")
else:
print("No internet")
#print("Sleep 5 seconds...")
time.sleep(5)
except KeyboardInterrupt:
print("Ctrl-C pressed")
GPIO.cleanup()
except Exception, e:
print("Eerror:"+ str(e))
#GPIO.cleanup()
finally:
GPIO.cleanup()