public ActionResult Send(MessageViewModel model)
        {
            // get the ids of the items selected:
            List<long> selectedIds = model.Devices.getSelectedIds();

            // Use the ids to retrieve the records for the selected people
            // from the database:
            var selectedDevices = from x in db.Devices
                                  where selectedIds.Contains(x.ID)
                                  select x;

            string message = null;
            switch (model.Devices.SelectedFunction)
            {
                case (int)MessageContants.RebootDevice:
                    message = "rb";
                    break;
                case (int)MessageContants.KillFirefox:
                    message = "kf";
                    break;
                case (int)MessageContants.ActivateQuickSupport:
                    message = "qs";
                    break;
                case (int)MessageContants.OpenURL:
                    message = "ou";
                    break;
                case (int)MessageContants.UploadScreenshot:
                    message = "uss";
                    break;
                case (int)MessageContants.ReporTaskerVersion:
                    message = "rtv";
                    break;
                case (int)MessageContants.UpdateTasker:
                    message = "ut";
                    break;
                case (int)MessageContants.UpdateStandardWallpaper:
                    message = "usw";
                    break;
                case (int)MessageContants.DecodeImage:
                    message = "di";
                    break;
                case (int)MessageContants.UploadTaskerDataFile:
                    message = "utdf";
                    break;
                case (int)MessageContants.RestartNetwork:
                    message = "rn";
                    break;
                case (int)MessageContants.GetDefaultURL:
                    message = "gdu";
                    break;
                case (int)MessageContants.GetOpenDefaultURL:
                    message = "godu";
                    break;
                case (int)MessageContants.OpenDefaultURL:
                    message = "odu";
                    break;
                case (int)MessageContants.ShowDeviceID:
                    message = "sdid";
                    break;
                case (int)MessageContants.GetDefaultChannel:
                    message = "gdc";
                    break;
                case (int)MessageContants.GetOfflineChannel:
                    message = "goc";
                    break;
                case (int)MessageContants.GetOfflinePack:
                    message = "gop";
                    break;
                case (int)MessageContants.OpenOffline:
                    message = "of";
                    break;
                case (int)MessageContants.RestoreTasker:
                    message = "rt";
                    break;
                case (int)MessageContants.ShowOnline:
                    message = "son";
                    break;
                case (int)MessageContants.UpdateOfflineChannel:
                    message = "uoc";
                    break;
                case (int)MessageContants.UpdateOfflinePack:
                    message = "uop";
                    break;
                case (int)MessageContants.ReportOfflineVersion:
                    message = "rov";
                    break;
                case (int)MessageContants.ReportDefaults:
                    message = "rdef";
                    break;
                case (int)MessageContants.ClearCommandQ:
                    message = "ccq";
                    break;
                case (int)MessageContants.ClearMessageQ:
                    message = "cmq";
                    break;
                case (int)MessageContants.ReportCommandQ:
                    message = "rcq";
                    break;
                case (int)MessageContants.ReportCommandRun:
                    message = "rcr";
                    break;
                case (int)MessageContants.ReportMessageQ:
                    message = "rmq";
                    break;
                case (int)MessageContants.ClearOfflineFiles:
                    message = "cof";
                    break;
                //case (int)MessageContants.:
                //    message = "";
                //    break;
            }

            SendMessage(message, selectedDevices.ToList(), model.Devices.RequestIndex, model.Devices.RequestIndexRange);

            db.SaveChanges();

            return RedirectToAction("Index");
        }
        public ActionResult Index(int? page)
        {
            ttTVMS.Models.MessageViewModel viewModel = new MessageViewModel();
            int pageSize = 20;
            int pageNumber = (page ?? 1);

            var messages = db.Messages.OrderByDescending(r => r.ID);

            List<SelectListItem> selectItems = new List<SelectListItem>();
            Array msgConstants = Enum.GetValues(typeof(MessageContants));
            Array.Sort(msgConstants);
            foreach (int msg in msgConstants)
            {
                selectItems.Add(new SelectListItem() { Text = Enum.GetName(typeof(MessageContants), msg), Value = msg.ToString() });
            }

            ViewBag.Functions = new SelectList(selectItems.ToArray(), "Value", "Text");

            var model = new DeviceSelectionViewModel();
            var items =
                from d in db.Devices
                    select d;
                //join r in db.Requests on d.ID equals r.DeviceID into dr
                //join l in db.DeviceLogs on d.ID equals l.DeviceID into dl
                //select new
                //{
                //    Device = d,
                //    LastRequest = dr.OrderByDescending(r => r.ID).Take(1).FirstOrDefault(),
                //    LastLog = dl.OrderByDescending(r => r.ID).Take(1).FirstOrDefault()
                //};

            foreach (var item in items) // (var device in db.Devices.Include(d => d.Requests.Where(dw => DateTime.Now.Subtract(dw.RequestDate).TotalDays ).Include(d => d.DeviceLogs))
            {
                var editorViewModel = new SelectDeviceEditorViewModel()
                {
                    ID = item.ID, // item.Device.ID,
                    AndriodVer = item.AndriodVer, // .Device.AndriodVer,
                    CreationDate = item.CreationDate, // .Device.CreationDate,
                    CustomerID = item.CustomerID, // .Device.CustomerID,
                    DeviceCode = item.DeviceCode, // .Device.DeviceCode,
                    DeviceType = item.DeviceType, // .Device.DeviceType,
                    ModelNo = item.ModelNo, // .Device.ModelNo,
                    LastRequestDate = item.LastRequestTime, // item.LastRequest == null ? (DateTime?)null : item.LastRequest.RequestDate,
                    LastRequestID = item.LastRequestId, // item.LastRequest == null ? (long?)null : item.LastRequest.ID,
                    LastLogDate = item.LastLogTime, // item.LastLog == null ? (DateTime?)null : item.LastLog.CreationDate,
                    LastLogID = item.LastLogId, // item.LastLog == null ? (long?)null : item.LastLog.ID,

                    Selected = item.DeviceType != 1
                };
                model.Devices.Add(editorViewModel);
            }
            viewModel.Devices = model;
            viewModel.Messages = messages.ToPagedList(pageNumber, pageSize);

            return View(viewModel);
        }