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); } }
public void DeleteOutstandingSlaveCommPackages() { // Get packages outstanding IEnumerable <CommunicationPackage> outstandingCPs = this.CommunicationPackages.Where(x => x.Response == null && x.Status == null && x.CommunicationType != (int)CommunicationPackage.UpdateType.Result); foreach (CommunicationPackage cp in outstandingCPs) { CommunicationPackage oCp = CommunicationPackage.Populate(cp.CommunicationId); oCp.Status = "CANCELLED"; oCp.Save(); } }
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); } }
public static CommunicationPackage CreateCommunication(int deviceId, UpdateType commType, int?workOrderId) { CommunicationPackage comm = new CommunicationPackage(); comm.TargetDeviceId = deviceId; comm.CommunicationType = (int)commType; comm.SubmitDate = DateTime.Now.AddSeconds(-35); // This is a hack, should be a nullable type comm.WorkOrderId = workOrderId; comm.context = new marcdissertation_dbEntities(); comm = comm.context.CommunicationPackages.Add(comm); errors = comm.context.GetValidationErrors(); try { comm.context.SaveChanges(); } catch { throw App.ExceptionFormatter(errors); } return(comm); }
private void ProcessCommunication(CommunicationPackage cp) { try { // CommunicationPackage cp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommunicationPackage>(cpSerialized); UserDevice ud = UserDevice.Populate(cp.TargetDeviceId); ActiveDevice ad; // Have to try and catch the exception, since might not exist in the current context try { ad = ActiveDevice.Populate(ud.DeviceId); } catch (Exception) { ad = null; } CommunicationPackage fetchRequest = CommunicationPackage.CreateFetchRequest(cp.TargetDeviceId); // If last fetch from device was less than 3 mins ago, don't send GCM if (ad != null && ad.LastFetch < DateTime.Now.AddMinutes(-1)) { // Send GCM if (fetchRequest.SendAttempts < 3 && fetchRequest.SubmitDate < DateTime.Now.AddSeconds(-30)) { Pusher.SendNotification(ud.GCMCode, fetchRequest.Serialize()); fetchRequest.SubmitDate = DateTime.Now; fetchRequest.SendAttempts++; fetchRequest.Save(); } else if (fetchRequest.SendAttempts >= 3 && fetchRequest.SubmitDate < DateTime.Now.AddSeconds(-40)) { // Device might not be active, delete it from the active device list. try { ad.Delete(); } catch { } // If not results, re-arrange WO List<int?> workOrdersToBeRearranged = ud.CommunicationPackages.Where(x => x.Status == null && x.Response == null && x.CommunicationType != (int)CommunicationPackage.UpdateType.Result).Select(x => x.WorkOrderId).ToList(); // Delete current Comm Packages to the device if they are requests for work. ud.DeleteOutstandingSlaveCommPackages(); // Rearange work orders foreach (int? woId in workOrdersToBeRearranged) { if (woId.HasValue) NewWorkOrders.Send(new BrokeredMessage(woId.Value)); } } } // handle if less than 10 ... etc.. } catch (Exception e) { Debug.WriteLine(e.Message); } }