timolex / Radiodawg

Radiodawg Volumio webradio watchdog
MIT License
1 stars 1 forks source link

great watchdog .... i made some extra things ... based on the domain of the webradio #6

Open camelcamro opened 1 year ago

camelcamro commented 1 year ago

hello, great job - simple and very effiicent !! great job ...

only as you want to improve ... i made few small changes .. not comfort to make an extra fork on github ... anyhow ...

camelcamro commented 11 months ago

sorry, I'm not familar .... but here my code for checking more things - like if stream is OK and so on ...

import os
import time
import sys

SCRIPT_NAME = "WEBRADIO_WATCHDOG:"
TIMEOUT_SEC = 10
DNS_TO_QUERY = "8.8.8.8"
MUTE = " > /dev/null 2>&1"
MIN_DROPPED_PACKETS = 2
CONSIDER_PLAYBACK_STATUS = True

def stop_playback():
    print(SCRIPT_NAME + "Stopping Volumio playback...")
    os.system("volumio stop")

def start_playback():
    print(SCRIPT_NAME + "Starting Volumio playback...")
    os.system("volumio play")

def is_net_reachable():
    WEBRADIO_DOMAN_CMD = "volumio status | grep -e '\"uri\":' | awk -F[/:] '{print $5}' | tr -d '\n'"
    DNS_TO_QUERY = os.popen(WEBRADIO_DOMAN_CMD).read()
    #DEBUG
    #print(SCRIPT_NAME + "WEBRADIO_DOMAN = *" + DNS_TO_QUERY + "*")
    #response = os.system("ping -c 1 " + WEBRADIO_DOMAN + MUTE)
    ping_check = "ping -c 1 " + DNS_TO_QUERY + MUTE
    #DEBUG
    #print(SCRIPT_NAME + ping_check)
    response = os.system(ping_check)

    if response is 0:
        return True
    else:
        return False

def is_connection_down():
    dropped_packets = 0
    if is_net_reachable():
        return False
    else:
        dropped_packets += 1
        while dropped_packets <= MIN_DROPPED_PACKETS:
            if dropped_packets == MIN_DROPPED_PACKETS:
                return True
            else:
                if is_net_reachable():
                    return False
                dropped_packets += 1

def is_streaming_webradio():
    is_in_playback_state = os.system("volumio status | grep status | grep play" + MUTE)
    is_type_webradio = os.system("volumio status | grep service | grep webradio" + MUTE)
    if CONSIDER_PLAYBACK_STATUS and not is_in_playback_state is 0: 
        print(SCRIPT_NAME + " Stream not in status: *play*. Ignoring check")
        return False
    elif CONSIDER_PLAYBACK_STATUS and not is_type_webradio is 0: 
        print(SCRIPT_NAME + " Stream not having service: *webradio*. Ignoring check")
        return False
    else:
        return True

def is_streaming_webradio_valid():
    is_in_playback_bitrate = os.system("volumio status | grep bitrate" + MUTE)
    if not is_in_playback_bitrate is 0:
        print(SCRIPT_NAME + " Error - Stream not having a valid *bitrate*")
        return False
    return True

while True:
    if is_streaming_webradio():
        if not is_streaming_webradio_valid():
            print(SCRIPT_NAME + " webradio STREAM not valid. Restarting stream and playback !")
            stop_playback()
            time.sleep(TIMEOUT_SEC)
            start_playback()
        elif is_connection_down():
            print(SCRIPT_NAME + DNS_TO_QUERY + " is not reachable (" + str(MIN_DROPPED_PACKETS) 
                    + " subsequently dropped packets), stopping Volumio playback")
            stop_playback()
            while not is_net_reachable():
                print(SCRIPT_NAME + DNS_TO_QUERY + " is still not reachable, trying again in " + str(TIMEOUT_SEC) + " sec")
                time.sleep(TIMEOUT_SEC)
            print(SCRIPT_NAME + DNS_TO_QUERY + " is reachable again, resuming Volumio playback now")
            start_playback()
            #sometimes, it still needs an extra "start" when coming back from internet interuption or anything else
            time.sleep(15)
            start_playback()
        else:
            print(SCRIPT_NAME + " webradio reachable and stream looking good. Playback ongoing")

    time.sleep(TIMEOUT_SEC)