-
Notifications
You must be signed in to change notification settings - Fork 8
/
provider_impl.rs
65 lines (52 loc) · 2.16 KB
/
provider_impl.rs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT
use digital_twin_model::sdv_v0 as sdv;
use log::{debug, info};
use samples_protobuf_data_access::tutorial_grpc::v1::digital_twin_provider_tutorial_server::DigitalTwinProviderTutorial;
use samples_protobuf_data_access::tutorial_grpc::v1::{
GetRequest, GetResponse, InvokeRequest, InvokeResponse,
};
use tonic::{Request, Response, Status};
#[derive(Debug, Default)]
pub struct ProviderImpl {}
#[tonic::async_trait]
impl DigitalTwinProviderTutorial for ProviderImpl {
/// Get operation.
///
/// # Arguments
/// * `request` - Get request.
async fn get(&self, request: Request<GetRequest>) -> Result<Response<GetResponse>, Status> {
let request_inner = request.into_inner();
let entity_id: String = request_inner.entity_id.clone();
let value = match entity_id.as_str() {
sdv::hvac::ambient_air_temperature::ID => "70",
sdv::hvac::is_air_conditioning_active::ID => "true",
_ => "NULL",
};
let get_response = GetResponse { property_value: String::from(value) };
Ok(Response::new(get_response))
}
/// Invoke operation.
///
/// # Arguments
/// * `request` - Invoke request.
async fn invoke(
&self,
request: Request<InvokeRequest>,
) -> Result<Response<InvokeResponse>, Status> {
debug!("Got an invoke request: {request:?}");
let request_inner = request.into_inner();
let entity_id: String = request_inner.entity_id.clone();
let payload: String = request_inner.payload;
info!("Received an invoke request from for entity id {entity_id} with payload '{payload}'");
let response_message: String = if entity_id == sdv::hmi::show_notification::ID {
format!("Displaying notification '{payload}'")
} else {
format!("Error notification: The entity id {entity_id} is not recognized.")
};
info!("Sending an invoke response for entity id {entity_id}");
let response = InvokeResponse { response: response_message };
Ok(Response::new(response))
}
}