コード例 #1
0
        private bool ProcessMessage(string correlationToken, string eventConverterName, DateTimeOffset messageTime, string messagePayload, string originator, bool bIsTargetedMessage, out string error, out bool bSendAck)
        {
            bool bSuccess = false;

            bSendAck = true;

            if (bIsTargetedMessage)
            {
                if (_nodeOwnerManager.RegisterOwnerCandidate(correlationToken, originator, TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID.ToString(), true))
                {
                    var notification = new TSM(MyBaseEngine.GetEngineName(), $"MESHRECEIVER_ACK_NOTIFY:;:{correlationToken}:;:{originator}:;:{bIsTargetedMessage}");
                    TheCommCore.PublishCentral(notification, false);
                }
            }
            else
            {
                if (_nodeOwnerManager.CheckOwner(originator, TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID.ToString()) != true)
                {
                    _nodeOwnerManager.RequestOwnership(correlationToken, originator, TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID.ToString());
                }
            }

            IEventConverter eventConverter = null;

            if (!string.IsNullOrEmpty(eventConverterName))
            {
                eventConverter = TheEventConverters.GetEventConverter(eventConverterName);
                if (eventConverter == null)
                {
                    TheBaseAssets.MySYSLOG.WriteToLog(180001, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("Mesh Receiver", $"Unknown event converter in incoming MESHSENDER_DATA {this.MyBaseThing.FriendlyName}: {eventConverterName}", eMsgLevel.l1_Error, TSM.L(eDEBUG_LEVELS.OFF) ? null : messagePayload));
                }
            }
            else
            {
                eventConverter = TheEventConverters.GetEventConverter(ReceiverEventConverter);
                if (eventConverter == null)
                {
                    TheBaseAssets.MySYSLOG.WriteToLog(180002, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("Mesh Receiver", $"Unknown default event converter {this.MyBaseThing.FriendlyName}: {ReceiverEventConverter}", eMsgLevel.l1_Error));
                }
            }

            //if (EnableDataLogging)
            //{
            //    try
            //    {
            //        lock (dataLoggerLock)
            //        {
            //            System.IO.File.AppendAllText("meshreceiverdata.log", $"{{\"TimeReceived\":\"{DateTimeOffset.Now:O}\", \"PLS\": {pMsg.Message.PLS},\"TXT\":{pMsg.Message.TXT}}},\r\n");
            //        }
            //    }
            //    catch (Exception e)
            //    {
            //        TheBaseAssets.MySYSLOG.WriteToLog(180003, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("Mongo Writer", $"Unable to log data to file: {this.MyBaseThing.FriendlyName}", eMsgLevel.l3_ImportantMessage, e.ToString()));
            //    }
            //}

            if (eventConverter == null)
            {
                error = "Unknown event converter";
            }
            else
            {
                if (IsConnected)
                {
                    try
                    {
                        var now = DateTimeOffset.Now;
                        if (!string.IsNullOrEmpty(TimeDriftPropertyName))
                        {
                            // Calculate time difference (in minutes) between the sender and this node
                            var timeDrift = now - messageTime;
                            timeDrift = TimeSpan.FromMinutes(timeDrift.TotalMinutes > 0 ? Math.Floor(timeDrift.TotalMinutes) : Math.Ceiling(timeDrift.TotalMinutes));
                            if (eventConverter.StaticPropsToAdd == null)
                            {
                                eventConverter.StaticPropsToAdd = new Dictionary <string, object>();
                            }
                            eventConverter.StaticPropsToAdd[TimeDriftPropertyName] = System.Xml.XmlConvert.ToString(timeDrift);
                        }
                        if (!string.IsNullOrEmpty(SenderTimePropertyName))
                        {
                            if (eventConverter.StaticPropsToAdd == null)
                            {
                                eventConverter.StaticPropsToAdd = new Dictionary <string, object>();
                            }
                            eventConverter.StaticPropsToAdd[SenderTimePropertyName] = messageTime;
                        }
                        bool bDetectedOtherOwner = false;
                        // TODO Support picking a thing for formats that don't carry a thing id
                        eventConverter.NewThingCallback = async(t) =>
                        {
                            if (await _nodeOwnerManager.CheckOwnerAsync(correlationToken, originator, TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID.ToString()).ConfigureAwait(false) == false)
                            {
                                bDetectedOtherOwner = true;
                                return(false);
                            }
                            return(true);
                        };
                        error = eventConverter.ProcessEventData(null, messagePayload, now);
                        if (bDetectedOtherOwner)
                        {
                            error    = "Detected other receiver";
                            bSendAck = false;
                            bSuccess = false;
                        }
                        else
                        {
                            if (String.IsNullOrEmpty(error))
                            {
                                ReceiveCount++;
                                bSuccess = true;
                            }
                            else
                            {
                                TheBaseAssets.MySYSLOG.WriteToLog(180004, new TSM("Mesh Receiver", "Error processing data", eMsgLevel.l1_Error, error));
                                ReceiveErrorCount++;
                            }
                            LastReceiveTime = now;
                        }
                    }
                    catch (Exception e)
                    {
                        error = "Error parsing data: " + e.Message;
                        TheBaseAssets.MySYSLOG.WriteToLog(180005, new TSM("Mesh Receiver", "Error processing data", eMsgLevel.l1_Error, e.ToString()));
                        bSuccess = false;
                        ReceiveErrorCount++;
                    }

                    if (bSuccess)
                    {
                        MyBaseThing.StatusLevel = 1;
                    }
                }
                else
                {
                    error = "Not connected";
                }
            }

            return(bSuccess);
        }