AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
isobus_virtual_terminal_client_update_helper.cpp
Go to the documentation of this file.
1//================================================================================================
8//================================================================================================
9
11
13
14namespace isobus
15{
16 VirtualTerminalClientUpdateHelper::VirtualTerminalClientUpdateHelper(std::shared_ptr<VirtualTerminalClient> client) :
17 VirtualTerminalClientStateTracker(nullptr == client ? nullptr : client->get_internal_control_function()),
18 vtClient(client)
19 {
20 if (nullptr == client)
21 {
22 LOG_ERROR("[VTStateHelper] constructor: client is nullptr");
23 return;
24 }
25 numericValueChangeEventHandle = client->get_vt_change_numeric_value_event_dispatcher().add_listener(
26 std::bind(&VirtualTerminalClientUpdateHelper::process_numeric_value_change_event, this, std::placeholders::_1));
27 }
28
30 {
31 if (nullptr != vtClient)
32 {
33 vtClient->get_vt_change_numeric_value_event_dispatcher().remove_listener(numericValueChangeEventHandle);
34 }
35 }
36
37 bool VirtualTerminalClientUpdateHelper::set_numeric_value(std::uint16_t object_id, std::uint32_t value)
38 {
39 if (nullptr == client)
40 {
41 LOG_ERROR("[VTStateHelper] set_numeric_value: client is nullptr");
42 return false;
43 }
44 if (numericValueStates.find(object_id) == numericValueStates.end())
45 {
46 LOG_WARNING("[VTStateHelper] set_numeric_value: objectId %lu not tracked", object_id);
47 return false;
48 }
49 if (numericValueStates.at(object_id) == value)
50 {
51 return true;
52 }
53
54 bool success = vtClient->send_change_numeric_value(object_id, value);
55 if (success)
56 {
57 numericValueStates[object_id] = value;
58 }
59 return success;
60 }
61
62 bool VirtualTerminalClientUpdateHelper::increase_numeric_value(std::uint16_t object_id, std::uint32_t step)
63 {
64 return set_numeric_value(object_id, get_numeric_value(object_id) + step);
65 }
66
67 bool VirtualTerminalClientUpdateHelper::decrease_numeric_value(std::uint16_t object_id, std::uint32_t step)
68 {
69 return set_numeric_value(object_id, get_numeric_value(object_id) - step);
70 }
71
72 void VirtualTerminalClientUpdateHelper::set_callback_validate_numeric_value(const std::function<bool(std::uint16_t, std::uint32_t)> &callback)
73 {
75 }
76
78 {
79 if (numericValueStates.find(event.objectID) == numericValueStates.end())
80 {
81 // Only proccess numeric value changes for tracked objects.
82 return;
83 }
84
85 if (numericValueStates.at(event.objectID) == event.value)
86 {
87 // Do not process the event if the value has not changed.
88 return;
89 }
90
91 std::uint32_t targetValue = event.value; // Default to the value received in the event.
93 {
94 // If the callback function returns false, reject the change by sending the previous value.
95 targetValue = numericValueStates.at(event.objectID);
96 }
97 vtClient->send_change_numeric_value(event.objectID, targetValue);
98 }
99
100 bool VirtualTerminalClientUpdateHelper::set_active_data_or_alarm_mask(std::uint16_t workingSetId, std::uint16_t dataOrAlarmMaskId)
101 {
102 if (nullptr == client)
103 {
104 LOG_ERROR("[VTStateHelper] set_active_data_or_alarm_mask: client is nullptr");
105 return false;
106 }
107 if (activeDataOrAlarmMask == dataOrAlarmMaskId)
108 {
109 return true;
110 }
111
112 bool success = vtClient->send_change_active_mask(workingSetId, dataOrAlarmMaskId);
113 if (success)
114 {
115 activeDataOrAlarmMask = dataOrAlarmMaskId;
116 }
117 return success;
118 }
119
120 bool VirtualTerminalClientUpdateHelper::set_active_soft_key_mask(VirtualTerminalClient::MaskType maskType, std::uint16_t maskId, std::uint16_t softKeyMaskId)
121 {
122 if (nullptr == client)
123 {
124 LOG_ERROR("[VTStateHelper] set_active_soft_key_mask: client is nullptr");
125 return false;
126 }
127 if (softKeyMasks.find(maskId) == softKeyMasks.end())
128 {
129 LOG_WARNING("[VTStateHelper] set_active_soft_key_mask: data/alarm mask '%lu' not tracked", maskId);
130 return false;
131 }
132 if (softKeyMasks.at(maskId) == softKeyMaskId)
133 {
134 return true;
135 }
136
137 bool success = vtClient->send_change_softkey_mask(maskType, maskId, softKeyMaskId);
138 if (success)
139 {
140 softKeyMasks[maskId] = softKeyMaskId;
141 }
142 return success;
143 }
144
145 bool VirtualTerminalClientUpdateHelper::set_attribute(std::uint16_t objectId, std::uint8_t attribute, std::uint32_t value)
146 {
147 if (nullptr == client)
148 {
149 LOG_ERROR("[VTStateHelper] set_attribute: client is nullptr");
150 return false;
151 }
152 if (attributeStates.find(objectId) == attributeStates.end())
153 {
154 LOG_ERROR("[VTStateHelper] set_attribute: objectId %lu not tracked", objectId);
155 return false;
156 }
157 if (attributeStates.at(objectId).find(attribute) == attributeStates.at(objectId).end())
158 {
159 LOG_WARNING("[VTStateHelper] set_attribute: attribute %lu of objectId %lu not tracked", attribute, objectId);
160 return false;
161 }
162 if (attributeStates.at(objectId).at(attribute) == value)
163 {
164 return true;
165 }
166
167 bool success = vtClient->send_change_attribute(objectId, attribute, value);
168 if (success)
169 {
170 attributeStates[objectId][attribute] = value;
171 }
172 return success;
173 }
174
175} // namespace isobus
A class that acts as a logging sink. The intent is that someone could make their own derived class of...
A helper class to update and track the state of an active working set.
std::shared_ptr< ControlFunction > client
The control function of the virtual terminal client to track.
std::uint16_t activeDataOrAlarmMask
Holds the data/alarm mask currently visible on the server for this client.
std::map< std::uint16_t, std::uint16_t > softKeyMasks
Holds the data/alarms masks with their associated soft keys masks for tracked objects.
std::map< std::uint16_t, std::map< std::uint8_t, std::uint32_t > > attributeStates
std::uint32_t get_numeric_value(std::uint16_t objectId) const
Gets the current numeric value of a tracked object.
bool set_active_soft_key_mask(VirtualTerminalClient::MaskType maskType, std::uint16_t maskId, std::uint16_t softKeyMaskId)
Sets the active soft key mask.
bool decrease_numeric_value(std::uint16_t objectId, std::uint32_t step=1)
Decreases the numeric value of a tracked object.
void process_numeric_value_change_event(const VirtualTerminalClient::VTChangeNumericValueEvent &event)
Processes a numeric value change event.
EventCallbackHandle numericValueChangeEventHandle
Holds the handle to the numeric value change event listener.
std::shared_ptr< VirtualTerminalClient > vtClient
Holds the vt client.
bool increase_numeric_value(std::uint16_t objectId, std::uint32_t step=1)
Increases the numeric value of a tracked object.
VirtualTerminalClientUpdateHelper(std::shared_ptr< VirtualTerminalClient > client)
The constructor of class to help update the state of an active working set.
~VirtualTerminalClientUpdateHelper()
The destructor of class to unregister event listeners.
std::function< bool(std::uint16_t, std::uint32_t)> callbackValidateNumericValue
Holds the callback function to validate a numeric value change.
void set_callback_validate_numeric_value(const std::function< bool(std::uint16_t, std::uint32_t)> &callback)
Register a callback function to validate a numeric value change of a tracked object....
bool set_attribute(std::uint16_t objectId, std::uint8_t attribute, std::uint32_t value)
Sets the value of an attribute of a tracked object.
bool set_active_data_or_alarm_mask(std::uint16_t workingSetId, std::uint16_t dataOrAlarmMaskId)
Sets the active data/alarm mask.
bool set_numeric_value(std::uint16_t objectId, std::uint32_t value)
Sets the numeric value of a tracked object.
A helper class to update and track the state of an active working set.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
A struct for storing information of a VT change numeric value event.