예제 #1
0
        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);
            }
        }
예제 #2
0
        private void ProcessUpdatedWorkOrders()
        {
            ProcessingUpdatedWorkOrders = true;
            BrokeredMessage updatedWorkOrderMessage = null;

            updatedWorkOrderMessage = UpdatedWorkOrders.Receive();


            try {
                while (updatedWorkOrderMessage != null)
                {
                    // Process the message
                    SharedClasses.WorkOrderUpdate wou = updatedWorkOrderMessage.GetBody <SharedClasses.WorkOrderUpdate>();

                    using (WorkOrder wo = WorkOrder.Populate(wou.WorkOrderId)) {
                        Trace.WriteLine("Processing Update", wou.WorkOrderId.ToString());

                        switch (wou.WorkOrderUpdateType)
                        {
                        case SharedClasses.WorkOrderUpdate.UpdateType.Acknowledge:
                            // Update database with last spoke time and message
                            wo.SlaveWorkOrderLastCommunication = DateTime.Now;
                            wo.WorkOrderStatus = "SLAVE_ACKNOWLEDGED";
                            wo.Save();

                            break;

                        case SharedClasses.WorkOrderUpdate.UpdateType.Cancel:
                            if (wo.WorkOrderStatus == "SLAVE_ACKNOWLEDGED")
                            {
                                // Send message to cancel
                                CommunicationPackage.CreateCommunication(wo.SlaveWorkerId.Value, CommunicationPackage.UpdateType.Cancel, wo.WorkOrderId);
                            }

                            if (wo.CommunicationPackages.Count() > 0)
                            {
                                // Have to notify device to cancel
                                foreach (CommunicationPackage cp in wo.CommunicationPackages.Where(x => x.CommunicationType != (int)CommunicationPackage.UpdateType.Cancel))
                                {
                                    if (cp.Response == null && cp.Status == null)
                                    {
                                        // Cancel
                                        using (CommunicationPackage oCp = CommunicationPackage.Populate(cp.CommunicationId)) {
                                            oCp.Status = "USER_CANCELLED";
                                            oCp.Save();
                                        }
                                    }
                                }

                                wo.WorkOrderStatus = "USER_CANCELLED";
                            }
                            else
                            {
                                wo.WorkOrderStatus = "CANCELLED";
                            }

                            wo.Save();
                            break;

                        case SharedClasses.WorkOrderUpdate.UpdateType.SubmitResult:
                            // Update database with result.
                            wo.SlaveWorkOrderLastCommunication = DateTime.Now;
                            wo.WorkOrderResultJson             = wou.ResultJson;
                            wo.WorkOrderStatus      = "RESULT_RECEIVED";
                            wo.StartComputationTime = wou.ComputationStartTime;
                            wo.EndComputationTime   = wou.ComputationEndTime;
                            wo.DeserialiationTime   = wou.RequestDeserialisationTime;
                            wo.SerialisationTime    = wou.ResultSerialisationTime;

                            wo.Save();

                            // Send result to requesting device

                            CommunicationPackage.CreateCommunication(wo.DeviceId, CommunicationPackage.UpdateType.Result, wo.WorkOrderId);

                            //   CommunicationPackages.Send(new BrokeredMessage(cp.Serialize()));


                            break;

                        case SharedClasses.WorkOrderUpdate.UpdateType.MarkBeingComputed:
                            // Update database
                            wo.SlaveWorkOrderLastCommunication = DateTime.Now;
                            wo.WorkOrderStatus = "BEING_COMPUTED";
                            wo.Save();

                            break;
                        }
                    }

                    updatedWorkOrderMessage.Complete();
                    updatedWorkOrderMessage = UpdatedWorkOrders.Receive();
                }
            } catch (Exception e) {
                Debug.WriteLine(e.Message);
            }
            ProcessingUpdatedWorkOrders = false;
        }