def user_options(id): if machine_id != id: raise WrongMachineID return { "tracking": ControlThread.user_options(), "recording": ControlThreadVideoRecording.user_options() }
def controls(id, action): global control global record if id != machine_id: raise WrongMachineID if action == 'start': data = request.json tracking_json_data.update(data) control = None control = ControlThread(machine_id=machine_id, name=machine_name, version=version, ethoscope_dir=ETHOSCOPE_DIR, data=tracking_json_data) control.start() return info(id) elif action in ['stop', 'close', 'poweroff']: if control.info['status'] == 'running' or control.info[ 'status'] == "recording": # logging.info("Stopping monitor") logging.warning("Stopping monitor") control.stop() logging.warning("Joining monitor") control.join() logging.warning("Monitor joined") logging.warning("Monitor stopped") # logging.info("Monitor stopped") if action == 'close': close() if action == 'poweroff': logging.info("Stopping monitor due to poweroff request") logging.info("Powering off Device.") call('poweroff') return info(id) elif action == 'start_record': data = request.json #json_data.update(data) logging.warning("Recording video, data is %s" % str(data)) data = request.json recording_json_data.update(data) control = None control = ControlThreadVideoRecording(machine_id=machine_id, name=machine_name, version=version, ethoscope_dir=ETHOSCOPE_DIR, data=recording_json_data) control.start() return info(id) else: raise Exception("No such action: %s" % action)
def user_options(id): ''' Passing back options regarding what information can be changed on the the device. This populates the form on the node GUI ''' if machine_id != id: raise WrongMachineID return { "tracking": ControlThread.user_options(), "recording": ControlThreadVideoRecording.user_options(), "streaming": {}, "update_machine": { "machine_options": [{ "overview": "Machine information that can be set by the user", "arguments": [{ "type": "number", "name": "etho_number", "description": "An ID number (1-999) unique to this ethoscope", "default": get_machine_info(id)['machine-number'] }, { "type": "boolean", "name": "isexperimental", "description": "Specify if the ethoscope is to be treated as experimental", "default": isExperimental() }, { "type": "str", "name": "node_ip", "description": "The IP address that you want to record as the node (do not change this value unless you know what you are doing!)", "default": get_machine_info(id)['node_ip'] }, { "type": "str", "name": "ESSID", "description": "The name of the WIFI SSID", "default": get_machine_info(id)['WIFI_SSID'] }, { "type": "str", "name": "Key", "description": "The WPA password for the WIFI SSID", "default": get_machine_info(id)['WIFI_PASSWORD'] }], "name": "Ethoscope Options" }], } }
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)
if option_dict["json"]: with open(option_dict["json"]) as f: json_data= json.loads(f.read()) else: data = None json_data = {} ETHOSCOPE_DIR = option_dict["results_dir"] if option_dict["record_video"]: recording_json_data = json_data control = ControlThreadVideoRecording(machine_id=machine_id, name=machine_name, version=version, ethoscope_dir=ETHOSCOPE_DIR, data=recording_json_data) else: tracking_json_data = json_data 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")
def controls(id, action): global control global record if id != machine_id: raise WrongMachineID if action == 'start': data = request.json tracking_json_data.update(data) control = None control = ControlThread(machine_id=machine_id, name=machine_name, version=version, ethoscope_dir=ETHOSCOPE_DIR, data=tracking_json_data) control.start() return info(id) elif action in ['stop', 'close', 'poweroff']: if control.info['status'] == 'running' or control.info[ 'status'] == "recording": # logging.info("Stopping monitor") logging.warning("Stopping monitor") control.stop() logging.warning("Joining monitor") control.join() logging.warning("Monitor joined") logging.warning("Monitor stopped") # logging.info("Monitor stopped") if action == 'close': close() if action == 'poweroff': logging.info("Stopping monitor due to poweroff request") logging.info("Powering off Device.") call('poweroff') return info(id) elif action == 'start_record': data = request.json #json_data.update(data) logging.warning("Recording video, data is %s" % str(data)) data = request.json recording_json_data.update(data) control = None control = ControlThreadVideoRecording(machine_id=machine_id, name=machine_name, version=version, ethoscope_dir=ETHOSCOPE_DIR, data=recording_json_data) control.start() return info(id) elif action == 'settime': # TODO check the status of ntp and maybe refuse to set the time if ntp is working timeString = "%02d%02d%02d%02d" % ( request.json["month"], request.json["date"], request.json["hours"], request.json["minutes"]) logging.warning("Requested that the time be set to " + str(timeString)) call(["date", "--utc", timeString]) return info(id) else: raise Exception("No such action: %s" % action)
def controls(id, action): global control global record if id != machine_id: raise WrongMachineID if action == 'start': data = bottle.request.json tracking_json_data.update(data) control = None control = ControlThread(machine_id=machine_id, name=machine_name, version=version, ethoscope_dir=ETHOSCOPE_DIR, data=tracking_json_data) control.start() return info(id) elif action in ['stop', 'close', 'poweroff', 'reboot', 'restart']: if control.info['status'] in ['running', 'recording', 'streaming']: logging.info("Stopping monitor") control.stop() logging.info("Joining monitor") control.join() logging.info("Monitor joined") logging.info("Monitor stopped") if action == 'close': close() if action == 'poweroff': logging.info("Stopping monitor due to poweroff request") logging.info("Powering off Device.") subprocess.call('poweroff') if action == 'reboot': logging.info("Stopping monitor due to reboot request") logging.info("Powering off Device.") subprocess.call('reboot') if action == 'restart': logging.info("Restarting service") subprocess.call(['systemctl', 'restart', 'ethoscope_device']) return info(id) elif action in ['start_record', 'stream']: data = bottle.request.json recording_json_data.update(data) logging.warning("Recording or Streaming video, data is %s" % str(data)) control = None control = ControlThreadVideoRecording(machine_id=machine_id, name=machine_name, version=version, ethoscope_dir=ETHOSCOPE_DIR, data=recording_json_data) control.start() return info(id) else: raise Exception("No such action: %s" % action)
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.'
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.'