Exemplo n.º 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"));
        }
Exemplo n.º 2
0
        public IActionResult Create(TimeTaskCreateViewModel model)
        {
            if (model.TimeStamp.CompareTo(DateTime.Now.AddMinutes(1)) <= 0)
            {
                ModelState.AddModelError("", "Date must be minimum 1 minute in the future");
                return(View(model));
            }

            TimeTask timeTask = _mapper.Map <TimeTask>(model);

            _timeTaskRepository.CreateTimeTask(timeTask);
            _socketRepository.Savechanges();

            string backgroundJobId = BackgroundJob.Schedule(() => ExecuteTimeTask(timeTask), timeTask.TimeStamp);

            timeTask.BackgroundJobId = backgroundJobId;

            _timeTaskRepository.EditTimeTask(timeTask);
            _socketRepository.Savechanges();

            return(RedirectToRoute("TimeTask", new { id = model.SocketId }));
        }