Esempio n. 1
0
        public static void Main(string[] args)
        {
            // init and start
            var config = new HomeConfiguration();
            var app    = new HomeApplication();

            app.StartServices(config);

            // finalize
            void Shutdown()
            {
                System.Console.WriteLine("\nApplication is shutting down...");
                app.StopServices();
                System.Console.WriteLine("Done");
            }

            AssemblyLoadContext.Default.Unloading += context => { Shutdown(); };
            System.Console.CancelKeyPress         += (sender, eventArgs) => { Shutdown(); };

            // wait
            System.Console.WriteLine("Service is available. Press Ctrl+C to exit.");
            var done = new AutoResetEvent(false);

            done.WaitOne();
        }
        public async Task <IActionResult> Get()
        {
            var openhabItems = await _itemsApi.GetItemsAsync(metadata : "dialogflow", tags : Token, recursive : true);

            var zones = openhabItems.Where(ohi => ohi.GroupNames.Count == 1 &&
                                           ohi.Type == "Group" &&
                                           ((dynamic)ohi.Metadata?["dialogflow"])?.config.zone != "Internal" &&
                                           ((dynamic)ohi.Metadata?["dialogflow"])?.config.room == null).ToList();
            var rooms = openhabItems.Where(ohi => ohi.GroupNames.Count == 2 &&
                                           ohi.Type == "Group" &&
                                           ((dynamic)ohi.Metadata?["dialogflow"])?.config.zone != "Internal" &&
                                           ((dynamic)ohi.Metadata?["dialogflow"])?.config.type == null).ToList();
            var devices = openhabItems.Where(i => ((dynamic)i.Metadata?["dialogflow"])?.config.zone != null &&
                                             ((dynamic)i.Metadata?["dialogflow"])?.config.zone != "Internal").ToList();

            var configuration = new HomeConfiguration
            {
                Id    = Group,
                Uuid  = Uuid,
                Token = Token,
                Zones = zones.Select(z => new Zone
                {
                    Id          = z.Name,
                    Name        = ((dynamic)z.Metadata?["dialogflow"])?.config.zone,
                    Description = z.Label,
                    Rooms       = rooms.Where(r => r.GroupNames.Contains(z.Name)).Select(r => new Room
                    {
                        Id          = r.Name,
                        Name        = ((dynamic)r.Metadata?["dialogflow"])?.config.room,
                        Description = r.Label,
                        Devices     = devices.Where(d => d.GroupNames.Contains(r.Name)).Select(d => new Device
                        {
                            Id          = d.Name,
                            Description = d.Label,
                            Room        = ((dynamic)d.Metadata?["dialogflow"])?.config.room,
                            Zone        = ((dynamic)d.Metadata?["dialogflow"])?.config.zone,
                            Type        = ((dynamic)d.Metadata?["dialogflow"])?.config.type,
                            OpenhabType = d.Type
                        })
                    })
                })
            };

            return(Ok(configuration));
        }