# -*- coding: utf-8 -*-

from robotremoteserver import RobotRemoteServer
from library import KwLibrary
import platform
import os
import signal

def main():
    RobotRemoteServer(library=KwLibrary(), host='0.0.0.0', port=9999)

def kill_pid():
    """
    功能：杀掉旧的进程
    """
    sys = platform.system()
    if sys == "Windows":
        with os.popen('netstat -aon|findstr "9999"') as res:
            res = res.read().split('\n')
        result = []
        for line in res:
            temp = [i for i in line.split(' ') if i != '']
            if len(temp) > 4 and temp[1] == "0.0.0.0:9999":
                result.append(
                    {'pid': temp[4], 'address': temp[1], 'state': temp[3]})
                os.popen("taskkill -pid {} -f".format(temp[4]))
    else:
        pid = os.popen(
            "sudo -i netstat -nlp | grep :9999 | awk '{print $7}' | awk -F '/' '{ print $1 }'",
            'r',
            1).read().strip('\n')
        if pid != "":
            os.kill(int(pid), signal.SIGKILL)