|
1 | 1 | use crate::availability::AvailabilityConfig;
|
2 | 2 | use crate::availability::AvailabilityInput;
|
3 | 3 | use crate::availability::AvailabilityOutput;
|
4 |
| -use crate::availability::C8yJsonInventoryUpdate; |
| 4 | +use crate::availability::C8yJsonEmptyInventoryUpdate; |
5 | 5 | use crate::availability::C8ySmartRestSetInterval117;
|
6 | 6 | use crate::availability::TimerStart;
|
7 | 7 | use async_trait::async_trait;
|
8 | 8 | use c8y_api::smartrest::topic::C8yTopic;
|
9 |
| -use serde_json::json; |
10 | 9 | use std::collections::HashMap;
|
11 | 10 | use tedge_actors::Actor;
|
12 | 11 | use tedge_actors::LoggingSender;
|
@@ -156,44 +155,40 @@ impl AvailabilityActor {
|
156 | 155 | /// Insert a <"device topic ID" - "health entity topic ID" and "external ID"> pair into the map.
|
157 | 156 | /// If @health is provided in the registration message, use the value as long as it's valid as entity topic ID.
|
158 | 157 | /// If @health is not provided, use the "tedge-agent" service topic ID as default.
|
159 |
| - /// @id is the only source to know the device's external ID. Hence, @id must be provided in the registration message. |
160 | 158 | fn update_device_service_pair(
|
161 | 159 | &mut self,
|
162 | 160 | registration_message: &EntityRegistrationMessage,
|
163 | 161 | ) -> RegistrationResult {
|
164 | 162 | let source = ®istration_message.topic_id;
|
165 | 163 |
|
166 |
| - let result = match registration_message.health_endpoint.clone() { |
| 164 | + let maybe_health_topic_id = match registration_message.health_endpoint.clone() { |
167 | 165 | None => registration_message
|
168 | 166 | .topic_id
|
169 |
| - .default_service_for_device("tedge-agent") |
170 |
| - .ok_or_else( || format!("The entity is not in the default topic scheme. Please specify '@health' to enable availability monitoring for the device '{source}'")), |
171 |
| - Some(topic_id) => Ok(topic_id), |
| 167 | + .default_service_for_device("tedge-agent"), |
| 168 | + Some(topic_id) => Some(topic_id), |
172 | 169 | };
|
173 | 170 |
|
174 |
| - match result { |
175 |
| - Ok(health_topic_id) => match registration_message.external_id.clone() { |
176 |
| - None => RegistrationResult::Skip(format!("Registration message is skipped since '@id' is missing in the payload. source: '{source}', {registration_message:?}")), |
177 |
| - Some(external_id) => { |
178 |
| - if let Some(ids) = self.device_ids_map.get(source) { |
179 |
| - if ids.health_topic_id == health_topic_id { |
180 |
| - return RegistrationResult::Skip(format!("Registration message is skipped since no health endpoint change is detected. source: '{source}', {registration_message:?}")) |
181 |
| - } |
| 171 | + match (maybe_health_topic_id, registration_message.external_id.clone()) { |
| 172 | + (None, _) => RegistrationResult::Error(format!("The entity is not in the default topic scheme. Please specify '@health' to enable availability monitoring for the device '{source}'")), |
| 173 | + (_, None) => RegistrationResult::Error(format!("External ID is missing from the received registration message. source: '{source}', {registration_message:?}")), |
| 174 | + (Some(health_topic_id), Some(external_id)) => { |
| 175 | + if let Some(ids) = self.device_ids_map.get(source) { |
| 176 | + if ids.health_topic_id == health_topic_id { |
| 177 | + return RegistrationResult::Skip(format!("Registration message is skipped since no health endpoint change is detected. source: '{source}', {registration_message:?}")) |
182 | 178 | }
|
| 179 | + } |
183 | 180 |
|
184 |
| - match self.device_ids_map.insert( |
185 |
| - source.clone(), |
186 |
| - DeviceIds { |
187 |
| - health_topic_id, |
188 |
| - external_id, |
189 |
| - }, |
190 |
| - ) { |
191 |
| - None => RegistrationResult::New, |
192 |
| - Some(_) => RegistrationResult::Update, |
193 |
| - } |
| 181 | + match self.device_ids_map.insert( |
| 182 | + source.clone(), |
| 183 | + DeviceIds { |
| 184 | + health_topic_id, |
| 185 | + external_id, |
| 186 | + }, |
| 187 | + ) { |
| 188 | + None => RegistrationResult::New, |
| 189 | + Some(_) => RegistrationResult::Update, |
194 | 190 | }
|
195 |
| - }, |
196 |
| - Err(err) => RegistrationResult::Error(err), |
| 191 | + } |
197 | 192 | }
|
198 | 193 | }
|
199 | 194 |
|
@@ -264,9 +259,8 @@ impl AvailabilityActor {
|
264 | 259 | if let Some(health_status) = self.health_status_map.get(service_topic_id) {
|
265 | 260 | // Send an empty JSON over MQTT message if the target service status is "up"
|
266 | 261 | if health_status.status == Status::Up {
|
267 |
| - let json_over_mqtt = C8yJsonInventoryUpdate { |
| 262 | + let json_over_mqtt = C8yJsonEmptyInventoryUpdate { |
268 | 263 | external_id: external_id.into(),
|
269 |
| - payload: json!({}), |
270 | 264 | prefix: self.config.c8y_prefix.clone(),
|
271 | 265 | };
|
272 | 266 | self.message_box.send(json_over_mqtt.into()).await?;
|
|
0 commit comments