-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprinkler
executable file
·64 lines (51 loc) · 1.25 KB
/
sprinkler
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
#!/usr/bin/env python
from mr_shed_output import output, init
import time
import sys
import datetime
import os
from ConfigParser import SafeConfigParser
# CONSTANTS
OUTPUT_NUM_MAP = {
"lawn": 2,
"garden_a": 3,
"garden_b": 4,
}
# CONFIG
config = SafeConfigParser()
config.read(os.path.dirname(os.path.realpath(__file__)) + '/config.ini')
# FUNCTION DEFS
def log(msg):
now = datetime.datetime.now()
str = now.strftime("%Y-%m-%d %H:%M") + " >> " + msg
print str
open(config.get('logging','log_file'),"a").write(str + "\n")
###############
# RUN-TIME
###############
init()
# ARGUMENTS
if (len(sys.argv) != 3):
print "Invalid number of arguments"
print "Should be ./sprinkler ZONE ACTION"
sys.exit()
zone = str(sys.argv[1])
action = str(sys.argv[2])
if (zone == 'all'):
if (action == 'on'):
print "Invalid, cannot turn on all zones, only turn all off"
elif (action == 'off'):
log("Turning ALL sprinklers OFF")
# output(1,'off')
output(2,'off')
output(3,'off')
output(4,'off')
else:
print "Invalid action \"" + action + "\""
elif zone in OUTPUT_NUM_MAP:
log("Turning " + zone.upper() + " sprinklers " + action.upper())
output(OUTPUT_NUM_MAP[zone], action)
else:
# Invalid zone
print "Invalid zone \"" + zone + "\""
sys.stdout.flush()