public void StartService(string directory)
        {
            var serviceFile = Path.Combine(directory, "service.json");

            if (!File.Exists(serviceFile))
            {
                log.Error("Configuration file service.json not found");
                return;
            }

            var vendors = Path.Combine(directory, "vendors.json");

            if (!File.Exists(Path.Combine(directory, "vendors.json")))
            {
                log.Error("Vendors file vendors.json not found");
                return;
            }

            var serviceConfig = JsonConvert.DeserializeObject <ServiceConfig>(File.ReadAllText(serviceFile));

            HostFactory.Run(x =>
            {
                x.Service <LightsService>(s =>
                {
                    s.ConstructUsing(name => new LightsService(serviceConfig, new MonitoringManager(VedorsManager.Load(vendors)), new LightsManagerFactory(), TaskPoolScheduler.Default));
                    s.WhenStarted(tc => tc.Start());
                    s.WhenStopped(tc => tc.Stop());
                });

                x.RunAsLocalSystem();
                x.SetDescription("Dash button Hue Monitoring Service");
                x.SetDisplayName("DashHue Service");
                x.SetServiceName("DashHueScanner");
            });
        }
Beispiel #2
0
        public override void Execute()
        {
            log.Info("Finding Dash Buttons...");
            var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var vendors   = Path.Combine(directory, "vendors.json");

            if (!File.Exists(Path.Combine(directory, "vendors.json")))
            {
                log.Error("Vendors file vendors.json not found");
                return;
            }

            var serviceFile = Path.Combine(directory, "service.json");
            ConcurrentDictionary <string, string> buttonsRegister = new ConcurrentDictionary <string, string>();
            ServiceConfig serviceConfig = new ServiceConfig();

            if (File.Exists(serviceFile))
            {
                serviceConfig = JsonConvert.DeserializeObject <ServiceConfig>(File.ReadAllText(serviceFile));
                if (serviceConfig.Buttons == null)
                {
                    serviceConfig.Buttons = new Dictionary <string, ButtonConfig>();
                }

                foreach (var existingButton in serviceConfig.Buttons)
                {
                    buttonsRegister[existingButton.Value.Mac] = existingButton.Key;
                }
            }

            try
            {
                MonitoringManager manager = new MonitoringManager(VedorsManager.Load(vendors));
                manager.StartListening()
                .Where(item => item.Vendor.Organization.ToLower().Contains("amazon"))
                .Subscribe(
                    item =>
                {
                    var name = item.Mac.GetMacName();
                    if (!buttonsRegister.TryAdd(name, name))
                    {
                        log.Info("Existing dashbutton found: {0}", item);
                        return;
                    }

                    log.Info("NEW dashbutton found: {0}", item);
                    lock (serviceConfig)
                    {
                        ButtonConfig config = new ButtonConfig();
                        config.Mac          = name;
                        serviceConfig.Buttons.Add(name, config);
                        config.Actions = new ButtonAction[] { };
                        File.WriteAllText(serviceFile, JsonConvert.SerializeObject(serviceConfig));
                    }
                });
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }