# Path: pyfeld/pingTest.py
# def ping_test_alive(ip):
#     cmd = "ping -W 1 -c 1 " + ip
#     try:
#         process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#     except Exception as e:
#         return 0
#     output_received = ""
#     while True:
#         line = process.stdout.readline()
#         if len(line) == 0 and process.poll() != None:
#             break
#         v = line.decode('utf-8')
#         output_received += v
#     return "1 received" in output_received


import subprocess
import sys
import threading
import re
from time import sleep
from texttable import Texttable
from pyfeld.pingTest import ping_test_alive
    from pyfeld.rfcmd import RfCmd

def single_device_update(free_to_kill, ip, url):
    cmd = sshcmd + ip + " raumfeld-update --force " + url
    print("running cmd: "+cmd)
    free_to_kill.runCommand(cmd)


def force_update(url):
    print("Force updating with url " + url)
    ips = get_ips()
    processes = list()
    device_pingable = dict()
    free_to_kill = UpdateProcessesFreeToKill()
    count = 0
    for ip in ips:
        proc = threading.Thread(target=single_device_update, args=(free_to_kill, ip, url))
        proc.start()
        processes.append(proc)
        device_pingable[ip] = True
        count += 1

    temp_count = count
    print("Waiting for action...")
    sleep(5)
    while count > 0:
        sleep(10)
        print("")
        for ip in ips:
            if device_pingable[ip]:
                print("testing if ping alive: " + ip + " " + str(RfCmd.map_ip_to_friendly_name(ip)))

# Path: pyfeld/pingTest.py
# def ping_test_alive(ip):
#     cmd = "ping -W 1 -c 1 " + ip
#     try:
#         process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#     except Exception as e:
#         return 0
#     output_received = ""
#     while True:
#         line = process.stdout.readline()
#         if len(line) == 0 and process.poll() != None:
#             break
#         v = line.decode('utf-8')
#         output_received += v
#     return "1 received" in output_received


# Path: pyfeld/rfmacro.py
import subprocess
import sys
import threading
import re
from time import sleep
from texttable import Texttable
from pyfeld.pingTest import ping_test_alive
    from pyfeld.rfcmd import RfCmd


def single_device_update(free_to_kill, ip, url):
    cmd = sshcmd + ip + " raumfeld-update --force " + url
    print("running cmd: "+cmd)
    free_to_kill.runCommand(cmd)


def force_update(url):
    print("Force updating with url " + url)
    ips = get_ips()
    processes = list()
    device_pingable = dict()
    free_to_kill = UpdateProcessesFreeToKill()
    count = 0
    for ip in ips:
        proc = threading.Thread(target=single_device_update, args=(free_to_kill, ip, url))
        proc.start()
        processes.append(proc)
        device_pingable[ip] = True
        count += 1

    temp_count = count
    print("Waiting for action...")
    sleep(5)
    while count > 0:
        sleep(10)
        print("")
        for ip in ips:
            if device_pingable[ip]:
                print("testing if ping alive: " + ip + " " + str(RfCmd.map_ip_to_friendly_name(ip)))

                if not ping_test_alive(ip):