Example #1
0
        /// <summary>
        /// Given a device and a set of data returned from the SensorTag API,
        /// transforms the data from arrays of values, into individual readings
        /// for each time, device and set of values. Then indexes them into
        /// ElasticSearch.
        /// </summary>
        /// <param name="device">Device.</param>
        /// <param name="data">Data.</param>
        private ICollection <SensorReading> CreateSensorData(SensorDevice device, RawTempData data)
        {
            Utils.Log("Processing {0} data points...", data.d.Count());
            const double maxBatteryPercent = 3.0 / 100.0;

            List <SensorReading> readings = new List <SensorReading>();

            foreach (var datapoint in data.d)
            {
                readings.Add(new SensorReading
                {
                    timestamp         = datapoint.time,
                    device            = device,
                    temperature       = datapoint.temp_degC,
                    lux               = datapoint.lux,
                    humidity          = datapoint.cap,
                    battery           = datapoint.battery_volts,
                    batteryPercentage = (int)(datapoint.battery_volts / maxBatteryPercent)
                });
            }

            if (readings.Any())
            {
                Utils.Log("Found {0} readings for device '{1}' (latest: {2:dd-MMM-yy HH:mm:ss}).", readings.Count(), device.name, readings.Max(x => x.timestamp));
            }

            return(readings);
        }
        /// <summary>
        /// Given a device and a set of data returned from the SensorTag API,
        /// transforms the data from arrays of values, into individual readings
        /// for each time, device and set of values. Then indexes them into
        /// ElasticSearch.
        /// </summary>
        /// <param name="device">Device.</param>
        /// <param name="data">Data.</param>
        private ICollection <SensorReading> CreateSensorData(SensorDevice device, RawTempData data, TagInfo tag)
        {
            List <SensorReading> readings = new List <SensorReading>();

            if (data.d.Any())
            {
                Utils.Log("Processing {0} data points...", data.d.Count());


                foreach (var datapoint in data.d)
                {
                    int?battPercent = null;

                    // The battery info from the Tag will be for right now (i.e., when we
                    // queried it). So only use it to fill in the battery percentage for
                    // data points in the last 24 hours.
                    if (Math.Abs(DateTime.Now.Subtract(datapoint.time).Hours) <= 24)
                    {
                        battPercent = (int)(tag.batteryRemaining * 100);
                    }

                    readings.Add(new SensorReading
                    {
                        timestamp         = datapoint.time,
                        device            = device,
                        temperature       = datapoint.temp_degC,
                        lux               = datapoint.lux,
                        humidity          = datapoint.cap,
                        battery           = datapoint.battery_volts,
                        batteryPercentage = battPercent
                    });
                }
            }

            if (readings.Any())
            {
                Utils.Log("Found {0} readings for device '{1}' (latest: {2:dd-MMM-yy HH:mm:ss}).", readings.Count(), device.name, readings.Max(x => x.timestamp));
            }
            else
            {
                Utils.Log("No readings found in data for device '{0}'.", device.name);
            }

            return(readings);
        }