Merge branch 'ConnectionController' of https://sourcecode.socialcoding.bosch.com/scm/~hpl2fe/polychat into ConnectionController

This commit is contained in:
Patrick Hellebrand
2021-09-23 10:56:21 +02:00
2 changed files with 94 additions and 2 deletions

View File

@@ -5,8 +5,10 @@ using System.Net;
using SocketIOSharp.Server;
using SocketIOSharp.Server.Client;
using PolyChat.Models;
using System.IO;
using System.Threading;
using System;
using System.Text.Json;
using Newtonsoft.Json;
namespace PolyChat
{
@@ -33,6 +35,8 @@ namespace PolyChat
{
UIController = uiController;
OwnIP = getIP();
loadChats();
//SaveChats("10", "{das ist ein test}");
Serve();
}
@@ -95,10 +99,12 @@ namespace PolyChat
Debug.WriteLine($"{type} -> {ip} content: {content}");
JObject json = new JObject(
new JProperty("type", type),
new JProperty("content", content)
new JProperty("content", content),
new JProperty("timestamp", DateTime.Now.ToString())
);
Debug.WriteLine($"json: {json.ToString()}");
Connections[ip].SendMessage(json.ToString());
SaveChats(ip, json.ToString());
}
private void OnMessage(string ip, JToken[] data)
@@ -108,6 +114,7 @@ namespace PolyChat
{
Debug.WriteLine("RAW: " + data[0]);
UIController.OnIncomingMessage(ip, data[0].ToString());
SaveChats(ip, data[0].ToString());
}
else Debug.WriteLine("Undefined: " + data);
}
@@ -150,5 +157,89 @@ namespace PolyChat
}
return null;
}
/// <summary>
/// sends chatlogs as json array to uiController wit corrosponding ip
/// </summary>
/// <param name="ip"></param>
public void loadChats()
{
//load dir and create if non existant
if (Directory.Exists("U:\\PolyChat\\Saves"))
{
Debug.WriteLine("--Path exists.--");
}
else
{
Directory.CreateDirectory("U:\\PolyChat\\Saves");
Debug.WriteLine("--Path Created--.");
}
//go through all files and send ip and json array to ui
String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves");
if (filepaths.Length > 0)
{
foreach (String path in filepaths)
{
Debug.WriteLine("---Loading Saves---");
Debug.WriteLine("--" + path + "--");
String jsonArr = File.ReadAllText(path);
String ip = Path.GetFileName(path);
ip = ip.Substring(0, ip.Length - 4);
Debug.WriteLine("-" + ip + "-");
Debug.WriteLine(jsonArr);
UIController.OnIncomingMessages(ip, jsonArr);
}
}
}
/// <summary>
/// Saves incoming chat message to
/// </summary>
/// <param name="ip"></param>
/// <param name="json"></param>
public void SaveChats(String ip, String json)
{
//Vielleicht noch so machen dass die mit gleicher ip nacheinander gemacht
//werden damit es ncith zu überschreibungen kommt vielleicth auch ganz oben oder am ende ne
//writing flag setzen oder auch in der datei selbst ne flag setzen
//also save fils from myself
new Thread(() =>
{
if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt"))
{
Debug.WriteLine("--File allready exists--");
//check for integraty of file
string output = File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt");
Debug.WriteLine($"---{output}---");
if (output.Substring(0, 1).Equals("[") && output.Substring(output.Length - 1, 1).Equals("]"))
{
Debug.WriteLine("--adding new chatmessage--");
//structure intact
//save new chat
String saved = output.Substring(0, output.Length - 1);
output = saved + ", " + json + " ]";
File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt");
File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", output);
}
else
{
Debug.WriteLine("--Structure not intact--");
Debug.WriteLine("--redoing file--");
//structure not intact
//redo file
File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt");
File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]");
}
}
else
{
Debug.WriteLine("--Creating new File--");
//setup file
File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]");
}
}).Start();
}
}
}

View File

@@ -164,6 +164,7 @@ namespace PolyChat
});
}
private void OnDeleteChat(object sender = null, RoutedEventArgs e = null)
{
Controller.CloseChat(selectedPartner.Code);