async void lightSwitchClient_onCmdScene(object sender, OnSceneEventArgs args) { if (!args.LightSwitchClient.IsAuthenticated) { args.LightSwitchClient.Disconnect(); } int sceneId = int.TryParse(args.SceneId, out sceneId) ? sceneId : 0; using (var context = new ZvsContext(EntityContextConnection)) { var bcmd = await context.BuiltinCommands.FirstOrDefaultAsync(c => c.UniqueIdentifier == "RUN_SCENE", CancellationToken); if (bcmd == null) { const string error = "Cannot locate zvs command"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } var r = await RunCommandAsync(bcmd.Id, sceneId.ToString(), string.Empty, CancellationToken); if (r.HasError) { await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(r.Message)); } else { await BroadcastCommandAsync(LightSwitchProtocol.CreateMsgCmd(r.Message)); } } }
async void lightSwitchClient_onCmdZList(object sender, LightSwitchClientEventArgs args) { await Log.ReportInfoFormatAsync(CancellationToken, "Received [{0}] User requested zone list", args.LightSwitchClient.RemoteEndPoint); await SendZoneListAsync(args.LightSwitchClient); await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateEndListCmd()); }
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())); } }
async void lightSwitchClient_onCmdServer(object sender, LightSwitchClientEventArgs args) { if (!args.LightSwitchClient.IsAuthenticated) { args.LightSwitchClient.Disconnect(); } await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateVersionCmd(Utils.ApplicationNameAndVersion)); }
public async Task StopLightSwitchServer() { await BroadcastCommandAsync(LightSwitchProtocol.CreateMsgCmd("Server shutting down...")); foreach (var client in GetSafeClients()) { client.Disconnect(); } _cts.Cancel(); }
async void lightSwitchClient_onCmdThermTemp(object sender, OnThermTempEventArgs args) { if (!args.LightSwitchClient.IsAuthenticated) { args.LightSwitchClient.Disconnect(); } int deviceId = int.TryParse(args.DeviceId, out deviceId) ? deviceId : 0; using (var context = new ZvsContext(EntityContextConnection)) { var d = await context.Devices .Include(o => o.Type.Adapter) .Include(o => o.Commands) .FirstOrDefaultAsync(o => o.Id == deviceId, CancellationToken); if (d == null) { const string error = "Cannot locate thermostat"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } var commandName = args.Mode == "2" ? "Heating 1" : "Cooling 1"; var dcmd = d.Commands.FirstOrDefault(c => c.CustomData1 == commandName); if (dcmd == null) { const string error = "Cannot locate zvs command"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } var cmdMsg = $"[{args.LightSwitchClient.RemoteEndPoint}] Executed command '{dcmd.Name}' on '{d.Name}'."; await Log.ReportInfoAsync(cmdMsg, CancellationToken); var commandResult = await RunCommandAsync(dcmd.Id, args.Temp, string.Empty, CancellationToken); if (commandResult.HasError) { await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(commandResult.Message)); } else { await BroadcastCommandAsync(LightSwitchProtocol.CreateMsgCmd(cmdMsg)); } } }
async void lightSwitchClient_onCmdList(object sender, LightSwitchClientEventArgs args) { if (!args.LightSwitchClient.IsAuthenticated) { args.LightSwitchClient.Disconnect(); } await Log.ReportInfoFormatAsync(CancellationToken, "Received [{0}] User requested device list", args.LightSwitchClient.RemoteEndPoint); await SendDeviceListAsync(args.LightSwitchClient); await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateEndListCmd()); }
async void lightSwitchClient_ConnectionEstabilished(object sender, LightSwitchClientEventArgs args) { if (_lightSwitchClients.Count > MaxConnectionsSettings) { await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateMsgCmd("Max clients reached!")); args.LightSwitchClient.Disconnect(); } await Log.ReportInfoFormatAsync(CancellationToken, "Client {0} connected", args.LightSwitchClient.RemoteEndPoint); await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateInfoCmdFormat("LightSwitch zVirtualScenes Plug-in (Active Connections {0}){1}", _lightSwitchClients.Count, Environment.NewLine)); }
async void lightSwitchClient_onCmdPassword(object sender, OnPasswordEventArgs args) { var lightSwitchClient = args.LightSwitchClient; var hashedPassword = EncodePassword($"{lightSwitchClient.Nonce}:{PasswordSetting}"); if (args.Password.StartsWith(hashedPassword)) { lightSwitchClient.IsAuthenticated = true; await SendLightSwitchCommand(lightSwitchClient, LightSwitchProtocol.CreateVersionCmd(Utils.ApplicationNameAndVersion)); await Log.ReportInfoFormatAsync(CancellationToken, "Received [{0}] User Authenticated", lightSwitchClient.RemoteEndPoint); } else { lightSwitchClient.IsAuthenticated = false; lightSwitchClient.Disconnect(); } }
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())); } } }
async void SpeechPlugin_OnEntityUpdated(object sender, NotifyEntityChangeContext.ChangeNotifications <DeviceValue> .EntityUpdatedArgs e) { using (var context = new ZvsContext(EntityContextConnection)) { var dv = await context.DeviceValues .Include(o => o.Device) .Include(o => o.Device.Type) .FirstOrDefaultAsync(v => v.Id == e.NewEntity.Id, CancellationToken); var newValue = e.NewEntity.Value; if (dv == null) { return; } if (dv.Name != "Basic") { return; } if (!_zvsTypeToLsType.ContainsKey(dv.Device.Type.UniqueIdentifier)) { return; } var level = newValue; var type = _zvsTypeToLsType[dv.Device.Type.UniqueIdentifier]; int l; int.TryParse(newValue, out l); if (dv.Device.Type.UniqueIdentifier == "SWITCH") { level = (l > 0 ? "255" : "0"); } await BroadcastCommandAsync(LightSwitchProtocol.CreateUpdateCmd(dv.Device.Name, dv.Device.Id.ToString(), level, type)); await BroadcastCommandAsync(LightSwitchProtocol.CreateEndListCmd()); await BroadcastCommandAsync(LightSwitchProtocol.CreateMsgCmdFormat("'{0}' {1} changed to {2}", dv.Device.Name, dv.Name, newValue)); } }
async void lightSwitchClient_onCmdZone(object sender, OnZoneEventArgs args) { int groupId = int.TryParse(args.ZoneId, out groupId) ? groupId : 0; var cmdUniqId = (args.Level.Equals("255") ? "GROUP_ON" : "GROUP_OFF"); using (var context = new ZvsContext(EntityContextConnection)) { var g = await context.Groups.FirstOrDefaultAsync(o => o.Id == groupId); if (g == null) { return; } var zvsCmd = await context.BuiltinCommands.FirstOrDefaultAsync(c => c.UniqueIdentifier == cmdUniqId, CancellationToken); if (zvsCmd == null) { const string error = "Cannot locate zvs command"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } var result = $"[{args.LightSwitchClient.RemoteEndPoint}] Ran {zvsCmd.Name} on group '{g.Name}'"; await Log.ReportInfoAsync(result, CancellationToken); var r = await RunCommandAsync(zvsCmd.Id, g.Id.ToString(), string.Empty, CancellationToken); if (r.HasError) { await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(r.Message)); } else { await BroadcastCommandAsync(LightSwitchProtocol.CreateMsgCmd(result)); } } }
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)); } } }
async void lightSwitchClient_onCmdThermMode(object sender, OnThermModeEventArgs args) { if (!args.LightSwitchClient.IsAuthenticated) { args.LightSwitchClient.Disconnect(); } int deviceId = int.TryParse(args.DeviceId, out deviceId) ? deviceId : 0; int mode = int.TryParse(args.Mode, out mode) ? mode : 0; string arg1 = null; string arg2 = null; Command command = null; var cmdMsg = string.Empty; //PLUGINNAME-0 -->CmdName,Arg using (var context = new ZvsContext(EntityContextConnection)) { var d = await context.Devices .Include(o => o.Type.Adapter) .Include(o => o.Type.Commands) .Include(o => o.Commands) .FirstOrDefaultAsync(o => o.Id == deviceId, CancellationToken); if (d == null) { const string error = "Cannot locate thermostat"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } if (mode < 6) { var commandName = string.Empty; switch (args.Mode) { case "0": commandName = "Mode"; arg1 = "Off"; break; case "1": commandName = "Mode"; arg1 = "Auto"; break; case "2": commandName = "Mode"; arg1 = "Heat"; break; case "3": commandName = "Mode"; arg1 = "Cool"; break; case "4": commandName = "Fan Mode"; arg1 = "On Low"; break; case "5": commandName = "Fan Mode"; arg1 = "Auto Low"; break; } var dcmd = d.Commands.FirstOrDefault(c => c.CustomData1 == commandName); if (dcmd == null) { const string error = "Cannot locate zvs command"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } command = dcmd; cmdMsg = $"[{args.LightSwitchClient.RemoteEndPoint}] Executed command '{dcmd.Name}' on '{d.Name}'."; } else { switch (mode) { case 6: { var dcmd = d.Type.Commands.FirstOrDefault(c => c.UniqueIdentifier == "SETENERGYMODE"); if (dcmd == null) { const string error = "Cannot locate zvs command"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } command = dcmd; arg1 = string.Empty; arg2 = d.Id.ToString(); cmdMsg = $"[{args.LightSwitchClient.RemoteEndPoint}] Executed command '{dcmd.Name}' on '{d.Name}'."; } break; case 7: { var dcmd = d.Type.Commands.FirstOrDefault(c => c.UniqueIdentifier == "SETCONFORTMODE"); if (dcmd == null) { const string error = "Cannot locate zvs command"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } command = dcmd; arg1 = string.Empty; arg2 = d.Id.ToString(); cmdMsg = $"[{args.LightSwitchClient.RemoteEndPoint}] Executed command '{dcmd.Name}' on '{d.Name}'."; } break; } } } if (command == null) { const string error = "Cannot locate zvs command"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } await Log.ReportInfoAsync(cmdMsg, CancellationToken); var commandResult = await RunCommandAsync(command.Id, arg1, arg2, CancellationToken); if (commandResult.HasError) { await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(commandResult.Message)); } else { await BroadcastCommandAsync(LightSwitchProtocol.CreateMsgCmd(cmdMsg)); } }
async void lightSwitchClient_onCmdDevice(object sender, OnDeviceEventArgs args) { if (!args.LightSwitchClient.IsAuthenticated) { args.LightSwitchClient.Disconnect(); } int deviceId = int.TryParse(args.DeviceId, out deviceId) ? deviceId : 0; byte level = byte.TryParse(args.Level, out level) ? level : (byte)0; using (var context = new ZvsContext(EntityContextConnection)) { var d = await context.Devices .Include(o => o.Type) .Include(o => o.Type.Commands) .FirstOrDefaultAsync(o => o.Id == deviceId); if (d == null) { const string error = "Cannot locate device"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } string arg1 = null; string arg2 = null; Command command = null; var cmdMsg = string.Empty; #region Find Command switch (d.Type.UniqueIdentifier.ToLower()) { case "switch": { var cmdUniqueId = (level == 0 ? "TURNOFF" : "TURNON"); var dtcmd = d.Type.Commands.FirstOrDefault(c => c.UniqueIdentifier == cmdUniqueId); if (dtcmd == null) { const string error = "Cannot locate zvs command"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } cmdMsg = $"[{args.LightSwitchClient.RemoteEndPoint}] Executed command '{dtcmd.Name}' on '{d.Name}'."; command = dtcmd; arg1 = string.Empty; arg2 = d.Id.ToString(); break; } case "dimmer": { var l = (level == 255 ? "99" : level.ToString()); var dcmd = d.Commands.FirstOrDefault(c => c.CustomData1 == "Basic"); if (dcmd == null) { const string error = "Cannot locate zvs command"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } cmdMsg = $"[{args.LightSwitchClient.RemoteEndPoint}] Executed command '{dcmd.Name}' on '{d.Name}'."; command = dcmd; arg1 = l; arg2 = d.Id.ToString(); break; } } #endregion if (command == null) { const string error = "Cannot locate zvs command"; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(error)); await Log.ReportErrorAsync(error, CancellationToken); return; } await Log.ReportInfoAsync(cmdMsg, CancellationToken); var commandResult = await RunCommandAsync(command.Id, arg1, arg2, CancellationToken); if (commandResult.HasError) { await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateErrorMsgCmd(commandResult.Message)); } else { await BroadcastCommandAsync(LightSwitchProtocol.CreateMsgCmd(cmdMsg)); } } }
async void lightSwitchClient_onCmdIphone(object sender, LightSwitchClientEventArgs args) { args.LightSwitchClient.IsAuthenticated = false; await SendLightSwitchCommand(args.LightSwitchClient, LightSwitchProtocol.CreateCookieCmd(args.LightSwitchClient.Nonce.ToString())); }