-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer
executable file
·208 lines (161 loc) · 4.5 KB
/
player
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/python3
import os
import random
import RPi.GPIO as GPIO
import smbus
from datetime import datetime
from datetime import timedelta
from pygame import mixer
from subprocess import call
from time import sleep
mediaPath = "/home/pi/gPodder/Downloads"
streamPath = "test.mp3" # "https://detektor.fm/stream/mp3/musik/"
bus = smbus.SMBus(1)
sensorAddress = 0x70
chainPin = 24
chainOldValue = False
currentHour = 0
####
#
# Helper
#
####
# https://thispointer.com/python-how-to-get-list-of-files-in-directory-and-sub-directories/
def ReadListOfFiles(dirName):
listOfFile = os.listdir(dirName)
allFiles = list()
for entry in listOfFile:
fullPath = os.path.join(dirName, entry)
if os.path.isdir(fullPath):
allFiles = allFiles + ReadListOfFiles(fullPath)
else:
allFiles.append(fullPath)
# print("ReadListOfFiles, allFiles: ", allFiles)
return allFiles
def IsNextHour():
global currentHour
nextHour = datetime.now().hour
if currentHour != nextHour:
currentHour = nextHour
return True
else:
return False
def DeleteOldFilesInFolder(path, age):
#print("DeleteOldFilesInFolder, path: ", path)
for dirpath, dirname, filenames in os.walk(path):
for old_file in filenames:
curpath = os.path.join(dirpath, old_file)
file_modified = datetime.fromtimestamp(
os.path.getmtime(curpath))
if datetime.now() - file_modified > timedelta(hours=age):
# print("DeleteOldFilesInFolder, curpath: ", curpath)
os.remove(curpath)
####
#
# Player
#
####
def Stop():
# print("Stop")
mixer.music.stop()
def Play():
# print("Play")
mixer.music.play()
def Load(filePath):
# print("Load", filePath)
mixer.music.load(filePath)
def SetVolume():
hourOfDay = datetime.now().time().hou
# print("SetVolume hourOfDay: ", hourOfDay)
if hourOfDay < 8 or hourOfDay > 22:
# print("SetVolume, to 70%")
mixer.music.set_volume(0.7)
else:
# print("SetVolume: 100")
mixer.music.set_volume(0.7)
def IsPlaying():
# print("IsPlaying")
isPlaying = mixer.music.get_busy()
# print("IsPlaying", isPlaying)
return isPlaying
def PlayRadio():
# print("PlayRadio")
if IsPlaying() == False:
Load(streamPath)
Play()
else:
Stop()
def PlayPodcast():
# print("PlayPodcast")
if IsPlaying() == False:
files = ReadListOfFiles(mediaPath)
indexToPlay = random.randint(0, len(files)-1)
# print("PlayPodcast, indexToPlay: ", indexToPlay)
Load(files[indexToPlay])
Play()
####
#
# Podcast management
#
####
def DownloadPodcasts():
# print("DownloadPodcasts")
call(["gpo", "update"])
call(["gpo", "download"])
def CleanPodcastDir():
# print("CleanPodcastDir")
# DeleteOldFilesInFolder(os.path.join(mediaPath, "Feinkost"), 24)
# DeleteOldFilesInFolder(os.path.join(mediaPath, "MissionEnergiewende"), 24)
DeleteOldFilesInFolder(os.path.join(mediaPath, "Nachrichten"), 1)
# DeleteOldFilesInFolder(os.path.join(mediaPath, "Ombudsmann"), 24)
####
#
# Sensors
#
####
def ReadChainSwitch():
global chainOldValue
chainNewValue = GPIO.input(chainPin)
# sleep(1.0)
if chainNewValue != chainOldValue:
# print("ReadChainSwitch, somebody pulls the chain)"
chainOldValue = chainNewValue
return True
else:
return False
# https://www.linuxtut.com/en/8e8f385f35976dcc65e8/
def ReadUltraSonicSensor():
bus.write_i2c_block_data(sensorAddress, 0x00, [0x51])
sleep(0.1)
block = bus.read_i2c_block_data(sensorAddress, 0x00, 6)
if(block[0] == 6):
distance = (block[2] << 8 | block[3])
else:
distance = (block[2] << 8 | block[3]) + block[4]
# print("ReadUltraSonicSensor, distance: ", distance)
if distance < 70: # and distance > 30:
# print("ReadUltraSonicSensor, somebody sits on the toilet: ", distance)
return True
else:
# print("ReadUltraSonicSensor, somebody leafs the toilet: ", distance)
return False
####
#
# Main
#
####
def Main():
GPIO.setmode(GPIO.BCM)
GPIO.setup(chainPin, GPIO.IN)
#print("Main, starting the mixer")
mixer.init()
while True:
if ReadUltraSonicSensor():
PlayPodcast()
elif ReadChainSwitch():
PlayRadio()
elif IsNextHour():
CleanPodcastDir()
DownloadPodcasts()
sleep(1.0)
Main()