Example #1
0
 /// <summary>
 /// Send command to a client
 /// </summary>
 public async Task <Result> SendCommandAsync(LightSwitchCommand command)
 {
     if (command == null)
     {
         throw new ArgumentNullException(nameof(command));
     }
     if (command.RawCommand.Length > 0 && TcpClient.Connected)
     {
         try
         {
             var byteArray    = command.ToBytes();
             var clientStream = TcpClient.GetStream();
             await clientStream.WriteAsync(byteArray, 0, byteArray.Length);
         }
         catch (SocketException se)
         {
             return(Result.ReportError($"LightSwitch Socket Exception: {se.Message}"));
         }
         catch (Exception e)
         {
             return(Result.ReportError(string.Format("LightSwitch Exception: " + e.Message)));
         }
         OnDataSent(this, new LightSwitchDataEventArgs(this, command.RawCommand));
     }
     return(Result.ReportSuccess());
 }
 /// <summary>
 /// Sends a message to ALL connected clients.
 /// </summary>
 /// <param name="command">the message to send</param>
 public async Task BroadcastCommandAsync(LightSwitchCommand command)
 {
     foreach (var client in GetSafeClients())
         await SendLightSwitchCommand(client, command);
 }
 private async Task SendLightSwitchCommand(LightSwitchClient lightSwitchClient, LightSwitchCommand lightSwitchCommand)
 {
     var sendCommandResult = await lightSwitchClient.SendCommandAsync(lightSwitchCommand);
     if (sendCommandResult.HasError)
         await Log.ReportErrorAsync(sendCommandResult.Message, CancellationToken);
 }
 /// <summary>
 /// Send command to a client
 /// </summary>
 public async Task<Result> SendCommandAsync(LightSwitchCommand command)
 {
     if (command == null) throw new ArgumentNullException(nameof(command));
     if (command.RawCommand.Length > 0 && TcpClient.Connected)
     {
         try
         {
             var byteArray = command.ToBytes();
             var clientStream = TcpClient.GetStream();
             await clientStream.WriteAsync(byteArray, 0, byteArray.Length);
         }
         catch (SocketException se)
         {
             return Result.ReportError($"LightSwitch Socket Exception: {se.Message}");
         }
         catch (Exception e)
         {
             return Result.ReportError(string.Format("LightSwitch Exception: " + e.Message));
         }
         OnDataSent(this, new LightSwitchDataEventArgs(this, command.RawCommand));
     }
     return Result.ReportSuccess();
 }