-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
123 lines (87 loc) · 2.71 KB
/
main.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
# coding: utf-8
from itinerary import get_time_to_work_text
from weather import get_weather_at_home_text
import RPi.GPIO as GPIO
import time
from datetime import datetime, timedelta
import vlc
import os
# consts
CHANNEL_PROXIMITY_SENSOR = 7
MINUTES_TO_KEEP_ON = 10
DEPARTURE_HOUR = 9
DEPARTURE_MINUTE = 0
# sound level
cmd = "amixer set PCM -- 100%"
os.system(cmd)
# vars
is_playing = False
last_say_hour = ''
last_run_datetime = datetime.now() + timedelta(days=-1)
player = vlc.MediaPlayer(
"http://chai5she.lb.vip.cdn.dvmr.fr/franceinter-midfi.mp3")
def setup_proximity_sensor():
GPIO.setmode(GPIO.BOARD)
GPIO.setup(CHANNEL_PROXIMITY_SENSOR, GPIO.IN)
def say(text):
print text
cmd = 'pico2wave -l fr-FR -w lookdave.wav ".. %s" && aplay lookdave.wav' % text
os.system(cmd)
def presence_detected():
global last_run_datetime
today_datetime = datetime.now().replace(hour=5, minute=0)
is_first_detection_today = last_run_datetime < today_datetime
is_morning = time.localtime().tm_hour >= 6 and time.localtime().tm_hour <= 11
if is_first_detection_today and is_morning:
last_run_datetime = datetime.now()
is_playing = False
say(get_weather_at_home_text())
if is_week_day():
say(get_time_to_work_text(DEPARTURE_HOUR, DEPARTURE_MINUTE))
player.play()
def absence_detected():
is_playing = False
player.stop()
def is_week_day():
weekday = datetime.now().weekday()
return weekday >= 0 and weekday < 5
def say_hour():
# global last_say_hour
current_text = 'il est %s heure %s' % (
time.localtime().tm_hour, time.localtime().tm_min)
if last_say_hour is not current_text:
last_say_hour = current_text
player.stop()
say(current_text)
time.sleep(3)
player.play()
def run_loop():
global last_run_datetime
THRESHOLD = 500
total = 0
nb_off = 0
was_on = False
time_to_stop = 0
say("Radio Raspberry prêt")
while True:
value = GPIO.input(CHANNEL_PROXIMITY_SENSOR)
if value == GPIO.LOW:
nb_off = nb_off + 1
total = total + 1
if is_playing and time.localtime().tm_min % 5 == 0:
say_hour()
if total >= THRESHOLD:
if nb_off < THRESHOLD * 0.3:
time_to_stop = time.time() + (MINUTES_TO_KEEP_ON * 60)
if not was_on:
presence_detected()
was_on = True
elif was_on and time.time() > time_to_stop:
absence_detected()
was_on = False
total = 0
nb_off = 0
sleep_time = 0.002
time.sleep(sleep_time)
setup_proximity_sensor()
run_loop()