Ejemplo n.º 1
0
        /// <summary>
        /// Queries the data in the sensor tag.
        /// </summary>
        public ICollection <SensorReading> QueryHiveData(HiveService service, List <SensorDevice> allDevices)
        {
            List <SensorReading> readings = new List <SensorReading>();

            var hiveBattChannel = service.GetHiveChannel(HiveService.ChannelType.battery);
            var hiveTempChannel = service.GetHiveChannel(HiveService.ChannelType.temperature);

            var fromDate = getDeviceHighWaterMark(hiveTempChannel.UUID);
            var toDate   = DateTime.UtcNow;

            SensorDevice device = new SensorDevice {
                uuid = hiveTempChannel.UUID, name = "Hive", type = "HiveHome", location = ""
            };

            allDevices.Add(device);

            Utils.Log("Querying hive {0} data between {1} and {2}...", hiveTempChannel.id, fromDate, toDate);

            var values     = service.QueryHiveValues(hiveTempChannel, device, fromDate, toDate, settings.refreshPeriodMins);
            var battValues = service.QueryHiveValues(hiveBattChannel, device, fromDate, toDate, settings.refreshPeriodMins);

            if (values.Any())
            {
                SensorReading lastReading = null;

                foreach (var pair in values)
                {
                    lastReading = new SensorReading
                    {
                        timestamp   = Utils.getFromEpoch(pair.Key),
                        temperature = pair.Value,
                        lux         = null,
                        humidity    = null,
                        device      = device
                    };

                    if (battValues.ContainsKey(pair.Key))
                    {
                        lastReading.battery = battValues[pair.Key];
                    }

                    readings.Add(lastReading);
                }

                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 for device {1}.", device.name);
                }

                // Now query the current battery level, and set it in the most recent reading.
                lastReading.batteryPercentage = service.QueryHiveBattery();
            }

            return(readings);
        }