private IEnumerable <LightSwitchClient> GetSafeClients() { var clientCount = _lightSwitchClients.Count(); var clients = new LightSwitchClient[clientCount]; _lightSwitchClients.CopyTo(clients); return(clients); }
private async Task SendZoneListAsync(LightSwitchClient client) { using (var context = new ZvsContext(EntityContextConnection)) foreach (var g in await context.Groups.OrderBy(o => o.Name).ToListAsync()) { await SendLightSwitchCommand(client, LightSwitchProtocol.CreateZoneCmd(g.Name, g.Id.ToString())); } }
public async void StartLightSwitchServer() { _cts = new CancellationTokenSource(); var listener = new TcpListener(IPAddress.Any, PortSetting); listener.Server.NoDelay = true; listener.Server.LingerState = new LingerOption(true, 2); listener.Start(); await Log.ReportInfoFormatAsync(CancellationToken, "LightSwitch server started on port {0}", PortSetting); NotifyEntityChangeContext.ChangeNotifications <DeviceValue> .OnEntityUpdated += SpeechPlugin_OnEntityUpdated; while (true) { var task = listener.AcceptTcpClientAsync(); try { task.Wait(_cts.Token); } catch (OperationCanceledException e) { if (e.CancellationToken == _cts.Token) { break; } } var client = task.Result; var lightSwitchClient = new LightSwitchClient(client); _lightSwitchClients.Add(lightSwitchClient); lightSwitchClient.OnConnectionEstabilished += lightSwitchClient_ConnectionEstabilished; lightSwitchClient.OnConnectionClosed += lightSwitchClient_ConnectionClosed; lightSwitchClient.OnDataReceived += lightSwitchClient_DataReceived; lightSwitchClient.OnDataSent += lightSwitchClient_DataSent; lightSwitchClient.OnCmdAList += lightSwitchClient_onCmdAList; lightSwitchClient.OnCmdDevice += lightSwitchClient_onCmdDevice; lightSwitchClient.OnCmdIphone += lightSwitchClient_onCmdIphone; lightSwitchClient.OnCmdList += lightSwitchClient_onCmdList; lightSwitchClient.OnCmdPassword += lightSwitchClient_onCmdPassword; lightSwitchClient.OnCmdScene += lightSwitchClient_onCmdScene; lightSwitchClient.OnCmdServer += lightSwitchClient_onCmdServer; lightSwitchClient.OnCmdSList += lightSwitchClient_onCmdSList; lightSwitchClient.OnCmdTerminate += lightSwitchClient_onCmdTerminate; lightSwitchClient.OnCmdThermMode += lightSwitchClient_onCmdThermMode; lightSwitchClient.OnCmdThermTemp += lightSwitchClient_onCmdThermTemp; lightSwitchClient.OnCmdVersion += lightSwitchClient_onCmdVersion; lightSwitchClient.OnCmdZList += lightSwitchClient_onCmdZList; lightSwitchClient.OnCmdZone += lightSwitchClient_onCmdZone; lightSwitchClient.StartMonitoring(); } listener.Stop(); NotifyEntityChangeContext.ChangeNotifications <DeviceValue> .OnEntityUpdated -= SpeechPlugin_OnEntityUpdated; await Log.ReportInfoAsync("LightSwitch server stopped", CancellationToken); }
private async Task SendLightSwitchCommand(LightSwitchClient lightSwitchClient, LightSwitchCommand lightSwitchCommand) { var sendCommandResult = await lightSwitchClient.SendCommandAsync(lightSwitchCommand); if (sendCommandResult.HasError) { await Log.ReportErrorAsync(sendCommandResult.Message, CancellationToken); } }
private async Task SendSceneListAsync(LightSwitchClient client) { using (var context = new ZvsContext(EntityContextConnection)) { var settingUid = SceneSettingUids.ShowInLightswitch.ToString(); var scenes = await context.Scenes .Where(o => (o.SettingValues.All(p => p.SceneSetting.UniqueIdentifier != settingUid)) || //Show all objects where no explicit setting has been create yet and the defaultSetting is to show o.SettingValues.Any(p => p.SceneSetting.UniqueIdentifier == settingUid && p.Value.Equals("true"))) //Show all objects where an explicit setting has been create and set to show .OrderBy(o => o.SortOrder) .ToListAsync(); foreach (var scene in scenes) { await SendLightSwitchCommand(client, LightSwitchProtocol.CreateSceneCmd(scene.Name, scene.Id.ToString())); } } }
private async Task SendDeviceListAsync(LightSwitchClient client) { using (var context = new ZvsContext(EntityContextConnection)) { //Get Devices var settingUid = DeviceSettingUids.ShowInLightswitch.ToString(); var devices = await context.Devices .Where(o => (o.DeviceSettingValues.All(p => p.DeviceSetting.UniqueIdentifier != settingUid)) || //Show all objects where no explicit setting has been create yet and the defaultSetting is to show o.DeviceSettingValues.Any(p => p.DeviceSetting.UniqueIdentifier == settingUid && p.Value.Equals("true"))) //Show all objects where an explicit setting has been create and set to show .Include(o => o.Type) .OrderBy(o => o.Location) .ThenBy(o => o.Name) .Where(o => o.Type.UniqueIdentifier != "Controller") .ToListAsync(); foreach (var device in devices) { if (!_zvsTypeToLsType.ContainsKey(device.Type.UniqueIdentifier)) { continue; } var level = ((int)device.CurrentLevelInt).ToString(); var type = _zvsTypeToLsType[device.Type.UniqueIdentifier]; if (device.Type.UniqueIdentifier == "switch") { level = (device.CurrentLevelInt > 0 ? "255" : "0"); } var deviceName = $"{(string.IsNullOrWhiteSpace(device.Location) ? "" : device.Location + " ")}{device.Name}"; await SendLightSwitchCommand(client, LightSwitchProtocol.CreateDeviceCmd(deviceName, device.Id.ToString(), level, type)); } } }