public TemperatureChartZoneModel[] GetTemperatureChartZones(TimeSpan period) { TimeSpan extendedPeriod = period.Add(TimeSpan.FromHours(6)); IEnumerable <DeviceStateLogRecord> states = _service.GetDeviceStateLog(extendedPeriod, Devices.Cooler, Devices.Heater); var zones = new List <TemperatureChartZoneModel>(); foreach (var statesByDevice in states.Reverse().GroupBy(state => state.Device)) { ZoneFormatBase format = ZoneFormatBase.GetFormat(statesByDevice.Key); var zonesByDevice = new List <TemperatureChartZoneModel>(); IEnumerable <DeviceStateLogRecord> uniqueConsecutiveStatesByDevice = getUniqueConsecutiveRecords(statesByDevice); foreach (DeviceStateLogRecord stateRecord in uniqueConsecutiveStatesByDevice) { if (stateRecord.State == DeviceStates.Off && zonesByDevice.Count == 0) { continue; } zonesByDevice.Add(createZone(stateRecord, format)); } // add format to infinite timespan if device is still switched on if (statesByDevice.Last().State == DeviceStates.On) { var recordToPresent = new DeviceStateLogRecord(DateTime.MinValue, statesByDevice.Key, DeviceStates.Off); zonesByDevice.Add(createZone(recordToPresent, format)); } zones.AddRange(zonesByDevice); } return(zones.OrderBy(zone => zone.Value.GetValueOrDefault(long.MaxValue)).ToArray()); }
private static TemperatureChartZoneModel createZone(DeviceStateLogRecord logRecord, ZoneFormatBase format) { return(new TemperatureChartZoneModel { Value = logRecord.DateTime == DateTime.MinValue ? (long?)null : logRecord.DateTime.ToUnixTimeMilliseconds(), Color = format.GetColor(logRecord.State), DashStyle = format.GetDashStyle(logRecord.State) }); }