-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlightbotactions.py
193 lines (159 loc) · 5.32 KB
/
lightbotactions.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import RPi.GPIO as GPIO
import subprocess
import time
import urllib.request
import signal
import mpd
valid_target = {'0': 15, '1': 16, '2': 11, '3': 12}
valid_state = {'on': GPIO.HIGH, 'off': GPIO.LOW}
mpd_server = {'host': 'localhost', "port": 6600}
def get_volume():
client = mpd.MPDClient()
client.connect(**mpd_server)
try:
vol = int(client.status()["volume"])
except:
vol = 70
if vol == 0:
vol = 70
return vol
prev_vol = get_volume()
def turn_off(greeting=False):
global prev_vol
client = mpd.MPDClient()
try:
client.connect(**mpd_server)
vol = int(client.status()["volume"])
if vol:
prev_vol = vol
print("Got " + str(prev_vol))
if greeting:
try:
subprocess.check_call(
["aplay", "/home/simark/audio_samples/graine.wav"])
except:
pass
client.setvol(0)
client.disconnect()
except mpd.ConnectionError:
pass
def turn_on(greeting=False):
global prev_vol
if prev_vol:
client = mpd.MPDClient()
try:
client.connect(**mpd_server)
print("Set " + str(prev_vol))
client.setvol(prev_vol)
prev_vol = None
client.disconnect()
if greeting:
try:
subprocess.check_call(
["aplay", "/home/simark/audio_samples/francois.wav"])
except:
pass
except mpd.ConnectionError:
pass
def get_plafond_status():
res = urllib.request.urlopen(
"http://station6.dorsal.polymtl.ca:9898").read()
if res == b'1':
return True
elif res == b'0':
return False
else:
raise Exception()
def handler(signum, frame):
print("SIGNAL COT")
adjust_music()
def adjust_music():
print("ajustons la musique!")
try:
presence = get_plafond_status()
if presence:
print("La lumière est allumée!")
turn_on(True)
else:
print("La lumière est éteinte!")
turn_off(True)
except:
print("Erreur de l'ajustement de la musique")
"""import schedule
scheduler = schedule.Scheduler()
scheduler.every(15).minutes.do(adjust_music)
stop = scheduler.run_continuously()
"""
class LightbotActions:
def __init__(self, irc):
GPIO.setmode(GPIO.BOARD)
for x in valid_target:
GPIO.setup(valid_target[x], GPIO.OUT)
self.irc = irc
self.last_toggle = 0
signal.signal(signal.SIGUSR1, handler)
# This next line doesn't seem to do anything
signal.siginterrupt(signal.SIGUSR1, False)
def dispose(self):
"""global stop
global scheduler
scheduler.clear()
stop.set()
"""
def action_light_turn(self, from_, chan, msg, parts):
if len(parts) != 2:
return
target = parts[0]
state = parts[1]
if target not in valid_target or state not in valid_state:
return
value = valid_state[state]
pin = valid_target[target]
GPIO.output(pin, value)
self.irc.privmsg(chan, "Light " + target + " is now " + state)
def action_light_status(self, from_, chan, msg, parts):
if len(parts) == 0:
for t in sorted(valid_target.keys()):
value = "on" if GPIO.input(valid_target[t]) else "off"
self.irc.privmsg(chan, "Light " + str(t) + ": " + value)
self.irc.privmsg(
chan, "Light local: " + ("on" if get_plafond_status() else "off"))
elif len(parts) == 1:
t = parts[0]
if t in valid_target:
value = "on" if GPIO.input(valid_target[t]) else "off"
self.irc.privmsg(chan, "Light " + str(t) + ": " + value)
def action_toggle(self, from_, chan, msg, parts):
t = time.time()
if t - self.last_toggle >= 5:
self.last_toggle = t
try:
signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGUSR1})
subprocess.call(
["/home/simark/avr/serieViaUSB/serieViaUSB", "-e", "-f", "/home/simark/avr/serieViaUSB/fichier"])
self.irc.privmsg(chan, "Your wish is my command")
# time.sleep(1)
if get_plafond_status():
self.irc.privmsg(chan, "Light is now on")
else:
self.irc.privmsg(chan, "Light is now off")
except:
raise
finally:
signal.pthread_sigmask(signal.SIG_UNBLOCK, {signal.SIGUSR1})
else:
self.irc.privmsg(
chan, "You have to wait 5 seconds between two toggles.")
def action_light(self, from_, chan, msg, parts):
if len(parts) == 0:
return
cmd = parts.pop(0)
if cmd == 'turn':
self.action_light_turn(from_, chan, msg, parts)
elif cmd == 'toggle':
self.action_toggle(from_, chan, msg, parts)
elif cmd == 'status':
self.action_light_status(from_, chan, msg, parts)
def on_chanmsg(self, from_, chan, msg):
parts = msg.split()[1:]
self.action_light(from_, chan, msg, parts)