Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
camelcamro opened this issue Aug 4, 2023 · 1 comment

Comments

@camelcamro
Copy link

camelcamro commented Aug 4, 2023

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
Copy link
Author

camelcamro commented Sep 20, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant