コード例 #1
0
        private bool OnMessageReceived(Message message)
        {
            try
            {
                Logger.DebugLogAmqpMessage($"Incomming AMQP Message on queue \"{_queueName}\": ", message);
                var jsonTxt = GetJsonBody(message);
                //Logger.Debug($"Incoming AMQP Message:\n{PrintAmqpMessage(jsonTxt)}");

                jsonTxt = jsonTxt.Replace("Content", "Resource");

                var jObject = JObject.Parse(jsonTxt);

                var name       = (string)jObject["Name"];
                var id         = (string)jObject["Id"];
                var refId      = (string)jObject["ReferenceId"];
                var assignedTo = (string)jObject["AssignedTo"];
                var deviceId   = DeviceId.TryParse(assignedTo);
                var type       = (string)jObject["Type"];

                if ((type == "Job" || type == "TodoList") && deviceId != null)
                {
                    var immediate = (bool)jObject["Immediate"];

                    var job = JsonConvert.DeserializeObject <Job>(jsonTxt);
                    Logger.Debug($"Valid Job received: {job.Name} - {job.Id} - {job.AssignedTo}");

                    Logger.Info($"Job({job.Name} => {deviceId.FullId}");

                    _PersistenceProvider.AddJob(deviceId, job);

                    if (immediate)
                    {
                        var token = jObject["NotificationPattern"];
                        if (token != null)
                        {
                            var pattern = token.ToObject <int>();
                            _FcmMessageHandler.SendGetDataWithNotification(pattern, deviceId);
                        }
                        else
                        {
                            _FcmMessageHandler.SendGetDataWithNotification(deviceId);
                        }
                    }
                    else
                    {
                        _FcmMessageHandler.SendGetDataWithoutNotification(deviceId);
                    }
                }
                else if (type == "Notification")
                {
                    var statusStr = (string)jObject["Status"];

                    if (name == "StatusUpdate" && Enum.TryParse(statusStr, true, out JobStatus status))
                    {
                        Logger.Debug($"Valid Job Status update received: Id:{id}, Status: {status}");

                        _PersistenceProvider.UpdateJobStatus(Guid.Parse(id), status);
                        var list = _PersistenceProvider.GetAllJobs()
                                   .Where(j => j != null && j.Id.Equals(Guid.Parse(id))).ToList();

                        foreach (var job in list)
                        {
                            DeviceId receiver;

                            if (job.AssignedTo != null)
                            {
                                receiver = DeviceId.TryParse(job.AssignedTo);
                            }
                            else if (job.CreatedBy != null)
                            {
                                receiver = DeviceId.TryParse(job.CreatedBy);
                            }
                            else
                            {
                                throw new Exception("Unknown Notification-Receiver!");
                            }

                            Logger.Info($"Notification({job.Name}) => {receiver.FullId}");
                            _FcmMessageHandler.SendGetDataWithoutNotification(receiver);
                        }
                    }
                    else
                    {
                        //Ignore other Notifications
                        Logger.Error("Unknown Notification from Amqp");
                    }
                }
                else if (type == "ValueMessage")
                {
                    var valueName = (string)jObject["ValueName"];

                    _PersistenceProvider.AddValue(valueName, new Value
                    {
                        Name = valueName,
                        Val  = jObject
                    });
                }

                return(true);
            }
            catch (Exception e)
            {
                Logger.Error("Exception in OnMessageReceived!", e);
            }

            return(true);
        }