public static AuthenticationToken AddAuthenticationToken(int deviceId, string username)
        {
            marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

            if (ctxt.AuthenticationTokens.Count(x => x.UserDevice.User.Username == username  && x.UserDevice.DeviceId == deviceId) > 0) {
               throw new Exception("Device already has authentication token.");
               //     return AuthenticationToken.Populate(deviceId);
            }

            AuthenticationToken at = new AuthenticationToken();
            at.DeviceId = deviceId;
            at.Token = GenerateAuthTokenString(username, deviceId);
            at.CreationDate = DateTime.Now;
            at.Username = username;
            at.context = ctxt;

            at = at.context.AuthenticationTokens.Add(at);

            IEnumerable<System.Data.Entity.Validation.DbEntityValidationResult> errors = at.context.GetValidationErrors();

            if (errors.Count() > 0) {
                throw App.ExceptionFormatter(errors);
            }

            at.context.SaveChanges();
            return at;
        }
Пример #2
0
        public static CommunicationPackage CreateFetchRequest(int deviceId)
        {
            // Check there isn't one that already exists and isn't acked yet.
            marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

            if (ctxt.CommunicationPackages.Where(x => x.TargetDeviceId == deviceId && x.CommunicationType == (int)UpdateType.FetchRequest && x.Status == null && x.Response == null).Count() == 0)
            {
                // Create new
                CommunicationPackage comm = new CommunicationPackage();
                comm.TargetDeviceId    = deviceId;
                comm.CommunicationType = (int)UpdateType.FetchRequest;
                comm.SubmitDate        = DateTime.Now;
                comm.WorkOrderId       = null;
                comm.context           = ctxt;

                comm   = comm.context.CommunicationPackages.Add(comm);
                errors = comm.context.GetValidationErrors();

                try {
                    comm.context.SaveChanges();
                } catch {
                    throw App.ExceptionFormatter(errors);
                }
                return(comm);
            }
            else
            {
                CommunicationPackage comm = ctxt.CommunicationPackages.Where(x => x.TargetDeviceId == deviceId && x.CommunicationType == (int)UpdateType.FetchRequest && x.Status == null && x.Response == null).First();
                comm.context = ctxt;
                return(comm);
            }
        }
Пример #3
0
        public static UserDevice AddUserDevice(string username, string deviceType, int deviceMemoryResource, int deviceProcRating, String gcmCode)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
                if (ctxt.UserDevices.Count(x => x.GCMCode == gcmCode) > 0) {
                    throw new Exception("Device can only be assigned to single user. Assigned to: " + ctxt.UserDevices.First(x => x.GCMCode == gcmCode).User.Username);
                }

                UserDevice ud = new UserDevice();
                ud.Username = username;
                ud.DeviceType = deviceType;
                ud.DeviceMemoryResource = deviceMemoryResource;
                ud.DeviceProcRating = deviceProcRating;
                ud.GCMCode = gcmCode;
                ud.context = ctxt;
                ud = ud.context.UserDevices.Add(ud);

                IEnumerable<System.Data.Entity.Validation.DbEntityValidationResult> errors = ud.context.GetValidationErrors();

                if (errors.Count() > 0) {
                    throw App.ExceptionFormatter(errors);
                }

                ud.context.SaveChanges();
                return ud;
            } catch (Exception e) {
                if (new marcdissertation_dbEntities().Users.Select(x => x.Username.Equals(username)).Count() == 0) {
                    throw new Exception("Username must exist");
                }

                throw e;
            }
        }
        public static AuthenticationToken AddAuthenticationToken(int deviceId, string username)
        {
            marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

            if (ctxt.AuthenticationTokens.Count(x => x.UserDevice.User.Username == username && x.UserDevice.DeviceId == deviceId) > 0)
            {
                throw new Exception("Device already has authentication token.");
                //     return AuthenticationToken.Populate(deviceId);
            }

            AuthenticationToken at = new AuthenticationToken();

            at.DeviceId     = deviceId;
            at.Token        = GenerateAuthTokenString(username, deviceId);
            at.CreationDate = DateTime.Now;
            at.Username     = username;
            at.context      = ctxt;

            at = at.context.AuthenticationTokens.Add(at);

            IEnumerable <System.Data.Entity.Validation.DbEntityValidationResult> errors = at.context.GetValidationErrors();

            if (errors.Count() > 0)
            {
                throw App.ExceptionFormatter(errors);
            }

            at.context.SaveChanges();
            return(at);
        }
Пример #5
0
        public static List <CommunicationPackage> GetTargetDeviceCommunications(int deviceId)
        {
            marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
            List <CommunicationPackage> cps  = (from x in ctxt.CommunicationPackages where x.TargetDeviceId == deviceId && x.Response == null && x.Status == null select x).ToList();


            return(cps);
        }
Пример #6
0
        public static List <CommunicationPackage> GetUnacknowledgedPackages(int timePeriodSecs)
        {
            DateTime dateToUse = DateTime.Now.AddSeconds(-timePeriodSecs);
            marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

            List <CommunicationPackage> unAcked = (from x in ctxt.CommunicationPackages
                                                   where x.DateAcknowledged == null && x.Status == null && x.SubmitDate <= dateToUse
                                                   select x).ToList();

            return(unAcked);
        }
Пример #7
0
        public static List<CommunicationPackage> GetUnacknowledgedPackages(int timePeriodSecs)
        {
            DateTime dateToUse = DateTime.Now.AddSeconds(-timePeriodSecs);
            marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

            List<CommunicationPackage> unAcked = (from x in ctxt.CommunicationPackages
                                                  where x.DateAcknowledged == null && x.Status == null && x.SubmitDate <= dateToUse
                                                  select x).ToList();

            return unAcked;
        }
Пример #8
0
 public static DeviceAppInstallation Populate(int applicaitonId, int deviceId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         DeviceAppInstallation dai = (from x in ctxt.DeviceAppInstallations
                 where x.DeviceId.Equals(deviceId) && x.ApplicationId.Equals(applicaitonId) select x).First();
         dai.context = ctxt;
         return dai;
     } catch {
         throw new Exception("Application device pair with ID given does not exist");
     }
 }
Пример #9
0
 public static DeviceAppInstallation Populate(int applicaitonId, int deviceId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         DeviceAppInstallation       dai  = (from x in ctxt.DeviceAppInstallations
                                             where x.DeviceId.Equals(deviceId) && x.ApplicationId.Equals(applicaitonId) select x).First();
         dai.context = ctxt;
         return(dai);
     } catch {
         throw new Exception("Application device pair with ID given does not exist");
     }
 }
Пример #10
0
 public static WorkApplication Populate(int ApplicationId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         WorkApplication             wa   = (from x in ctxt.WorkApplications
                                             where x.ApplicationId.Equals(ApplicationId)
                                             select x).First();
         wa.context = ctxt;
         return(wa);
     } catch {
         throw new Exception("Application with ID given does not exist");
     }
 }
Пример #11
0
 public static WorkApplication Populate(String applicationName)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         WorkApplication             wa   = (from x in ctxt.WorkApplications
                                             where x.ApplicationName == applicationName
                                             select x).First();
         wa.context = ctxt;
         return(wa);
     } catch {
         throw new Exception("Application with name given does not exist");
     }
 }
Пример #12
0
 public static AuthenticationToken Populate(int deviceId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         AuthenticationToken at = (from x in ctxt.AuthenticationTokens
                 where x.DeviceId == deviceId
                 select x).First();
         at.context = ctxt;
         return at;
     } catch {
         throw new Exception("Token does not exist");
     }
 }
Пример #13
0
 public static UserDevice Populate(String gcmId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         UserDevice ud = (from x in ctxt.UserDevices
                          where x.GCMCode.Equals(gcmId)
                          select x).First();
         ud.context = ctxt;
         return(ud);
     } catch {
         throw new Exception("Device does not exist");
     }
 }
Пример #14
0
 public static WorkApplication Populate(int ApplicationId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         WorkApplication wa = (from x in ctxt.WorkApplications
                 where x.ApplicationId.Equals(ApplicationId)
                 select x).First();
         wa.context = ctxt;
         return wa;
     } catch {
         throw new Exception("Application with ID given does not exist");
     }
 }
Пример #15
0
 public static WorkApplication Populate(String applicationName)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         WorkApplication wa =  (from x in ctxt.WorkApplications
                 where x.ApplicationName == applicationName
                 select x).First();
         wa.context = ctxt;
         return wa;
     } catch {
         throw new Exception("Application with name given does not exist");
     }
 }
Пример #16
0
 public static User Populate(int userId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         User usr = (from x in ctxt.Users
                     where x.UserId.Equals(userId)
                     select x).First();
         usr.context = ctxt;
         return(usr);
     } catch {
         throw new Exception("User does not exist");
     }
 }
Пример #17
0
 public static User Populate(int userId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         User usr =  (from x in ctxt.Users
                 where x.UserId.Equals(userId)
                 select x).First();
         usr.context = ctxt;
         return usr;
     } catch {
         throw new Exception("User does not exist");
     }
 }
Пример #18
0
 public static AuthenticationToken Populate(int deviceId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         AuthenticationToken         at   = (from x in ctxt.AuthenticationTokens
                                             where x.DeviceId == deviceId
                                             select x).First();
         at.context = ctxt;
         return(at);
     } catch {
         throw new Exception("Token does not exist");
     }
 }
        public static DeviceAccessRestriction Populate(int accessRestrictionId)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

                DeviceAccessRestriction dar = (from x in ctxt.DeviceAccessRestrictions
                                               where x.AccessRestrictionId == accessRestrictionId
                                               select x).First();
                dar.context = ctxt;
                return(dar);
            } catch (Exception ex) {
                throw ex;
            }
        }
Пример #20
0
        public static List <WorkOrder> GetWorkOrdersRequiringReassignment()
        {
            marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

            IEnumerable <WorkOrder> wos = (from x in ctxt.WorkOrders
                                           where x.WorkOrderStatus == "SLAVE_ACKNOWLEDGED"
                                           select x);

            // Get distinct app IDs
            IEnumerable <int> appIds = wos.Select(x => x.ApplicationId).Distinct();

            List <WorkOrder> offendingWorkOrders = new List <WorkOrder>();

            foreach (int appId in appIds)
            {
                // Get average then go thru the WOs in turn
                WorkApplication wa             = WorkApplication.Populate(appId);
                double          avgComputeTime = wa.GetAverageComputeTime();

                if (avgComputeTime == 0 || double.IsNaN(avgComputeTime))
                {
                    // New app, give 2 mins
                    avgComputeTime += 120;
                }
                else
                {
                    // Give some leeway
                    avgComputeTime += 40;
                }

                foreach (WorkOrder wo in wos)
                {
                    if (wo.ApplicationId == appId)
                    {
                        double waitTime = (DateTime.Now - wo.SlaveWorkerSubmit.Value).TotalSeconds;

                        if (waitTime > avgComputeTime)
                        {
                            offendingWorkOrders.Add(wo);
                        }
                    }
                }

                //List<WorkOrder> woss = wos.Where(x=> x.ApplicationId == appId && avgComputeTime <(DateTime.Now - x.SlaveWorkerSubmit.Value).Seconds).ToList();

                //offendingWorkOrders.AddRange(woss);
            }

            return(offendingWorkOrders);
        }
        public static DeviceAccessRestriction Populate(int accessRestrictionId)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

                DeviceAccessRestriction dar = (from x in ctxt.DeviceAccessRestrictions
                                where x.AccessRestrictionId == accessRestrictionId
                        select x).First();
                dar.context = ctxt;
                return dar;
            } catch (Exception ex) {
                throw ex;
            }
        }
Пример #22
0
 public static CommunicationPackage Populate(int communicationId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         // App.DbContext.Configuration.ProxyCreationEnabled = false;
         CommunicationPackage comm = (from x in ctxt.CommunicationPackages
                                      where x.CommunicationId == communicationId
                                      select x).First();
         // App.DbContext.Configuration.ProxyCreationEnabled = true;
         comm.context = ctxt;
         return(comm);
     } catch (Exception ex) {
         return(null);
     }
 }
Пример #23
0
        public static WorkOrder PopulateLocalDeviceId(int localDeviceId, int deviceId)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

                WorkOrder wo = (from x in ctxt.WorkOrders
                                where x.LocalDeviceId == localDeviceId && x.DeviceId == deviceId
                                select x).First();
                wo.context = ctxt;

                return(wo);
            } catch (Exception ex) {
                throw ex;
            }
        }
Пример #24
0
        public static UserDevice GetAvailableSlave(int appId, int deviceId)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

                //TODO: change this from commm package to work order
                IQueryable<UserDevice> uds = (from x in ctxt.ActiveDevices
                                 where (x.UserDevice.DeviceAppInstallations.Count(z => z.ApplicationId == appId) > 0) && x.UserDevice.WorkOrders1.Count(y => y.WorkOrderStatus != "RESULT_RECEIVED") < App.MAX_COMMS_SLAVE_QUEUED && x.DeviceId != deviceId orderby x.LastFetch ascending
                                 select x.UserDevice);

                return uds.First();
            } catch (Exception) {
                throw new Exception("No Device Available");
            }
        }
Пример #25
0
        public static UserDevice GetAvailableSlave(int appId, int deviceId)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

                //TODO: change this from commm package to work order
                IQueryable <UserDevice> uds = (from x in ctxt.ActiveDevices
                                               where (x.UserDevice.DeviceAppInstallations.Count(z => z.ApplicationId == appId) > 0) && x.UserDevice.WorkOrders1.Count(y => y.WorkOrderStatus != "RESULT_RECEIVED") < App.MAX_COMMS_SLAVE_QUEUED && x.DeviceId != deviceId orderby x.LastFetch ascending
                                               select x.UserDevice);

                return(uds.First());
            } catch (Exception) {
                throw new Exception("No Device Available");
            }
        }
Пример #26
0
        public static WorkOrder Populate(int workOrderId)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

                //  App.DbContext.Configuration.ProxyCreationEnabled = false;
                WorkOrder wo = (from x in ctxt.WorkOrders
                                where x.WorkOrderId == workOrderId
                                select x).First();
                wo.context = ctxt;
                //App.DbContext.Configuration.ProxyCreationEnabled = true;
                return(wo);
            } catch (Exception ex) {
                throw ex;
            }
        }
Пример #27
0
        public static List<WorkOrder> GetWorkOrdersRequiringReassignment()
        {
            marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

            IEnumerable<WorkOrder> wos = (from x in ctxt.WorkOrders
                                   where x.WorkOrderStatus == "SLAVE_ACKNOWLEDGED"
                                   select x);

            // Get distinct app IDs
            IEnumerable<int> appIds = wos.Select(x => x.ApplicationId).Distinct();

            List<WorkOrder> offendingWorkOrders = new List<WorkOrder>();

            foreach (int appId in appIds) {
                // Get average then go thru the WOs in turn
                WorkApplication wa = WorkApplication.Populate(appId);
                double avgComputeTime = wa.GetAverageComputeTime();

                if (avgComputeTime == 0 || double.IsNaN(avgComputeTime)) {
                    // New app, give 2 mins
                    avgComputeTime += 120;
                } else {
                    // Give some leeway
                    avgComputeTime += 40;
                }

                foreach (WorkOrder wo in wos) {
                    if (wo.ApplicationId == appId) {
                        double waitTime = (DateTime.Now - wo.SlaveWorkerSubmit.Value).TotalSeconds;

                        if (waitTime > avgComputeTime) {
                            offendingWorkOrders.Add(wo);
                        }

                    }
                }

                //List<WorkOrder> woss = wos.Where(x=> x.ApplicationId == appId && avgComputeTime <(DateTime.Now - x.SlaveWorkerSubmit.Value).Seconds).ToList();

                //offendingWorkOrders.AddRange(woss);

            }

            return offendingWorkOrders;
        }
Пример #28
0
        public static ActiveDevice Populate(int deviceId)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

              //  ad.context
                //ActiveDevice ad = new ActiveDevice();
                //ad.context = new marcdissertation_dbEntities();

                ActiveDevice ad = (from x in ctxt.ActiveDevices
                                   where x.DeviceId == deviceId
                                   select x).First();
                ad.context = ctxt;

                 return ad;
            } catch {
                throw new Exception("Device not active");
            }
        }
Пример #29
0
        public static ActiveDevice Populate(int deviceId)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

                //  ad.context
                //ActiveDevice ad = new ActiveDevice();
                //ad.context = new marcdissertation_dbEntities();


                ActiveDevice ad = (from x in ctxt.ActiveDevices
                                   where x.DeviceId == deviceId
                                   select x).First();
                ad.context = ctxt;

                return(ad);
            } catch {
                throw new Exception("Device not active");
            }
        }
Пример #30
0
        public static UserDevice AddUserDevice(string username, string deviceType, int deviceMemoryResource, int deviceProcRating, String gcmCode)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
                if (ctxt.UserDevices.Count(x => x.GCMCode == gcmCode) > 0)
                {
                    throw new Exception("Device can only be assigned to single user. Assigned to: " + ctxt.UserDevices.First(x => x.GCMCode == gcmCode).User.Username);
                }

                UserDevice ud = new UserDevice();
                ud.Username             = username;
                ud.DeviceType           = deviceType;
                ud.DeviceMemoryResource = deviceMemoryResource;
                ud.DeviceProcRating     = deviceProcRating;
                ud.GCMCode = gcmCode;
                ud.context = ctxt;
                ud         = ud.context.UserDevices.Add(ud);

                IEnumerable <System.Data.Entity.Validation.DbEntityValidationResult> errors = ud.context.GetValidationErrors();

                if (errors.Count() > 0)
                {
                    throw App.ExceptionFormatter(errors);
                }

                ud.context.SaveChanges();
                return(ud);
            } catch (Exception e) {
                if (new marcdissertation_dbEntities().Users.Select(x => x.Username.Equals(username)).Count() == 0)
                {
                    throw new Exception("Username must exist");
                }

                throw e;
            }
        }
Пример #31
0
        public static Boolean AuthenticateUser(string username, string password)
        {
            marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

            return(ctxt.Users.Count(x => x.Username.Equals(username) && x.Password.Equals(password)) == 1);
        }
Пример #32
0
        public static WorkOrder Populate(int workOrderId)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

              //  App.DbContext.Configuration.ProxyCreationEnabled = false;
                WorkOrder wo = (from x in ctxt.WorkOrders
                        where x.WorkOrderId == workOrderId
                        select x).First();
                wo.context = ctxt;
               //App.DbContext.Configuration.ProxyCreationEnabled = true;
                               return wo;
            } catch (Exception ex) {
                throw ex;
            }
        }
Пример #33
0
 public static UserDevice Populate(String gcmId)
 {
     try {
         marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
         UserDevice ud = (from x in ctxt.UserDevices
                 where x.GCMCode.Equals(gcmId)
                 select x).First();
         ud.context = ctxt;
         return ud;
     } catch {
         throw new Exception("Device does not exist");
     }
 }
Пример #34
0
        public static WorkOrder PopulateLocalDeviceId(int localDeviceId, int deviceId)
        {
            try {
                marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();

                WorkOrder wo = (from x in ctxt.WorkOrders
                                where x.LocalDeviceId == localDeviceId && x.DeviceId == deviceId
                                select x).First();
                wo.context = ctxt;

                return wo;
            } catch (Exception ex) {
                throw ex;
            }
        }
Пример #35
0
 public static Boolean AuthenticateUser(string username, string password)
 {
     marcdissertation_dbEntities ctxt = new marcdissertation_dbEntities();
     return ctxt.Users.Count(x => x.Username.Equals(username) && x.Password.Equals(password)) == 1;
 }