Exemplo n.º 1
0
        control = ControlThread(machine_id=machine_id,
                                name=machine_name,
                                version=version,
                                ethoscope_dir=ETHOSCOPE_DIR,
                                data=tracking_json_data)


    if option_dict["debug"]:
        logging.basicConfig(level=logging.DEBUG)
        logging.info("Logging using DEBUG SETTINGS")

    if option_dict["stop_after_run"]:
         control.set_evanescent(True) # kill program after first run

    if option_dict["run"] or control.was_interrupted:
        control.start()

    try:
        #######TO be remove when bottle changes to version 0.13
        server = "cherrypy"
        try:
            from cherrypy import wsgiserver
        except:
            #Trick bottle to think that cheroot is actulay cherrypy server adds the pacth to BOTTLE
            server_names["cherrypy"]=CherootServer(host='0.0.0.0', port=port)
            logging.warning("Cherrypy version is bigger than 9, we have to change to cheroot server")
            pass
        #########
        run(api, host='0.0.0.0', port=port, debug=option_dict["debug"], server='cherrypy')

    except Exception as e:
Exemplo n.º 2
0
class commandingThread(threading.Thread):
    def __init__(self, ethoscope_info, host='', port=5000):
        self.host = host
        self.port = port
        self.size = 1024

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.bind((self.host, self.port))
        self.sock.listen(5)

        self.ethoscope_info = ethoscope_info
        self.control = ControlThread(
            machine_id=ethoscope_info['MACHINE_ID'],
            name=ethoscope_info['MACHINE_NAME'],
            version=ethoscope_info['GIT_VERSION'],
            ethoscope_dir=ethoscope_info['ETHOSCOPE_DIR'],
            data=None)

        self.running = True
        threading.Thread.__init__(self)

    def stop(self):
        self.running = False
        #makes a dummy connection
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(
            (self.host, self.port))
        self.sock.close()

    def run(self):
        '''
        listen for new connections and processes them as they arrive
        '''

        while self.running:
            client, address = self.sock.accept()
            try:
                recv = client.recv(self.size)
                if recv:
                    message = json.loads(recv)
                    result = json.dumps({
                        'response':
                        self.action(message['command'], message['data'])
                    }).encode('utf-8')
                    client.send(result)
            except:
                pass

            client.close()

    def run_i(self):
        '''
        listen for new incoming clients
        creates a new subthread for each incoming client with a timeout of 60 seconds
        '''

        while True:
            client, address = self.sock.accept()
            client.settimeout(60)
            threading.Thread(target=self.listenToClient,
                             args=(client, address)).start()

    def listenToClient(self, client, address):
        '''
        start listening for registered client
        '''

        while True:
            try:
                recv = client.recv(self.size)

                if recv:
                    message = json.loads(recv)
                    result = json.dumps({
                        'response':
                        self.action(message['command'], message['data'])
                    }).encode('utf-8')
                    client.send(result)

            except:
                client.close()
                return False

    def action(self, action, data=None):
        '''
        act on client's instructions
        '''

        if not data and action in ['start', 'start_record']:
            return 'This action requires JSON data'

        if action == 'help':
            return "Commands that do not require JSON info: help, info, status, stop, stream.\nCommands that do require JSON info: start, start_record."

        elif action == 'info':
            return self.control.info

        elif action == 'status':
            return self.control.info['status']

        elif action == 'start' and data:
            self.control = ControlThread(
                machine_id=self.ethoscope_info['MACHINE_ID'],
                name=self.ethoscope_info['MACHINE_NAME'],
                version=self.ethoscope_info['GIT_VERSION'],
                ethoscope_dir=self.ethoscope_info['ETHOSCOPE_DIR'],
                data=data)
            self.control.start()

            logging.info("Starting tracking")
            return "Starting tracking activity"

        elif action == 'stream':
            self.control = ControlThreadVideoRecording(
                machine_id=self.ethoscope_info['MACHINE_ID'],
                name=self.ethoscope_info['MACHINE_NAME'],
                version=self.ethoscope_info['GIT_VERSION'],
                ethoscope_dir=self.ethoscope_info['ETHOSCOPE_DIR'],
                data=data)

            self.control.start()
            return "Starting streaming activity"

        elif action == 'start_record' and data:
            self.control = ControlThreadVideoRecording(
                machine_id=self.ethoscope_info['MACHINE_ID'],
                name=self.ethoscope_info['MACHINE_NAME'],
                version=self.ethoscope_info['GIT_VERSION'],
                ethoscope_dir=self.ethoscope_info['ETHOSCOPE_DIR'],
                data=data)

            self.control.start()
            return "Starting recording or streaming activity"

        elif action == 'stop' and self.control.info['status'] in [
                'running', 'recording', 'streaming'
        ]:
            logging.info("Stopping monitor")
            self.control.stop()
            logging.info("Joining monitor")
            self.control.join()
            logging.info("Monitor joined")
            logging.info("Monitor stopped")
            return "Stopping ethoscope activity"

        else:
            #raise Exception("No such command: %s. Available commands are info, status, start, stop, start_record, stream " % action)
            return 'This ethoscope action is not available.'