-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcallbacks.py
More file actions
102 lines (85 loc) · 3.9 KB
/
Copy pathcallbacks.py
File metadata and controls
102 lines (85 loc) · 3.9 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
#!/usr/bin/env python3
import time
import os
import argparse
import sys
import json
import subprocess
# Import our database helper
try:
from stats_db import StatsDatabase
db = StatsDatabase()
except Exception as e:
print(f"Error importing stats_db: {e}")
db = None
class Logger(object):
def __init__(self, filename="/home/fpp/media/logs/fpp-plugin-AdvancedStats.log"):
self.terminal = sys.stdout
self.log = open(filename, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
self.terminal.flush()
self.log.flush()
sys.stdout = Logger("/home/fpp/media/logs/fpp-plugin-AdvancedStats.log")
parser = argparse.ArgumentParser(description='Advanced Stats Plugin')
parser.add_argument('-l','--list', help='Plugin Actions',action='store_true')
parser.add_argument('--type', help='Callback type')
parser.add_argument('--data', help='JSON data for callback')
parser.add_argument('--gpio', help='GPIO pin number')
parser.add_argument('--state', help='GPIO pin state')
args = parser.parse_args()
if args.list:
# List available callbacks
print("media")
print("playlist")
print("sequence")
sys.exit(0)
# Handle callbacks
if args.type:
timestamp = int(time.time())
try:
# Parse JSON data if provided
data = {}
if args.data:
data = json.loads(args.data)
# Media callback (sequence start/stop)
if args.type == 'media':
media_type = data.get('type', '')
sequence_name = data.get('Sequence', data.get('sequence', ''))
action = data.get('Action', data.get('action', ''))
playlist_name = data.get('playlist', data.get('Playlist', ''))
if sequence_name and action and db:
if action == 'start':
db.log_sequence_event(sequence_name, 'start', playlist_name=playlist_name, trigger_source='fpp')
print(f"Logged sequence start: {sequence_name}" + (f" (playlist: {playlist_name})" if playlist_name else ""))
elif action == 'stop':
# Try to get duration if available
duration = data.get('duration', 0)
db.log_sequence_event(sequence_name, 'stop', playlist_name=playlist_name, duration=duration, trigger_source='fpp')
print(f"Logged sequence stop: {sequence_name}" + (f" (playlist: {playlist_name})" if playlist_name else ""))
# Playlist callback
elif args.type == 'playlist':
playlist_name = data.get('playlist', '')
action = data.get('action', '')
if playlist_name and action and db:
if action in ['start', 'stop']:
# Only log if it's NOT a standalone sequence file
# Standalone sequences end with .fseq and should only be tracked in sequence_history
# Real playlists don't have file extensions
if not playlist_name.endswith('.fseq'):
db.log_playlist_event(playlist_name, action, trigger_source='fpp')
print(f"Logged playlist {action}: {playlist_name}")
else:
print(f"Skipped logging standalone sequence as playlist: {playlist_name}")
# GPIO callback (if we add GPIO support)
elif args.type == 'gpio' or args.gpio:
pin_number = int(args.gpio) if args.gpio else data.get('pin', 0)
pin_state = int(args.state) if args.state else data.get('state', 0)
if pin_number > 0 and db:
db.log_gpio_event(pin_number, pin_state, event_type='trigger')
print(f"Logged GPIO event: Pin {pin_number} = {pin_state}")
except Exception as e:
print(f"Error processing callback: {e}")
sys.exit(1)