-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy path__init__.py
5624 lines (4869 loc) · 219 KB
/
__init__.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
# coding=utf-8
# encoding: utf-8
from __future__ import absolute_import, division, print_function, unicode_literals
from octoprint.util.version import get_octoprint_version_string
from tempfile import mkstemp
from datetime import timedelta
from imgurpython import ImgurClient
from imgurpython.helpers.error import ImgurClientError, ImgurClientRateLimitError
from pushbullet import Pushbullet
from pushover_complete import PushoverAPI
from rocketchat.api import RocketChatAPI
from matrix_client.client import MatrixClient
from matrix_client.client import Room as MatrixRoom
from PIL import Image
from octoprint.util import RepeatedTimer
from minio import Minio
from sarge import run, Capture, shell_quote
from discord_webhook import DiscordWebhook, DiscordEmbed
import octoprint.util
import octoprint.plugin
import six.moves.urllib.request, six.moves.urllib.error, six.moves.urllib.parse
import datetime
import base64
import six.moves.queue
import json
import os
import os.path
import uuid
import time
import datetime
import tinys3
import humanize
import time
import threading
import requests
import math
import re
import copy
import netifaces
import pytz
import socket
import pymsteams
from six import unichr
from six.moves import zip
SlackClient_Exists = False
WebClient_Exists = False
try:
# Python2
from slackclient import SlackClient
SlackClient_Exists = True
except ImportError:
# Python3
from slack_sdk import WebClient
WebClient_Exists = True
SLACK_TIMEOUT = 60
COMMAND_EXECUTION_WAIT = 10
VCGENPATHS = ["/opt/vc/bin/vcgencmd", "/usr/bin/vcgencmd", "/bin/vcgencmd"]
class OctoslackPlugin(
octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.StartupPlugin,
octoprint.plugin.ShutdownPlugin,
octoprint.plugin.ProgressPlugin,
octoprint.plugin.EventHandlerPlugin,
octoprint.plugin.TemplatePlugin,
):
##TODO FEATURE - generate an animated gif of the print - easy enough if we can find a python ib to create the gif (images2gif is buggy & moviepy, imageio, and and visvis which rely on numpy haven't worked out as I never neven let numpy try to finish installing after 5/10 minutes on my RasPi3)
##TODO FEATURE - add the timelapse gallery for cancelled/failed/completed as a single image
##TODO FEATURE - Add support for Imgur image title + description
##TODO FEATURE - Optionally upload timelapse video to youtube & send a Slack message when the upload is complete
##TODO FEATURE - Define a third set of messages for each event to allow sending M117 commands to the printer
##TODO ENHANCEMENT - The progress event fires on gcode uploads and triggers Octoslack events. Test and fix if necessary.
##TODO ENHANCEMENT - Consider extending the progress snapshot minimum interval beyond Slack to other providers
##TODO ENHANCEMENT - Add Persoanl Token, emoji, avatar, and other formatting enhancements to Rocket.API once a library supports them (or update the libs yourself)
##TODO We've certainly moved past "it's time to refactor" line. Both the UI/JS/Python code need to be refactored
##TODO add multi-cam support: https://plugins.octoprint.org/plugins/multicam/
##~~ SettingsPlugin mixin
def get_settings_defaults(self):
return {
"connection_method": "APITOKEN",
"slack_apitoken_config": {
"api_token": "",
"messages_query_delay": 5,
"alternate_bot_username": "",
"enable_commands": True,
"commands_positive_reaction": ":thumbsup:",
"commands_negative_reaction": ":thumbsdown:",
"commands_processing_reaction": ":stopwatch:",
"commands_unauthorized_reaction": ":lock:",
},
"slack_webhook_config": {"webhook_url": ""},
"slack_rtm_enabled_commands": {
"help": {"enabled": True, "restricted": False},
"status": {"enabled": True, "restricted": False},
"stop": {"enabled": True, "restricted": False},
"pause": {"enabled": True, "restricted": False},
"resume": {"enabled": True, "restricted": False},
},
"slack_rtm_authorized_users": "",
"channel": "",
"pushbullet_config": {"access_token": "", "channel": ""},
"pushover_config": {"app_token": "", "user_key": ""},
"rocketchat_config": {
"server_url": "",
"username": "",
"password": "",
"channel": "",
},
"matrix_config": {
"server_url": "",
"access_token": "",
"user_id": "",
"channel": "",
},
"discord_config": {
"webhook_urls": "",
"alternate_username": "",
"avatar_url": "",
},
"teams_config": {"webhook_urls": ""},
"ignore_cancel_fail_event": True,
"mattermost_compatability_mode": False,
"include_raspi_temp": True,
"snapshot_upload_method": "NONE",
"imgur_config": {
"client_id": "",
"client_secret": "",
"refresh_token": "",
"album_id": "",
},
"s3_config": {
"AWSAccessKey": "",
"AWSsecretKey": "",
"s3Bucket": "",
"file_expire_days": -1,
"URLStyle": "PATH",
},
"minio_config": {
"AccessKey": "",
"SecretKey": "",
"Bucket": "",
"Endpoint": "s3.amazonaws.com",
"secure": True,
},
"additional_snapshot_urls": "",
"snapshot_arrangement": "HORIZONTAL", ##HORIZTONAL or VERTICAL or GRID
"time_format": "HUMAN", ##FUZZY or EXACT or HUMAN
"supported_events": {
##Not a real event but we'll leverage the same config structure
"Help": {
"Enabled": True,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Help - Supported commands :question:",
"Fallback": "",
"Color": "good",
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"IncludeSupportedCommands": True,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"Startup": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Octoprint service started :chart_with_upwards_trend:",
"Fallback": "Octoprint service started",
"Color": "good",
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": True,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"Shutdown": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Octoprint service stopped :chart_with_downwards_trend:",
"Fallback": "Octoprint service stopped",
"Color": "good",
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"Connecting": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Connecting to printer :satellite:",
"Fallback": "Connecting to printer",
"Color": "good",
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"Connected": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Successfully connected to printer :computer:",
"Fallback": "Successfully connected to printer",
"Color": "good",
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"Disconnecting": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Printer disconnecting :confused:",
"Fallback": "Printer disconnecting",
"Color": "warning",
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"Disconnected": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Printer disconnected :worried:",
"Fallback": "Printer disconnected",
"Color": "danger",
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"Error": {
"Enabled": True,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Printer error :fire:",
"Fallback": "Printer error: {error}",
"Color": "danger",
"CaptureSnapshot": True,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"PrintStarted": {
"Enabled": True,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: A new print has started :rocket:",
"Fallback": "Print started: {print_name}, Estimate: {remaining_time}",
"Color": "good",
"CaptureSnapshot": True,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": True,
"ReportJobOrigEstimate": True,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"PrintFailed": {
"Enabled": True,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Print failed :bomb:",
"Fallback": "Print failed: {print_name}",
"Color": "danger",
"CaptureSnapshot": True,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": True,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"PrintCancelling": {
"Enabled": True,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Print is being cancelled :no_good:",
"Fallback": "Print is being cancelled: {print_name}",
"Color": "warning",
"CaptureSnapshot": True,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": True,
"ReportJobOrigEstimate": False,
"ReportJobProgress": True,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"PrintCancelled": {
"Enabled": True,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Print cancelled :no_good:",
"Fallback": "Print cancelled: {print_name}",
"Color": "warning",
"CaptureSnapshot": True,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": True,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportFinalPrintTime": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"PrintDone": {
"Enabled": True,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Print finished successfully :dancer:",
"Fallback": "Print finished successfully: {print_name}, Time: {elapsed_time}",
"Color": "good",
"CaptureSnapshot": True,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": True,
"ReportJobOrigEstimate": True,
"ReportJobProgress": False,
"ReportFinalPrintTime": True,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
##Not a real event but we'll leverage the same config structure
"Progress": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Print progress {pct_complete} :horse_racing:",
"Fallback": "Print progress: {pct_complete} - {print_name}, Elapsed: {elapsed_time}, Remaining: {remaining_time}",
"Color": "good",
"CaptureSnapshot": True,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": True,
"ReportJobOrigEstimate": False,
"ReportJobProgress": True,
"ReportMovieStatus": False,
"UpdateMethod": "NEW_MESSAGE",
# Minimum time in minutes to wait before uploading a snapshot again for a progress upload
"SlackMinSnapshotUpdateInterval": 10,
"IntervalPct": 25,
"IntervalHeight": 0,
"IntervalTime": 0,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
##Not a real event but we'll leverage the same config structure
"GcodeEvent": {
"Enabled": False, ##Overwritten by each event
"ChannelOverride": "", ##Overwritten by each event
"Message": "", ##Overwritten by each event
"Fallback": "", ##Overwritten by each event
"Color": "good", ##Hardcoded to 'good' for now
"CaptureSnapshot": False, ##Overwritten by each event
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": True,
"ReportJobOrigEstimate": False,
"ReportJobProgress": True,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
##Not a real event but we'll leverage the same config structure
"Heartbeat": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Heartbeat - Printer status: {printer_status} :heartbeat:",
"Fallback": "Heartbeat - Printer status: {printer_status}",
"Color": "good", ##Color may be updated in process_slack_event
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportMovieStatus": False,
"IntervalTime": 60,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"PrintPaused": {
"Enabled": True,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Print paused :zzz:",
"Fallback": "Print paused: {pct_complete} - {print_name}",
"Color": "warning",
"CaptureSnapshot": True,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": True,
"ReportJobOrigEstimate": True,
"ReportJobProgress": True,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"PrintResumed": {
"Enabled": True,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Print resumed :runner:",
"Fallback": "Print resumed: {pct_complete} - {print_name}",
"Color": "good",
"CaptureSnapshot": True,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": True,
"ReportJobOrigEstimate": True,
"ReportJobProgress": True,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"MetadataAnalysisStarted": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: File analysis started :runner:",
"Fallback": "File metadata analysis started: {print_name}",
"Color": "good",
"CaptureSnapshot": False,
"ReportPrinterState": False,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"MetadataAnalysisFinished": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: File analysis complete :ok_hand:",
"Fallback": "File metadata analysis complete: {print_name}",
"Color": "good",
"CaptureSnapshot": False,
"ReportPrinterState": False,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportMovieStatus": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"MovieRendering": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Timelapse movie rendering :clapper:",
"Fallback": "Timelapse movie rendering: {print_name}",
"Color": "good",
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportMovieStatus": True,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"MovieDone": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Timelapse movie rendering complete :movie_camera:",
"Fallback": "Timelapse movie rendering complete: {print_name}",
"Color": "good",
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportMovieStatus": True,
"UploadMovie": False,
"UploadMovieLink": False,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
"MovieFailed": {
"Enabled": False,
"ChannelOverride": "",
"Message": ":heavy_minus_sign: Timelapse movie rendering failed :boom:",
"Fallback": "Timelapse movie rendering failed: {print_name}, Error: {error}",
"Color": "danger",
"CaptureSnapshot": False,
"ReportPrinterState": True,
"ReportEnvironment": False,
"ReportJobState": False,
"ReportJobOrigEstimate": False,
"ReportJobProgress": False,
"ReportMovieStatus": True,
"PushoverSound": "pushover",
"PushoverPriority": 0,
"CommandEnabled": False,
"CaptureCommandReturnCode": False,
"CaptureCommandOutput": False,
"Command": "",
"MinNotificationInterval": 0,
},
},
"gcode_events": "",
"timezones": "|".join(pytz.common_timezones),
"timezone": "OS_Default",
"eta_date_format": "hh:mm tt <fuzzy date>",
}
def get_settings_restricted_paths(self):
return dict(
admin=[
["slack_apitoken_config", "api_token"],
["slack_webhook_config", "webhook_url"],
["pushbullet_config", "access_token"],
["pushover_config", "app_token"],
["rocketchat_config", "username"],
["rocketchat_config", "password"],
["matrix_config", "access_token"],
["s3_config", "AWSAccessKey"],
["s3_config", "AWSsecretKey"],
["s3_config", "s3Bucket"],
["minio_config", "AccessKey"],
["minio_config", "SecretKey"],
["minio_config", "Bucket"],
["minio_config", "Endpoint"],
["minio_config", "secure"],
["imgur_config", "client_id"],
["imgur_config", "client_secret"],
["imgur_config", "refresh_token"],
["imgur_config", "album_id"],
["additional_snapshot_urls"],
]
)
def get_settings_version(self):
return 2
def on_settings_migrate(self, target, current=None):
if current == None:
return
if current < 2: ##All 1 --> 2 changes
existing_alt_username = self._settings.get(
["slack_identity"], merged=True
).get("username")
if existing_alt_username:
self._settings.set(
["slack_apitoken_config", "alternate_bot_username"],
existing_alt_username,
)
def on_settings_save(self, data):
try:
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
self.start_bot_listener()
self.update_progress_timer()
self.update_heartbeat_timer()
self.update_gcode_sent_listeners()
self.find_vcgencmd_path()
self._slack_next_progress_snapshot_time = 0
except Exception as e:
self._logger.exception(
"Error executing post-save actions, Error: " + str(e)
)
def on_settings_initialized(self):
self.find_vcgencmd_path()
##~ TemplatePlugin mixin
##def get_template_vars(self):
## return dict()
def get_template_configs(self):
return [dict(type="settings", custom_bindings=False)]
##~~ AssetPlugin mixin
def get_assets(self):
return dict(
js=["js/Octoslack.js"],
css=["css/Octoslack.css"],
less=["less/Octoslack.less"],
)
##~~ Softwareupdate hook
def get_update_information(self):
# Define the configuration for your plugin to use with the Software Update
# Plugin here. See https://github.com/foosel/OctoPrint/wiki/Plugin:-Software-Update
# for details.
return dict(
Octoslack=dict(
displayName="Octoslack",
displayVersion=self._plugin_version,
# version check: github repository
type="github_release",
user="fraschetti",
repo="Octoslack",
current=self._plugin_version,
# update method: pip
pip="https://github.com/fraschetti/Octoslack/archive/{target_version}.zip",
)
)
##~~ StartupPlugin mixin
def on_after_startup(self):
self.start_bot_listener()
self.update_gcode_sent_listeners()
self.start_heartbeat_timer()
##~~ ShutdownPlugin mixin
def on_shutdown(self):
self.stop_bot_listener()
self._logger.debug("Stopped Slack bot client")
self.stop_progress_timer()
self.stop_heartbeat_timer()
##~~ PrintProgress mixin
def on_print_progress(self, location, path, progress):
try:
progress_interval = int(
self._settings.get(["supported_events"], merged=True)
.get("Progress")
.get("IntervalPct")
)
self._logger.debug(
"Progress: "
+ str(progress)
+ " - IntervalPct: "
+ str(progress_interval)
)
if (
progress > 0
and progress < 100
and progress_interval > 0
and progress % progress_interval == 0
):
self.handle_event(
"Progress", None, {"progress": progress}, False, False, None
)
except Exception as e:
self._logger.exception("Error processing progress event, Error: " + str(e))
##~~ EventPlugin mixin
def progress_timer_tick(self):
self._logger.debug("Progress timer tick")
self.handle_event("Progress", None, {}, False, False, None)
print_cancel_time = None
progress_timer = None
heartbeat_timer = None
def start_progress_timer(self):
progress_event = self._settings.get(["supported_events"], merged=True).get(
"Progress"
)
progress_notification_enabled = progress_event.get("Enabled")
progress_command_enabled = progress_event.get("CommandEnabled")
if not progress_notification_enabled and not progress_command_enabled:
return
progress_timer_interval = int(progress_event.get("IntervalTime"))
if (
progress_timer_interval > 0
and (self._printer.is_printing() or self._printer.is_paused())
and not self._printer.is_ready()
):
self._logger.debug(
"Starting progress timer: " + str(progress_timer_interval) + "min(s)"
)
self.progress_timer = RepeatedTimer(
progress_timer_interval * 60, self.progress_timer_tick, run_first=False
)
self.progress_timer.start()
def update_progress_timer(self):
restart = False
progress_event = self._settings.get(["supported_events"], merged=True).get(
"Progress"
)
progress_notification_enabled = progress_event.get("Enabled")
progress_command_enabled = progress_event.get("CommandEnabled")
if not progress_notification_enabled and not progress_command_enabled:
self.stop_progress_timer()
return
new_interval = int(progress_event.get("IntervalTime"))
if self.progress_timer == None and new_interval > 0:
restart = True
else:
existing_interval = 0
if not self.progress_timer == None:
existing_interval = self.progress_timer.interval
##OctoPrint wraps the interval in a lambda function
if callable(existing_interval):
existing_interval = existing_interval()
existing_interval = int(existing_interval / 60)
self._logger.debug("New progress interval: " + str(new_interval))
self._logger.debug(
"Previous progress interval: " + str(existing_interval)
)
if new_interval != existing_interval:
restart = True
if restart and new_interval > 0:
self.stop_progress_timer()
self.start_progress_timer()
def stop_progress_timer(self):
if not self.progress_timer == None:
self._logger.debug("Stopping progress timer")
self.progress_timer.cancel()
self.progress_timer = None
def heartbeat_timer_tick(self):
self._logger.debug("Heartbeat timer tick")
##Color may be updated in process_slack_event
self.handle_event("Heartbeat", None, {}, False, False, None)
def start_heartbeat_timer(self):
heartbeat_event = self._settings.get(["supported_events"], merged=True).get(
"Heartbeat"
)
heartbeat_notification_enabled = heartbeat_event.get("Enabled")
heartbeat_command_enabled = heartbeat_event.get("CommandEnabled")
if not heartbeat_notification_enabled and not heartbeat_command_enabled:
return
heartbeat_timer_interval = int(heartbeat_event.get("IntervalTime"))
if heartbeat_timer_interval > 0:
self._logger.debug(
"Starting heartbeat timer: " + str(heartbeat_timer_interval) + "min(s)"
)
self.heartbeat_timer = RepeatedTimer(
heartbeat_timer_interval * 60,
self.heartbeat_timer_tick,
run_first=False,
)
self.heartbeat_timer.start()
def update_heartbeat_timer(self):
restart = False
heartbeat_event = self._settings.get(["supported_events"], merged=True).get(
"Heartbeat"
)
heartbeat_notification_enabled = heartbeat_event.get("Enabled")
heartbeat_command_enabled = heartbeat_event.get("CommandEnabled")
if not heartbeat_notification_enabled and not heartbeat_command_enabled:
self.stop_heartbeat_timer()
return
new_interval = int(heartbeat_event.get("IntervalTime"))
if self.heartbeat_timer == None and new_interval > 0:
restart = True
else:
existing_interval = 0
if not self.heartbeat_timer == None:
existing_interval = self.heartbeat_timer.interval
##OctoPrint wraps the interval in a lambda function
if callable(existing_interval):
existing_interval = existing_interval()
existing_interval = int(existing_interval / 60)
self._logger.debug("New heartbeat interval: " + str(new_interval))
self._logger.debug(
"Previous heartbeat interval: " + str(existing_interval)
)
if new_interval != existing_interval:
restart = True
if restart and new_interval > 0:
self.stop_heartbeat_timer()
self.start_heartbeat_timer()
def stop_heartbeat_timer(self):
if not self.heartbeat_timer == None:
self._logger.debug("Stopping heartbeat timer")
self.heartbeat_timer.cancel()
self.heartbeat_timer = None
last_trigger_height = 0.0
def process_zheight_change(self, payload):
if not self._printer.is_printing():
return False
if not "new" in payload:
return False
height_interval = float(
self._settings.get(["supported_events"], merged=True)
.get("Progress")
.get("IntervalHeight")
)
if height_interval <= 0:
return False
new = payload["new"]
if new <= self.last_trigger_height:
return False
if new >= (self.last_trigger_height + height_interval):
self._logger.debug(
"ZChange interval: "
+ str(height_interval)
+ ", Last trigger height: "
+ str(self.last_trigger_height)
+ ", Payload: "
+ json.dumps(payload)
)
self.last_trigger_height = new