-
Notifications
You must be signed in to change notification settings - Fork 3
/
pipresents.py
1009 lines (808 loc) · 38.7 KB
/
pipresents.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
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/env python3
"""
Pi Presents is a toolkit for constructing and deploying multimedia interactive presentations
on the Raspberry Pi.
It is aimed at primarily at musems, exhibitions and galleries
but has many other applications including digital signage
Version 1.5 [pipresents-kms]
Copyright 2012/2013/2014/2015/2016/2017/2018/2019/2020/2021/2022/2023, Ken Thompson
See github for licence conditions
See readme.md and manual.pdf for instructions.
"""
import sys
if sys.version_info[0] != 3:
sys.stdout.write("ERROR: Pi Presents requires python 3\nHint: python3 pipresents.py .......\n")
exit(102)
import os
import signal
from subprocess import call, check_output
import time
import gc
from tkinter import Tk, Canvas
import tkinter.messagebox
from time import sleep
# import objgraph
from pp_options import command_options
from pp_displaymanager import DisplayManager
from pp_showlist import ShowList
from pp_showmanager import ShowManager
from pp_screendriver import ScreenDriver
from pp_timeofday import TimeOfDay
from pp_utils import Monitor
from pp_utils import StopWatch
from pp_animate import Animate
from pp_oscdriver import OSCDriver
from pp_network import Mailer, Network
from pp_iopluginmanager import IOPluginManager
from pp_countermanager import CounterManager
from pp_beepplayer import BeepPlayer
from pp_audiomanager import AudioManager
from pp_vlcdriver import Logger
class PiPresents(object):
def pipresents_version(self):
vitems=self.pipresents_issue.split('.')
if len(vitems)==2:
# cope with 2 digit version numbers before 1.3.2
return 1000*int(vitems[0])+100*int(vitems[1])
else:
return 1000*int(vitems[0])+100*int(vitems[1])+int(vitems[2])
def __init__(self):
# gc.set_debug(gc.DEBUG_UNCOLLECTABLE|gc.DEBUG_INSTANCES|gc.DEBUG_OBJECTS|gc.DEBUG_SAVEALL)
gc.set_debug(gc.DEBUG_UNCOLLECTABLE|gc.DEBUG_SAVEALL)
self.pipresents_issue="1.5.3"
self.pipresents_minorissue = '1.5.3f'
StopWatch.global_enable=False
# set up the handler for SIGTERM
signal.signal(signal.SIGTERM,self.handle_sigterm)
# ****************************************
# Initialisation
# ***************************************
# get command line options
self.options=command_options()
# print (self.options)
# get Pi Presents code directory
pp_dir=sys.path[0]
self.pp_dir=pp_dir
if not os.path.exists(pp_dir+"/pipresents.py"):
if self.options['manager'] is False:
tkinter.messagebox.showwarning("Pi Presents","Bad Application Directory")
exit(102)
# Initialise logging and tracing
# initlize VLCDriver logger
self.logger=Logger(enabled=True)
self.logger.init()
# TURN OFF REST OF LOGGING IN pp_vlcdriver.py
# Init main monitor
Monitor.log_path=pp_dir
self.mon=Monitor()
# Init in PiPresents only
self.mon.init()
# uncomment to enable control of logging from within a class
# Monitor.enable_in_code = True # enables control of log level in the code for a class - self.mon.set_log_level()
# make a shorter list to log/trace only some classes without using enable_in_code.
Monitor.classes = ['PiPresents',
'HyperlinkShow','RadioButtonShow','ArtLiveShow','ArtMediaShow','MediaShow','LiveShow','MenuShow',
'GapShow','Show','ArtShow',
'VibePlayer','AudioPlayer','ImagePlayer','MenuPlayer','MessagePlayer','Player','VLCPlayer','ChromePlayer',
'MediaList','LiveList','ShowList',
'PathManager','ControlsManager','ShowManager','TrackPluginManager','IOPluginManager',
'MplayerDriver','MPVPlayer','MPVDriver',
'TimeOfDay','ScreenDriver','Animate','OSCDriver','CounterManager',
'Network','Mailer',
'pp_gpiozerodriver']
# Monitor.classes=['MediaShow','GapShow','Show']
# Monitor.classes=['OSCDriver']
# get global log level from command line
Monitor.log_level = int(self.options['debug'])
Monitor.manager = self.options['manager']
# print self.options['manager']
self.mon.newline(3)
self.mon.sched (self,None, "Pi Presents is starting, Version:"+self.pipresents_minorissue + ' at '+time.strftime("%Y-%m-%d %H:%M.%S"))
self.mon.log (self, "Pi Presents is starting, Version:"+self.pipresents_minorissue+ ' at '+time.strftime("%Y-%m-%d %H:%M.%S"))
# self.mon.log (self," OS and separator:" + os.name +' ' + os.sep)
self.mon.log(self,"sys.path[0] - location of code: "+sys.path[0])
# log version of RPi OS
if os.path.exists("/boot/issue.txt"):
ifile=open("/boot/issue.txt")
self.mon.log(self,'\nRaspberry Pi OS: '+ifile.read())
elif os.path.exists("/boot/firmware/issue.txt"):
ifile=open("/boot/firmware/issue.txt")
self.mon.log(self,'\nRaspberry Pi OS: '+ifile.read())
else:
pass
# log GPU memory
self.mon.log(self,'\nGPU Memory: '+ check_output(["vcgencmd", "get_mem", "gpu"],universal_newlines=True))
if os.geteuid() == 0:
print('Do not run Pi Presents with sudo')
self.mon.log(self,'Do not run Pi Presents with sudo')
self.mon.finish()
sys.exit(102)
if "DESKTOP_SESSION" not in os.environ:
print('Pi Presents must be run from the Desktop')
self.mon.log(self,'Pi Presents must be run from the Desktop')
self.mon.finish()
sys.exit(102)
else:
self.mon.log(self,'Desktop is '+ os.environ['DESKTOP_SESSION'])
# optional other classes used
self.root=None
self.ppio=None
self.tod=None
self.dm=None
self.animate=None
self.ioplugin_manager=None
self.oscdriver=None
self.osc_enabled=False
self.tod_enabled=False
self.email_enabled=False
user=os.getenv('USER')
if user is None:
tkinter.messagebox.showwarning("You must be logged in to run Pi Presents")
exit(102)
#if user !='pi':
#self.mon.warn(self,"You must be logged as pi to use GPIO")
self.mon.log(self,'User is: '+ user)
# self.mon.log(self,"os.getenv('HOME') - user home directory (not used): " + os.getenv('HOME')) # does not work
# self.mon.log(self,"os.path.expanduser('~') - user home directory: " + os.path.expanduser('~')) # does not work
# check network is available
self.network_connected=False
self.network_details=False
self.interface=''
self.ip=''
self.unit=''
# sets self.network_connected and self.network_details
self.init_network()
# start the mailer and send email when PP starts
self.email_enabled=False
if self.network_connected is True:
self.init_mailer()
if self.email_enabled is True and self.mailer.email_at_start is True:
subject= '[Pi Presents] ' + self.unit + ': PP Started on ' + time.strftime("%Y-%m-%d %H:%M")
message = time.strftime("%Y-%m-%d %H:%M") + '\nUnit: ' + self.unit + ' Profile: '+ self.options['profile']+ '\n ' + self.interface + '\n ' + self.ip
self.send_email('start',subject,message)
# get profile path from -p option
if self.options['profile'] != '':
self.pp_profile_path="/pp_profiles/"+self.options['profile']
else:
self.mon.err(self,"Profile not specified in command ")
self.end('error','Profile not specified with the commands -p option')
# get directory containing pp_home from the command,
if self.options['home'] == "":
home = os.sep+ 'home' + os.sep + user + os.sep+"pp_home"
else:
home = self.options['home'] + os.sep+ "pp_home"
self.mon.log(self,"pp_home directory is: " + home)
# check if pp_home exists.
# try for 10 seconds to allow usb stick to automount
found=False
for i in range (1, 10):
self.mon.log(self,"Trying pp_home at: " + home + " (" + str(i)+')')
if os.path.exists(home):
found=True
self.pp_home=home
break
time.sleep (1)
if found is True:
self.mon.log(self,"Found Requested Home Directory, using pp_home at: " + home)
else:
self.mon.err(self,"Failed to find pp_home directory at " + home)
self.end('error',"Failed to find pp_home directory at " + home)
# check profile exists
self.pp_profile=self.pp_home+self.pp_profile_path
if os.path.exists(self.pp_profile):
self.mon.sched(self,None,"Running profile: " + self.pp_profile_path)
self.mon.log(self,"Found Requested profile - pp_profile directory is: " + self.pp_profile)
else:
self.mon.err(self,"Failed to find requested profile: "+ self.pp_profile)
self.end('error',"Failed to find requested profile: "+ self.pp_profile)
self.mon.start_stats(self.options['profile'])
# initialise and read the showlist in the profile
self.showlist=ShowList()
self.showlist_file= self.pp_profile+ "/pp_showlist.json"
if os.path.exists(self.showlist_file):
self.showlist.open_json(self.showlist_file)
else:
self.mon.err(self,"showlist not found at "+self.showlist_file)
self.end('error',"showlist not found at "+self.showlist_file)
# check profile and Pi Presents issues are compatible
if self.showlist.profile_version() != self.pipresents_version():
self.mon.err(self,"Version of showlist " + self.showlist.profile_version_string + " is not same as Pi Presents")
self.end('error',"Version of showlist " + self.showlist.profile_version_string + " is not same as Pi Presents")
# get the 'start' show from the showlist
index = self.showlist.index_of_start_show()
if index >=0:
self.showlist.select(index)
self.starter_show=self.showlist.selected_show()
else:
self.mon.err(self,"Show [start] not found in showlist")
self.end('error',"Show [start] not found in showlist")
# ********************
# SET UP THE GUI
# ********************
# turn off the screenblanking and saver
if self.options['noblank'] is True:
call(["xset","s", "off"])
#call(["xset","s", "-dpms"])
call(["xset","s", "noblank"])
# find connected displays and create a canvas for each display
self.dm=DisplayManager()
status,message,self.root=self.dm.init(self.options,self.handle_user_abort,self.pp_dir,False)
if status != 'normal':
self.mon.err(self,message)
self.end('error',message)
self.mon.log(self,str(DisplayManager.num_displays)+ ' Displays are connected:')
for display_id in DisplayManager.displays:
if self.dm.has_canvas(display_id):
canvas_obj= self.dm.canvas_widget(display_id)
canvas_obj.config(bg=self.starter_show['background-colour'])
name=self.dm.name_of_display(display_id)
width,height=self.dm.real_display_dimensions(display_id)
x,y=self.dm.real_display_position(display_id)
self.mon.log(self,' - '+ name + ' Id: '+str(display_id) + ' '+str(x)+'+'+str(y)+'+'+str(width)+'*'+str(height))
if self.dm.does_display_overlap():
self.mon.log(self,'WARNING, Two or More displays overlap')
# ****************************************
# INITIALISE THE TOUCHSCREEN DRIVER
# ****************************************
# each driver takes a set of inputs, binds them to symboic names
# and sets up a callback which returns the symbolic name when an input event occurs
self.sr=ScreenDriver()
# read the screen click area config file
reason,message = self.sr.read(pp_dir,self.pp_home,self.pp_profile)
if reason == 'error':
self.end('error','cannot find, or error in screen.cfg')
# create click areas on the canvases, must be polygon as outline rectangles are not filled as far as find_closest goes
reason,message = self.sr.make_click_areas(self.handle_input_event)
if reason == 'error':
self.mon.err(self,message)
self.end('error',message)
# ****************************************
# INITIALISE THE APPLICATION AND START
# ****************************************
self.shutdown_required=False
self.reboot_required=False
self.terminate_required=False
self.exitpipresents_required=False
self.restartpipresents_required=False
#initialise the Audio manager
self.audiomanager=AudioManager()
status,message=self.audiomanager.init(self.pp_dir)
if status == 'error':
self.mon.err(self,message)
self.end('error',message)
self.mon.log(self,'Audio Devices:')
self.mon.log(self,'Name'+' Connected'+' Sink Name')
for name in AudioManager.profile_names:
status,message,sink_name=self.audiomanager.get_sink(name)
if status=='normal':
sink=sink_name
if sink=='':
sink='sink not defined, taskbar device will be used '
connected = ' '
else:
sink='ERROR '+message
conn= self.audiomanager.sink_connected(sink)
if conn:
connected='yes'
else:
connected ='No'
self.mon.log(self,name +' '+ connected + ' '+ sink)
# initialise the Beeps Player
self.bp=BeepPlayer()
self.bp.init(self.pp_home,self.pp_profile)
if self.options['vibes'] is True:
# test I2C is enabled
com=['sudo' ,'raspi-config' ,'nonint' ,'get_i2c']
result= check_output(com,universal_newlines=True)
#print ('I2C-pp',result)
if int(result) == 1:
#print ('pp-error')
self.mon.err(self,"\nI2C interface must be enabled for Vibes")
self.end('error',"I2C interface must be enabled for Vibes")
else:
#init vibe player
from pp_vibeplayer import VibePlayer
print ('pp _init vibeplayer')
self.vp=VibePlayer()
# initialise the I/O plugins by importing their drivers
self.ioplugin_manager=IOPluginManager()
reason,message=self.ioplugin_manager.init(self.pp_dir,self.pp_profile,self.root,self.handle_input_event,self.pp_home)
if reason == 'error':
self.mon.err(self,message)
self.end('error',message)
# kick off animation sequencer
self.animate = Animate()
self.animate.init(pp_dir,self.pp_home,self.pp_profile,self.root,200,self.handle_output_event)
self.animate.poll()
#create a showmanager ready for time of day scheduler and osc server
show_id=-1
self.show_manager=ShowManager(show_id,self.showlist,self.starter_show,self.root,self.pp_dir,self.pp_profile,self.pp_home)
# first time through set callback to terminate Pi Presents if all shows have ended.
self.show_manager.init(self.all_shows_ended_callback,self.handle_command,self.showlist)
# Register all the shows in the showlist
reason,message=self.show_manager.register_shows()
if reason == 'error':
self.mon.err(self,message)
self.end('error',message)
# Init OSCDriver, read config and start OSC server
self.osc_enabled=False
if self.network_connected is True:
if os.path.exists(self.pp_profile + os.sep + 'pp_io_config'+ os.sep + 'osc.cfg'):
self.oscdriver=OSCDriver()
reason,message=self.oscdriver.init(self.pp_profile,
self.unit,self.interface,self.ip,
self.handle_command,self.handle_input_event,self.e_osc_handle_animate)
if reason == 'error':
self.mon.err(self,message)
self.end('error',message)
else:
self.osc_enabled=True
self.root.after(1000,self.oscdriver.start_server())
# initialise ToD scheduler calculating schedule for today
self.tod=TimeOfDay()
reason,message,self.tod_enabled = self.tod.init(pp_dir,self.pp_home,self.pp_profile,self.showlist,self.root,self.handle_command)
if reason == 'error':
self.mon.err(self,message)
self.end('error',message)
# warn if the network not available when ToD required
if self.tod_enabled is True and self.network_connected is False:
self.mon.warn(self,'Network not connected so Time of Day scheduler may be using the internal clock')
# init the counter manager
self.counter_manager=CounterManager()
if self.starter_show['counters-store'] == 'yes':
store_enable=True
else:
store_enable=False
reason,message=self.counter_manager.init(self.pp_profile + '/counters.cfg',
store_enable, self.options['loadcounters'],self.starter_show['counters-initial'])
if reason == 'error':
self.mon.err(self,message)
self.end('error',message)
# warn about start shows and scheduler
if self.starter_show['start-show']=='' and self.tod_enabled is False:
self.mon.sched(self,None,"No Start Shows in Start Show and no shows scheduled")
self.mon.warn(self,"No Start Shows in Start Show and no shows scheduled")
if self.starter_show['start-show'] !='' and self.tod_enabled is True:
self.mon.sched(self,None,"Start Shows in Start Show and shows scheduled - conflict?")
self.mon.warn(self,"Start Shows in Start Show and shows scheduled - conflict?")
# run the start shows
self.run_start_shows()
# kick off the time of day scheduler which may run additional shows
if self.tod_enabled is True:
self.tod.poll()
# start the I/O plugins input event generation
self.ioplugin_manager.start()
# start Tkinters event loop
self.root.mainloop( )
# *********************
# RUN START SHOWS
# ********************
def run_start_shows(self):
self.mon.trace(self,'run start shows')
# parse the start shows field and start the initial shows
show_refs=self.starter_show['start-show'].split()
for show_ref in show_refs:
reason,message=self.show_manager.control_a_show(show_ref,'open')
if reason == 'error':
self.mon.err(self,message)
self.terminate()
#self.end(reason,message)
# *********************
# User inputs
# ********************
def e_osc_handle_animate(self,line):
#jump out of server thread
self.root.after(1, lambda arg=line: self.osc_handle_animate(arg))
def osc_handle_animate(self,line):
self.mon.log(self,"animate command received: "+ line)
#osc sends output events as a string
reason,message,delay,name,param_type,param_values=self.animate.parse_animate_fields(line)
if reason == 'error':
self.mon.err(self,message)
self.end(reason,message)
self.handle_output_event(name,param_type,param_values,0)
def show_control_handle_animate(self,line):
self.mon.log(self,"animate show control command received: "+ line)
line = '0 ' + line
reason,message,delay,name,param_type,param_values=self.animate.parse_animate_fields(line)
# print (reason,message,delay,name,param_type,param_values)
if reason == 'error':
self.mon.err(self,message)
self.end(reason,message)
self.handle_output_event(name,param_type,param_values,0)
# output events are animate commands
def handle_output_event(self,symbol,param_type,param_values,req_time):
reason,message=self.ioplugin_manager.handle_output_event(symbol,param_type,param_values,req_time)
if reason =='error':
self.mon.err(self,message)
self.end(reason,message)
# all input events call this callback providing a symbolic name.
# handle events that affect PP overall, otherwise pass to all active shows
def handle_input_event(self,symbol,source):
self.mon.log(self,"event received: "+symbol + ' from '+ source)
if symbol == 'pp-terminate':
self.handle_user_abort()
elif symbol == 'pp-shutdown':
self.mon.err(self,'pp-shutdown removed in version 1.3.3a, see Release Notes')
self.end('error','pp-shutdown removed in version 1.3.3a, see Release Notes')
elif symbol == 'pp-shutdownnow':
# need root.after to get out of st thread
self.root.after(1,self.shutdownnow_pressed)
return
elif symbol == 'pp-exitpipresents':
self.exitpipresents_required=True
if self.show_manager.all_shows_exited() is True:
# need root.after to grt out of st thread
self.root.after(1,self.e_all_shows_ended_callback)
return
reason,message= self.show_manager.exit_all_shows()
elif symbol == 'pp-restartpipresents':
self.restartpipresents_required=True
if self.show_manager.all_shows_exited() is True:
# need root.after to grt out of st thread
self.root.after(1,self.e_all_shows_ended_callback)
return
reason,message= self.show_manager.exit_all_shows()
else:
# pass the input event to all registered shows
for show in self.show_manager.shows:
show_obj=show[ShowManager.SHOW_OBJ]
if show_obj is not None:
show_obj.handle_input_event(symbol)
# commands are generated by tracks and shows
# they can open or close shows, generate input events and do special tasks
# commands also generate osc outputs to other computers
# handles one command provided as a line of text
def handle_command(self,command_text,source='',show=''):
# print 'PIPRESENTS ',command_text,'\n Source',source,'from',show
self.mon.log(self,"command received: " + command_text)
if command_text.strip()=="":
return
fields= command_text.split()
if fields[0] in ('osc','OSC'):
if self.osc_enabled is True:
status,message=self.oscdriver.parse_osc_command(fields[1:])
if status=='warn':
self.mon.warn(self,message)
if status=='error':
self.mon.err(self,message)
self.end('error',message)
return
else:
return
if fields[0] =='counter':
status,message=self.counter_manager.parse_counter_command(fields[1:])
if status=='error':
self.mon.err(self,message)
self.end('error',message)
return
if fields[0]=='beep':
status,message=self.bp.play_show_beep(command_text)
if status == 'error':
self.mon.err(self,message)
self.end('error',message)
return
return
if fields[0]=='vibe':
if self.options['vibes'] is True:
status,message=self.vp.play_show_vibe(command_text)
if status == 'error':
self.mon.err(self,message)
self.end('error',message)
return
return
else:
self.mon.err(self,'Enable vibes using --vibes command line option')
self.end('error','')
return
if fields[0]=='backlight':
# on, off, inc val, dec val, set val fade val duration
status,message=self.dm.do_backlight_command(command_text)
if status == 'error':
self.mon.err(self,message)
self.end('error',message)
return
return
if fields[0] =='monitor':
status,message = self.dm.handle_monitor_command(fields[1:])
if status == 'error':
self.mon.err(self,message)
self.end('error',message)
return
return
if fields[0] =='cec':
status,message=self.handle_cec_command(fields[1:])
if status == 'error':
self.mon.err(self,message)
self.end('error',message)
return
return
if fields[0] =='animate':
self.show_control_handle_animate(' '.join(fields[1:]))
return
# show commands
show_command=fields[0]
if len(fields)>1:
show_ref=fields[1]
else:
show_ref=''
if show_command in ('open','close','closeall','openexclusive'):
self.mon.sched(self, TimeOfDay.now,command_text + ' received from show:'+show)
if self.shutdown_required is False and self.terminate_required is False:
reason,message=self.show_manager.control_a_show(show_ref,show_command)
else:
return
elif show_command == 'event':
self.handle_input_event(show_ref,'Show Control')
return
elif show_command == 'exitpipresents':
self.exitpipresents_required=True
if self.show_manager.all_shows_exited() is True:
# need root.after to get out of st thread
self.root.after(1,self.e_all_shows_ended_callback)
return
else:
reason,message= self.show_manager.exit_all_shows()
elif show_command == 'shutdownnow':
# need root.after to get out of st thread
self.root.after(1,self.shutdownnow_pressed)
return
elif show_command == 'reboot':
# need root.after to get out of st thread
self.root.after(1,self.reboot_pressed)
return
else:
reason='error'
message = 'command not recognised: '+ show_command
if reason=='error':
self.mon.err(self,message)
self.end('error',message)
return
return
def handle_cec_command(self,args):
if len(args)==0:
return 'error','no arguments for CEC command'
if len(args)==1:
device='0'
if args[0] == 'scan':
com = 'echo scan | cec-client -s -d 1'
#print (com)
os.system(com)
return 'normal',''
if args[0] == 'as':
com = 'echo as | cec-client -s -d 1'
#print (com)
os.system(com)
return 'normal',''
if len(args)==2:
device = args[1]
if not device.isdigit():
return 'error', 'device is not a positive integer'
if args[0] == 'on':
com= 'echo "on '+ device +'" | cec-client -s -d 1'
#print (com)
os.system(com)
return 'normal',''
elif args[0] == 'standby':
com= 'echo "standby '+ device +'" | cec-client -s -d 1'
#print (com)
os.system(com)
return 'normal',''
else:
return 'error', 'Unknown CEC command: '+ args[0]
# deal with differnt commands/input events
def shutdownnow_pressed(self):
self.shutdown_required=True
if self.show_manager.all_shows_exited() is True:
self.all_shows_ended_callback('normal','no shows running')
else:
# calls exit method of all shows, results in all_shows_closed_callback
self.show_manager.exit_all_shows()
def reboot_pressed(self):
self.reboot_required=True
if self.show_manager.all_shows_exited() is True:
self.all_shows_ended_callback('normal','no shows running')
else:
# calls exit method of all shows, results in all_shows_closed_callback
self.show_manager.exit_all_shows()
def handle_sigterm(self,signum,fframe):
self.mon.log(self,'SIGTERM received - '+ str(signum))
self.terminate()
def handle_user_abort(self):
self.mon.log(self,'User abort received')
self.terminate()
def terminate(self):
self.mon.log(self, "terminate received")
self.terminate_required=True
needs_termination=False
for show in self.show_manager.shows:
# print show[ShowManager.SHOW_OBJ], show[ShowManager.SHOW_REF]
if show[ShowManager.SHOW_OBJ] is not None:
needs_termination=True
self.mon.log(self,"Sent terminate to show "+ show[ShowManager.SHOW_REF])
# call shows terminate method
# eventually the show will exit and after all shows have exited all_shows_callback will be executed.
show[ShowManager.SHOW_OBJ].terminate()
if needs_termination is False:
self.end('killed','killed - no termination of shows required')
# ******************************
# Ending Pi Presents after all the showers and players are closed
# **************************
def e_all_shows_ended_callback(self):
self.all_shows_ended_callback('normal','no shows running')
# callback from ShowManager when all shows have ended
def all_shows_ended_callback(self,reason,message):
for display_name in DisplayManager.display_map:
status,message,display_id,canvas_obj=self.dm.id_of_canvas(display_name)
if status != 'normal':
continue
canvas_obj.config(bg=self.starter_show['background-colour'])
if reason in ('killed','error') or self.shutdown_required is True or self.exitpipresents_required is True or self.reboot_required is True or self.restartpipresents_required is True:
self.end(reason,message)
def end(self,reason,message):
self.mon.log(self,"Pi Presents ending with reason: " + reason)
if self.root is not None:
self.root.destroy()
self.tidy_up()
if reason == 'killed':
if self.email_enabled is True and self.mailer.email_on_terminate is True:
subject= '[Pi Presents] ' + self.unit + ': PP Exited with reason: Terminated'
message = time.strftime("%Y-%m-%d %H:%M") + '\n ' + self.unit + '\n ' + self.interface + '\n ' + self.ip
self.send_email(reason,subject,message)
self.mon.sched(self, None,"Pi Presents Terminated, au revoir\n")
self.mon.log(self, "Pi Presents Terminated, au revoir")
# close logging files
self.mon.finish()
#print('Uncollectable Garbage',gc.collect())
# objgraph.show_backrefs(objgraph.by_type('Canvas'),filename='backrefs.png')
sys.exit(101)
elif reason == 'error':
if self.email_enabled is True and self.mailer.email_on_error is True:
subject= '[Pi Presents] ' + self.unit + ': PP Exited with reason: Error'
message_text = 'Download log for error message\n'+ time.strftime("%Y-%m-%d %H:%M") + '\n ' + self.unit + '\n ' + self.interface + '\n ' + self.ip
self.send_email(reason,subject,message_text)
self.mon.sched(self,None, "Pi Presents closing because of error, sorry\n")
self.mon.log(self, "Pi Presents closing because of error, sorry")
# close logging files
self.mon.finish()
#print('uncollectable garbage',gc.collect())
sys.exit(102)
else:
self.mon.sched(self,None,"Pi Presents exiting normally, bye\n")
self.mon.log(self,"Pi Presents exiting normally, bye")
# close logging files
self.mon.finish()
if self.restartpipresents_required is True:
#print ('restart')
return
if self.reboot_required is True:
# print 'REBOOT'
call (['sudo','reboot'])
if self.shutdown_required is True:
# print 'SHUTDOWN'
call (['sudo','shutdown','now','SHUTTING DOWN'])
#print('uncollectable garbage',gc.collect())
sys.exit(100)
# tidy up all the peripheral bits of Pi Presents
def tidy_up(self):
self.mon.log(self, "Tidying Up")
# backlight
if self.dm != None:
self.dm.terminate()
# turn screen blanking back on
if self.options['noblank'] is True:
call(["xset","s", "on"])
#call(["xset","s", "+dpms"])
call(["xset","s", "blank"])
# tidy up animation
if self.animate is not None:
self.animate.terminate()
# tidy up i/o plugins
if self.ioplugin_manager != None:
self.ioplugin_manager.terminate()
if self.osc_enabled is True:
self.oscdriver.terminate()
# tidy up time of day scheduler
if self.tod_enabled is True:
self.tod.terminate()
# *******************************
# Connecting to network and email
# *******************************
def init_network(self):
timeout=int(self.options['nonetwork'])
if timeout== 0:
self.network_connected=False
self.unit=''
self.ip=''
self.interface=''
return
self.network=Network()
self.network_connected=False
# try to connect to network
self.mon.log (self, 'Waiting up to '+ str(timeout) + ' seconds for network')
success=self.network.wait_for_network(timeout)
if success is False:
self.mon.warn(self,'Failed to connect to network after ' + str(timeout) + ' seconds')
# tkMessageBox.showwarning("Pi Presents","Failed to connect to network so using fake-hwclock")
return
self.network_connected=True
self.mon.sched (self, None,'Time after network check is '+ time.strftime("%Y-%m-%d %H:%M.%S"))
self.mon.log (self, 'Time after network check is '+ time.strftime("%Y-%m-%d %H:%M.%S"))
# Get web configuration
self.network_details=False
network_options_file_path=self.pp_dir+os.sep+'pp_config'+os.sep+'pp_web.cfg'
if not os.path.exists(network_options_file_path):
self.mon.warn(self,"pp_web.cfg not found at "+network_options_file_path)
return
self.mon.log(self, 'Found pp_web.cfg in ' + network_options_file_path)
self.network.read_config(network_options_file_path)
self.unit=self.network.unit
# get interface and IP details of preferred interface
self.interface,self.ip = self.network.get_preferred_ip()
if self.interface == '':
self.network_connected=False
return
self.network_details=True
self.mon.log (self, 'Network details ' + self.unit + ' ' + self.interface + ' ' +self.ip)
def init_mailer(self):
self.email_enabled=False
email_file_path = self.pp_dir+os.sep+'pp_config'+os.sep+'pp_email.cfg'
if not os.path.exists(email_file_path):
self.mon.log(self,'pp_email.cfg not found at ' + email_file_path)
return
self.mon.log(self,'Found pp_email.cfg at ' + email_file_path)
self.mailer=Mailer()
self.mailer.read_config(email_file_path)
# all Ok so can enable email if config file allows it.
if self.mailer.email_allowed is True:
self.email_enabled=True
self.mon.log (self,'Email Enabled')
def try_connect(self):
tries=1
while True:
success, error = self.mailer.connect()
if success is True:
return True
else:
self.mon.log(self,'Failed to connect to email SMTP server ' + str(tries) + '\n ' +str(error))
tries +=1
if tries >5:
self.mon.log(self,'Failed to connect to email SMTP server after ' + str(tries))
return False
def send_email(self,reason,subject,message):
if self.try_connect() is False:
return False
else:
success,error = self.mailer.send(subject,message)
if success is False:
self.mon.log(self, 'Failed to send email: ' + str(error))
success,error=self.mailer.disconnect()
if success is False:
self.mon.log(self,'Failed disconnect after send:' + str(error))
return False
else:
self.mon.log(self,'Sent email for ' + reason)
success,error=self.mailer.disconnect()
if success is False:
self.mon.log(self,'Failed disconnect from email server ' + str(error))
return True
if __name__ == '__main__':
# wait for environment variables to stabilize. Required for Jessie autostart
tries=0
success=False
while tries < 40:
# get directory holding the code
code_dir=sys.path[0]
code_path=code_dir+os.sep+'pipresents.py'
if os.path.exists(code_path):
success =True
break
tries +=1
sleep (0.5)
if success is False: