use crate::alink::aiot_module::{get_aiot_json, ModuleRecvKind};
use crate::alink::alink_topic::ALinkSubscribeTopic;
use crate::{alink::AlinkResponse, Error};
use enum_iterator::IntoEnumIterator;
use enum_kinds::EnumKind;
use log::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use spin::Lazy;
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RemoteConfigFileInfo {
pub config_id: String,
pub config_size: u64,
pub sign: String,
pub sign_method: String,
pub url: String,
pub get_type: String,
}
pub type RemoteConfigGetReply = AlinkResponse<Option<RemoteConfigFileInfo>>;
pub type RemoteConfigPush = AlinkResponse<Option<RemoteConfigFileInfo>>;
#[derive(Debug, EnumKind)]
#[enum_kind(RemoteConfigRecvKind, derive(Serialize, IntoEnumIterator, Deserialize))]
pub enum RemoteConfigRecv {
RemoteConfigGetReply(RemoteConfigGetReply),
RemoteConfigPush(RemoteConfigPush),
}
impl ModuleRecvKind for super::RecvKind {
type Recv = super::Recv;
fn to_payload(&self, payload: &[u8], _: &Vec<String>) -> crate::Result<Self::Recv> {
let json_str = get_aiot_json(payload);
match *self {
Self::RemoteConfigGetReply => Ok(Self::Recv::RemoteConfigGetReply(
serde_json::from_str(&json_str)?,
)),
Self::RemoteConfigPush => Ok(Self::Recv::RemoteConfigPush(serde_json::from_str(
&json_str,
)?)),
}
}
fn get_topic(&self) -> ALinkSubscribeTopic {
match *self {
Self::RemoteConfigGetReply => {
ALinkSubscribeTopic::new("/sys/+/+/thing/config/get_reply")
}
Self::RemoteConfigPush => ALinkSubscribeTopic::new("/sys/+/+/thing/config/push"),
}
}
}