-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c7bda9
commit 6b379af
Showing
101 changed files
with
37,591 additions
and
20 deletions.
There are no files selected for viewing
Binary file modified
BIN
+16.2 KB
(120%)
NET2MQTT_console/.vs/NET2MQTT_console/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file removed
BIN
-21 KB
..._console/.vs/NET2MQTT_console/FileContentIndex/2b90248c-c823-46bb-a367-8514abf3dce4.vsidx
Binary file not shown.
Binary file removed
BIN
-17.7 KB
..._console/.vs/NET2MQTT_console/FileContentIndex/733cecb2-6c13-4297-8c03-32c3f56db913.vsidx
Binary file not shown.
Binary file added
BIN
+33 KB
..._console/.vs/NET2MQTT_console/FileContentIndex/94433112-267d-4ffa-9aca-1701a79e94fc.vsidx
Binary file not shown.
Binary file added
BIN
+30.1 KB
..._console/.vs/NET2MQTT_console/FileContentIndex/94bd170e-baa3-4517-8cf0-0feaa8750a83.vsidx
Binary file not shown.
Binary file removed
BIN
-17.2 KB
..._console/.vs/NET2MQTT_console/FileContentIndex/d90aa042-ad9a-41e0-a592-65bc222c4aad.vsidx
Binary file not shown.
Binary file added
BIN
+29.2 KB
..._console/.vs/NET2MQTT_console/FileContentIndex/e034dce8-337d-40ad-b42b-c0d412e788ce.vsidx
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+17.8 KB
(110%)
NET2MQTT_console/.vs/ProjectEvaluation/net2mqtt_console.metadata.v5.1
Binary file not shown.
Binary file modified
BIN
-327 KB
(50%)
NET2MQTT_console/.vs/ProjectEvaluation/net2mqtt_console.projects.v5.1
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); |
Oops, something went wrong.