public Measurement GetWeight(bool single = false) { try { if (clockPin == null || dataPin == null) { return(null); } // TODO: sample sensors and report weight -- mocked for now //return new Measurement(165.0f, Measurement.UnitsOfMeasure.Pounds); device = new HX711(clockPin, dataPin); var w = _GetOutputData(); Debug.WriteLine($"Single:{w}"); var c = Calibrated(w); if (!CheckAnomaly(c)) { PriorMeasurements.Enqueue(new Measurement(c, Measurement.UnitsOfMeasure.Ounces)); } Debug.WriteLine($"Current Avg:{PriorMeasurements.Average(i => i.Amount)}"); #if DEBUG foreach (var item in PriorMeasurements) { Debug.WriteLine($" {item.Amount}"); } #endif if (single) { return(new Measurement(w, Measurement.UnitsOfMeasure.Ounces)); } else { return(new Measurement(PriorMeasurements.Average(i => i.Amount), Measurement.UnitsOfMeasure.Ounces)); } } catch (Exception ex) { Debug.WriteLine($"Error,{ex.Message}"); KegLogger.KegLogException(ex, "Weight:GetWeight", SeverityLevel.Critical); KegLogger.KegLogTrace(ex.Message, "Weight:GetWeight", SeverityLevel.Warning, new Dictionary <string, string>() { { "ClockPin", CalibrationSettings[WeightClockGpioPinNumberSetting].ToString() }, { "DataPin", CalibrationSettings[WeightDataGpioPinNumberSetting].ToString() } }); } return(null); }
private bool CheckAnomaly(float newValue) { if (PriorMeasurements.Count > 2 && newValue > PriorMeasurements.Average(i => i.Amount) * 2) { Debug.WriteLine($" Anomaly: {newValue}"); KegLogger.KegLogTrace("Anomaly Detected!", "Weight:GetWeight", SeverityLevel.Error, new Dictionary <string, string>() { { "Weight", newValue.ToString() } }); return(true); } else { return(false); } }
public void Initialize() { if (IsInitialized) { return; } //IsInitialized = true; int pinNumber = Int32.Parse(CalibrationSettings[FlowGPIOPinNumberSetting].ToString()); var c = GpioController.GetDefault(); if (c != null) { try { _pin = c.OpenPin(pinNumber); if (_pin != null) { if (_pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp)) { _pin.SetDriveMode(GpioPinDriveMode.InputPullUp); } _pin.ValueChanged += _pin_ValueChanged; } } catch (Exception ex) { Debug.WriteLine($"Exception:{ex.Message}"); KegLogger.KegLogException(ex, "Flow:Initialize", SeverityLevel.Critical); KegLogger.KegLogTrace(ex.Message, "Flow:Initialize", SeverityLevel.Error, new Dictionary <string, string>() { { "PinNumber", CalibrationSettings[FlowGPIOPinNumberSetting].ToString() } }); //Pin used exception //TODO } } IsInitialized = true; }
public void Initialize() { var gpio = GpioController.GetDefault(); if (gpio == null) { _pin = null; Debug.WriteLine("Flow Control:Unable to initialize, no GPIO controller."); KegLogger.KegLogTrace("Custom: Flow Control:Unable to initialize, no GPIO controller.", "FlowControl:Initialize", SeverityLevel.Error, new Dictionary <string, string>() { { "PinNumber", CalibrationSettings[FlowControlGpioPinNumberSetting].ToString() }, { "IsActive", _isActive.ToString() } }); return; } if (_pin == null) { try { _pin = gpio.OpenPin(Int32.Parse(CalibrationSettings[FlowControlGpioPinNumberSetting].ToString()), GpioSharingMode.Exclusive); _pin.Write(_isActive); _pin.SetDriveMode(GpioPinDriveMode.Output); } catch (Exception ex) { Debug.WriteLine($"Flow Control:Unable to initialize, PIN Null.{ex.Message}"); KegLogger.KegLogException(ex, "FlowControl:Initialize", SeverityLevel.Critical); KegLogger.KegLogTrace(ex.Message, "FlowControl:Initialize", SeverityLevel.Critical, new Dictionary <string, string>() { { "PinNumber", CalibrationSettings[FlowControlGpioPinNumberSetting].ToString() }, { "IsActive", _isActive.ToString() } }); } } }
// takes an instant reading or returns the average accumulated value, and returns // the interpreted value adjusted by calibration settings. public async Task <Models.Measurement> GetTemperature() { Measurement result = _lastMeasurement; try { if (_pin == null || _dht11 == null) { int pinNumber = TEMPERATUREDATAPIN; // default if (CalibrationSettings.ContainsKey(TempGpioPinNumberSetting)) { pinNumber = Int32.Parse(CalibrationSettings[TempGpioPinNumberSetting].ToString()); } var c = GpioController.GetDefault(); if (c != null) { _pin = c.OpenPin(pinNumber, GpioSharingMode.Exclusive); if (_pin != null) { _dht11 = new Dht11(_pin, GpioPinDriveMode.Input); } } } if (_dht11 == null) { return(null); } DhtReading reading = await _dht11.GetReadingAsync().AsTask(); for (int retries = 0; retries < 10; retries++) { if (reading.TimedOut) { Debug.Write("."); } else if (!reading.IsValid) { Debug.Write("x"); } else { break; } } if (reading.IsValid) { //Debug.WriteLine($"Temp reading = {reading.Temperature}"); Measurement.UnitsOfMeasure units = Measurement.UnitsOfMeasure.Fahrenheit; if (CalibrationSettings.ContainsKey(PreferredTemperatureUnits)) { units = (Measurement.UnitsOfMeasure)Enum.Parse(typeof(Measurement.UnitsOfMeasure), CalibrationSettings[PreferredTemperatureUnits].ToString()); } Measurement adjust = new Measurement(0.0f, Measurement.UnitsOfMeasure.Fahrenheit); if (CalibrationSettings.ContainsKey(AdjustTemperatureSetting)) { adjust = CalibrationSettings[AdjustTemperatureSetting] as Measurement; } float temperature = (float)reading.Temperature; temperature = Convert(Measurement.UnitsOfMeasure.Celsius, units, temperature); adjust.Amount = Convert(adjust.Units, units, adjust.Amount); adjust.Units = units; temperature += adjust.Amount; // now that we know they are in the same (preferred) units result = new Measurement(temperature, units); _lastMeasurement = result; OnTemperatureChanged(new MeasurementChangedEventArgs(result)); } else { KegLogger.KegLogException(new TemperatureException("Custom: Unable to Read temperature."), "GetTemperature", SeverityLevel.Warning); Debug.WriteLine($"Unable to read temperature."); } } catch (Exception ex) { Debug.WriteLine("Error, " + ex.Message); KegLogger.KegLogException(ex, "GetTemperature", SeverityLevel.Critical); } return(result); }