Commit 9dde9c07 authored by robocoders.ai's avatar robocoders.ai

Implemented configuration management and improved error handling/logging

parent bde2b83d
[General]
log_file = /tmp/streaming_control.log
log_level = INFO
[Web]
stream_url = https://192.168.42.1/HLS/record/Live.m3u8
port = 5000
[Commands]
private_stefy = smblur_private_stefy
private_leeloo = smblur_private_leeloo
private_jasmin = smblur_private_jasmin
private_other = smblur_private
toggle_stefy = smblur_stefy
toggle_leeloo = smblur_leeloo
toggle_jasmin = smblur_jasmin
toggle_others = smblur_shine
tease = smblur_tease
tease_all = smblur_teaseall
open = smblur_clean
......@@ -13,6 +13,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import argparse
import os
import sys
......@@ -20,7 +21,9 @@ import signal
import threading
import webbrowser
import logging
from logging.handlers import RotatingFileHandler
import socket
import configparser
from flask import Flask, render_template, request
import subprocess
......@@ -28,12 +31,19 @@ import tkinter as tk
from tkinter import font as tkFont
import vlc
# Read configuration
config = configparser.ConfigParser()
config.read('config.ini')
# Setup logging
log_file = config.get('General', 'log_file', fallback='/tmp/streaming_control.log')
log_level = config.get('General', 'log_level', fallback='INFO')
logging.basicConfig(
level=logging.INFO,
level=getattr(logging, log_level),
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('/tmp/streaming_control.log'),
RotatingFileHandler(log_file, maxBytes=1024*1024, backupCount=5),
logging.StreamHandler(sys.stdout)
]
)
......@@ -43,24 +53,7 @@ logger = logging.getLogger(__name__)
flask_app = Flask(__name__)
# Command Mapping
COMMANDS = {
# Private commands
'private_stefy': 'smblur_private_stefy',
'private_leeloo': 'smblur_private_leeloo',
'private_jasmin': 'smblur_private_jasmin',
'private_other': 'smblur_private',
# Open/Close commands
'toggle_stefy': 'smblur_stefy',
'toggle_leeloo': 'smblur_leeloo',
'toggle_jasmin': 'smblur_jasmin',
'toggle_others': 'smblur_shine',
# Special commands
'tease': 'smblur_tease',
'tease_all': 'smblur_teaseall',
'open': 'smblur_clean'
}
COMMANDS = dict(config['Commands'])
def run_command(command):
try:
......@@ -88,7 +81,7 @@ def execute():
@flask_app.route('/stream')
def stream():
stream_url = "https://192.168.42.1/HLS/record/Live.m3u8"
stream_url = config.get('Web', 'stream_url', fallback="https://192.168.42.1/HLS/record/Live.m3u8")
return render_template('stream.html', stream_url=stream_url)
def create_daemon():
......@@ -100,7 +93,7 @@ def create_daemon():
# Exit first parent
sys.exit(0)
except OSError as err:
sys.stderr.write(f'Fork #1 failed: {err}\n')
logger.error(f'Fork #1 failed: {err}')
sys.exit(1)
# Decouple from parent environment
......@@ -115,7 +108,7 @@ def create_daemon():
# Exit from second parent
sys.exit(0)
except OSError as err:
sys.stderr.write(f'Fork #2 failed: {err}\n')
logger.error(f'Fork #2 failed: {err}')
sys.exit(1)
# Redirect standard file descriptors
......@@ -142,39 +135,13 @@ def create_daemon():
# Exit the current process
sys.exit(0)
except Exception as err:
sys.stderr.write(f'Failed to create background process: {err}\n')
logger.error(f'Failed to create background process: {err}')
sys.exit(1)
else:
sys.stderr.write(f'Unsupported operating system: {os.name}\n')
sys.exit(1)
# Decouple from parent environment
os.chdir('/')
os.setsid()
os.umask(0)
# Second fork
try:
pid = os.fork()
if pid > 0:
# Exit second parent
sys.exit(0)
except OSError as err:
sys.stderr.write(f'Fork #2 failed: {err}\n')
logger.error(f'Unsupported operating system: {os.name}')
sys.exit(1)
# Redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = open(os.devnull, 'r')
so = open(os.devnull, 'a+')
se = open(os.devnull, 'a+')
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
def check_port_available(port):
"""Check if a port is available"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment