forked from AgOpenGPS-Official/AOG-TaskController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_controller.cpp
More file actions
692 lines (624 loc) · 27 KB
/
task_controller.cpp
File metadata and controls
692 lines (624 loc) · 27 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/**
* @author Daan Steenbergen
* @brief An ISOBUS Task Controller for AgOpenGPS
* @version 0.1
* @date 2025-1-20
*
* @copyright 2025 Daan Steenbergen
*/
#include "task_controller.hpp"
#include "settings.hpp"
#include "isobus/isobus/isobus_device_descriptor_object_pool_helpers.hpp"
#include "isobus/isobus/isobus_task_controller_server.hpp"
#include <bitset>
#include <fstream>
#include <iostream>
void ClientState::set_number_of_sections(std::uint8_t number)
{
numberOfSections = number;
sectionSetpointStates.resize(number);
sectionActualStates.resize(number);
sectionToElementNumber.resize(number, 0); // Initialize all sections mapped to element 0 by default
}
void ClientState::set_section_setpoint_state(std::uint8_t section, std::uint8_t state)
{
if (section < numberOfSections)
{
sectionSetpointStates[section] = state;
}
}
void ClientState::set_section_actual_state(std::uint8_t section, std::uint8_t state)
{
if (section < numberOfSections)
{
sectionActualStates[section] = state;
}
}
std::uint16_t ClientState::get_element_number_for_section(std::uint8_t section) const
{
if (section < numberOfSections && section < sectionToElementNumber.size())
{
return sectionToElementNumber[section];
}
return 0;
}
void ClientState::set_element_number_for_section(std::uint8_t section, std::uint16_t elementNumber)
{
if (section < numberOfSections && section < sectionToElementNumber.size())
{
sectionToElementNumber[section] = elementNumber;
}
}
std::uint8_t ClientState::get_number_of_sections() const
{
return numberOfSections;
}
std::uint8_t ClientState::get_section_setpoint_state(std::uint8_t section) const
{
if (section < numberOfSections)
{
return sectionSetpointStates[section];
}
return SectionState::NOT_INSTALLED;
}
std::uint8_t ClientState::get_section_actual_state(std::uint8_t section) const
{
if (section < numberOfSections)
{
// Check if the element or any parent is off
std::uint16_t elementNumber = get_element_number_for_section(section);
if (is_element_or_parent_off(elementNumber))
{
return SectionState::OFF;
}
return sectionActualStates[section];
}
return SectionState::NOT_INSTALLED;
}
bool ClientState::is_any_section_setpoint_on() const
{
for (std::uint8_t state : sectionSetpointStates)
{
if (state == SectionState::ON)
{
return true;
}
}
return false;
}
bool ClientState::get_setpoint_work_state() const
{
return setpointWorkState;
}
void ClientState::set_setpoint_work_state(bool state)
{
setpointWorkState = state;
}
bool ClientState::get_actual_work_state() const
{
return actualWorkState;
}
void ClientState::set_actual_work_state(bool state)
{
actualWorkState = state;
}
bool ClientState::is_section_control_enabled() const
{
return isSectionControlEnabled;
}
void ClientState::set_section_control_enabled(bool state)
{
isSectionControlEnabled = state;
}
isobus::DeviceDescriptorObjectPool &ClientState::get_pool()
{
return pool;
}
bool ClientState::are_measurement_commands_sent() const
{
return areMeasurementCommandsSent;
}
void ClientState::mark_measurement_commands_sent()
{
areMeasurementCommandsSent = true;
}
std::uint16_t ClientState::get_element_number_for_ddi(isobus::DataDescriptionIndex ddi) const
{
auto it = ddiToElementNumber.find(ddi);
if (it != ddiToElementNumber.end())
{
return it->second;
}
std::cout << "Cached element number not found for DDI " << static_cast<int>(ddi) << std::endl;
return 0;
}
void ClientState::set_element_number_for_ddi(isobus::DataDescriptionIndex ddi, std::uint16_t elementNumber)
{
ddiToElementNumber[ddi] = elementNumber;
}
bool ClientState::has_element_number_for_ddi(isobus::DataDescriptionIndex ddi) const
{
return ddiToElementNumber.find(ddi) != ddiToElementNumber.end();
}
bool ClientState::is_element_or_parent_off(std::uint16_t elementNumber) const
{
// Check if this element is off
bool elementWorkState;
if (try_get_element_work_state(elementNumber, elementWorkState) && !elementWorkState)
{
return true; // Element is off
}
// Find the parent element(s) by searching through the pool
// Use const_cast to access non-const methods on the pool from a const context
auto &nonConstPool = const_cast<isobus::DeviceDescriptorObjectPool &>(pool);
for (std::uint32_t i = 0; i < nonConstPool.size(); i++)
{
auto object = nonConstPool.get_object_by_index(i);
if (object->get_object_type() == isobus::task_controller_object::ObjectTypes::DeviceElement)
{
auto elementObject = std::dynamic_pointer_cast<isobus::task_controller_object::DeviceElementObject>(object);
// Check if this element has elementNumber as a child
for (std::uint16_t childId : elementObject->get_child_object_ids())
{
// Find the child object
for (std::uint32_t j = 0; j < nonConstPool.size(); j++)
{
auto childObject = nonConstPool.get_object_by_index(j);
if (childObject && childObject->get_object_id() == childId)
{
if (childObject->get_object_type() == isobus::task_controller_object::ObjectTypes::DeviceElement)
{
auto childElementObject = std::dynamic_pointer_cast<isobus::task_controller_object::DeviceElementObject>(childObject);
if (childElementObject && childElementObject->get_element_number() == elementNumber)
{
// Found the parent, recursively check if parent or its parents are off
return is_element_or_parent_off(elementObject->get_element_number());
}
}
}
}
}
}
}
return false; // No parent found or no parents are off
}
void ClientState::set_element_work_state(std::uint16_t elementNumber, bool isWorking)
{
elementWorkStates[elementNumber] = isWorking;
}
bool ClientState::try_get_element_work_state(std::uint16_t elementNumber, bool &isWorking) const
{
auto it = elementWorkStates.find(elementNumber);
if (it != elementWorkStates.end())
{
isWorking = it->second;
return true;
}
return false;
}
MyTCServer::MyTCServer(std::shared_ptr<isobus::InternalControlFunction> internalControlFunction) :
TaskControllerServer(internalControlFunction,
1, // AOG limits to 1 boom
64, // AOG limits to 16 sections of unique width but can be 64 by using zones
64, // 64 channels for position based control
isobus::TaskControllerOptions()
.with_implement_section_control(), // We support section control
TaskControllerVersion::SecondEditionDraft)
{
}
bool MyTCServer::activate_object_pool(std::shared_ptr<isobus::ControlFunction> partnerCF, ObjectPoolActivationError &, ObjectPoolErrorCodes &, std::uint16_t &, std::uint16_t &)
{
// Safety check to make sure partnerCF has uploaded a DDOP
if (uploadedPools.find(partnerCF) == uploadedPools.end())
{
return false;
}
// Initialize a new client state
auto state = ClientState();
// state.get_pool().set_task_controller_compatibility_level(get_active_client(partnerCF)->reportedVersion);
state.get_pool().set_task_controller_compatibility_level(static_cast<std::uint8_t>(TaskControllerVersion::SecondEditionDraft));
bool deserialized = false;
while (!uploadedPools[partnerCF].empty())
{
auto binaryPool = uploadedPools[partnerCF].front();
uploadedPools[partnerCF].pop();
deserialized = state.get_pool().deserialize_binary_object_pool(binaryPool.data(), static_cast<std::uint32_t>(binaryPool.size()), partnerCF->get_NAME());
}
if (deserialized)
{
std::cout << "Successfully deserialized device descriptor object pool." << std::endl;
// Save to NVM
std::shared_ptr<isobus::task_controller_object::DeviceObject> deviceObject;
for (std::uint16_t i = 0; i < state.get_pool().size(); i++)
{
auto object = state.get_pool().get_object_by_index(i);
if (object->get_object_type() == isobus::task_controller_object::ObjectTypes::Device)
{
deviceObject = std::static_pointer_cast<isobus::task_controller_object::DeviceObject>(object);
break;
}
}
auto labelBytes = deviceObject->get_localization_label();
std::string label(reinterpret_cast<const char *>(labelBytes.data()), labelBytes.size());
// trim at first occurrence of null or ETX (0x03)
auto it = std::find_if(label.begin(), label.end(), [](char c) { return c == '\0' || static_cast<unsigned char>(c) == 0x03; });
label.erase(it, label.end());
auto fileName = std::to_string(partnerCF->get_NAME().get_full_name()) + "\\" + label + ".ddop";
std::vector<std::uint8_t> binaryPool;
if (state.get_pool().generate_binary_object_pool(binaryPool))
{
std::ofstream outFile(Settings::get_filename_path(fileName), std::ios::binary);
if (outFile.is_open())
{
outFile.write(reinterpret_cast<const char *>(binaryPool.data()), binaryPool.size());
outFile.close();
std::cout << "Saved DDOP to file: " << fileName << std::endl;
}
else
{
std::cout << "Unable to save DDOP to NVM. (Failed to open file) file: " << fileName << std::endl;
}
}
else
{
std::cout << "Unable to save DDOP to NVM. (Failed to generate binary object pool)" << std::endl;
}
auto implement = isobus::DeviceDescriptorObjectPoolHelper::get_implement_geometry(state.get_pool());
std::uint8_t numberOfSections = 0;
std::cout << "Implement geometry: " << std::endl;
std::cout << "Number of booms=" << implement.booms.size() << std::endl;
for (const auto &boom : implement.booms)
{
std::cout << "Boom: id=" << static_cast<int>(boom.elementNumber) << std::endl;
for (const auto &subBoom : boom.subBooms)
{
std::cout << "SubBoom: id=" << static_cast<int>(subBoom.elementNumber) << std::endl;
for (const auto §ion : subBoom.sections)
{
numberOfSections++;
std::cout << "Section: id=" << static_cast<int>(section.elementNumber) << std::endl;
std::cout << "X Offset: " << section.xOffset_mm.get() << std::endl;
std::cout << "Y Offset: " << section.yOffset_mm.get() << std::endl;
std::cout << "Z Offset: " << section.zOffset_mm.get() << std::endl;
std::cout << "Width: " << section.width_mm.get() << std::endl;
}
}
for (const auto §ion : boom.sections)
{
numberOfSections++;
std::cout << "Section: id=" << static_cast<int>(section.elementNumber) << std::endl;
std::cout << "X Offset: " << section.xOffset_mm.get() << std::endl;
std::cout << "Y Offset: " << section.yOffset_mm.get() << std::endl;
std::cout << "Z Offset: " << section.zOffset_mm.get() << std::endl;
std::cout << "Width: " << section.width_mm.get() << std::endl;
}
}
state.set_number_of_sections(numberOfSections);
}
else
{
std::cout << "Failed to deserialize device descriptor object pool." << std::endl;
return false;
}
clients[partnerCF] = state;
return true;
}
bool MyTCServer::change_designator(std::shared_ptr<isobus::ControlFunction>, std::uint16_t, const std::vector<std::uint8_t> &)
{
return true;
}
bool MyTCServer::deactivate_object_pool(std::shared_ptr<isobus::ControlFunction> partnerCF)
{
clients.erase(partnerCF);
uploadedPools.erase(partnerCF);
return true;
}
bool MyTCServer::delete_device_descriptor_object_pool(std::shared_ptr<isobus::ControlFunction> partnerCF, ObjectPoolDeletionErrors &)
{
clients.erase(partnerCF);
uploadedPools.erase(partnerCF);
return true;
}
bool MyTCServer::get_is_stored_device_descriptor_object_pool_by_structure_label(std::shared_ptr<isobus::ControlFunction>, const std::vector<std::uint8_t> &, const std::vector<std::uint8_t> &)
{
return false;
}
bool MyTCServer::get_is_stored_device_descriptor_object_pool_by_localization_label(std::shared_ptr<isobus::ControlFunction>, const std::array<std::uint8_t, 7> &)
{
return false;
}
bool MyTCServer::get_is_enough_memory_available(std::uint32_t)
{
return true;
}
void MyTCServer::identify_task_controller(std::uint8_t)
{
// When this is called, the TC is supposed to display its TC number for 3 seconds if possible (which is passed into this function).
// Your TC's number is your function code + 1, in the range of 1-32.
}
void MyTCServer::on_client_timeout(std::shared_ptr<isobus::ControlFunction> partner)
{
// Cleanup the client state
clients.erase(partner);
}
void MyTCServer::on_process_data_acknowledge(std::shared_ptr<isobus::ControlFunction> partner,
std::uint16_t dataDescriptionIndex,
std::uint16_t elementNumber,
std::uint8_t errorCodesFromClient,
ProcessDataCommands processDataCommand)
{
// This callback lets you know when a client sends a process data acknowledge (PDACK) message to you
std::cout << "Received process data acknowledge from client " << int(partner->get_address()) << " for DDI " << dataDescriptionIndex << " element " << elementNumber << " with error codes " << std::bitset<8>(errorCodesFromClient) << " and command " << static_cast<int>(processDataCommand) << std::endl;
}
bool MyTCServer::on_value_command(std::shared_ptr<isobus::ControlFunction> partner,
std::uint16_t dataDescriptionIndex,
std::uint16_t elementNumber,
std::int32_t processDataValue,
std::uint8_t &errorCodes)
{
switch (dataDescriptionIndex)
{
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState1_16):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState17_32):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState33_48):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState49_64):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState65_80):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState81_96):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState97_112):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState113_128):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState129_144):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState145_160):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState161_176):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState177_192):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState193_208):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState209_224):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState225_240):
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState241_256):
{
std::uint8_t sectionIndexOffset = NUMBER_SECTIONS_PER_CONDENSED_MESSAGE * static_cast<std::uint8_t>(dataDescriptionIndex - static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState1_16));
for (std::uint_fast8_t i = 0; i < NUMBER_SECTIONS_PER_CONDENSED_MESSAGE; i++)
{
std::uint8_t sectionState = ((processDataValue >> (2 * i)) & 0x03);
clients[partner].set_section_actual_state(i + sectionIndexOffset, sectionState);
clients[partner].set_element_number_for_section(i + sectionIndexOffset, elementNumber);
}
}
break;
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::SectionControlState):
{
clients[partner].set_section_control_enabled(processDataValue == 1);
}
break;
case static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualWorkState):
{
// Store the work state per element rather than globally
clients[partner].set_element_work_state(elementNumber, processDataValue == 1);
}
}
return true;
}
bool MyTCServer::store_device_descriptor_object_pool(std::shared_ptr<isobus::ControlFunction> partnerCF, const std::vector<std::uint8_t> &binaryPool, bool appendToPool)
{
if (uploadedPools.find(partnerCF) == uploadedPools.end())
{
uploadedPools[partnerCF] = std::queue<std::vector<std::uint8_t>>();
}
uploadedPools[partnerCF].push(binaryPool);
return true;
}
std::map<std::shared_ptr<isobus::ControlFunction>, ClientState> &MyTCServer::get_clients()
{
return clients;
}
void MyTCServer::request_measurement_commands()
{
for (auto &client : clients)
{
if (!client.second.are_measurement_commands_sent())
{
// Find all actual (condensed) work state DDIs and request them to trigger "On Change" and "Time Interval"
for (std::uint32_t i = 0; i < client.second.get_pool().size(); i++)
{
auto object = client.second.get_pool().get_object_by_index(i);
if (object->get_object_type() == isobus::task_controller_object::ObjectTypes::DeviceProcessData)
{
auto processDataObject = std::dynamic_pointer_cast<isobus::task_controller_object::DeviceProcessDataObject>(object);
if (processDataObject->get_ddi() == static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualWorkState) ||
(processDataObject->get_ddi() >= static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState1_16) &&
processDataObject->get_ddi() <= static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState241_256)) ||
(processDataObject->get_ddi() >= static_cast<std::uint16_t>(isobus::DataDescriptionIndex::CondensedSectionOverrideState1_16) &&
processDataObject->get_ddi() <= static_cast<std::uint16_t>(isobus::DataDescriptionIndex::CondensedSectionOverrideState241_256)))
{
// Loop over all objects to find the elements that are the parents of the actual condensed work state objects
for (std::uint32_t j = 0; j < client.second.get_pool().size(); j++)
{
auto parentObject = client.second.get_pool().get_object_by_index(j);
if (parentObject->get_object_type() == isobus::task_controller_object::ObjectTypes::DeviceElement)
{
auto elementObject = std::dynamic_pointer_cast<isobus::task_controller_object::DeviceElementObject>(parentObject);
for (std::uint16_t elementObjectChild : elementObject->get_child_object_ids())
{
if (elementObjectChild == processDataObject->get_object_id())
{
// TODO: This is a bit of a hack, but it works for now
client.second.set_element_number_for_ddi(static_cast<isobus::DataDescriptionIndex>(processDataObject->get_ddi()), elementObject->get_element_number());
const auto &entryB = isobus::DataDictionary::get_entry(processDataObject->get_ddi());
std::cout << "Mapped DDI " << processDataObject->get_ddi() << " (" << entryB.to_string() << ") to element "
<< elementObject->get_element_number() << std::endl;
if (processDataObject->has_trigger_method(isobus::task_controller_object::DeviceProcessDataObject::AvailableTriggerMethods::OnChange))
{
send_change_threshold_measurement_command(client.first, processDataObject->get_ddi(), elementObject->get_element_number(), 1);
std::cout << "Subscribed (OnChange) to DDI " << processDataObject->get_ddi() << " (" << entryB.to_string() << ") for element "
<< elementObject->get_element_number() << std::endl;
}
if (processDataObject->has_trigger_method(isobus::task_controller_object::DeviceProcessDataObject::AvailableTriggerMethods::TimeInterval))
{
send_time_interval_measurement_command(client.first, processDataObject->get_ddi(), elementObject->get_element_number(), 1000);
}
}
}
}
}
}
}
}
// Find all section control state DDIs and request them to trigger "On Change"
for (std::uint32_t i = 0; i < client.second.get_pool().size(); i++)
{
auto object = client.second.get_pool().get_object_by_index(i);
if (object->get_object_type() == isobus::task_controller_object::ObjectTypes::DeviceProcessData)
{
auto processDataObject = std::dynamic_pointer_cast<isobus::task_controller_object::DeviceProcessDataObject>(object);
if (processDataObject->get_ddi() == static_cast<std::uint16_t>(isobus::DataDescriptionIndex::SectionControlState) ||
processDataObject->get_ddi() == static_cast<std::uint16_t>(isobus::DataDescriptionIndex::SetpointWorkState) ||
(processDataObject->get_ddi() >= static_cast<std::uint16_t>(isobus::DataDescriptionIndex::SetpointCondensedWorkState1_16) &&
processDataObject->get_ddi() <= static_cast<std::uint16_t>(isobus::DataDescriptionIndex::SetpointCondensedWorkState241_256)))
{
// Loop over all objects to find the elements that are the parents of the section control state objects
for (std::uint32_t j = 0; j < client.second.get_pool().size(); j++)
{
auto parentObject = client.second.get_pool().get_object_by_index(j);
if (parentObject->get_object_type() == isobus::task_controller_object::ObjectTypes::DeviceElement)
{
auto elementObject = std::dynamic_pointer_cast<isobus::task_controller_object::DeviceElementObject>(parentObject);
for (std::uint16_t elementObjectChild : elementObject->get_child_object_ids())
{
if (elementObjectChild == processDataObject->get_object_id())
{
// TODO: This is a bit of a hack, but it works for now
client.second.set_element_number_for_ddi(static_cast<isobus::DataDescriptionIndex>(processDataObject->get_ddi()), elementObject->get_element_number());
const auto &entryB = isobus::DataDictionary::get_entry(processDataObject->get_ddi());
if (processDataObject->has_trigger_method(isobus::task_controller_object::DeviceProcessDataObject::AvailableTriggerMethods::OnChange))
{
send_change_threshold_measurement_command(client.first, processDataObject->get_ddi(), elementObject->get_element_number(), 1);
std::cout << "Subscribed (OnChange) to DDI " << processDataObject->get_ddi() << " (" << entryB.to_string() << ") for element "
<< elementObject->get_element_number() << std::endl;
}
else
{
std::cout << "Mapped (no OnChange) DDI " << processDataObject->get_ddi() << " (" << entryB.to_string() << ") to element "
<< elementObject->get_element_number() << std::endl;
}
}
}
}
}
}
}
}
std::cout << "Measurement commands sent." << std::endl;
client.second.mark_measurement_commands_sent();
}
}
}
void MyTCServer::update_section_states(std::vector<bool> §ionStates)
{
for (auto &client : clients)
{
auto &state = client.second;
if (!state.is_section_control_enabled())
{
// According to standard, the section setpoint states should only be sent when in auto mode
return;
}
bool requiresUpdate = false;
for (std::uint8_t i = 0; i < state.get_number_of_sections(); i++)
{
if ((i % NUMBER_SECTIONS_PER_CONDENSED_MESSAGE == 0) && requiresUpdate)
{
// Send the previous 16 sections
std::uint8_t ddiOffset = (i / NUMBER_SECTIONS_PER_CONDENSED_MESSAGE) - 1;
send_section_setpoint_states(client.first, ddiOffset);
requiresUpdate = false;
}
if (i < sectionStates.size())
{
if (sectionStates[i] != (state.get_section_setpoint_state(i) == SectionState::ON))
{
state.set_section_setpoint_state(i, sectionStates[i] ? SectionState::ON : SectionState::OFF);
requiresUpdate = true;
}
}
}
if (requiresUpdate)
{
std::uint8_t ddiOffset = (state.get_number_of_sections() - 1) / NUMBER_SECTIONS_PER_CONDENSED_MESSAGE;
send_section_setpoint_states(client.first, ddiOffset);
}
}
}
void MyTCServer::update_section_control_enabled(bool enabled)
{
for (auto &client : clients)
{
if (client.second.is_section_control_enabled() != enabled)
{
client.second.set_section_control_enabled(enabled);
send_section_control_state(client.first, enabled);
}
}
}
void MyTCServer::send_section_setpoint_states(std::shared_ptr<isobus::ControlFunction> client, std::uint8_t ddiOffset)
{
std::uint8_t sectionOffset = ddiOffset * NUMBER_SECTIONS_PER_CONDENSED_MESSAGE;
std::uint32_t value = 0;
for (std::uint8_t i = 0; i < NUMBER_SECTIONS_PER_CONDENSED_MESSAGE; i++)
{
value |= (clients[client].get_section_setpoint_state(sectionOffset + i) << (2 * i));
}
// Modern ECU? (DDI 290 SetpointCondensedWorkState1_16 exists)
std::uint16_t ddiTarget = static_cast<std::uint16_t>(isobus::DataDescriptionIndex::SetpointCondensedWorkState1_16) + ddiOffset;
// Legacy ECU? (DDI 161 ActualCondensedWorkState1_16 exists and Settable)
std::uint16_t ddiTargetLegacy = static_cast<std::uint16_t>(isobus::DataDescriptionIndex::ActualCondensedWorkState1_16) + ddiOffset;
if (clients[client].has_element_number_for_ddi(static_cast<isobus::DataDescriptionIndex>(ddiTarget)))
{
std::uint16_t elementNumber = clients[client].get_element_number_for_ddi(static_cast<isobus::DataDescriptionIndex>(ddiTarget));
send_set_value(client, ddiTarget, elementNumber, value);
bool setpointWorkState = clients[client].is_any_section_setpoint_on();
if ((clients[client].get_setpoint_work_state() != setpointWorkState) && clients[client].has_element_number_for_ddi(isobus::DataDescriptionIndex::SetpointWorkState))
{
send_set_value(client, static_cast<std::uint16_t>(isobus::DataDescriptionIndex::SetpointWorkState), clients[client].get_element_number_for_ddi(isobus::DataDescriptionIndex::SetpointWorkState), setpointWorkState ? 1 : 0);
clients[client].set_setpoint_work_state(setpointWorkState);
}
else if (!clients[client].has_element_number_for_ddi(isobus::DataDescriptionIndex::SetpointWorkState))
{
std::cout << "[TC Server] DDI 289 (SetpointWorkState) not available!" << std::endl;
}
}
else if (clients[client].has_element_number_for_ddi(static_cast<isobus::DataDescriptionIndex>(ddiTargetLegacy)))
{
if (is_ddi_settable(client, ddiTargetLegacy))
{
send_set_value(client, ddiTargetLegacy, clients[client].get_element_number_for_ddi(static_cast<isobus::DataDescriptionIndex>(ddiTargetLegacy)), value);
}
else
{
std::cout << "[TC Server] Legacy DDI " << ddiTargetLegacy << " (ActualCondensedWorkState) is not settable!" << std::endl;
}
return;
}
else
{
std::cout << "[TC Server] Neither condensed nor controllable-actual work state supported Missing DDI 290 and 141!" << std::endl;
}
}
void MyTCServer::send_section_control_state(std::shared_ptr<isobus::ControlFunction> client, bool enabled)
{
send_set_value(client, static_cast<std::uint16_t>(isobus::DataDescriptionIndex::SectionControlState), clients[client].get_element_number_for_ddi(isobus::DataDescriptionIndex::SectionControlState), enabled ? 1 : 0);
}
bool MyTCServer::is_ddi_settable(std::shared_ptr<isobus::ControlFunction> client, std::uint16_t ddi)
{
for (std::uint32_t i = 0; i < clients[client].get_pool().size(); i++)
{
auto object = clients[client].get_pool().get_object_by_index(i);
if (object->get_object_type() == isobus::task_controller_object::ObjectTypes::DeviceProcessData)
{
auto processDataObject = std::dynamic_pointer_cast<isobus::task_controller_object::DeviceProcessDataObject>(object);
if (processDataObject->get_ddi() == ddi)
{
return processDataObject->has_property(isobus::task_controller_object::DeviceProcessDataObject::PropertiesBit::Settable);
}
}
}
return false;
}