Swap out Newtonsoft.Json for XML, as it wasn't working right.
This commit is contained in:
parent
131157c50b
commit
a28845d2bd
6 changed files with 86 additions and 18 deletions
|
@ -6,7 +6,6 @@ using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using S22.Xmpp;
|
using S22.Xmpp;
|
||||||
using S22.Xmpp.Client;
|
using S22.Xmpp.Client;
|
||||||
using S22.Xmpp.Im;
|
using S22.Xmpp.Im;
|
||||||
|
@ -23,7 +22,7 @@ namespace RhinoReminds
|
||||||
public string Hostname => Jid.Split('@')[1];
|
public string Hostname => Jid.Split('@')[1];
|
||||||
private readonly string password;
|
private readonly string password;
|
||||||
|
|
||||||
public string ReminderFilePath { get; set; } = "./Reminders.json";
|
public string ReminderFilePath { get; set; } = "./reminders.xml";
|
||||||
private ReminderList reminderList = new ReminderList();
|
private ReminderList reminderList = new ReminderList();
|
||||||
|
|
||||||
private XmppClient client;
|
private XmppClient client;
|
||||||
|
@ -39,7 +38,7 @@ namespace RhinoReminds
|
||||||
if (File.Exists(ReminderFilePath))
|
if (File.Exists(ReminderFilePath))
|
||||||
{
|
{
|
||||||
Console.WriteLine($"[Rhino/Startup] Loading reminders list from {ReminderFilePath}");
|
Console.WriteLine($"[Rhino/Startup] Loading reminders list from {ReminderFilePath}");
|
||||||
reminderList = JsonConvert.DeserializeObject<ReminderList>(File.ReadAllText(ReminderFilePath));
|
reminderList = ReminderList.FromXmlFile(ReminderFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
client = new XmppClient(Hostname, Username, password);
|
client = new XmppClient(Hostname, Username, password);
|
||||||
|
|
|
@ -9,7 +9,7 @@ namespace RhinoReminds
|
||||||
{
|
{
|
||||||
public class ProgramSettings
|
public class ProgramSettings
|
||||||
{
|
{
|
||||||
public string Filepath = "./reminders.json";
|
public string Filepath = "./reminders.xml";
|
||||||
public string Jid = null;
|
public string Jid = null;
|
||||||
|
|
||||||
public string Password = null;
|
public string Password = null;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using Newtonsoft.Json;
|
using System.Net;
|
||||||
|
using System.Xml;
|
||||||
using S22.Xmpp;
|
using S22.Xmpp;
|
||||||
|
|
||||||
namespace RhinoReminds
|
namespace RhinoReminds
|
||||||
|
@ -9,7 +10,6 @@ namespace RhinoReminds
|
||||||
{
|
{
|
||||||
public int Id { get; }
|
public int Id { get; }
|
||||||
public string Jid { get; }
|
public string Jid { get; }
|
||||||
[JsonIgnore]
|
|
||||||
public Jid JidObj => new Jid(Jid);
|
public Jid JidObj => new Jid(Jid);
|
||||||
public DateTime Time { get; }
|
public DateTime Time { get; }
|
||||||
public string Message { get; }
|
public string Message { get; }
|
||||||
|
@ -26,5 +26,47 @@ namespace RhinoReminds
|
||||||
{
|
{
|
||||||
return $"[Reminder Id={Id}, Jid={Jid}, Time={Time}, Message={Message}";
|
return $"[Reminder Id={Id}, Jid={Jid}, Time={Time}, Message={Message}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region XML
|
||||||
|
|
||||||
|
|
||||||
|
public void WriteToXml(XmlWriter xml)
|
||||||
|
{
|
||||||
|
xml.WriteStartElement("Reminder");
|
||||||
|
|
||||||
|
xml.WriteElementString("Id", Id.ToString());
|
||||||
|
xml.WriteElementString("Jid", Jid);
|
||||||
|
xml.WriteElementString("Time", Time.ToString());
|
||||||
|
xml.WriteElementString("Message", WebUtility.HtmlEncode(Message));
|
||||||
|
|
||||||
|
xml.WriteEndElement();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Reminder FromXml(XmlNode xml)
|
||||||
|
{
|
||||||
|
int id = -1; string jid = string.Empty, message = string.Empty; DateTime dateTime = DateTime.Now;
|
||||||
|
|
||||||
|
foreach (XmlNode subNode in xml.ChildNodes) {
|
||||||
|
switch (subNode.Name) {
|
||||||
|
case "Id":
|
||||||
|
id = int.Parse(subNode.InnerText);
|
||||||
|
break;
|
||||||
|
case "Jid":
|
||||||
|
jid = subNode.InnerText;
|
||||||
|
break;
|
||||||
|
case "Time":
|
||||||
|
dateTime = DateTime.Parse(subNode.InnerText);
|
||||||
|
break;
|
||||||
|
case "Message":
|
||||||
|
message = WebUtility.HtmlDecode(subNode.InnerText);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Reminder(id, jid, dateTime, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,19 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Newtonsoft.Json;
|
using System.Linq;
|
||||||
|
using System.Xml;
|
||||||
using S22.Xmpp;
|
using S22.Xmpp;
|
||||||
|
|
||||||
namespace RhinoReminds
|
namespace RhinoReminds
|
||||||
{
|
{
|
||||||
public delegate void OnReminderListUpdateHandler(object sender, Reminder newReminder);
|
public delegate void OnReminderListUpdateHandler(object sender, Reminder newReminder);
|
||||||
|
|
||||||
[JsonObject(MemberSerialization.OptIn)]
|
|
||||||
public class ReminderList
|
public class ReminderList
|
||||||
{
|
{
|
||||||
[JsonProperty]
|
private int nextId = 0;
|
||||||
public int NextId { get; private set; } = 0;
|
private readonly object saveLock = new object();
|
||||||
private object saveLock = new object();
|
|
||||||
|
|
||||||
[JsonProperty]
|
|
||||||
public SortedList<DateTime, Reminder> Reminders = new SortedList<DateTime, Reminder>();
|
public SortedList<DateTime, Reminder> Reminders = new SortedList<DateTime, Reminder>();
|
||||||
|
|
||||||
public event OnReminderListUpdateHandler OnReminderListUpdate;
|
public event OnReminderListUpdateHandler OnReminderListUpdate;
|
||||||
|
@ -26,7 +24,7 @@ namespace RhinoReminds
|
||||||
}
|
}
|
||||||
|
|
||||||
public Reminder CreateReminder(Jid inJid, DateTime time, string message) {
|
public Reminder CreateReminder(Jid inJid, DateTime time, string message) {
|
||||||
Reminder result = new Reminder(NextId++, $"{inJid.Node}@{inJid.Domain}", time, message);
|
Reminder result = new Reminder(nextId++, $"{inJid.Node}@{inJid.Domain}", time, message);
|
||||||
Console.WriteLine($"[Rhino/ReminderList] Created reminder {result}");
|
Console.WriteLine($"[Rhino/ReminderList] Created reminder {result}");
|
||||||
Reminders.Add(time, result);
|
Reminders.Add(time, result);
|
||||||
OnReminderListUpdate(this, result);
|
OnReminderListUpdate(this, result);
|
||||||
|
@ -38,7 +36,24 @@ namespace RhinoReminds
|
||||||
// Make sure that the reminder thread doesn't try to save the reminders at the exact same time
|
// Make sure that the reminder thread doesn't try to save the reminders at the exact same time
|
||||||
// we receive a request for a new reminder
|
// we receive a request for a new reminder
|
||||||
lock (saveLock) { // FUTURE: We could go lockless here with some work, but it's not worth it for the teeny chance & low overhead
|
lock (saveLock) { // FUTURE: We could go lockless here with some work, but it's not worth it for the teeny chance & low overhead
|
||||||
File.WriteAllText(filename, JsonConvert.SerializeObject(this));
|
XmlWriter xml = XmlWriter.Create(
|
||||||
|
filename,
|
||||||
|
new XmlWriterSettings() { Indent = true }
|
||||||
|
);
|
||||||
|
|
||||||
|
xml.WriteStartDocument();
|
||||||
|
xml.WriteStartElement("ReminderList");
|
||||||
|
|
||||||
|
xml.WriteElementString("NextId", nextId.ToString());
|
||||||
|
|
||||||
|
xml.WriteStartElement("Reminders");
|
||||||
|
foreach (Reminder nextReminder in Reminders.Values)
|
||||||
|
nextReminder.WriteToXml(xml);
|
||||||
|
xml.WriteEndElement();
|
||||||
|
|
||||||
|
xml.WriteEndElement();
|
||||||
|
xml.WriteEndDocument();
|
||||||
|
xml.Close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,5 +68,20 @@ namespace RhinoReminds
|
||||||
public void DeleteReminder(Reminder nextReminder) {
|
public void DeleteReminder(Reminder nextReminder) {
|
||||||
Reminders.Remove(nextReminder.Time);
|
Reminders.Remove(nextReminder.Time);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ReminderList FromXmlFile(string filepath)
|
||||||
|
{
|
||||||
|
XmlDocument xml = new XmlDocument();
|
||||||
|
xml.Load(filepath);
|
||||||
|
|
||||||
|
ReminderList result = new ReminderList();
|
||||||
|
result.nextId = int.Parse(xml.GetElementsByTagName("NextId")[0].InnerText);
|
||||||
|
foreach (XmlNode reminderXML in xml.GetElementsByTagName("Reminders")[0].ChildNodes) {
|
||||||
|
Reminder nextReminder = Reminder.FromXml(reminderXML);
|
||||||
|
result.Reminders.Add(nextReminder.Time, nextReminder);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,9 +34,6 @@
|
||||||
<Reference Include="S22.Xmpp">
|
<Reference Include="S22.Xmpp">
|
||||||
<HintPath>..\packages\S22.Xmpp.1.0.0.0\lib\net45\S22.Xmpp.dll</HintPath>
|
<HintPath>..\packages\S22.Xmpp.1.0.0.0\lib\net45\S22.Xmpp.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="Newtonsoft.Json">
|
|
||||||
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="mscorlib" />
|
<Reference Include="mscorlib" />
|
||||||
<Reference Include="Microsoft.Recognizers.Definitions">
|
<Reference Include="Microsoft.Recognizers.Definitions">
|
||||||
<HintPath>..\packages\Microsoft.Recognizers.Text.1.1.2\lib\net462\Microsoft.Recognizers.Definitions.dll</HintPath>
|
<HintPath>..\packages\Microsoft.Recognizers.Text.1.1.2\lib\net462\Microsoft.Recognizers.Definitions.dll</HintPath>
|
||||||
|
@ -59,6 +56,7 @@
|
||||||
<Reference Include="System.ValueTuple">
|
<Reference Include="System.ValueTuple">
|
||||||
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
|
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
<package id="Microsoft.Recognizers.Text.DateTime" version="1.1.2" targetFramework="net47" />
|
<package id="Microsoft.Recognizers.Text.DateTime" version="1.1.2" targetFramework="net47" />
|
||||||
<package id="Microsoft.Recognizers.Text.Number" version="1.1.2" targetFramework="net47" />
|
<package id="Microsoft.Recognizers.Text.Number" version="1.1.2" targetFramework="net47" />
|
||||||
<package id="Microsoft.Recognizers.Text.NumberWithUnit" version="1.1.2" targetFramework="net47" />
|
<package id="Microsoft.Recognizers.Text.NumberWithUnit" version="1.1.2" targetFramework="net47" />
|
||||||
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net47" />
|
|
||||||
<package id="S22.Xmpp" version="1.0.0.0" targetFramework="net47" />
|
<package id="S22.Xmpp" version="1.0.0.0" targetFramework="net47" />
|
||||||
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net47" />
|
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net47" />
|
||||||
<package id="System.ValueTuple" version="4.5.0" targetFramework="net47" />
|
<package id="System.ValueTuple" version="4.5.0" targetFramework="net47" />
|
||||||
|
|
Loading…
Reference in a new issue