Beispiel #1
0
        public ThermostatLog Save()
        {
            ThermostatLog response = null;

            using (var db = new ThermoFeaturesDbContext()) {
                DateTimeOffset tolerance = DateTimeOffset.Now.AddHours(-5);

                response = db.ThermostatLogs
                           .Where(l => l.Stamp > tolerance)
                           .OrderByDescending(l => l.Stamp).FirstOrDefault();

                if (response == null)
                {
                    Console.WriteLine("No recent thermostat log found, pulling new records:");
                    List <Device> devices = GetDetails();
                    string        json    = JsonConvert.SerializeObject(devices);

                    response = new ThermostatLog()
                    {
                        Id    = Guid.NewGuid().ToString(),
                        Json  = json,
                        Stamp = DateTimeOffset.Now,
                    };

                    db.Add(response);
                    db.SaveChanges();
                }
                else
                {
                    Console.WriteLine($"Using thermostat log pulled at: {response.Stamp.ToString("M/d/yyyy h:mmtt")}");
                }
            }

            return(response);
        }
        public void Parse(ThermostatLog log)
        {
            List <Device> devices = JsonConvert.DeserializeObject <List <Device> >(log.Json);

            devices = devices
                      .OrderBy(d => d.Brand)
                      .ThenBy(d => d.ModelName)
                      .ToList();

            List <string> features = GetAllFeatures(devices);

            string line = "Brand, Model, Model #, ";

            line += string.Join(", ", features);
            Console.WriteLine(line);

            foreach (Device device in devices)
            {
                line = $"{device.Brand}, {device.ModelName}, {device.ModelNumber}, ";

                List <string> supportedFeatures = new List <string>();
                foreach (string feature in features)
                {
                    string supported = "";
                    if (device.Features.Contains(feature))
                    {
                        supported = "1";
                    }
                    supportedFeatures.Add(supported);
                }
                line += string.Join(", ", supportedFeatures);
                Console.WriteLine(line);
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            if (!File.Exists("local.db"))
            {
                throw new Exception("local.db not found, please run \"dotnet ef database update\" or the \"Run Migrations\" build step to build initial database.");
            }

            ApiService       service = new ApiService();
            ThermostatLog    log     = service.Save();
            ThermostatParser parser  = new ThermostatParser();

            parser.Parse(log);
        }