Example #1
0
        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"));
        }