public IActionResult Create(SocketCreateViewModel model) { bool isDeviceIdExist = _socketRepository.Sockets.Any(s => s.DeviceId == model.DeviceId); if (isDeviceIdExist) { ModelState.AddModelError("", "This device id is already asigned!"); return(View(model)); } result = new Dictionary <string, bool?> { { model.DeviceId, null } }; _mqttAppClient.SubscribeToMany(new string[] { "sockets/" + model.DeviceId }); _mqttAppClient.Client.MqttMsgPublishReceived += AckReceived; _mqttAppClient.Publish(model.DeviceId, "Check"); System.Threading.Thread.Sleep(1000); if (result[model.DeviceId] == null) { ModelState.AddModelError("", "Failed connection attempt, make shure that socket is connected!"); return(View(model)); } Socket socket = _mapper.Map <Socket>(model); _socketRepository.CreateSocket(socket); _socketRepository.Savechanges(); return(RedirectToAction("Index", "Socket")); }
public async Task <IActionResult> Create(int id) { Room room = _roomRepository.GetRoomById(id); if (room == null) { return(new NotFoundResult()); } Socket socket = new Socket { Room = room, RoomId = id }; AuthorizationResult authorizationResult = await _authorizationService .AuthorizeAsync(User, socket, Operations.Create); if (!authorizationResult.Succeeded) { return(new ForbidResult()); } SocketCreateViewModel socketViewModel = _mapper.Map <SocketCreateViewModel>(socket); return(View(socketViewModel)); }