private void DoWOSweep() { // while (!IsStopped) { if (!WOSweepOccuring) { WOSweepOccuring = true; List <WorkOrder> WosWaitingLongerThanAverage = WorkOrder.GetWorkOrdersRequiringReassignment(); // Re-arrange WOs foreach (WorkOrder wo in WosWaitingLongerThanAverage) { // Remove device from active ids try { ActiveDevice.Populate(wo.SlaveWorkerId.Value).Delete(); } catch { } using (WorkOrder oWO = WorkOrder.Populate(wo.WorkOrderId)) { oWO.WorkOrderStatus = "BEING_REASSIGNED"; oWO.Save(); } // Rearrange WO NewWorkOrders.Send(new BrokeredMessage(wo.WorkOrderId)); } WOSweepOccuring = false; } }
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); } }
public void MarkDeviceInactive(string at, int deviceId) { AuthenticationToken oAt = new AuthSvc().AuthUser(at, -1, deviceId); try { ActiveDevice ad = ActiveDevice.Populate(deviceId); ad.Delete(); //TODO: Re-assign work orders } catch { } }
public static void UpdateDeviceLastCommunicated(int deviceId) { if (IsDeviceActive(deviceId)) { ActiveDevice ad = ActiveDevice.Populate(deviceId); ad.LastActiveSend = DateTime.Now; ad.Save(); } else { ActiveDevice.CreateActiveDevice(deviceId); } }