public SmartObjectExecutionResult Execute(string ipAddress, int port, ESmartCommands command) { SmartObjectExecutionResult result = new SmartObjectExecutionResult(); try { TcpClient client = new TcpClient(ipAddress, port); byte[] data = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(((int)command).ToString())); NetworkStream stream = client.GetStream(); stream.Write(data, 0, data.Length); data = new Byte[256]; string responseData = string.Empty; int bytes = stream.Read(data, 0, data.Length); responseData = Encoding.ASCII.GetString(data, 0, bytes); stream.Close(); client.Close(); result = new SmartObjectExecutionResult(responseData); } catch (Exception e) { AddNotification("Error", e.Message); } return(result); }
public ExecuteSmartObjectCommandResult Handle(ExecuteSmartObjectCommand command) { ExecuteSmartObjectCommandResult result = new ExecuteSmartObjectCommandResult(); ObjectId smartObjectId = new ObjectId(); if (string.IsNullOrEmpty(command.SmartObjectId) || !ObjectId.TryParse(command.SmartObjectId, out smartObjectId)) { AddNotification(nameof(command.SmartObjectId), ENotifications.InvalidId); } if (Valid) { SmartObject smartObject = _smartObjectRepository.Get(smartObjectId); if (smartObject == null && _smartObjectRepository.Valid) { AddNotification(nameof(smartObject), ENotifications.DoesNotExist); } if (Valid) { SmartObjectExecutionResult executionResult = _smartObjectCommunicationService.Execute(smartObject.IpAddress, smartObject.Port, command.Action); if (executionResult.Valid) { result = new ExecuteSmartObjectCommandResult(HttpStatusCode.OK, Notifications, executionResult.Result); } } else { result = new ExecuteSmartObjectCommandResult(HttpStatusCode.BadRequest, Notifications); } } else { result = new ExecuteSmartObjectCommandResult(HttpStatusCode.BadRequest, Notifications); } return(result); }