def gravity_manage(request, sensor_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: sensor = GravitySensor.objects.get(id=sensor_id) except ObjectDoesNotExist: messages.error(request, u'Unable to load sensor with ID {}'.format(sensor_id)) return redirect('gravity_log_list') context = {'active_device': sensor} if sensor.sensor_type == GravitySensor.SENSOR_ISPINDEL: # I am sure there is an easier way to do this, I just can't think of it at the moment initial = { 'a': sensor.ispindel_configuration.third_degree_coefficient, 'b': sensor.ispindel_configuration.second_degree_coefficient, 'c': sensor.ispindel_configuration.first_degree_coefficient, 'd': sensor.ispindel_configuration.constant_term, } ispindel_coefficient_form = forms.IspindelCoefficientForm( initial=initial) context['ispindel_coefficient_form'] = ispindel_coefficient_form calibration_points = IspindelGravityCalibrationPoint.objects.filter( sensor=sensor.ispindel_configuration).order_by('angle') context['ispindel_calibration_points'] = calibration_points ispindel_calibration_form = forms.IspindelCalibrationPointForm( initial={'sensor': sensor.ispindel_configuration}) context['ispindel_calibration_form'] = ispindel_calibration_form if sensor.sensor_type == GravitySensor.SENSOR_TILT: # I am sure there is an easier way to do this, I just can't think of it at the moment initial = { 'b': sensor.tilt_configuration.grav_second_degree_coefficient, 'c': sensor.tilt_configuration.grav_first_degree_coefficient, 'd': sensor.tilt_configuration.grav_constant_term, } tilt_coefficient_form = forms.TiltCoefficientForm(initial=initial) context['tilt_coefficient_form'] = tilt_coefficient_form calibration_points = TiltGravityCalibrationPoint.objects.filter( sensor=sensor.tilt_configuration).order_by('tilt_measured_gravity') context['tilt_calibration_points'] = calibration_points tilt_calibration_form = forms.TiltGravityCalibrationPointForm( initial={'sensor': sensor.tilt_configuration}) context['tilt_calibration_form'] = tilt_calibration_form return render(request, template_name='gravity/gravity_manage.html', context=context)
def gravity_tilt_coefficients(request, sensor_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: sensor = GravitySensor.objects.get(id=sensor_id) except: messages.error(request, u'Unable to load sensor with ID {}'.format(sensor_id)) return redirect('gravity_log_list') if sensor.sensor_type != GravitySensor.SENSOR_TILT: messages.error( request, u'Sensor {} is not a Tilt and cannot be configured in this way'. format(sensor_id)) return redirect('gravity_log_list') if request.POST: tilt_coefficient_form = forms.TiltCoefficientForm(request.POST) if tilt_coefficient_form.is_valid(): # sensor.tilt_configuration.grav_third_degree_coefficient = tilt_coefficient_form.cleaned_data['a'] sensor.tilt_configuration.grav_second_degree_coefficient = tilt_coefficient_form.cleaned_data[ 'b'] sensor.tilt_configuration.grav_first_degree_coefficient = tilt_coefficient_form.cleaned_data[ 'c'] sensor.tilt_configuration.grav_constant_term = tilt_coefficient_form.cleaned_data[ 'd'] if sensor.tilt_configuration.coefficients_up_to_date: # If we are manually setting the coefficients, then we'll assume they're up to date sensor.tilt_configuration.coefficients_up_to_date = True sensor.tilt_configuration.save() messages.success(request, u"Coefficients updated") else: messages.error(request, u"Invalid coefficients provided") else: messages.error(request, u"No coefficients provided") return redirect("gravity_manage", sensor_id=sensor_id)
def gravity_manage(request, sensor_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: sensor = GravitySensor.objects.get(id=sensor_id) except ObjectDoesNotExist: messages.error(request, u'Unable to load sensor with ID {}'.format(sensor_id)) return redirect('gravity_log_list') context = {'active_device': sensor} if sensor.sensor_type == GravitySensor.SENSOR_ISPINDEL: # I am sure there is an easier way to do this, I just can't think of it at the moment try: initial = { 'a': sensor.ispindel_configuration.third_degree_coefficient, 'b': sensor.ispindel_configuration.second_degree_coefficient, 'c': sensor.ispindel_configuration.first_degree_coefficient, 'd': sensor.ispindel_configuration.constant_term, 't': sensor.ispindel_configuration.temperature_correction, } except ObjectDoesNotExist: # The sensor is in an inconsistent state. Delete it. messages.error( request, f"The gravity sensor {sensor.name} had incomplete configuration and was deleted" ) sensor.delete() return redirect("siteroot") ispindel_coefficient_form = forms.IspindelCoefficientForm( initial=initial) context['ispindel_coefficient_form'] = ispindel_coefficient_form calibration_points = IspindelGravityCalibrationPoint.objects.filter( sensor=sensor.ispindel_configuration).order_by('angle') context['ispindel_calibration_points'] = calibration_points ispindel_calibration_form = forms.IspindelCalibrationPointForm( initial={'sensor': sensor.ispindel_configuration}) context['ispindel_calibration_form'] = ispindel_calibration_form #context['temp_display_format'] = config.DATE_TIME_FORMAT_DISPLAY return render(request, template_name='gravity/gravity_manage_ispindel.html', context=context) elif sensor.sensor_type == GravitySensor.SENSOR_TILT: # I am sure there is an easier way to do this, I just can't think of it at the moment try: initial = { 'b': sensor.tilt_configuration.grav_second_degree_coefficient, 'c': sensor.tilt_configuration.grav_first_degree_coefficient, 'd': sensor.tilt_configuration.grav_constant_term, } except ObjectDoesNotExist: # The sensor is in an inconsistent state. Delete it. messages.error( request, f"The gravity sensor {sensor.name} had incomplete configuration and was deleted" ) sensor.delete() return redirect("siteroot") tilt_coefficient_form = forms.TiltCoefficientForm(initial=initial) context['tilt_coefficient_form'] = tilt_coefficient_form calibration_points = TiltGravityCalibrationPoint.objects.filter( sensor=sensor.tilt_configuration).order_by('tilt_measured_gravity') context['tilt_calibration_points'] = calibration_points tilt_calibration_form = forms.TiltGravityCalibrationPointForm( initial={'sensor': sensor.tilt_configuration}) context['tilt_calibration_form'] = tilt_calibration_form tilt_extras = sensor.tilt_configuration.load_extras_from_redis() context['tilt_extras'] = tilt_extras if sensor.tilt_configuration.connection_type == TiltConfiguration.CONNECTION_BRIDGE: # For TiltBridges, we want to give the user the info necessary to configure the device to communicate with # Fermentrack fermentrack_host = request.META['HTTP_HOST'] context[ 'fermentrack_url'] = f"http://{fermentrack_host}/tiltbridge/" return render(request, template_name='gravity/gravity_manage_tilt.html', context=context) else: return render(request, template_name='gravity/gravity_manage.html', context=context)
def gravity_manage(request, sensor_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: sensor = GravitySensor.objects.get(id=sensor_id) except ObjectDoesNotExist: messages.error(request, u'Unable to load sensor with ID {}'.format(sensor_id)) return redirect('gravity_log_list') context = {'active_device': sensor} if sensor.sensor_type == GravitySensor.SENSOR_ISPINDEL: # I am sure there is an easier way to do this, I just can't think of it at the moment try: initial = { 'a': sensor.ispindel_configuration.third_degree_coefficient, 'b': sensor.ispindel_configuration.second_degree_coefficient, 'c': sensor.ispindel_configuration.first_degree_coefficient, 'd': sensor.ispindel_configuration.constant_term, } except ObjectDoesNotExist: # The sensor is in an inconsistent state. Delete it. messages.error( request, u"The gravity sensor {} had incomplete configuration and was deleted" .format(sensor.name)) sensor.delete() return redirect("siteroot") ispindel_coefficient_form = forms.IspindelCoefficientForm( initial=initial) context['ispindel_coefficient_form'] = ispindel_coefficient_form calibration_points = IspindelGravityCalibrationPoint.objects.filter( sensor=sensor.ispindel_configuration).order_by('angle') context['ispindel_calibration_points'] = calibration_points ispindel_calibration_form = forms.IspindelCalibrationPointForm( initial={'sensor': sensor.ispindel_configuration}) context['ispindel_calibration_form'] = ispindel_calibration_form return render(request, template_name='gravity/gravity_manage_ispindel.html', context=context) elif sensor.sensor_type == GravitySensor.SENSOR_TILT: # I am sure there is an easier way to do this, I just can't think of it at the moment try: initial = { 'b': sensor.tilt_configuration.grav_second_degree_coefficient, 'c': sensor.tilt_configuration.grav_first_degree_coefficient, 'd': sensor.tilt_configuration.grav_constant_term, } except ObjectDoesNotExist: # The sensor is in an inconsistent state. Delete it. messages.error( request, u"The gravity sensor {} had incomplete configuration and was deleted" .format(sensor.name)) sensor.delete() return redirect("siteroot") tilt_coefficient_form = forms.TiltCoefficientForm(initial=initial) context['tilt_coefficient_form'] = tilt_coefficient_form calibration_points = TiltGravityCalibrationPoint.objects.filter( sensor=sensor.tilt_configuration).order_by('tilt_measured_gravity') context['tilt_calibration_points'] = calibration_points tilt_calibration_form = forms.TiltGravityCalibrationPointForm( initial={'sensor': sensor.tilt_configuration}) context['tilt_calibration_form'] = tilt_calibration_form if sensor.tilt_configuration.connection_type == TiltConfiguration.CONNECTION_BRIDGE: # For TiltBridges, we want to give the user the info necessary to configure the device to communicate with # Fermentrack fermentrack_host = request.META['HTTP_HOST'] try: if ":" in fermentrack_host: fermentrack_host = fermentrack_host[:fermentrack_host. find(":")] ais = socket.getaddrinfo(fermentrack_host, 0, 0, 0, 0) ip_list = [result[-1][0] for result in ais] ip_list = list(set(ip_list)) resolved_address = ip_list[0] fermentrack_url = "http://{}/tiltbridge/".format( resolved_address) except: # For some reason we failed to resolve the IP address of Fermentrack fermentrack_url = "<Error - Unable to resolve Fermentrack IP address>" context['fermentrack_url'] = fermentrack_url return render(request, template_name='gravity/gravity_manage_tilt.html', context=context) else: return render(request, template_name='gravity/gravity_manage.html', context=context)