This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMozzie.cpp
312 lines (184 loc) · 6.16 KB
/
Mozzie.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Mozzie - An MQTT plugin for the X-Plane flight simulator.
Copyright (C) 2017 Ben Russell - br@x-plugins.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Mozzie.h"
#include "XPDrefsFile.h"
#include <iostream>
#include <XPLMUtilities.h>
#include <mosquittopp.h>
#include <cstring>
// Heavily influenced by; http://wiki.neuromeka.net/index.php?title=Implementing_MQTT_Client_using_C%2B%2B_with_libmosquitto#MQTT_Wrapper
void Mozzie::_setup_published_datarefs() {
XPDrefsFile drefs_publish = XPDrefsFile( "mozzie_publish.txt" );
size_t dref_count = drefs_publish.svDatarefNames.size();
for( size_t dx=0; dx < dref_count ; dx++ ){
dref_details dref = drefs_publish.svDatarefNames[dx];
std::string sMsg = " publish:(" + dref.name + ")(" + dref.type + ")\r\n";
Mozzie::debug( sMsg );
_datarefs.push_back( new XPDref( dref.name ) ); //FIXME: does not account for data type or array index selection
}
}
void Mozzie::_setup_subscribed_datarefs() {
}
Mozzie::Mozzie( const std::string &id ) : mosquittopp( id.c_str() ){
mosqpp::lib_init();
_setup_published_datarefs();
_setup_subscribed_datarefs();
}
Mozzie::~Mozzie() {
Mozzie::debug("Mozzie destructor..\n");
}
bool Mozzie::open( const std::string &host, int port) {
int keepalive = 120; //seconds
connect( host.c_str(), port, keepalive ); // Connect to MQTT broker.
}
void Mozzie::close(){
this->publish(
NULL, //mid
"sim/connected", //topic
2, //payload length
(void*)"0", //payload
0, //qos
0 //retain
);
this->loop();
disconnect();
}
void Mozzie::on_connect(int rc) {
char caDbg[1024];
snprintf( caDbg, 1024, "Connected. Code: %d\n", rc );
Mozzie::debug( caDbg );
if( 0 == rc ){
// Issue a connection notice over MQTT.
this->publish(
NULL, //mid
"sim/connected", //topic
2, //payload length
(void*)"1", //payload
0, //qos
0 //retain
);
} //if we connected..
} //::on_connect()
void Mozzie::on_message(const struct mosquitto_message *message) {
Mozzie::debug("::on_message..\n");
} //::on_message()
void Mozzie::on_subscribe(int mid, int quos_count, const int *granted_quos) {
Mozzie::debug("Subscription succeeded.\n");
} //::on_subscribe()
void Mozzie::xp_data_pump() {
// Iterate over a list of exported datarefs.
for (std::vector<XPDref*>::iterator it = _datarefs.begin() ; it != _datarefs.end(); ++it){
XPDref* dref = *it;
// reads from x-plane and does compare with internal cache
dref->update();
// check to see if we need to update the MQTT server with a new value?
if( dref->_send_it ){
#if 1
//send as converted text
char caTmp[16];
snprintf( caTmp, 16, "%0.5f", dref->_val );
size_t payload_size = strlen( caTmp );
publish(
NULL, //mid - int
dref->_name.c_str(), //topic - char*
payload_size, //payload length - int
(void*)caTmp, //payload - void*
0, //qos - int: 0,1,2
false //retain - bool
);
#else
//send as binary
publish(
NULL, //mid - int
dref->_name.c_str(), //topic - char*
4, //payload length - int
(void*)&dref->_val, //payload - void*
0, //qos - int: 0,1,2
true //retain - bool
);
#endif
} //send it?
} //loop over all queued drefs
} //::data_pump()
// Static
void Mozzie::debug( const std::string msg ){
std::string sOutput = "Mozzie: " + msg;
#if 1
XPLMDebugString( sOutput.c_str() );
#else
printf( "%s", sOutput.c_str() );
#endif
} //::debug()
// X-Plane flight loop that handles MQTT processing.
// Static
float Mozzie::flcb(float inElapsedSinceLastCall,
float inElapsedTimeSinceLastFlightLoop,
int inCounter,
void *inRefcon) {
Mozzie* tmp_mozzie = reinterpret_cast<Mozzie*>(inRefcon);
tmp_mozzie->loop(); //mosquitto lib data pump
tmp_mozzie->xp_data_pump(); //x-plane io data pump
return -1.f; // Every frame.
}
void Mozzie::ipc_msg(
XPLMPluginID inFromWho,
int inMessage,
void * inParam
){
switch( inMessage ){
// Connection Params
case e_Mozzie_Set_Client_ID:
break;
case e_Mozzie_Set_Host:
break;
case e_Mozzie_Set_Port:
break;
// Connection Control
case e_Mozzie_Connect:
break;
case e_Mozzie_Disconnect:
this->disconnect();
break;
// Subscription Control
case e_Mozzie_Set_QOS:
break;
case e_Mozzie_Subscribe:
/*
this->subscribe(
mid,
sub name,
qos
);
*/
break;
case e_Mozzie_UnSubscribe:
break;
// Publication Control
case e_Mozzie_Publish:
break;
case e_Mozzie_UnPublish:
break;
// Message Control
case e_Mozzie_Set_Topic:
break;
case e_Mozzie_Set_Message:
break;
case e_Mozzie_Send:
break;
default:
break;
}
}
//eof