示例#1
0
        static void ProcessContactAdd(N1mmXmlContactInfo ci)
        {
            if (!OnlyProcessOriginals || (OnlyProcessOriginals && ci.IsOriginal == "True"))
            {                            // True = contact was originated on this computer. Without this check, in a multiple n1mm scenario,
                                         // if this computer has All QSOs selected, we will receive the contact twice. We only want
                                         // the one from the PC on which the contact was logged. This does mean every n1mm instance
                                         // will need to be configured to send datagrams to us. That seems reasonable.

                Console.WriteLine($"Adding a contact: {ci.Call}  / {ci.Operator}");

                var contactRepo = new ContactDbRepo(pathToDb);

                ContactDbRow row = Mappers.Map(ci);

                contactRepo.Add(row);
            }
            else
            {
                Console.WriteLine($"Skipping a non-original contact: {ci.Call} / {ci.Operator}");
            }
        }
示例#2
0
        static void ProcessDatagram(byte[] msg)
        {
            bool isAdd, isReplace, isDelete;

            isAdd = isReplace = isDelete = false;

            try
            {
                if (N1mmXmlContactInfo.TryParse(msg, out N1mmXmlContactInfo ci))
                {
                    isAdd = true;
                    //Console.WriteLine($"Adding a contact: {ci.Call}");
                    ProcessContactAdd(ci);
                }
                else if (N1mmXmlContactReplace.TryParse(msg, out N1mmXmlContactReplace cr))
                {
                    isReplace = true;
                    //Console.WriteLine($"Replacing a contact: {cr.Call}");
                    ProcessContactReplace(cr);
                }
                else if (ContactDelete.TryParse(msg, out ContactDelete cd))
                {
                    isDelete = true;
                    //Console.WriteLine($"Deleting a contact: {cd.Call}");
                    ProcessContactDelete(cd);
                }
            }
            finally
            {
                try
                {
                    string rawFolder = Path.Combine(Environment.CurrentDirectory, "datagrams");
                    if (!Directory.Exists(rawFolder))
                    {
                        Directory.CreateDirectory(rawFolder);
                    }

                    string targetFolder;

                    if (isAdd)
                    {
                        targetFolder = Path.Combine(rawFolder, "ContactAdd");
                    }
                    else if (isReplace)
                    {
                        targetFolder = Path.Combine(rawFolder, "ContactReplace");
                    }
                    else if (isDelete)
                    {
                        targetFolder = Path.Combine(rawFolder, "ContactDelete");
                    }
                    else
                    {
                        targetFolder = rawFolder;
                    }

                    if (!Directory.Exists(targetFolder))
                    {
                        Directory.CreateDirectory(targetFolder);
                    }

                    string rawFile = Path.Combine(targetFolder, string.Format("{0:yyyyMMdd-HHmmss.fff}.xml", DateTime.Now));

                    File.WriteAllBytes(rawFile, msg);
                }
                catch (Exception ex)
                {
                    Log("Could not write datagram: {0}", ex);
                }
            }

            /*else
             * {
             *  string str;
             *  try
             *  {
             *      str = Encoding.UTF8.GetString(msg);
             *  }
             *  catch (Exception)
             *  {
             *      Log("Bad datagram, not UTF8: {0}", msg.ToHexBytes());
             *      return;
             *  }
             *
             *  if (!string.IsNullOrWhiteSpace(str))
             *  {
             *      string rename = GetRootElementName(str);
             *      if (!string.IsNullOrWhiteSpace(rename))
             *      {
             *          Log("Not a known datagram: {0}", rename);
             *      }
             *      else
             *      {
             *          Log("Received garbage: {0}", str.Truncate());
             *      }
             *  }
             *  else
             *  {
             *      Log("Received whitespace");
             *  }
             * }*/
        }