added Csharp project RobotLib

main
Jonas Arnold 4 years ago
parent c2c8f12a05
commit 03d739aca8
  1. 8
      ADIS_Csharp/ADIS_Csharp.sln
  2. 14
      ADIS_Csharp/RobotLib/BatteryEventArgs.cs
  3. 35
      ADIS_Csharp/RobotLib/ClassDiagram1.cd
  4. 87
      ADIS_Csharp/RobotLib/Com.cs
  5. 51
      ADIS_Csharp/RobotLib/DevBase.cs
  6. 59
      ADIS_Csharp/RobotLib/DevBattery.cs
  7. 18
      ADIS_Csharp/RobotLib/DevBuzzer.cs
  8. 19
      ADIS_Csharp/RobotLib/MessageEventArgs.cs
  9. 46
      ADIS_Csharp/RobotLib/Robot.cs
  10. 11
      ADIS_Csharp/RobotLib/RobotLib.csproj

@ -15,7 +15,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sync-Ueb04-NestedMonitor",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sync-Ueb05-LostSignals", "Sync-Ueb05-LostSignals\Sync-Ueb05-LostSignals.csproj", "{8605252D-7232-4E7A-B1E0-BF273947F3CB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MqttTest", "MqttTest\MqttTest.csproj", "{EB76DC93-79D0-4837-8A49-D825CDE4C6A3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MqttTest", "MqttTest\MqttTest.csproj", "{EB76DC93-79D0-4837-8A49-D825CDE4C6A3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RobotLib", "RobotLib\RobotLib.csproj", "{915E2889-F10D-4D02-8313-308F642EC64F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -51,6 +53,10 @@ Global
{EB76DC93-79D0-4837-8A49-D825CDE4C6A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EB76DC93-79D0-4837-8A49-D825CDE4C6A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EB76DC93-79D0-4837-8A49-D825CDE4C6A3}.Release|Any CPU.Build.0 = Release|Any CPU
{915E2889-F10D-4D02-8313-308F642EC64F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{915E2889-F10D-4D02-8313-308F642EC64F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{915E2889-F10D-4D02-8313-308F642EC64F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{915E2889-F10D-4D02-8313-308F642EC64F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -0,0 +1,14 @@
using System;
namespace RobotLib
{
public class BatteryEventArgs : EventArgs
{
public float Voltage { get; }
public BatteryEventArgs(float voltage)
{
Voltage = voltage;
}
}
}

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<ClassDiagram MajorVersion="1" MinorVersion="1">
<Class Name="RobotLib.Robot">
<Position X="0.5" Y="0.5" Width="2" />
<TypeIdentifier>
<HashCode>AAQAAAAAAAAAAAAAAAAAAIAAAAEAAAAAQAAAAAAAAAg=</HashCode>
<FileName>Robot.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="RobotLib.Com">
<Position X="3" Y="0.5" Width="2" />
<Compartments>
<Compartment Name="Fields" Collapsed="true" />
</Compartments>
<TypeIdentifier>
<HashCode>IAQAAAAAAAAAAAFAAAAACIgAAAAAAAAAAAAAAAAEAAI=</HashCode>
<FileName>Com.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="RobotLib.MessageEventArgs">
<Position X="8" Y="0.5" Width="2" />
<TypeIdentifier>
<HashCode>AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAA=</HashCode>
<FileName>MessageEventArgs.cs</FileName>
</TypeIdentifier>
</Class>
<Class Name="RobotLib.DevBase">
<Position X="5.5" Y="0.5" Width="2" />
<TypeIdentifier>
<HashCode>AAAAAAAAAABAAAAAACAgCAgAAAEAAQAAAAAAAAAAAAA=</HashCode>
<FileName>DevBase.cs</FileName>
</TypeIdentifier>
</Class>
<Font Name="Segoe UI" Size="9" />
</ClassDiagram>

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace RobotLib
{
public class Com
{
private static readonly NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
private UdpClient udpClient;
private Thread thread;
public event EventHandler<MessageEventArgs> MessageReveived;
public Com()
{
}
public Com(string host, int port)
{
Connect(host, port);
}
public bool IsConnected { get; private set; }
public void Connect(string host, int port)
{
if (!IsConnected)
{
udpClient = new UdpClient();
udpClient.Connect(host, port);
IsConnected = true;
thread = new Thread(Run);
thread.IsBackground = true;
thread.Start();
}
}
public void Disconnect()
{
IsConnected = false;
if (thread != null)
{
thread.Interrupt();
thread.Join();
}
}
public void Run()
{
while(IsConnected)
{
try
{
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);
byte[] dataReceived = udpClient.Receive(ref remote);
string msg = Encoding.ASCII.GetString(dataReceived);
log.Trace($"Msg received from {remote}: {msg}");
MessageReveived?.Invoke(this, new MessageEventArgs(msg));
}
catch (ThreadInterruptedException)
{
return;
}
catch (Exception ex)
{
log.Fatal(ex, "Exception in Receiver-Thread\r\n" + ex);
}
}
}
public void SendMsg(string msg)
{
byte[] data = Encoding.ASCII.GetBytes(msg);
udpClient.Send(data, data.Length);
log.Trace($"Msg sent to {udpClient.Client.RemoteEndPoint}: {msg}");
}
}
}

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RobotLib
{
public abstract class DevBase
{
protected NLog.Logger log { get; private set; }
public DevBase(Com com) : this(com, string.Empty) { }
public DevBase(Com com, string keyword)
{
log = NLog.LogManager.GetLogger(GetType().ToString());
Keyword = keyword;
Com = com;
com.MessageReveived += MessageReveived;
}
protected string Keyword { get; }
protected Com Com { get; }
protected virtual void MessageReveived(object sender, MessageEventArgs e)
{
if (e.Message.StartsWith(Keyword))
{
ParseMessage(e.Message);
}
}
protected void SendMessage(string message)
{
if (Com.IsConnected)
{
log.Trace("Esp32> " + message);
Com.SendMsg(message);
}
else
{
log.Warn("Not connected! Could not send message: " + message);
}
}
protected virtual void ParseMessage(string message) { }
public virtual void Refresh() { }
}
}

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RobotLib
{
public class DevBattery : DevBase
{
private float voltage;
public event EventHandler<BatteryEventArgs> BatteryChanged;
public float Voltage
{
get { return voltage; }
private set
{
// value changed?
if(voltage != value)
{
// set new voltage and raise event
voltage = value;
BatteryChanged?.Invoke(this, new BatteryEventArgs(voltage));
}
}
}
public DevBattery(Com com) : base(com, "battery") { }
public override void Refresh()
{
SendMessage("get battery status!");
}
protected override void ParseMessage(string message)
{
// example message = "Battery: 1.25 V"
var keyValue = message.Trim().Split(':');
string key = keyValue[0].Trim();
string value = keyValue[1].Trim();
switch (key)
{
case "battery":
value = value.Trim(' ', 'V');
this.Voltage = float.Parse(value);
break;
default:
log.Warn($"Unkown element {key}: {value} in message {message}");
break;
}
}
}
}

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RobotLib
{
public class DevBuzzer : DevBase
{
public DevBuzzer(Com com) : base(com) { }
public void Beep(int freq, int timeMs)
{
SendMessage($"robo sendcmd buzzer buzz {freq} {timeMs}!");
}
}
}

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RobotLib
{
public class MessageEventArgs : EventArgs
{
public MessageEventArgs(string msg)
{
Message = msg;
}
public string Message { get; }
}
}

@ -0,0 +1,46 @@
using System.Threading;
namespace RobotLib
{
public class Robot
{
public static Robot Instance { get; } = new Robot();
private Robot()
{
Com = new Com();
Buzzer = new DevBuzzer(Com);
Battery = new DevBattery(Com);
Timer timer = new Timer(TimerCallback);
timer.Change(2000, 10000);
}
public Com Com { get; }
public DevBuzzer Buzzer { get; }
public DevBattery Battery { get; }
public void Connect(string host, int port)
{
Com.Connect(host, port);
}
public void Disconnect()
{
Com.Disconnect();
}
private void TimerCallback(object state)
{
if (Com.IsConnected)
{
Battery.Refresh();
}
}
}
}

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="5.1.0" />
</ItemGroup>
</Project>
Loading…
Cancel
Save