Beispiel #1
0
        // ReSharper disable once InconsistentNaming
        public Job TransformReadOnlyJob(DeviceId deviceId, Job job)
        {
            var refJob = _persistenceProvider.FindJobById(job.ReferenceId)?.Job;

            if (refJob == null)
            {
                return(null);
            }

            refJob.Status = JobStatus.Done;
            _persistenceProvider.UpdateJobStatus(refJob.Id, JobStatus.Done);

            //Generate new Job with new GUID, no RefID, for Persistence
            var newJob = new Job
            {
                Id          = Guid.NewGuid(),
                Name        = "WzBruchMeldenRO",
                Type        = "Job",
                ReferenceId = Guid.Empty,
                Status      = JobStatus.InProgress,
                CreatedAt   = job.CreatedAt,
                CreatedBy   = job.CreatedBy,
                AssignedTo  = job.CreatedBy,
                Immediate   = false,
                Priority    = job.Priority,
                Resource    = job.Resource,
            };

            newJob.Resource.Add("list_title", "Werkzeugbruch");
            newJob.Resource.Add("list_text", $"Art-Nr.: {GetFromRes(newJob.Resource, "text")} - Maschine: {GetFromRes(newJob.Resource, "subject")}");

            _persistenceProvider.AddJob(deviceId, newJob);
            _fcmMessageHandler.SendGetDataWithoutNotification(deviceId);

            //Set new JobGUID to RUleEngine-Job and send it.
            job.Id          = newJob.Id;
            job.ReferenceId = Guid.Empty;
            return(job);
        }
        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);
        }