forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestPlugin.cs
142 lines (119 loc) · 4.49 KB
/
TestPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MissionPlanner.Plugin;
using MissionPlanner;
using System.Windows.Forms;
using System.IO;
using System.Security.Authentication;
using System.Threading.Tasks;
using MissionPlanner.Utilities;
using MQTTnet;
using MQTTnet.Client.Receiving;
using MQTTnet.Server;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class TestPlugin : Plugin
{
string _Name = "Test Plugin";
string _Version = "0.1";
string _Author = "Michael Oborne";
private TelstraUTM UTM;
private JArray UAVS;
private string clientid = "michael";
public override string Name
{
get { return _Name; }
}
public override string Version
{
get { return _Version; }
}
public override string Author
{
get { return _Author; }
}
public override bool Init()
{
loopratehz = 0.2f;
UTM = new TelstraUTM();
return true;
}
public override bool Loaded()
{
{
var mqttServer = new MqttFactory().CreateMqttServer();
var tlsoptions = new MqttServerTlsTcpEndpointOptions()
{
RemoteCertificateValidationCallback =
(sender, certificate, chain, errors) => { return true; },
SslProtocol = SslProtocols.Tls12,
CheckCertificateRevocation = false,
ClientCertificateRequired = true,
Certificate = new byte[0]
};
var options = new MqttServerOptionsBuilder()
//.WithEncryptedEndpointPort(1883)
//.WithEncryptionSslProtocol(SslProtocols.Tls12)
//.WithRemoteCertificateValidationCallback((sender, certificate, chain, errors) => { return true; })
.Build();
//options.TlsEndpointOptions.ClientCertificateRequired = true;
//options.TlsEndpointOptions.CheckCertificateRevocation = false;
mqttServer.StartAsync(options);
mqttServer.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
{
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
Console.WriteLine();
});
mqttServer.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(a =>
{
Console.WriteLine("### CLIENT CONNECTED ###");
Console.WriteLine(a.ClientId);
});
}
Task.Run(() => {
try
{
// UTM.confighardware("com8");
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.ForegroundColor = ConsoleColor.White;
});
//UTM.test();
/*
UTM.StartMQTT(clientid, @"C:\Users\michael\Desktop\Hex\laam-mqtt-control\Hardware\pi_zero\certs\ca.crt",
@"C:\Users\michael\Desktop\Hex\laam-mqtt-control\Hardware\pi_zero\certs\client.crt",
@"C:\Users\michael\Desktop\Hex\laam-mqtt-control\Hardware\pi_zero\certs\client.key");
*/
if (UTM.MQTTClient != null)
UTM.MQTTClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(e =>
{
Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###");
Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}");
Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}");
Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}");
Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}");
var json = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
var obj = JsonConvert.DeserializeObject<JObject>(json);
var item = obj?["telemetry"]?["altitude"];
});
return true;
}
public override bool Loop()
{
UTM.Telemetry(clientid);
return true;
}
public override bool Exit()
{
return true;
}
}