Skip to content

Commit

Permalink
Merge branch 'main' into pahearn_RPPL-2520
Browse files Browse the repository at this point in the history
  • Loading branch information
pahearn73 committed Oct 31, 2024
2 parents 491134a + 03e4332 commit 3248b8f
Show file tree
Hide file tree
Showing 10 changed files with 1,232 additions and 138 deletions.
35 changes: 17 additions & 18 deletions core/main/src/broker/endpoint_broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ impl BrokerOutputForwarder {
if let Some(filter) = broker_request.rule.transform.get_transform_data(
super::rules_engine::RuleTransformType::Response,
) {
apply_response(filter, &rpc_request, &mut response);
apply_response(filter, &rpc_request.ctx.method, &mut response);
} else if response.result.is_none() && response.error.is_none() {
response.result = Some(Value::Null);
}
Expand Down Expand Up @@ -810,17 +810,17 @@ async fn forward_extn_event(
}
}

fn apply_response(
pub fn apply_response(
result_response_filter: String,
rpc_request: &RpcRequest,
method: &str,
response: &mut JsonRpcApiResponse,
) {
match serde_json::to_value(response.clone()) {
Ok(input) => {
match jq_compile(
input,
&result_response_filter,
format!("{}_response", rpc_request.ctx.method),
format!("{}_response", method),
) {
Ok(jq_out) => {
trace!(
Expand Down Expand Up @@ -880,7 +880,6 @@ fn apply_filter(broker_request: &BrokerRequest, result: &Value, rpc_request: &Rp
&filter,
format!("{}_event filter", rpc_request.ctx.method),
) {
println!("apply_filter: {:?}", r);
if r.is_null() {
return false;
} else {
Expand Down Expand Up @@ -1023,7 +1022,7 @@ mod tests {
let filter = "if .result and .result.success then (.result.stbVersion | split(\"_\") [0]) elif .error then if .error.code == -32601 then {error: { code: -1, message: \"Unknown method.\" }} else \"Error occurred with a different code\" end else \"No result or recognizable error\" end".to_string();
//let mut response = JsonRpcApiResponse::mock();
//response.error = Some(error);
apply_response(filter, &rpc_request, &mut output.data);
apply_response(filter, &rpc_request.ctx.method, &mut output.data);
//let msg = output.data.error.unwrap().get("message").unwrap().clone();
assert_eq!(
output.data.error.unwrap().get("message").unwrap().clone(),
Expand All @@ -1038,7 +1037,7 @@ mod tests {
let filter = "if .result and .result.success then .result.value elif .error.code==22 or .error.code==43 then null else .error end".to_string();
//let mut response = JsonRpcApiResponse::mock();
//response.error = Some(error);
apply_response(filter, &rpc_request, &mut output.data);
apply_response(filter, &rpc_request.ctx.method, &mut output.data);
assert_eq!(output.data.error, None);
assert_eq!(output.data.result.unwrap(), serde_json::Value::Null);

Expand All @@ -1050,7 +1049,7 @@ mod tests {
let filter = "if .result and .result.success then .result.value elif .error.code==22 or .error.code==43 then null else { error: .error } end".to_string();
//let mut response = JsonRpcApiResponse::mock();
//response.error = Some(error.clone());
apply_response(filter, &rpc_request, &mut output.data);
apply_response(filter, &rpc_request.ctx.method, &mut output.data);
assert_eq!(output.data.error, Some(error));
}

Expand All @@ -1077,7 +1076,7 @@ mod tests {
let mut data = JsonRpcApiResponse::mock();
data.result = Some(result);
let mut output: BrokerOutput = BrokerOutput { data: data.clone() };
apply_response(filter, &rpc_request, &mut output.data);
apply_response(filter, &rpc_request.ctx.method, &mut output.data);
assert_eq!(output.data.result.unwrap(), "SCXI11BEI".to_string());

// device.videoResolution
Expand All @@ -1087,7 +1086,7 @@ mod tests {
response.result = Some(result);
//let data = JsonRpcApiResponse::mock();
//let mut output: BrokerOutput = BrokerOutput { data: data.clone() };
apply_response(filter, &rpc_request, &mut response);
apply_response(filter, &rpc_request.ctx.method, &mut response);
assert_eq!(response.result.unwrap(), json!([1920, 1080]));

// device.audio
Expand All @@ -1097,7 +1096,7 @@ mod tests {
let filter = "if .result and .result.success then .result | {\"stereo\": (.supportedAudioFormat | index(\"PCM\") > 0),\"dolbyDigital5.1\": (.supportedAudioFormat | index(\"DOLBY AC3\") > 0),\"dolbyDigital5.1plus\": (.supportedAudioFormat | index(\"DOLBY EAC3\") > 0),\"dolbyAtmos\": (.supportedAudioFormat | index(\"DOLBY EAC3 ATMOS\") > 0)} elif .error then if .error.code == -32601 then \"Unknown method.\" else \"Error occurred with a different code\" end else \"No result or recognizable error\" end".to_string();
let mut response = JsonRpcApiResponse::mock();
response.result = Some(result);
apply_response(filter, &rpc_request, &mut response);
apply_response(filter, &rpc_request.ctx.method, &mut response);
assert_eq!(
response.result.unwrap(),
json!({"dolbyAtmos": true, "dolbyDigital5.1": true, "dolbyDigital5.1plus": false, "stereo": true})
Expand All @@ -1112,7 +1111,7 @@ mod tests {
let filter = "if .result and .result.success then (.result.interfaces | .[] | select(.connected) | {\"state\": \"connected\",\"type\": .interface | ascii_downcase }) elif .error then if .error.code == -32601 then \"Unknown method.\" else \"Error occurred with a different code\" end else \"No result or recognizable error\" end".to_string();
let mut response = JsonRpcApiResponse::mock();
response.result = Some(result);
apply_response(filter, &rpc_request, &mut response);
apply_response(filter, &rpc_request.ctx.method, &mut response);
assert_eq!(
response.result.unwrap(),
json!({"state":"connected", "type":"wifi"})
Expand All @@ -1125,7 +1124,7 @@ mod tests {
let filter = "if .result.success then (if .result.friendlyName | length == 0 then \"Living Room\" else .result.friendlyName end) else \"Living Room\" end".to_string();
let mut response = JsonRpcApiResponse::mock();
response.result = Some(result);
apply_response(filter, &rpc_request, &mut response);
apply_response(filter, &rpc_request.ctx.method, &mut response);
assert_eq!(response.result.unwrap(), json!("my_device"));

// localization.language
Expand All @@ -1136,7 +1135,7 @@ mod tests {
.to_string();
let mut response = JsonRpcApiResponse::mock();
response.result = Some(result);
apply_response(filter, &rpc_request, &mut response);
apply_response(filter, &rpc_request.ctx.method, &mut response);

assert_eq!(response.result.unwrap(), json!("FR"));

Expand All @@ -1147,7 +1146,7 @@ mod tests {
let filter = "if .result.success then (if .result.friendlyName | length == 0 then \"Living Room\" else .result.friendlyName end) else \"Living Room\" end".to_string();
let mut response = JsonRpcApiResponse::mock();
response.result = Some(result);
apply_response(filter, &rpc_request, &mut response);
apply_response(filter, &rpc_request.ctx.method, &mut response);

assert_eq!(response.result.unwrap(), json!("my_device"));

Expand All @@ -1158,7 +1157,7 @@ mod tests {
let filter = "if .result.success then null else { code: -32100, message: \"couldn't set skip restriction\" } end".to_string();
let mut response = JsonRpcApiResponse::mock();
response.result = Some(result);
apply_response(filter, &rpc_request, &mut response);
apply_response(filter, &rpc_request.ctx.method, &mut response);

assert_eq!(response.result.unwrap(), serde_json::Value::Null);

Expand All @@ -1169,7 +1168,7 @@ mod tests {
let filter = "if .result.success then .result.value elif .error.code==22 or .error.code==43 then \"null\" else .error end".to_string();
let mut response = JsonRpcApiResponse::mock();
response.result = Some(result);
apply_response(filter, &rpc_request, &mut response);
apply_response(filter, &rpc_request.ctx.method, &mut response);
assert_eq!(response.result.unwrap(), "some_value");

// localization.countryCode
Expand All @@ -1179,7 +1178,7 @@ mod tests {
let filter = "if .result.success then if .result.territory == \"ITA\" then \"IT\" elif .result.territory == \"GBR\" then \"GB\" elif .result.territory == \"IRL\" then \"IE\" elif .result.territory == \"DEU\" then \"DE\" elif .result.territory == \"AUS\" then \"AU\" else \"GB\" end end".to_string();
let mut response = JsonRpcApiResponse::mock();
response.result = Some(result);
apply_response(filter, &rpc_request, &mut response);
apply_response(filter, &rpc_request.ctx.method, &mut response);
assert_eq!(response.result.unwrap(), "GB");
}
}
15 changes: 12 additions & 3 deletions core/main/src/broker/rules_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ripple_sdk::{
serde_json::Value,
utils::error::RippleError,
};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{fs, path::Path};

Expand Down Expand Up @@ -84,21 +84,27 @@ pub enum RuleEndpointProtocol {
Thunder,
}

#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Rule {
pub alias: String,
// Not every rule needs transform
#[serde(default)]
pub transform: RuleTransform,
#[serde(skip_serializing_if = "Option::is_none")]
pub filter: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub endpoint: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Default)]
#[derive(Debug, Clone, Deserialize, Default, Serialize)]
pub struct RuleTransform {
#[serde(skip_serializing_if = "Option::is_none")]
pub request: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub response: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub event: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub event_decorator_method: Option<String>,
}

Expand Down Expand Up @@ -203,6 +209,9 @@ impl RuleEngine {
}
None
}
pub fn get_rule_by_method(&self, method: &str) -> Option<Rule> {
self.rules.rules.get(&method.to_lowercase()).cloned()
}
}

pub fn jq_compile(input: Value, filter: &str, reference: String) -> Result<Value, RippleError> {
Expand Down
1 change: 1 addition & 0 deletions core/main/src/broker/thunder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
// SPDX-License-Identifier: Apache-2.0
//
pub mod thunder_plugins_status_mgr;
pub mod user_data_migrator;
22 changes: 14 additions & 8 deletions core/main/src/broker/thunder/thunder_plugins_status_mgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ impl StatusManager {
}
}
} else if let Some(_e) = &data.error {
Self::on_thunder_error_response(self, callback, data, &callsign.to_string()).await;
self.on_thunder_error_response(callback, data, &callsign.to_string())
.await;
}
}

Expand All @@ -386,20 +387,25 @@ impl StatusManager {
data: &JsonRpcApiResponse,
request: &str,
) {
let result = match &data.result {
Some(result) => result,
None => return,
};

let callsign = match request.split('@').last() {
Some(callsign) => callsign.trim_matches(|c| c == '"' || c == '}'),
None => return,
None => "",
};

let result = match &data.result {
Some(result) => result,
None => {
self.on_thunder_error_response(callback, data, &callsign.to_string())
.await;
return;
}
};

let status_res: Vec<Status> = match serde_json::from_value(result.clone()) {
Ok(status_res) => status_res,
Err(_) => {
Self::on_thunder_error_response(self, callback, data, &callsign.to_string()).await;
self.on_thunder_error_response(callback, data, &callsign.to_string())
.await;
return;
}
};
Expand Down
Loading

0 comments on commit 3248b8f

Please sign in to comment.