예제 #1
0
 public static void CreateCsv(this MultiDeviceRawData raw, string folder)
 {
     foreach (QuickType.Comp sensorType in Enum.GetValues(typeof(QuickType.Comp)))
     {
         CreateCsv(raw, folder, sensorType);
     }
 }
예제 #2
0
        /// <summary>
        /// Uses the Awair API to output sets of data from your devices.  This uses the raw data which has 10 second intervals and only one hour can be pulled per API call.
        /// <br/>1. Raw API response per device
        /// <br/>2. CSV of sensor readings per device
        /// <br/>3. CSV of sensor readings (all devices) per sensor type.  Useful for graphing as a scatter plot in Excel.
        /// </summary>
        /// <param name="endtime">End of one hour time span to get data. Any string that can be parsed into a DateTime.  One example 2020-02-20T05:00:00.000Z</param>
        /// <returns></returns>
        private static async Task Main(string endtime = "")
        {
            DateTime to;

            if (string.IsNullOrWhiteSpace(endtime))
            {
                to = DateTime.UtcNow;
            }
            else
            {
                to = DateTime.Parse(endtime).ToUniversalTime();
            }

            await Init();

            var from = to.AddHours(-1.0);

            Console.WriteLine($"Getting raw data from {from.ToLocalTime()} to {to.ToLocalTime()}");
            Console.WriteLine($"UTC times: {from} to {endtime}");

            var calls = devices.DevicesDevices.Select(x => api.GetDeviceRawAirDataAsync(x, from, to, true));
            var raws  = (await Task.WhenAll(calls)).ToList();

            foreach (var r in raws)
            {
                r.CreateDeviceCsv(Environment.CurrentDirectory);
            }

            var multidata = new MultiDeviceRawData(raws);

            multidata.CreateCsv(Environment.CurrentDirectory);
        }
예제 #3
0
        public static void CreateCsv(this MultiDeviceRawData raw, string folder, QuickType.Comp sensorType)
        {
            var file = Path.Combine(folder, $"Multi-{sensorType}.csv");

            Console.WriteLine($"Writing {sensorType} data from all devices to {file}");

            using var writer = new StreamWriter(file);
            using var csv = new CsvWriter(writer, System.Globalization.CultureInfo.InvariantCulture);

            var names = raw.RawAirData.Select(x => x.Device.Name).ToList();
            names.Sort();
            WriteHeader(csv, names);

            var data = raw.RawAirData.SelectMany(x => x.FlatData);

            // Write the rows
            foreach (var item in data)
            {
                csv.WriteField(item.Timestamp);
                foreach (var name in names)
                {
                    if (item.Device.Name == name)
                    {
                        var value = sensorType switch
                        {
                            QuickType.Comp.Co2 => item.Co2,
                            QuickType.Comp.Humid => item.Humid,
                            QuickType.Comp.Lux => item.Lux,
                            QuickType.Comp.Pm25 => item.Pm25,
                            QuickType.Comp.SplA => item.SplA,
                            QuickType.Comp.Temp => item.Temp,
                            QuickType.Comp.Voc => item.Voc,
                            _ => item.Score,
                        };

                        csv.WriteField(value);
                    }
                    else
                    {
                        csv.WriteField(string.Empty);
                    }
                }

                csv.NextRecord();
            }
        }