Ejemplo n.º 1
0
        /// <summary>
        /// This is the event handler that runs every time the Arduino sends a new message.
        /// It parses the message to an array and sends the values to both the graph
        /// and the SaMi object.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Characteristic_ValueUpdated(object sender, Plugin.BLE.Abstractions.EventArgs.CharacteristicUpdatedEventArgs e)
        {
            string valueString = Encoding.Default.GetString(e.Characteristic.Value);
            int    count       = valueString.Length - valueString.Replace(",", "").Length;

            if (count != 5 || valueString[0] == ',')
            {
                return;
            }
            double[] values;

            /*This part is wrapped in a try/catch because BLE messages can be a bit jumbled when
             * connection has just been established. Jumbled message won't parse, of course,
             * so they get skipped.*/
            try
            {
                values = Array.ConvertAll(valueString.Split(','), Double.Parse);
            }
            catch
            {
                return;
            }
            SenProReading newReading = new SenProReading {
                DateTime = DateTime.Now, Values = values
            };
            double dateTimePlot = DateTimeAxis.ToDouble(newReading.DateTime);

            (plot.Model.Series[0] as LineSeries).Points.Add(new DataPoint(dateTimePlot, newReading.Values[0]));
            (plot.Model.Series[1] as LineSeries).Points.Add(new DataPoint(dateTimePlot, newReading.Values[1]));
            (plot.Model.Series[2] as LineSeries).Points.Add(new DataPoint(dateTimePlot, newReading.Values[2]));
            (plot.Model.Series[3] as LineSeries).Points.Add(new DataPoint(dateTimePlot, newReading.Values[3]));
            (plot.Model.Series[4] as LineSeries).Points.Add(new DataPoint(dateTimePlot, newReading.Values[4]));
            (plot.Model.Series[5] as LineSeries).Points.Add(new DataPoint(dateTimePlot, newReading.Values[5]));

            if ((plot.Model.Series[0] as LineSeries).Points.Count > 15)
            {
                (plot.Model.Series[0] as LineSeries).Points.RemoveAt(0);
                (plot.Model.Series[1] as LineSeries).Points.RemoveAt(0);
                (plot.Model.Series[2] as LineSeries).Points.RemoveAt(0);
                (plot.Model.Series[3] as LineSeries).Points.RemoveAt(0);
                (plot.Model.Series[4] as LineSeries).Points.RemoveAt(0);
                (plot.Model.Series[5] as LineSeries).Points.RemoveAt(0);
            }

            plot.Model.Axes[0].Maximum = DateTimeAxis.ToDouble(newReading.DateTime.AddSeconds(2));
            plot.Model.Axes[0].Minimum = DateTimeAxis.ToDouble(newReading.DateTime.AddSeconds(-8));

            samiClient.AddMeasurement(newReading);

            if (samiClient.MeasurementMinuteCheck())
            {
                samiClient.SaveResults();
            }

            Device.BeginInvokeOnMainThread(() =>
            {
                plot.InvalidatePlot();
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This makes a MeasurementModel based on the current timestamp, consisting of the sensor readings
        /// done on that specific time.
        /// </summary>
        /// <param name="readingInput"></param>
        public void AddMeasurement(SenProReading readingInput)
        {
            MeasurementModel m = new MeasurementModel()
            {
                Object    = "Halax Prototype",
                Tag       = modelTag,
                Timestamp = readingInput.DateTime
            };

            DataModel sensorOne = new DataModel()
            {
                Tag   = "Sensor 1",
                Value = readingInput.Values[0]
            };

            DataModel sensorTwo = new DataModel()
            {
                Tag   = "Sensor 2",
                Value = readingInput.Values[1]
            };

            DataModel sensorThree = new DataModel()
            {
                Tag   = "Sensor 3",
                Value = readingInput.Values[2]
            };

            DataModel sensorFour = new DataModel()
            {
                Tag   = "Sensor 4",
                Value = readingInput.Values[3]
            };

            DataModel sensorFive = new DataModel()
            {
                Tag   = "Sensor 5",
                Value = readingInput.Values[4]
            };

            DataModel sensorSix = new DataModel()
            {
                Tag   = "Sensor 6",
                Value = readingInput.Values[5]
            };

            m.Data    = new DataModel[6];
            m.Data[0] = sensorOne;
            m.Data[1] = sensorTwo;
            m.Data[2] = sensorThree;
            m.Data[3] = sensorFour;
            m.Data[4] = sensorFive;
            m.Data[5] = sensorSix;

            measurements.Add(m);
        }