Skip to content

Commit

Permalink
* WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
runinrain64 committed Dec 9, 2023
1 parent 1c7bda9 commit 6b379af
Show file tree
Hide file tree
Showing 101 changed files with 37,591 additions and 20 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified NET2MQTT_console/.vs/NET2MQTT_console/v17/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
16 changes: 14 additions & 2 deletions NET2MQTT_console/NET2MQTT_console.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32811.315
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SnSYS_IoT", "..\SnSYS_IoT\SnSYS_IoT.csproj", "{E30F9234-2925-4FE4-925C-18762F77EA1E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SnSYS_IoT", "..\SnSYS_IoT\SnSYS_IoT.csproj", "{E30F9234-2925-4FE4-925C-18762F77EA1E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestApp", "..\UnitTestApp\UnitTestApp.csproj", "{2329D490-8209-4C9A-8541-46B0016FA2CD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestApp", "..\UnitTestApp\UnitTestApp.csproj", "{2329D490-8209-4C9A-8541-46B0016FA2CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetGent_V", "NetGent_V\NetGent_V.csproj", "{7B8C7DFC-A178-4582-B1F6-430379D8A44C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetGent_F", "NetGent_F\NetGent_F.csproj", "{2CEBAE23-57AF-41F8-AAAC-131E6E59B1EC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +25,14 @@ Global
{2329D490-8209-4C9A-8541-46B0016FA2CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2329D490-8209-4C9A-8541-46B0016FA2CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2329D490-8209-4C9A-8541-46B0016FA2CD}.Release|Any CPU.Build.0 = Release|Any CPU
{7B8C7DFC-A178-4582-B1F6-430379D8A44C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7B8C7DFC-A178-4582-B1F6-430379D8A44C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B8C7DFC-A178-4582-B1F6-430379D8A44C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B8C7DFC-A178-4582-B1F6-430379D8A44C}.Release|Any CPU.Build.0 = Release|Any CPU
{2CEBAE23-57AF-41F8-AAAC-131E6E59B1EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2CEBAE23-57AF-41F8-AAAC-131E6E59B1EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2CEBAE23-57AF-41F8-AAAC-131E6E59B1EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2CEBAE23-57AF-41F8-AAAC-131E6E59B1EC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
212 changes: 212 additions & 0 deletions NET2MQTT_console/NetGent_F/NetGent_F.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using SnSYS_IoT;

namespace NetGent_F
{
/// <summary>
/// Network Agent in Fleet Side
/// Provide TCP Socket Client. It will send connection request to "GITOS" when it get the trigger from "GITOS".
/// </summary>
internal class NetGent_F: IDisposable
{
const string AzureHostName = "airgazerIoT.azure-devices.net";
const string AzureEventHubEP = "sb://iothub-ns-airgazerio-24368083-52e3bbdbc1.servicebus.windows.net/";
const string AzureEventHubPath = "airgazeriot";

const string SASKey = "IoguqxF4OBoCUenJN1ZC5kOP2rnuo4lp9KeeyC49O64=";
const string SASKeyName = "iothubowner";

const string AzureDeviceConnectionString = "HostName=airgazerIoT.azure-devices.net;DeviceId=testVehicle01;SharedAccessKey=gHsfleh0PJvQ2B6TQyKgzHXx/0s5NnsXxULJzmNMAro=";

private Dictionary<string, Socket?> tcpClientList;
private IoTFleet fleetTwin;
private CancellationTokenSource? taskTokenSrc;
private bool isStarted;

public NetGent_F()
{
this.isStarted = false;
this.taskTokenSrc = null;
this.tcpClientList = new Dictionary<string, Socket?>();

var connectionString = string.Format($"HostName={AzureHostName};SharedAccessKeyName={SASKeyName};SharedAccessKey={SASKey}");

fleetTwin = new IoTFleet(connectionString, AzureEventHubEP, AzureEventHubPath, SASKey, SASKeyName);
fleetTwin.IoTMessageEvent += FleetTwin_IoTMessageEvent;
}

/// <summary>
/// Start Network Agent in Fleet Side
/// </summary>
public int Start()
{
int ret = 0;

if (this.isStarted == false)
{
if (fleetTwin != null)
{
ret = fleetTwin.Connect2Cloud();
this.isStarted = (ret == 1) ? true : false;

if (this.isStarted == true && this.taskTokenSrc == null)
{
this.taskTokenSrc = new CancellationTokenSource();
}
}
}

return ret;
}

/// <summary>
/// Stop Metwork Agent in Fleet Side
/// </summary>
public int Stop()
{
int ret = 0;

if (fleetTwin != null)
{
ret = fleetTwin.Disconnect2Cloud();
this.isStarted = false;
}

if (this.taskTokenSrc != null)
{
this.taskTokenSrc.Cancel();
}

foreach (var item in this.tcpClientList)
{
var socket = item.Value;
if (socket != null)
{
socket.Close(1000);
}
}

return ret;
}

/// <summary>
/// Add TCP Client into a client list.
/// </summary>
public int AddTcpClient(string IPaddress, int portNum)
{
int ret = 0;

if (string.IsNullOrEmpty(IPaddress) == false && this.isStarted == false)
{
var IPPort = IPaddress + ":" + portNum;
var serversocket = CreateTcpSocketClients(IPaddress, portNum);

ret = (tcpClientList.TryAdd(IPPort, serversocket) == false) ? -1 : 1;

if (ret == 1 && serversocket != null && this.taskTokenSrc != null)
{
Task.Factory.StartNew(() =>
{
Socket thisSocket = serversocket;
string thisIP = IPaddress;
int thisPort = portNum;
byte[] indata = new byte[1024];

while( true )
{
int nbytes = thisSocket.Receive(indata);

if (nbytes > 0)
{
int ret = SendNet2MqttMessage(thisIP, thisPort, indata, nbytes);
}
}
}, this.taskTokenSrc.Token);
}
}

return ret;
}


/// <summary>
/// Delete TCP Client from a client list
/// </summary>
public int DelTcpClient(string IPaddress, int portNum)
{
int ret = 0;

if (string.IsNullOrEmpty(IPaddress) == false)
{
var IPPort = IPaddress + ":" + portNum;

if (tcpClientList.TryGetValue(IPPort, out var serversocket) == true)
{
if (serversocket != null)
{
serversocket.Close(1000); // timeout = 1sec.
}
ret = (tcpClientList.Remove(IPPort) == false) ? -1 : 1;
}
}

return ret;
}

private Socket? CreateTcpSocketClients(string ip, int port)
{
Socket? clientSocket = null;

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ip), port);

if (ipep != null)
{
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

try
{
clientSocket.Connect(ipep);
}
catch (Exception ex)
{
Console.WriteLine("Exception: can't connect to the server");
clientSocket = null;
}
}

return clientSocket;
}

public void Dispose()
{
this.Stop();
if (this.taskTokenSrc != null)
{
this.taskTokenSrc.Dispose();
}
}

private int SendNet2MqttMessage(string IP, int portNum, byte[] payload, int ndata)
{
int ret = 0;

return ret;
}

void FleetTwin_IoTMessageEvent(object sender, IoTFleet.IoTMessageEventArgs e)
{
Console.WriteLine($"{e.DeviceID} sent {e.Message}");
// parsing the message
// get IP and Port, payload
// Does this number exist in a list.
// if yes, call its sender with payload.
}
}
}
14 changes: 14 additions & 0 deletions NET2MQTT_console/NetGent_F/NetGent_F.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\SnSYS_IoT\SnSYS_IoT.csproj" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions NET2MQTT_console/NetGent_F/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
Loading

0 comments on commit 6b379af

Please sign in to comment.