def get_unit_address(self, update, context): """ Get the address of a unit in order to send commands """ name = context.args[0] update.message.reply_text(f"Checking database...") time.sleep(0.5) unit_address = dbcontrol.get_unit_address(name) update.message.reply_text( f"Stored address for {name.upper()}: {unit_address}")
def toggle_image_detection(self, update, context): name = context.args[0] duration = context.args[1] unit_address = dbcontrol.get_unit_address(name) try: commands.vid_comd(unit_address, self.command_channel, "image_detection", duration) update.message.reply_text( f"Object detection command send to {name}") except Exception as e: update.message.reply_text(f"Command error: {e}")
def cpu_comd(self, update, context): name = context.args[0] command = context.args[1] unit_address = dbcontrol.get_unit_address(name) try: commands.cpu_comd(unit_address, self.command_channel, command) update.message.reply_text( f"[CPU: {command.upper()}] command sent to {name}") except Exception as e: update.message.reply_text(f"{e}")
def stop_wifi_scan(self, update, context): name = context.args[0] command = "STOP_SCAN" time = "n/a" unit_address = dbcontrol.get_unit_address(name) try: commands.wifi_comd(unit_address, self.command_channel, command, time) update.message.reply_text( f"Terminate wifi scan command sent to {name}") except Exception as e: update.message.reply_text(f"Unable to terminate wifi scan. {e}")
def get_unit_status(self, update, context): name = context.args[0] unit_address = dbcontrol.get_unit_address(name) update.message.reply_text(f"Checking...") time.sleep(0.5) unit_status = commands.get_unit_status(name, unit_address) if unit_status == "no_connection": update.message.reply_text(f"Cannot connect to {name}") else: update.message.reply_text( f"{name.upper()} status: {unit_status[0]}") update.message.reply_text(f"Latest STATREP: {unit_status[1]}")
def stop_stream_comd(self, update, context): name = context.args[0] # if context.args[1]: # stream_time = context.args[1] # else: unit_address = dbcontrol.get_unit_address(name) command = "stopstream" try: commands.vid_comd(unit_address, self.command_channel, command, "n/a") update.message.reply_text(f"Stop stream command sent to {name}") except Exception as e: update.message.reply_text(f"Unable to initiate video stream. {e}")
def wifi_comd(self, update, context): name = context.args[0] command = context.args[1] if context.args[2]: scan_time = context.args[2] else: scan_time = "continuous" unit_address = dbcontrol.get_unit_address(name) try: commands.wifi_comd(unit_address, self.command_channel, command, scan_time) update.message.reply_text(f"Wifi scan command sent to {name}") except Exception as e: update.message.reply_text(f"Unable to initiate wifi scan. {e}")
def send_comd(self, update, context): pic_comd = ['pic', 'picture', 'img', 'image', 'photo', 'photograph'] # print(update) chat_id = update['message']['chat']['id'] file_sent = False name = context.args[0] unit_address = dbcontrol.get_unit_address( name) #maybe add an if unit address is None "unit not found" filetype = context.args[1] if filetype in pic_comd: filetype = "image" vid_length = "n/a" update.message.reply_text(f"Requesting camera shot from {name}...") else: print("[HUB] DEBUG: error not ready yet, will send image for now") filetype = "image" vid_length = "n/a" # requested_filename = file_root + datetime.now().strftime("%Y%m%d-%H%M%S") + "-" + name + ".jpg" # print(f"Searching for {requested_filename}") try: commands.send_file(unit_address, self.command_channel, filetype, vid_length) # for attempt in range(1,12): # try: # updater.bot.sendPhoto(chat_id, photo=open(requested_filename, "rb"), timeout=50, caption=f"Image from {name}") # file_sent = True # break # except: # time.sleep(attempt) # if not file_sent: # update.message.reply_text(f"Nothing received from {name}") except Exception as e: update.message.reply_text(f"Unable to complete: {e}")
def move_servo(self, update, context): """ Get the address of a unit in order to send commands """ name = context.args[0] unit_address = dbcontrol.get_unit_address(name) if len( context.args ) > 1: # if more than one argument, it is a specific move command, not autorotate axis = context.args[1] posn = context.args[2] try: commands.servo_move(unit_address, self.command_channel, axis, posn) time.sleep(0.5) update.message.reply_text( f"[{axis.upper()}: {posn}] servo command sent to {name}") except Exception as e: update.message.reply_text(f"Unable to send command to {name}") time.sleep(0.5) update.message.reply_text(f"{e}") else: # if just one argument - which is the name of the unit - it is an autorotate command print("[HUB] BOT: Move servo function; autorotate command") axis = "<AUTOROTATE>" position = "n/a" try: commands.servo_move(unit_address, self.command_channel, axis, position) time.sleep(0.5) update.message.reply_text( f"[{axis.upper()}] servo command sent to {name}") except Exception as e: update.message.reply_text(f"Unable to send command to {name}") time.sleep(0.5) update.message.reply_text(f"{e}")
def unit_profile(request, unitname): unit = Unit.objects.get(name=unitname) context = { 'unit':unit, 'mapbox_token':config('mapbox_token'), 'form':CommandForm() } # send command to unit (and save command to hub db) if request.method == "POST": form = CommandForm(request.POST) if form.is_valid(): command = form.save(commit=False) command.user = request.user command.unit = unit command.save() try: if command.command[:3] == "FC_": commands.fc_comd(dbcontrol.get_unit_address(unitname), commands.command_channel, command.command) print("FC COMMAND") else: commands.interface_command(unitname, command.command) print("Sent to hub command router") record_activity(unitname, command.command) messages.success(request, f'{command.command} command sent to {unit.name}') return render(request, 'units/unit_profile.html', context) except Exception as e: record_activity(unitname, f"{command.command} send failed") messages.success(request, f'Failed to send command to {unitname} - {e}') return render(request, 'units/unit_profile.html', context) return render(request, 'units/unit_profile.html', context)
def fc_comd(self, update, context): name = context.args[0] command = context.args[1] update.message.reply_text(f"Sending launch command to {name}") unit_address = dbcontrol.get_unit_address(name) commands.fc_comd(unit_address, self.command_channel, command)
def interface_command(unit, command): unitaddr = dbcontrol.get_unit_address(unit) if command == "WIFI_SCAN": wifi_comd(unitaddr, command_channel, command, 5) elif command == "CAMERA_SHOT": send_file(unitaddr, command_channel, 'image', 'N/A')