-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnavtex.py
73 lines (65 loc) · 1.98 KB
/
navtex.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
import serial
import smtplib
import time
import ConfigParser
import sys
def readConfig(configfile):
config = ConfigParser.RawConfigParser()
config.read(configfile)
res = {
"server":config.get("email", "server"),
"username":config.get("email", "username"),
"password":config.get("email","password"),
"sender": config.get("email","sender"),
"recipient":config.get("email","recipient"),
"serialport":config.get("serial","port"),
"baudrate":config.getint("serial","baud"),
"storagepath":config.get("storage","path")
}
return res
def sendMessage(message,config):
print message
smtp = smtplib.SMTP(config["server"]);
smtp.login(config["username"],config["password"]);
smtp.sendmail(config["sender"],config["recipient"],message)
smtp.quit()
def saveMessage(message,config):
lt = time.localtime()
filename = config["storagepath"] + "navtex_" + time.strftime("%Y-%m-%d-%H-%M-%S",lt) + ".txt"
f = open(filename,'w')
f.write(message)
f.close()
def getConfigFilename():
if (len(sys.argv) > 1):
return sys.argv[1]
else:
print "Usage: python navtex.py configfile.cfg"
return None
#Read config-filename from commandline argument
configfilename = getConfigFilename()
if configfilename == None:
sys.exit(0)
#read configfile
config = readConfig(configfilename)
print "Using config: "
print config
#init serial port
try:
ser = serial.Serial(config["serialport"],config["baudrate"],timeout=None)
except:
print "Error: cannot connect to serial port"
sys.exit(1)
#waiting for messages
print "waiting for new..."
message = ""
for line in ser:
print line
if line.startswith(">"):
lt = time.localtime()
message = time.strftime("%Y-%m-%d %H:%M:%S\n",lt)
if line.startswith("<"):
message += line
sendMessage(message,config)
saveMessage(message,config)
else:
message += line