diff --git a/ErgometerLibrary/ChatMessage.cs b/ErgometerLibrary/ChatMessage.cs
new file mode 100644
index 0000000..1b19465
--- /dev/null
+++ b/ErgometerLibrary/ChatMessage.cs
@@ -0,0 +1,21 @@
+namespace ErgometerLibrary
+{
+ public class ChatMessage
+ {
+ public string Message { get; }
+ public string Name { get; }
+ public double TimeStamp { get; set;}
+
+ public ChatMessage(string name, string message)
+ {
+ Name = name;
+ Message = message;
+ TimeStamp = Helper.Now;
+ }
+
+ public override string ToString()
+ {
+ return $"[{Name}] <{TimeStamp}> - {Message}";
+ }
+ }
+}
\ No newline at end of file
diff --git a/ErgometerLibrary/ComPort.cs b/ErgometerLibrary/ComPort.cs
new file mode 100644
index 0000000..176a372
--- /dev/null
+++ b/ErgometerLibrary/ComPort.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Collections.Generic;
+using System.IO.Ports;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ErgometerLibrary
+{
+ public class ComPort
+ {
+ private SerialPort comPort;
+
+ public ComPort()
+ {
+ comPort = null;
+ }
+
+ public Boolean Connect(string port)
+ {
+ comPort = new SerialPort();
+ comPort.PortName = port;
+ comPort.DataBits = 8;
+ comPort.Parity = Parity.None;
+ comPort.StopBits = StopBits.One;
+ comPort.BaudRate = 9600;
+
+ comPort.Open();
+
+ return comPort.IsOpen;
+ }
+
+ public Boolean Disconnect()
+ {
+ comPort.Close();
+ return !comPort.IsOpen;
+ }
+
+ public Boolean IsOpen()
+ {
+ if (comPort != null)
+ return comPort.IsOpen;
+ else
+ return false;
+ }
+
+ public void Write(string input)
+ {
+ if (IsOpen())
+ {
+ comPort.WriteLine(input);
+ }
+ }
+
+ public string Read()
+ {
+ if (IsOpen())
+ {
+ return comPort.ReadLine();
+ }
+
+ return "";
+ }
+ }
+}
diff --git a/ErgometerLibrary/ErgometerLibrary.csproj b/ErgometerLibrary/ErgometerLibrary.csproj
index ec42b20..f53e6dc 100644
--- a/ErgometerLibrary/ErgometerLibrary.csproj
+++ b/ErgometerLibrary/ErgometerLibrary.csproj
@@ -1,10 +1,10 @@
-
+
Debug
AnyCPU
- cc20ff4e-8751-42a1-a32b-0e5b22452cac
+ {CC20FF4E-8751-42A1-A32B-0E5B22452CAC}
Library
Properties
ErgometerLibrary
@@ -30,25 +30,32 @@
4
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+ ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll
+ True
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
-
-
+
\ No newline at end of file
diff --git a/ErgometerLibrary/FileHandler.cs b/ErgometerLibrary/FileHandler.cs
new file mode 100644
index 0000000..4333b82
--- /dev/null
+++ b/ErgometerLibrary/FileHandler.cs
@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+
+namespace ErgometerLibrary
+{
+ public class FileHandler
+ {
+ public static string DataFolder { get; } = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "Ergometer");
+
+ public static void CheckDataFolder()
+ {
+ if(! Directory.Exists(DataFolder))
+ {
+ Directory.CreateDirectory(DataFolder);
+ }
+ }
+
+ public static int GenerateSession()
+ {
+ string[] existingSessions = Directory.GetDirectories(DataFolder);
+
+ Random rand = new Random();
+ int sessionID = rand.Next(int.MinValue, int.MaxValue);
+
+
+ while (existingSessions.Contains(sessionID.ToString()))
+ {
+ sessionID = rand.Next(int.MinValue, int.MaxValue);
+ }
+
+ return sessionID;
+ }
+
+ public static void CreateSession(int session, string naam)
+ {
+ Directory.CreateDirectory(GetSessionFolder(session));
+ File.Create(Path.Combine(GetSessionFolder(session), "session.prop"));
+ File.Create(Path.Combine(GetSessionFolder(session), "metingen.ergo"));
+ File.Create(Path.Combine(GetSessionFolder(session), "chat.log"));
+
+ File.WriteAllText(GetSessionFile(session), naam + "\n" + Helper.Now);
+ }
+
+ public static void WriteMetingen(int session, List metingen)
+ {
+ string json = Newtonsoft.Json.JsonConvert.SerializeObject(metingen);
+ File.WriteAllText(GetSessionMetingen(session), json);
+ Console.WriteLine("Writing metingen: " + GetSessionMetingen(session));
+ }
+
+ public static void WriteChat(int session, List chat)
+ {
+ string write = "";
+ foreach(ChatMessage c in chat)
+ {
+ write += c.ToString() + "\n";
+ }
+
+ File.WriteAllText(GetSessionChat(session), write);
+ Console.WriteLine("Writing chat: " + GetSessionChat(session));
+ }
+
+ private static string GetSessionFolder(int session)
+ {
+ return Path.Combine(DataFolder, session.ToString());
+ }
+ private static string GetSessionFile(int session)
+ {
+ return Path.Combine(DataFolder, session.ToString(), "session.prop");
+ }
+ private static string GetSessionMetingen(int session)
+ {
+ return Path.Combine(DataFolder, session.ToString(), "metingen.ergo");
+ }
+ private static string GetSessionChat(int session)
+ {
+ return Path.Combine(DataFolder, session.ToString(), "chat.log");
+ }
+ }
+}
diff --git a/ErgometerLibrary/Class1.cs b/ErgometerLibrary/Helper.cs
similarity index 54%
rename from ErgometerLibrary/Class1.cs
rename to ErgometerLibrary/Helper.cs
index 1d1b5b4..95d9c06 100644
--- a/ErgometerLibrary/Class1.cs
+++ b/ErgometerLibrary/Helper.cs
@@ -6,7 +6,8 @@ using System.Threading.Tasks;
namespace ErgometerLibrary
{
- public class Class1
+ class Helper
{
+ public static double Now { get { return (DateTime.Now - DateTime.Parse("1/1/1870 0:0:0")).TotalMilliseconds; } }
}
}
diff --git a/ErgometerLibrary/Meting.cs b/ErgometerLibrary/Meting.cs
new file mode 100644
index 0000000..451cde2
--- /dev/null
+++ b/ErgometerLibrary/Meting.cs
@@ -0,0 +1,96 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ErgometerLibrary
+{
+ public class Meting
+ {
+ public int HeartBeat { get; set; }
+ public int RPM { get; set; }
+ public double Speed { get; set; }
+ public double Distance { get; set; }
+ public int Power { get; set; }
+ public int Energy { get; set; }
+ public int Seconds { get; set; }
+ public int ActualPower { get; set; }
+ public double TimeStamp { get; }
+
+ public Meting(int heartbeat, int rpm, double speed, double distance, int power, int energy, int seconds, int actualpower, double timestamp)
+ {
+ HeartBeat = heartbeat;
+ RPM = rpm;
+ Speed = speed;
+ Distance = distance;
+ Power = power;
+ Energy = energy;
+ Seconds = seconds;
+ ActualPower = actualpower;
+ TimeStamp = timestamp;
+ }
+
+ public override string ToString()
+ {
+ string temp = "";
+ temp += "Heartbeat: " + HeartBeat + "\n";
+ temp += "RPM: " + RPM + "\n";
+ temp += "Speed: " + Speed + "\n";
+ temp += "Distance: " + Distance + "\n";
+ temp += "Power: " + Power + "\n";
+ temp += "Energy: " + Energy + "\n";
+ temp += "Seconds: " + Seconds + "\n";
+ temp += "ActualPower: " + ActualPower + "\n";
+ return temp;
+ }
+
+ public string ToCommand()
+ {
+ string temp = "";
+ temp += HeartBeat + "»";
+ temp += RPM + "»";
+ temp += Speed + "»";
+ temp += Distance + "»";
+ temp += Power + "»";
+ temp += Energy + "»";
+ temp += Seconds + "»";
+ temp += ActualPower + "»";
+ temp += TimeStamp;
+ return temp;
+ }
+
+ public static Meting Parse(string input)
+ {
+ return Parse(input, '\t');
+ }
+
+ public static Meting Parse(string input, char delimiter)
+ {
+ string[] status = input.Split(delimiter);
+ Console.WriteLine(status.Length);
+ if (status.Length != 8 && status.Length != 9)
+ {
+ return null;
+ }
+ int heartbeat = int.Parse(status[0]);
+ int rpm = int.Parse(status[1]);
+ double speed = double.Parse(status[2]) / 10;
+ double distance = double.Parse(status[3]) / 10;
+ int power = int.Parse(status[4]);
+ int energy = int.Parse(status[5]);
+ int actualpower = int.Parse(status[7]);
+
+ double timestamp = 0;
+ if (status.Length == 9)
+ timestamp = double.Parse(status[8]);
+ else
+ timestamp = Helper.Now;
+
+ string[] temp = status[6].Split(':');
+ int seconds = (int.Parse(temp[0]) * 60) + (int.Parse(temp[1]));
+
+ return new Meting(heartbeat, rpm, speed, distance, power, energy, seconds, actualpower,timestamp);
+ }
+ }
+}
diff --git a/ErgometerLibrary/NetCommand.cs b/ErgometerLibrary/NetCommand.cs
new file mode 100644
index 0000000..66f5f4d
--- /dev/null
+++ b/ErgometerLibrary/NetCommand.cs
@@ -0,0 +1,185 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ErgometerLibrary
+{
+ public class NetCommand
+ {
+ public enum CommandType { LOGIN, DATA, CHAT, LOGOUT, SESSION };
+
+
+ public double Timestamp { get; set; }
+ public int Session { get; set; }
+ public CommandType Type { get; set; }
+ public string DisplayName { get; set; }
+ public bool IsDoctor { get; set; }
+ public string Password { get; set; }
+ public string ChatMessage { get; set; }
+ public Meting Meting { get; set; }
+
+ public NetCommand(int session)
+ {
+ Type = CommandType.SESSION;
+ Session = session;
+ Timestamp = Helper.Now;
+ }
+
+ public NetCommand(CommandType commandtype, int session)
+ {
+ Type = commandtype;
+ Session = session;
+ Timestamp = Helper.Now;
+ }
+
+ public NetCommand(Meting m, int session)
+ {
+ Type = CommandType.DATA;
+ Session = session;
+ Timestamp = Helper.Now;
+
+ Meting = m;
+ }
+
+ public NetCommand(string chat, int session)
+ {
+ Type = CommandType.CHAT;
+ Session = session;
+ Timestamp = Helper.Now;
+
+ ChatMessage = chat;
+ }
+
+ public NetCommand(string name, bool doctor, int session)
+ {
+ Type = CommandType.LOGIN;
+ Session = session;
+ Timestamp = Helper.Now;
+
+ DisplayName = name;
+ IsDoctor = doctor;
+ }
+
+ public static NetCommand Parse(string command)
+ {
+ string[] com = command.Split('»');
+
+ int comType = int.Parse(com[0]);
+ int session = 0;
+ if (com[1].StartsWith("ses"))
+ session = int.Parse(com[1].Substring(3));
+ else
+ throw new FormatException("Error in NetCommend: " + com[1] + " is not a valid session.");
+
+ string[] args = new string[9];
+ for (int i = 2; i < com.Length; i++)
+ {
+ args[i - 2] = com[i];
+ }
+
+ switch (comType)
+ {
+ case 1:
+ return ParseLoginRequest(session, args);
+ case 2:
+ return ParseData(session, args);
+ case 3:
+ return ParseChatMessage(session, args);
+ case 4:
+ return ParseLogoutRequest(session, args);
+ case 5:
+ return ParseSession(session);
+ default:
+ throw new FormatException("Error in NetCommand: " + comType + " is not a valid command type.");
+ }
+ }
+
+ private static NetCommand ParseSession(int session)
+ {
+ NetCommand temp = new NetCommand(CommandType.SESSION, session);
+ return temp;
+ }
+
+ private static NetCommand ParseLogoutRequest(int session, string[] args)
+ {
+ if (args.Length != 1)
+ throw new MissingFieldException("Error in NetCommand: Logout Request is missing arguments");
+
+ NetCommand temp = new NetCommand(CommandType.LOGOUT, session);
+ if (args[0] != "logout")
+ throw new FormatException("Error in NetCommand: " + args[0] + " is not a valid logout request");
+
+ return temp;
+ }
+
+ private static NetCommand ParseChatMessage(int session, string[] args)
+ {
+ if (args.Length != 1)
+ throw new MissingFieldException("Error in NetCommand: Chat Message is missing arguments");
+
+ NetCommand temp = new NetCommand(CommandType.CHAT, session);
+ temp.ChatMessage = args[0];
+
+ return temp;
+ }
+
+ private static NetCommand ParseData(int session, string[] args)
+ {
+ if (args.Length != 9)
+ throw new MissingFieldException("Error in NetCommand: Data is missing arguments");
+
+ NetCommand temp = new NetCommand(CommandType.DATA, session);
+ temp.Meting = Meting.Parse(string.Join("\t", args));
+
+ return temp;
+ }
+
+ private static NetCommand ParseLoginRequest(int session, string[] args)
+ {
+ bool doctor = bool.Parse(args[1]);
+ if (doctor && args.Length != 3)
+ throw new MissingFieldException("Error in NetCommand: Doctor login is missing arguments");
+ else if (args.Length != 2)
+ throw new MissingFieldException("Error in NetCommand: Client login is missing arguments");
+
+ NetCommand temp = new NetCommand(CommandType.LOGIN, session);
+ temp.IsDoctor = doctor;
+ temp.DisplayName = args[0];
+ if (doctor)
+ temp.Password = args[2];
+
+ return temp;
+ }
+
+ public override string ToString()
+ {
+ string command = "";
+
+ switch (Type)
+ {
+ case CommandType.LOGIN:
+ command += "1»ses" + Session + "»" + DisplayName + "»" + IsDoctor + (IsDoctor ? "»" + Password : "");
+ break;
+ case CommandType.DATA:
+ command += "2»ses" + Session + "»" + Meting.ToCommand();
+ break;
+ case CommandType.CHAT:
+ command += "3»ses" + Session + "»" + ChatMessage;
+ break;
+ case CommandType.LOGOUT:
+ command += "4»ses" + Session + "»logout";
+ break;
+ case CommandType.SESSION:
+ command += "5»ses" + Session;
+ break;
+
+ default:
+ throw new FormatException("Error in NetCommand: Cannot find type of command");
+ }
+
+ return command;
+ }
+ }
+}
diff --git a/ErgometerLibrary/NetHelper.cs b/ErgometerLibrary/NetHelper.cs
new file mode 100644
index 0000000..2814e0e
--- /dev/null
+++ b/ErgometerLibrary/NetHelper.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+
+namespace ErgometerLibrary
+{
+ class NetHelper
+ {
+ public static void SendNetCommand(TcpClient client, NetCommand command)
+ {
+ byte[] b = Encoding.ASCII.GetBytes(command.ToString());
+ client.GetStream().Write(b, 0, b.Length);
+ client.GetStream().Flush();
+ }
+
+ public static NetCommand ReadNetCommand(TcpClient client)
+ {
+ byte[] bytesFrom = new byte[(int) client.ReceiveBufferSize];
+ client.GetStream().Read(bytesFrom, 0, (int)client.ReceiveBufferSize);
+ String response = Encoding.ASCII.GetString(bytesFrom);
+ NetCommand net = NetCommand.Parse(response);
+ return net;
+ }
+
+ public static IPAddress GetIP(string ipstring)
+ {
+ IPAddress ip;
+
+ bool ipIsOk = IPAddress.TryParse(ipstring, out ip);
+ if (!ipIsOk) { return null; }
+
+ return ip;
+ }
+ }
+}
diff --git a/ErgometerLibrary/Properties/AssemblyInfo.cs b/ErgometerLibrary/Properties/AssemblyInfo.cs
index a1697ab..2865952 100644
--- a/ErgometerLibrary/Properties/AssemblyInfo.cs
+++ b/ErgometerLibrary/Properties/AssemblyInfo.cs
@@ -6,9 +6,9 @@ using System.Runtime.InteropServices;
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ErgometerLibrary")]
-[assembly: AssemblyDescription("")]
+[assembly: AssemblyDescription("Library with the most common methods and classes for the Ergometer")]
[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("")]
+[assembly: AssemblyCompany("Projectgroep A5")]
[assembly: AssemblyProduct("ErgometerLibrary")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
+[assembly: AssemblyVersion("1.0.2.0")]
+[assembly: AssemblyFileVersion("1.0.2.0")]
diff --git a/ErgometerLibrary/packages.config b/ErgometerLibrary/packages.config
new file mode 100644
index 0000000..64b5d8e
--- /dev/null
+++ b/ErgometerLibrary/packages.config
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file