def device_guided_add_mdns(request, mdns_id): # TODO - Add user permissioning # if not request.user.has_perm('app.add_device'): # messages.error(request, 'Your account is not permissioned to add devices. Please contact an admin') # return redirect("/") # Originally I thought we needed to rescan the network, but so long as ESP8266 is the only device that supports WiFi # We can use mdns_id alone to handle the initial values # installed_devices, available_devices = mdnsLocator.find_mdns_devices() # # mdns_device = None # # for this_device in available_devices: # if this_device['mDNSname'] == mdns_id: # mdns_device = this_device # # if mdns_device is None: # redirect('device_guided_mdns') if request.POST: form = device_forms.DeviceForm(request.POST) if form.is_valid(): new_device = BrewPiDevice( device_name=form.cleaned_data['device_name'], temp_format=form.cleaned_data['temp_format'], data_point_log_interval=form.cleaned_data['data_point_log_interval'], useInetSocket=form.cleaned_data['useInetSocket'], socketPort=form.cleaned_data['socketPort'], socketHost=form.cleaned_data['socketHost'], serial_port=form.cleaned_data['serial_port'], serial_alt_port=form.cleaned_data['serial_alt_port'], board_type=form.cleaned_data['board_type'], socket_name=form.cleaned_data['socket_name'], connection_type=form.cleaned_data['connection_type'], wifi_host=form.cleaned_data['wifi_host'], wifi_port=form.cleaned_data['wifi_port'], ) new_device.save() messages.success(request, u'Device {} Added. Please wait a few seconds for controller to start'.format(new_device)) return redirect("/") else: return render_with_devices(request, template_name='setup/device_guided_add_mdns.html', context={'form': form}) else: random_port = random.randint(2000,3000) # If we were just passed to the form, provide the initial values initial_values = {'board_type': 'esp8266', 'wifi_host': mdns_id, 'wifi_port': 23, 'connection_type': 'wifi', 'socketPort': random_port, 'temp_format': config.TEMPERATURE_FORMAT} form = device_forms.DeviceForm(initial=initial_values) return render_with_devices(request, template_name='setup/device_guided_add_mdns.html', context={'form': form})
def add_device(request): # TODO - Add user permissioning # if not request.user.has_perm('app.add_device'): # messages.error(request, 'Your account is not permissioned to add devices. Please contact an admin') # return redirect("/") if request.POST: form = device_forms.DeviceForm(request.POST) if form.is_valid(): # TODO - Add support for editing to this new_device = BrewPiDevice( device_name=form.cleaned_data['device_name'], temp_format=form.cleaned_data['temp_format'], data_point_log_interval=form. cleaned_data['data_point_log_interval'], useInetSocket=form.cleaned_data['useInetSocket'], socketPort=form.cleaned_data['socketPort'], socketHost=form.cleaned_data['socketHost'], serial_port=form.cleaned_data['serial_port'], serial_alt_port=form.cleaned_data['serial_alt_port'], board_type=form.cleaned_data['board_type'], socket_name=form.cleaned_data['socket_name'], connection_type=form.cleaned_data['connection_type'], wifi_host=form.cleaned_data['wifi_host'], wifi_port=form.cleaned_data['wifi_port'], ) new_device.save() messages.success( request, 'Device {} Added.<br>Please wait a few seconds for controller to start' .format(new_device.device_name)) return redirect("/") else: return render_with_devices(request, template_name='device_add.html', context={'form': form}) else: # We don't want two devices to have the same port, and the port number doesn't really matter. Just # randomize it. random_port = random.randint(2000, 3000) initial_values = { 'socketPort': random_port, 'temp_format': config.TEMPERATURE_FORMAT } form = device_forms.DeviceForm(initial=initial_values) return render_with_devices(request, template_name='device_add.html', context={'form': form})
def device_guided_serial_autodetect(request, device_family): # TODO - Add user permissioning # if not request.user.has_perm('app.add_device'): # messages.error(request, 'Your account is not permissioned to add devices. Please contact an admin') # return redirect("/") # device_guided_serial_autodetect contains all 4 steps in the Serial autodetection guided setup. if not request.POST: # If we haven't had something posted to us, provide the instructions page. (Step 1) return render_with_devices( request, template_name='setup/device_guided_serial_autodetect_1.html', context={'device_family': device_family}) else: # Something was posted - figure out what step we're on by looking at the "step" field if 'step' not in request.POST: # We received a form, but not the right form. Redirect to the start of the autodetection flow. return render_with_devices( request, template_name='setup/device_guided_serial_autodetect_1.html', context={'device_family': device_family}) elif request.POST['step'] == "2": # Step 2 - Cache the current devices & present the next set of instructions to the user current_devices = serial_integration.cache_current_devices() return render_with_devices( request, template_name='setup/device_guided_serial_autodetect_2.html', context={ 'device_family': device_family, 'current_devices': current_devices }) elif request.POST['step'] == "3": # Step 3 - Detect newly-connected devices & prompt the user to select the one that corresponds to the # device they want to configure. _, _, _, new_devices_enriched = serial_integration.compare_current_devices_against_cache( device_family) return render_with_devices( request, template_name='setup/device_guided_serial_autodetect_3.html', context={ 'device_family': device_family, 'new_devices': new_devices_enriched }) elif request.POST['step'] == "4": # Step 4 - MAGIC. if 'serial_port' in request.POST: form = device_forms.DeviceForm(request.POST) if form.is_valid(): new_device = BrewPiDevice( device_name=form.cleaned_data['device_name'], temp_format=form.cleaned_data['temp_format'], data_point_log_interval=form. cleaned_data['data_point_log_interval'], useInetSocket=form.cleaned_data['useInetSocket'], socketPort=form.cleaned_data['socketPort'], socketHost=form.cleaned_data['socketHost'], serial_port=form.cleaned_data['serial_port'], serial_alt_port=form.cleaned_data['serial_alt_port'], board_type=form.cleaned_data['board_type'], socket_name=form.cleaned_data['socket_name'], connection_type=form.cleaned_data['connection_type'], wifi_host=form.cleaned_data['wifi_host'], wifi_port=form.cleaned_data['wifi_port'], prefer_connecting_via_udev=form. cleaned_data['prefer_connecting_via_udev'], ) new_device.save() # Once the device is added, go ahead and autodetect the udev serial number. if new_device.connection_type == BrewPiDevice.CONNECTION_SERIAL: new_device.set_udev_from_port() messages.success( request, u'Dispositivo {} Agregado. Espere unos segundos para comenzar' .format(new_device)) return redirect("/") else: return render_with_devices( request, template_name= 'setup/device_guided_serial_autodetect_4_add.html', context={ 'form': form, 'device_family': device_family }) else: random_port = random.randint(2000, 3000) # If we were just passed to the form, provide the initial values # TODO - Correctly determine 'board_type' if device_family == 'ESP8266': board_type = 'esp8266' elif device_family == 'Arduino': board_type = 'uno' else: # Invalid board type - shouldn't ever get here. messages.error( request, "Tipo de placa inválida para la autodetección por serial" ) return redirect("/") initial_values = { 'board_type': board_type, 'serial_port': request.POST['device'], 'connection_type': 'serial', 'socketPort': random_port, 'temp_format': config.TEMPERATURE_FORMAT } form = device_forms.DeviceForm(initial=initial_values) return render_with_devices( request, template_name= 'setup/device_guided_serial_autodetect_4_add.html', context={ 'form': form, 'device_family': device_family }) elif request.POST['step'] == "5": pass else: # The step number we received was invalid. Redirect to the start of the autodetection flow. return render_with_devices( request, template_name='setup/device_guided_serial_autodetect_1.html', context={'device_family': device_family})
def device_manage(request, device_id): # TODO - Add user permissioning # if not request.user.has_perm('app.edit_device'): # messages.error(request, 'Your account is not permissioned to edit devices. Please contact an admin') # return redirect("/") try: active_device = BrewPiDevice.objects.get(id=device_id) except: messages.error(request, "Unable to load device with ID {}".format(device_id)) return redirect('siteroot') # Forms posted back to device_manage are explicitly settings update forms if request.POST: form = device_forms.DeviceForm(request.POST) if form.is_valid(): # Update the device settings based on what we were passed via the form active_device.device_name = form.cleaned_data['device_name'] active_device.temp_format = form.cleaned_data['temp_format'] active_device.data_point_log_interval = form.cleaned_data[ 'data_point_log_interval'] active_device.useInetSocket = form.cleaned_data['useInetSocket'] active_device.socketPort = form.cleaned_data['socketPort'] active_device.socketHost = form.cleaned_data['socketHost'] active_device.serial_port = form.cleaned_data['serial_port'] active_device.serial_alt_port = form.cleaned_data[ 'serial_alt_port'] # Not going to allow editing the board type. Can revisit if there seems to be a need later # active_device.board_type=form.cleaned_data['board_type'] active_device.socket_name = form.cleaned_data['socket_name'] active_device.connection_type = form.cleaned_data[ 'connection_type'] active_device.wifi_host = form.cleaned_data['wifi_host'] active_device.wifi_port = form.cleaned_data['wifi_port'] active_device.save() messages.success( request, 'Device {} Updated.<br>Please wait a few seconds for the connection to restart' .format(active_device.device_name)) # TODO - Trigger Circus to reload properly, rather than using an external script cmd = "nohup utils/reset_circus.sh" subprocess.call(cmd, shell=True) return render_with_devices(request, template_name='device_manage.html', context={ 'form': form, 'active_device': active_device }) else: return render_with_devices(request, template_name='device_manage.html', context={ 'form': form, 'active_device': active_device }) else: # This would probably be easier if I was to use ModelForm instead of Form, but at this point I don't feel like # refactoring it. Project for later if need be. initial_values = { 'device_name': active_device.device_name, 'temp_format': active_device.temp_format, 'data_point_log_interval': active_device.data_point_log_interval, 'connection_type': active_device.connection_type, 'useInetSocket': active_device.useInetSocket, 'socketPort': active_device.socketPort, 'socketHost': active_device.socketHost, 'serial_port': active_device.serial_port, 'serial_alt_port': active_device.serial_alt_port, 'board_type': active_device.board_type, 'socket_name': active_device.socket_name, 'wifi_host': active_device.wifi_host, 'wifi_port': active_device.wifi_port, } form = device_forms.DeviceForm(initial=initial_values) return render_with_devices(request, template_name='device_manage.html', context={ 'form': form, 'active_device': active_device })