use super::recv::RemoteConfigFileInfo;
use crate::Error;
use crate::{
alink::{global_id_next, AlinkRequest, AlinkResponse, SysAck, ALINK_VERSION},
http_downloader::{HttpDownloadConfig, HttpDownloader},
};
use log::debug;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fs;
use tempdir::TempDir;
impl super::Module {
pub async fn get(&self, ack: bool) -> crate::Result<()> {
let payload = RemoteConfigGetRequest {
id: global_id_next().to_string(),
params: RemoteConfigGetParams {
config_scope: "product".to_string(),
get_type: "file".to_string(),
},
version: ALINK_VERSION.to_string(),
sys: Some(SysAck { ack: ack.into() }),
method: None,
};
self.publish(
format!(
"/sys/{}/{}/thing/config/get",
self.three.product_key, self.three.device_name
),
&payload,
)
.await
}
pub async fn push_reply(&self, id: String, code: u64) -> crate::Result<()> {
let payload = AlinkResponse {
id,
code,
data: (),
message: None,
method: None,
version: None,
};
self.publish(
format!(
"/sys/{}/{}/thing/config/push_reply",
self.three.product_key, self.three.device_name
),
&payload,
)
.await
}
pub async fn download_config(
&mut self,
config_info: &RemoteConfigFileInfo,
) -> crate::Result<Vec<u8>> {
let config_id = config_info.config_id.clone();
let tmp_dir = TempDir::new("remote_config")?;
let file_path = tmp_dir.path().join(config_id.to_string());
let downloader = HttpDownloader::new(&config_info.url, file_path);
let config_file_path = downloader.start().await?;
let mut buffer = fs::read(config_file_path)?;
crate::util::validate(&buffer, &config_info.sign_method, &config_info.sign)?;
Ok(buffer)
}
}
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RemoteConfigGetParams {
pub config_scope: String,
pub get_type: String,
}
pub type RemoteConfigGetRequest = AlinkRequest<RemoteConfigGetParams>;