private async Task addControllerAsync(string ip, string authToken = null, bool setSettings = true)
        {
            try
            {
                Controller controller = new Controller(ip, authToken);
                if (clients.Any(c => c.IP.Equals(controller.IP)))
                {
                    controller.SelfDestruction();
                    Log.Info(string.Format("Client already Connected!" + Environment.NewLine + "{0}", controller));
                    return;
                }
                controller.AuthTokenReceived  += Controller_AuthTokenReceived;
                controller.UpdatedInfos       += Controller_UpdatedInfos;
                controller.PanelLayoutChanged += Controller_PanelLayoutChanged;
                clients.Add(controller);
                ControllerAdded?.Invoke(this, EventArgs.Empty);
                if (setSettings)
                {
                    string json = JsonConvert.SerializeObject(clients);
                    sm.SetKernelSetting(ESettingsType.APPLICATION, NANOLEAF_CONTROLLERS, json);
                }
                await Task.Delay(100);

                Log.Info($"Controller Added: {controller.ToString()}");

                bindInputAssignment();
            }
            catch (Exception e)
            {
                Log.Warn(string.Empty, e);
            }
        }
        protected override void startupPlugin()
        {
            Log.Info("Start");
            try
            {
                registerSettings();
                sm.SettingChanged += SettingChanged;


                ShowInInputAssignment = sm.GetKernelSetting <bool>(ESettingsType.APPLICATION, NANOLEAF_SHOW_IN_INPUTASSIGNMENT);
                Discover         = sm.GetKernelSetting <bool>(ESettingsType.APPLICATION, NANOLEAF_DISCOVER);
                AutoRequestToken = sm.GetKernelSetting <bool>(ESettingsType.APPLICATION, NANOLEAF_AUTOREQUEST_TOKEN);
                AutoConnect      = sm.GetKernelSetting <bool>(ESettingsType.APPLICATION, NANOLEAF_AUTOCONNECT);
                RefreshRate      = sm.GetKernelSetting <int>(ESettingsType.APPLICATION, NANOLEAF_REFRESH_RATE);

                if (AutoConnect)
                {
                    string jsonControllers = sm.GetKernelSetting <string>(ESettingsType.APPLICATION, NANOLEAF_CONTROLLERS);
                    JArray objControllers  = JsonConvert.DeserializeObject(jsonControllers) as JArray;
                    if (objControllers != null)
                    {
                        foreach (var c in objControllers)
                        {
                            var controller = new Controller(c);
                            clients.Add(controller);
                            controller.AuthTokenReceived  += Controller_AuthTokenReceived;
                            controller.UpdatedInfos       += Controller_UpdatedInfos;
                            controller.PanelLayoutChanged += Controller_PanelLayoutChanged;
                            ControllerAdded?.Invoke(this, EventArgs.Empty);
                        }
                    }
                }


                Communication.IPs = new IPAddress[] { IPAddress.Any };// KernelNetManager.getInstance().IPAddresses.Select(s=> IPAddress.Parse(s)).ToArray();
                if (Discover)
                {
                    Communication.StartDiscoveryTask();
                }
                if (ShowInInputAssignment)
                {
                    bindInputAssignment().GetAwaiter();
                }

                Communication.StartEventListener();
            }
            catch (Exception e)
            {
                Log.Error(string.Empty, e);
            }
            isStarted = true;
        }
        private void ValidateController(Controller controller)
        {
            bool isValid = controller.IsValid;

            controller.IsValid = (controller.IsValidATC && controller.Frequency.IsValidFrequency());
            if (isValid && !controller.IsValid)
            {
                ControllerDeleted?.Invoke(this, new ControllerEventArgs(controller));
            }
            else if (!isValid && controller.IsValid)
            {
                ControllerAdded?.Invoke(this, new ControllerEventArgs(controller));
            }
        }
示例#4
0
 private void OnControllerAdded(Controller controller)
 {
     ControllerAdded?.Invoke(controller);
 }