Merge pull request #1 from KettlerX7AvansA5/develop
Merge develop into master
This commit is contained in:
@@ -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}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>cc20ff4e-8751-42a1-a32b-0e5b22452cac</ProjectGuid>
|
||||
<ProjectGuid>{CC20FF4E-8751-42A1-A32B-0E5B22452CAC}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>ErgometerLibrary</RootNamespace>
|
||||
@@ -30,25 +30,32 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
|
||||
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
|
||||
<Reference Include="System.Data" />
|
||||
|
||||
<Reference Include="System.Net.Http" />
|
||||
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Class1.cs" />
|
||||
<Compile Include="ChatMessage.cs" />
|
||||
<Compile Include="Helper.cs" />
|
||||
<Compile Include="NetCommand.cs" />
|
||||
<Compile Include="ComPort.cs" />
|
||||
<Compile Include="FileHandler.cs" />
|
||||
<Compile Include="Meting.cs" />
|
||||
<Compile Include="NetHelper.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
@@ -57,5 +64,4 @@
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
|
||||
</Project>
|
||||
@@ -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<Meting> 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<ChatMessage> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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; } }
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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")]
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user