Repository navigation
Expand file tree
/
Copy patholoMain.py
More file actions
608 lines (502 loc) · 27.3 KB
/
Copy patholoMain.py
File metadata and controls
608 lines (502 loc) · 27.3 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#-*-coding:utf-8-*-
### TODO:
# - *** headless start: edit wpa_supplicant.conf
## BUGS AND IMPROVEMENTS:
# - mode change to life when touched results in an infinite loop
# - volume is not linearly increasing
import os, traceback, math, sys, time, datetime, logging
import queue
from statistics import mean
import spotipy
import spotipy.util as util
import spotipy.oauth2 as oauth2
import dbFunctions as fn
from oloFunctions import *
import busio, digitalio, board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
import RPi.GPIO as gpio
import sh
sh.init()
################## Create Logger ##################
from logging.handlers import TimedRotatingFileHandler
logger = logging.getLogger("Main Log")
logger.setLevel(logging.INFO)
if os.name == 'nt':
log_file = "C:\tmp\main.log"
else:
log_file = "/home/pi/Desktop/olo/log_main/main.log"
directory = "/home/pi/Desktop/olo/log_main/"
if not os.path.exists(directory):
os.makedirs(directory, exist_ok=True)
open(log_file, 'a')
handler = TimedRotatingFileHandler(log_file, when="midnight", interval=1)
logger.addHandler(handler)
###################################################
################################### Auxiliary Functions ###################################
current_milli_time = lambda: int(round(time.time() * 1000))
timenow = lambda: str(datetime.datetime.now()).split('.')[0]
############################### Initialize Global Variables ###############################
# SPOTIFY AUTH
token = None
sp = None
try:
token = fn.refreshSpotifyAuthToken(spotifyUsername=sh.spotify_username, client_id=sh.spotify_client_id, client_secret=sh.spotify_client_secret, redirect_uri=sh.spotify_redirect_uri, scope=sh.spotify_scope)
except:
token = fn.getSpotifyAuthToken(spotifyUsername=sh.spotify_username, scope=sh.spotify_scope, client_id=sh.spotify_client_id, client_secret=sh.spotify_client_secret, redirect_uri=sh.spotify_redirect_uri)
sp = spotipy.Spotify(auth=token)
# DB connection
conn = fn.getDBConn(sh.dbname)
cur = conn.cursor()
# Constants
TOTALCOUNT = fn.getTotalCount(cur);
BUCKETSIZE = 16
SLIDEROFFSET = 15
TOTALBUCKETS = int(1024/BUCKETSIZE);
LIFEWINDOWSIZE = fn.getLifeWindowSize(cur);
BASELIFEOFFSET = fn.getBaseTimestamp(cur); # smallest timestamp in DB; the timestamp of the first music listening entry
BUCKETWIDTH_DAY = 1350 # 86400/64
BUCKETWIDTH_YEAR = 492750 # (86400*365)/64
BUCKETWIDTH_LIFE = int(math.ceil(LIFEWINDOWSIZE/64))
RETRY_MAX = 1;
# Status Variables
stablizeSliderPos = queue.Queue(maxsize=10) # average out 10 values - long window; larger than 10 may lead to a processing delay
stablizePinSliderPos = queue.Queue(maxsize=5) # average out 5 values - short window
currSliderPos = 0
currVolume = None # [0, 100]
currBucket = 0 # [0, 63]
currMode = None # ('life, 'year', 'day')
retry = 0
bucketWidth = 0
bucketCounter = []
songsInABucket = 0;
refVolume = 0
refBucket = None
refSliderPos = -1
refMode = None
prevSliderPos = 0
targetSliderPos = None
# Timers
startTime = 0
currSongTime = 0
currSongTimestamp = 0
moveTimer = None;
changeModeTimer = None;
# Status Flags
isPlaying = False
isOn = False
isMoving = False
fadeoutFlag = False
switchSongFlag = False
pauseWhenOffFlag = False
changeModeFlag = False
skipBucketFlag = False
# create the spi bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# create the cs (chip select)
if (sh.CS == 8):
cs = digitalio.DigitalInOut(board.D8)
else:
cs = digitalio.DigitalInOut(board.D5)
# create the mcp object
mcp = MCP.MCP3008(spi, cs)
# GPIO configuration:
gpio.setmode(gpio.BCM)
gpio.setup(sh.onoff, gpio.IN, pull_up_down=gpio.PUD_DOWN) # pin 17 - on/off switch
gpio.setup(sh.mEnable, gpio.OUT) # pin 6 - motor driver enable
gpio.setup(sh.mLeft, gpio.OUT) # pin 13 - motor driver direction 1
gpio.setup(sh.mRight, gpio.OUT) # pin 12 - motor driver direction 2
gpio.setup(sh.switch1, gpio.IN) # pin 16 - three pole switch 1
gpio.setup(sh.switch2, gpio.IN) # pin 18 - three pole switch 2
gpio.output(sh.mEnable, True) # Enable motor driver
# turn off other outputs:
gpio.output(sh.mLeft, False)
gpio.output(sh.mRight, False)
############################### Helper Functions ###############################
def formatMs(ms):
sec = ms // 1000
m = sec // 60
s = sec % 60
if (s < 10):
s_s = '0' + str(s)
else:
s_s = str(s)
return str(m) + ":" + s_s
def fadeout():
global sp, currVolume, fadeoutFlag, refVolume
refVolume = currVolume
while (refVolume > 0):
refVolume = int(refVolume/1.5)
sp.volume(refVolume, device_id = sh.device_oloradio)
fadeoutFlag = False
# returns the start time and the current song's playtime in ms
def playSongInBucket(offset):
global sp, currBucket, currMode, bucketWidth, bucketCounter, currVolume, songsInABucket, prevSliderPos
global currSongTimestamp, startTime, currSongTime, isPlaying
song = fn.getTrackFromBucket(cur, currMode, offset+(currBucket*bucketWidth), bucketCounter[currBucket])
songURI = song[9]
res = sp.track(songURI)
currSongTimestamp = song[0]
currSongTime = int(res['duration_ms'])
sp.start_playback(device_id = sh.device_oloradio, uris = [songURI])
sp.volume(int(currVolume), device_id=sh.device_oloradio)
bucketCounter[currBucket] += 1;
fn.updateBucketCounters(cur, currBucket, bucketCounter[currBucket], currMode, conn=conn);
isPlaying = True
startTime = current_milli_time()
print("[{}]: ######################################################################################################".format(timenow()))
print("[{}]: ## Now playing: {} - {} ({})".format(timenow(), song[2], song[1], formatMs(currSongTime)))
print("[{}]: ## @ Bucket[{}]: {} out of {} songs".format(timenow(), currBucket, bucketCounter[currBucket], songsInABucket))
print("[{}]: ## Slider is at {} in [{} ~ {}] (BucketWidth: {}, Offset: {} ~ {})".format(timenow(), currSliderPos, 16*currBucket, 16*(currBucket+1)-1, bucketWidth, offset + currBucket*bucketWidth, offset + (currBucket+1)*bucketWidth))
print("[{}]: ## Mode: {}, Volume: {}".format(timenow(), currMode, currVolume))
print("[{}]: ######################################################################################################".format(timenow()))
logger.info("[{}]: ######################################################################################################".format(timenow()))
logger.info("[{}]: ## Now playing: {} - {} ({})".format(timenow(), song[2], song[1], formatMs(currSongTime)))
logger.info("[{}]: ## @ Bucket[{}]: {} out of {} songs".format(timenow(), currBucket, bucketCounter[currBucket], songsInABucket))
logger.info("[{}]: ## Slider is at {} in [{} ~ {}] (BucketWidth: {}, Offset: {} ~ {})".format(timenow(), currSliderPos, 16*currBucket, 16*(currBucket+1)-1, bucketWidth, offset + currBucket*bucketWidth, offset + (currBucket+1)*bucketWidth))
logger.info("[{}]: ## Mode: {}, Volume: {}".format(timenow(), currMode, currVolume))
logger.info("[{}]: ######################################################################################################".format(timenow()))
# Move the slider to the next non-empty buckets, updates currBucket, currSliderPos and songsInABucket
def gotoNextNonEmptyBucket(offset):
global bucketCounter, currMode, currBucket, currSliderPos, bucketWidth, songsInABucket, isMoving, moveTimer, skipBucketFlag
blockedSliderFlag = False;
bucketCounter = fn.getBucketCounters(cur, currMode)
tmpBucket = int(math.floor(currSliderPos/16))
tmpSongsInABucket = fn.getBucketCount(cur, currMode, offset + tmpBucket*bucketWidth, offset + (tmpBucket+1)*bucketWidth)
tmpSliderPos2 = None;
print("[{}]: @@ Scanning bucket[{}]: played {} out of {} songs".format(timenow(), tmpBucket, bucketCounter[tmpBucket], tmpSongsInABucket))
logger.info("[{}]: @@ Scanning bucket[{}]: played {} out of {} songs".format(timenow(), tmpBucket, bucketCounter[tmpBucket], tmpSongsInABucket))
# placeholder for previous values for recovery when stuck
prevBucket = tmpBucket
prevSliderPos = currSliderPos
prevSongsInABucket = tmpSongsInABucket
stuckPosBucket = None
stuckSliderPos = None
# iterate to find non-empty bucket while skipping empty buckets
while (tmpSongsInABucket == 0 or bucketCounter[tmpBucket] > tmpSongsInABucket):
# empty overflowing buckets
if (bucketCounter[tmpBucket] > tmpSongsInABucket):
fn.updateBucketCounters(cur, tmpBucket, 0, currMode, conn=conn);
print("[{}]: @@ Skipping bucket[{}]!!".format(timenow(), tmpBucket))
logger.info("[{}]: @@ Skipping bucket[{}]!!".format(timenow(), tmpBucket))
tmpBucket += 1;
tmpBucket = tmpBucket % 64;
tmpSliderPos2 = (tmpBucket*BUCKETSIZE) + SLIDEROFFSET
tmpSongsInABucket = fn.getBucketCount(cur, currMode, offset + tmpBucket*bucketWidth, offset + (tmpBucket+1)*bucketWidth)
#
if (bucketCounter[tmpBucket] == tmpSongsInABucket):
fn.updateBucketCounters(cur, tmpBucket, 0, currMode, conn=conn);
# if a bucket is not empty, play the bucket
if (tmpBucket != currBucket and tmpSliderPos2 is not None):
isMoving = True
skipBucketFlag = True
moveTimer = current_milli_time()
res = moveslider(tmpSliderPos2)
if (res < 0):
# read raw pin value at the stuck position
stuckSliderPos = sh.values[7]
stuckPosBucket = int(math.floor(stuckSliderPos/16))
stuckSongsInABucket = fn.getBucketCount(cur, currMode, offset + stuckPosBucket*bucketWidth, offset + (stuckPosBucket+1)*bucketWidth)
print("@@ Slider got stuck at @{} (B[{}] has {} songs)!!".format(stuckSliderPos, stuckPosBucket, stuckSongsInABucket))
# if the slider is stuck at a non-empty bucket, play that bucket
if (stuckSongsInABucket > 0):
print("@@@@ stuck at a NON-EMPTY bucket; just play this bucket,,")
tmpBucket = stuckPosBucket
songsInABucket = stuckSongsInABucket
# if the slider is stuck at an empty bucket, recover to the previous position
else:
print("@@@@ stuck at an EMPTY bucket..!! recovering the position...")
tmpbucket = prevBucket
songsInABucket = prevSongsInABucket
blockedSliderFlag = True
currBucket = tmpBucket
songsInABucket = tmpSongsInABucket
# go back to the original non-empty bucket
if (blockedSliderFlag):
blockedSliderFlag = False
moveslider(prevSliderPos)
def checkValues():
print("[{}]: ##### total songs: {}".format(timenow(), TOTALCOUNT))
print("[{}]: ##### Life mode base value: {}".format(timenow(), BASELIFEOFFSET))
logger.info("[{}]: ##### total songs: {}".format(timenow(), TOTALCOUNT))
logger.info("[{}]: ##### Life mode base value: {}".format(timenow(), BASELIFEOFFSET))
global isOn, isMoving, isPlaying, fadeoutFlag, moveTimer, switchSongFlag, pauseWhenOffFlag, changeModeFlag, changeModeTimer, skipBucketFlag
global currVolume, currSliderPos, currBucket, currSongTime, startTime, currMode, currSongTimestamp
global bucketWidth, bucketCounter, songsInABucket, stablizeSliderPos, stablizePinSliderPos, refBucket, refSliderPos, refMode, prevSliderPos, targetSliderPos
global conn, cur, sp
if (conn is None):
conn = fn.getDBConn(sh.dbname)
if (cur is None):
cur = conn.cursor()
while (True):
### read values
readValues();
timeframe();
pin_Volume = sh.values[4];
pin_Touch = sh.values[6]
pin_Mode = sh.timeframe
pin_SliderPos = sh.values[7]
isOn = gpio.input(sh.onoff)
# average values to get stablized slider position
if (pin_Touch < 100):
# long window
if (stablizeSliderPos.full()):
stablizeSliderPos.get()
stablizeSliderPos.put(pin_SliderPos)
avgPos = int(mean(list(stablizeSliderPos.queue)))
# short window
if (stablizePinSliderPos.full()):
stablizePinSliderPos.get()
stablizePinSliderPos.put(pin_SliderPos)
avgPinPos = int(mean(list(stablizePinSliderPos.queue)))
currSliderPos = avgPos;
currBucket = int(math.floor(currSliderPos/16))
if (refSliderPos < 0):
refSliderPos = currSliderPos
# Set initial offset and bucket width
# *** Offset is the life timestamp of the earliest entry in the entire listing history
offset = BASELIFEOFFSET
bucketWidth = BUCKETWIDTH_LIFE
if (pin_Mode == 'day'):
offset = 0;
bucketWidth = BUCKETWIDTH_DAY
elif (pin_Mode == 'year'):
offset = 0;
bucketWidth = BUCKETWIDTH_YEAR
# load bucket counters
bucketCounter = fn.getBucketCounters(cur, pin_Mode)
# Initialize volume
if (currVolume is None):
if (sh.OLO_ID == 1 or sh.OLO_ID == 3):
currVolume = int(pin_Volume/10);
else:
currVolume = int(linearlizeVolume(pin_Volume)/10);
if (pin_Touch < 100):
tmpSliderPos = avgPinPos
tmpBucket = int(math.floor(tmpSliderPos/16))
if (sh.OLO_ID == 1 or sh.OLO_ID == 3):
tmpVolume = int(pin_Volume/10)
else:
tmpVolume = int(linearlizeVolume(pin_Volume)/10);
# OLO is OFF
if (not isOn):
isPlaying = False
# gently turn off
if (currVolume > 0):
fadeoutFlag = True
if (fadeoutFlag):
fadeout()
if (pauseWhenOffFlag):
sp.pause_playback(device_id = sh.device_oloradio)
pauseWhenOffFlag = False
# OLO is ON
else:
if (not pauseWhenOffFlag):
pauseWhenOffFlag = True
if (currMode is None):
currMode = pin_Mode
### TODO: skipping buckets when NOT playing does redundant & consecutive scans as it reads each slider pos
### TODO: mode change -> move will stuck, not playing a song at new position
if (isMoving):
if (abs(refSliderPos - tmpSliderPos) > 10 and refBucket != tmpBucket):
print("[{}]: @@ Keep moving.. reset moveTimer: currPos: {}, tmpPos: {}".format(timenow(), currSliderPos, tmpSliderPos))
logger.info("[{}]: @@ Keep moving.. reset moveTimer: currPos: {}, tmpPos: {}".format(timenow(), currSliderPos, tmpSliderPos))
moveTimer = current_milli_time()
refBucket = tmpBucket;
refSliderPos = tmpSliderPos;
# the slider is stopped at a fixed position for more than 0.4s
if (abs(refSliderPos - tmpSliderPos) < 10 and refBucket == tmpBucket and (current_milli_time() - moveTimer) > 400):
print("[{}]: @@ Movement stopped!".format(timenow()))
logger.info("[{}]: @@ Movement stopped!".format(timenow()))
isMoving = False
if (not changeModeFlag):
switchSongFlag = True
else:
if (abs(currSliderPos - targetSliderPos) > 10):
switchSongFlag = True
fadeout()
changeModeFlag = False
changeModeTimer = None
if (skipBucketFlag):
skipBucketFlag = False
moveTimer = None
refBucket = currBucket
refSliderPos = currSliderPos
prevSliderPos = currSliderPos
currBucket = tmpBucket
print("[{}]: @@ Slider stopped at {} in bucket {}, currSliderPos: {}, refPos: {}".format(timenow(), pin_SliderPos, tmpBucket, currSliderPos, refSliderPos))
logger.info("[{}]: @@ Slider stopped at {} in bucket {}, currSliderPos: {}, refPos: {}".format(timenow(), pin_SliderPos, tmpBucket, currSliderPos, refSliderPos))
# OLO is on but the music is not playing (either OLO is just turned on or a song has just finished)
if (not isPlaying):
currMode = pin_Mode;
prevSliderPos = currSliderPos
gotoNextNonEmptyBucket(offset)
# do not start playing while skipping buckets
if (not skipBucketFlag):
playSongInBucket(offset)
switchSongFlag = False
# OLO is playing a song
else:
# Observe if the slider is moved. Must satisfy BOTH conditions to be considered as "moved".
# The slider is..
# i) moved more than the threshold of 12
# AND
# ii) moved to a different bucket
if (not isMoving and (abs(prevSliderPos - currSliderPos) > 10 or (currBucket != tmpBucket and abs(currSliderPos - tmpSliderPos) > 7))):
print("[{}]: @@ Movement detected: currPos: {}, tmpPos: {}, B[{}] -> B[{}]".format(timenow(), currSliderPos, tmpSliderPos, currBucket, tmpBucket))
logger.info("[{}]: @@ Movement detected: currPos: {}, tmpPos: {}".format(timenow(), currSliderPos, tmpSliderPos))
isMoving = True
refBucket = currBucket
refSliderPos = currSliderPos
if (moveTimer is None):
print("[{}]: @@@@ Setting a moveTimer".format(timenow()))
logger.info("[{}]: @@@@ Setting a moveTimer".format(timenow()))
moveTimer = current_milli_time()
if (not changeModeFlag):
fadeoutFlag = True
if (fadeoutFlag):
print("[{}]: @@@@ fading out...".format(timenow()))
logger.info("[{}]: @@@@ fading out...".format(timenow()))
fadeout();
# Slider is not moving or finished moving
if (not isMoving):
# Volume change
if (not fadeoutFlag and abs(currVolume - tmpVolume) > 5):
print("[{}]: @@ Volume change! {} -> {}".format(timenow(), currVolume, tmpVolume))
logger.info("[{}]: @@ Volume change! {} -> {}".format(timenow(), currVolume, tmpVolume))
currVolume = tmpVolume
if (currVolume > 100):
currVolume = 100;
sp.volume(int(currVolume), device_id=sh.device_oloradio)
### 2!!
if (switchSongFlag):
currMode = pin_Mode # silently update the mode when changed while moving
gotoNextNonEmptyBucket(offset)
# do not start playing while skipping buckets
if (not skipBucketFlag):
playSongInBucket(offset)
switchSongFlag = False
# Mode change
# * do not change the mode when touched
if (pin_Touch < 100 and currMode != pin_Mode):
changeModeFlag = True
refMode = pin_Mode
if (changeModeTimer is None):
print('[{}]: @@@ Mode Changed detected {} -> {}. Setting the timer!'.format(timenow(), refMode, pin_Mode))
logger.info('[{}]: @@@ Mode Changed detected {} -> {}. Setting the timer!'.format(timenow(), refMode, pin_Mode))
changeModeTimer = current_milli_time()
if (pin_Mode == 'err'):
continue;
# detect mode change
if (pin_Mode is not None and refMode is not None and refMode != pin_Mode):
print('[{}]: @@@ Another mode changed detected {} -> {}. Reset the timer!'.format(timenow(), refMode, pin_Mode))
logger.info('[{}]: @@@ Another mode changed detected {} -> {}. Reset the timer!'.format(timenow(), refMode, pin_Mode))
changeModeTimer = current_milli_time()
refMode = pin_Mode
# wait for 0.4s in case of rapid multiple mode changes
if (changeModeFlag and changeModeTimer is not None and (current_milli_time() - changeModeTimer > 400)):
changeModeTimer = None;
# change position only when the updated mode is different from the previous mode
if (refMode != currMode):
print('[{}]: @@@ Mode Changed!! {} -> {} '.format(timenow(), currMode, pin_Mode))
logger.info('[{}]: @@@ Mode Changed!! {} -> {} '.format(timenow(), currMode, pin_Mode))
prevMode = currMode
currMode = pin_Mode
refMode = None
# reset the bucketWidth
if (currMode == 'day'):
offset = 0;
bucketWidth = BUCKETWIDTH_DAY
elif (currMode == 'year'):
offset = 0;
bucketWidth = BUCKETWIDTH_YEAR
else:
offset = BASELIFEOFFSET
bucketWidth = BUCKETWIDTH_LIFE
bucketCounter = fn.getBucketCounters(cur, pin_Mode)
# get the new index based on the mode
indices = fn.findTrackIndex(cur, currMode, currSongTimestamp) # (INDEX, year, month, timeofday, month_offset, day_offset)
if (currMode == 'day'):
index = indices[5]
elif (currMode == 'year'):
index = indices[4]
else:
index = currSongTimestamp - offset
generalIndex = int(indices[0])-1 # index is 1 less than the order number
currBucket = int(math.floor(index/bucketWidth))
songsInABucket = fn.getBucketCount(cur, currMode, offset + currBucket*bucketWidth, offset + (currBucket+1)*bucketWidth)
targetSliderPos = (currBucket*BUCKETSIZE) + SLIDEROFFSET
res = moveslider(targetSliderPos)
# slider got stuck while changing mode
if (res < 0):
stuckSliderPos = sh.values[7]
stuckPosBucket = int(math.floor(stuckSliderPos/16))
stuckSongsInABucket = fn.getBucketCount(cur, currMode, offset + stuckPosBucket*bucketWidth, offset + (stuckPosBucket+1)*bucketWidth)
print("[{}]: WARNING!! Slider got stuck @{} while changing mode {} -> {}".format(timenow(), stuckSliderPos, prevMode, currMode))
logger.info("[{}]: WARNING!! Slider got stuck @{} while changing mode {} -> {}".format(timenow(), stuckSliderPos, prevMode, currMode))
# update bucket index and depth at the stuck position
currBucket = stuckPosBucket
songsInABucket = stuckSongsInABucket
print("[{}]: @@ Stuck at B[{}] in {} mode!! Playing {} out of {} songs".format(timenow(), stuckPosBucket, currMode, bucketCounter[currBucket], songsInABucket))
logger.info("[{}]: @@ Stuck at B[{}] in {} mode!! Playing {} out of {} songs".format(timenow(), stuckPosBucket, currMode, bucketCounter[currBucket], songsInABucket))
else:
print("[{}]: @@ New index: {} / {} in {} mode,, playing {} out of {} songs".format(timenow(), generalIndex, TOTALCOUNT, currMode, bucketCounter[currBucket], songsInABucket))
logger.info("[{}]: @@ New index: {} / {} in {} mode,, playing {} out of {} songs".format(timenow(), generalIndex, TOTALCOUNT, currMode, bucketCounter[currBucket], songsInABucket))
else:
# clean up temp variable and flag
changeModeFlag = False
refMode = None
# a song has ended
# wait 2 seconds to compensate processing time
if ((current_milli_time() - startTime) > currSongTime + 2000):
print("[{}]: @@ The song has ended!".format(timenow()))
logger.info("[{}]: @@ The song has ended!".format(timenow()))
isPlaying = False;
currSongTime = sys.maxsize
def stop():
sys.exit()
# -------------------------
def main():
global retry, conn, cur, isPlaying, isMoving, sp
# fn.wait_for_internet_connection()
while True:
try:
print("[{}]: ### Main is starting..".format(timenow()))
logger.info("[{}]: ### Main is starting..".format(timenow()))
checkValues()
except (KeyboardInterrupt, SystemExit):
hardstop()
conn.close()
raise
except:
print(traceback.format_exc())
print("[{}]: !! Acquiring new token,,".format(timenow()))
logger.info(traceback.format_exc())
logger.info("[{}]: !! Acquiring new token,,".format(timenow()))
try:
token = fn.refreshSpotifyAuthTOken(spotifyUsername=sh.spotify_username, client_id=sh.spotify_client_id, client_secret=sh.spotify_client_secret, redirect_uri=sh.spotify_redirect_uri, scope=sh.spotify_scope)
sp = spotipy.Spotify(auth=token)
except:
print("[{}]: !! Try restarting Raspotify, Sleeping for 1 second..".format(timenow()));
logger.info("[{}]: !! Try restarting Raspotify, Sleeping for 1 second..".format(timenow()))
# restart raspotify just in case
os.system("sudo systemctl restart raspotify")
time.sleep(1)
retry += 1;
if (retry >= RETRY_MAX):
print("[{}]: !!!! Couldn't refresh token,, restarting the script..".format(timenow()))
logger.info("[{}]: !!!! Couldn't refresh token,, restarting the script..".format(timenow()))
# restart the program
python = sys.executable
os.execl(python, python, * sys.argv)
continue;
conn.close()
conn = None
cur = None
isPlaying = False;
isMoving = False;
print("[{}]: !! Sleeping for 3 seconds,, Retry: {}".format(timenow(), retry))
logger.info("[{}]: !! Sleeping for 3 seconds,, Retry: {}".format(timenow(), retry))
time.sleep(3)
continue;
if __name__ == "__main__": main()