use crate::alink::aiot_module::{get_aiot_json, ModuleRecvKind};
use crate::alink::alink_topic::ALinkSubscribeTopic;
use crate::alink::{AlinkRequest, AlinkResponse};
use crate::subdev::base::DeviceInfoId;
use enum_iterator::IntoEnumIterator;
use enum_kinds::EnumKind;
use serde::{Deserialize, Serialize};
use super::base::DeviceInfoWithSecret;
#[derive(Debug, EnumKind)]
#[enum_kind(SubDevRecvKind, derive(Serialize, IntoEnumIterator, Deserialize))]
pub enum SubDevRecv {
SubDevLoginResponse(SubDevLoginResponse),
SubDevBatchLoginResponse(SubDevBatchLoginResponse),
SubDevLogoutResponse(SubDevLogoutResponse),
SubDevBatchLogoutResponse(SubDevBatchLogoutResponse),
SubDevDisableResponse(SubDevDisableResponse),
SubDevEnableResponse(SubDevEnableResponse),
SubDevDeleteResponse(SubDevDeleteResponse),
SubDevAddTopologicalRelationResponse(SubDevAddTopologicalRelationResponse),
SubDevDeleteTopologicalRelationResponse(SubDevDeleteTopologicalRelationResponse),
SubDevGetTopologicalRelationResponse(SubDevGetTopologicalRelationResponse),
SubDevDeviceReportResponse(SubDevDeviceReportResponse),
SubDevAddTopologicalRelationNotifyRequest(SubDevAddTopologicalRelationNotifyRequest),
SubDevChangeTopologicalRelationNotifyRequest(SubDevChangeTopologicalRelationNotifyRequest),
SubDevRegisterResponse(SubDevRegisterResponse),
}
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::SubDevLoginResponse => Ok(Self::Recv::SubDevLoginResponse(serde_json::from_str(
&json_str,
)?)),
Self::SubDevBatchLoginResponse => Ok(Self::Recv::SubDevBatchLoginResponse(
serde_json::from_str(&json_str)?,
)),
Self::SubDevLogoutResponse => Ok(Self::Recv::SubDevLogoutResponse(
serde_json::from_str(&json_str)?,
)),
Self::SubDevBatchLogoutResponse => Ok(Self::Recv::SubDevBatchLogoutResponse(
serde_json::from_str(&json_str)?,
)),
Self::SubDevDisableResponse => Ok(Self::Recv::SubDevDisableResponse(
serde_json::from_str(&json_str)?,
)),
Self::SubDevEnableResponse => Ok(Self::Recv::SubDevEnableResponse(
serde_json::from_str(&json_str)?,
)),
Self::SubDevDeleteResponse => Ok(Self::Recv::SubDevDeleteResponse(
serde_json::from_str(&json_str)?,
)),
Self::SubDevAddTopologicalRelationResponse => Ok(
Self::Recv::SubDevAddTopologicalRelationResponse(serde_json::from_str(&json_str)?),
),
Self::SubDevDeleteTopologicalRelationResponse => {
Ok(Self::Recv::SubDevDeleteTopologicalRelationResponse(
serde_json::from_str(&json_str)?,
))
}
Self::SubDevGetTopologicalRelationResponse => Ok(
Self::Recv::SubDevGetTopologicalRelationResponse(serde_json::from_str(&json_str)?),
),
Self::SubDevDeviceReportResponse => Ok(Self::Recv::SubDevDeviceReportResponse(
serde_json::from_str(&json_str)?,
)),
Self::SubDevAddTopologicalRelationNotifyRequest => {
Ok(Self::Recv::SubDevAddTopologicalRelationNotifyRequest(
serde_json::from_str(&json_str)?,
))
}
Self::SubDevChangeTopologicalRelationNotifyRequest => {
Ok(Self::Recv::SubDevChangeTopologicalRelationNotifyRequest(
serde_json::from_str(&json_str)?,
))
}
Self::SubDevRegisterResponse => Ok(Self::Recv::SubDevRegisterResponse(
serde_json::from_str(&json_str)?,
)),
}
}
fn get_topic(&self) -> ALinkSubscribeTopic {
match *self {
Self::SubDevLoginResponse => {
ALinkSubscribeTopic::new("/ext/session/+/+/combine/login_reply")
}
Self::SubDevBatchLoginResponse => {
ALinkSubscribeTopic::new("/ext/session/+/+/combine/batch_login_reply")
}
Self::SubDevLogoutResponse => {
ALinkSubscribeTopic::new("/ext/session/+/+/combine/logout_reply")
}
Self::SubDevBatchLogoutResponse => {
ALinkSubscribeTopic::new("/ext/session/+/+/combine/batch_logout_reply")
}
Self::SubDevDisableResponse => ALinkSubscribeTopic::new("/sys/+/+/thing/disable"),
Self::SubDevEnableResponse => ALinkSubscribeTopic::new("/sys/+/+/thing/enable"),
Self::SubDevDeleteResponse => ALinkSubscribeTopic::new("/sys/+/+/thing/delete"),
Self::SubDevAddTopologicalRelationResponse => {
ALinkSubscribeTopic::new("/sys/+/+/thing/topo/add_reply")
}
Self::SubDevDeleteTopologicalRelationResponse => {
ALinkSubscribeTopic::new("/sys/+/+/thing/topo/delete_reply")
}
Self::SubDevGetTopologicalRelationResponse => {
ALinkSubscribeTopic::new("/sys/+/+/thing/topo/get_reply")
}
Self::SubDevDeviceReportResponse => {
ALinkSubscribeTopic::new("/sys/+/+/thing/list/found_reply")
}
Self::SubDevAddTopologicalRelationNotifyRequest => {
ALinkSubscribeTopic::new("/sys/+/+/thing/topo/add/notify")
}
Self::SubDevChangeTopologicalRelationNotifyRequest => {
ALinkSubscribeTopic::new("/sys/+/+/thing/topo/change")
}
Self::SubDevRegisterResponse => {
ALinkSubscribeTopic::new("/sys/+/+/thing/sub/register_reply")
}
}
}
}
pub type SubDevLoginResponse = AlinkResponse<DeviceInfoId>;
pub type SubDevBatchLoginResponse = AlinkResponse<Vec<DeviceInfoId>>;
pub type SubDevLogoutResponse = AlinkResponse<DeviceInfoId>;
pub type SubDevBatchLogoutResponse = AlinkResponse<Vec<DeviceInfoId>>;
pub type SubDevDisableResponse = AlinkRequest;
pub type SubDevEnableResponse = AlinkRequest;
pub type SubDevDeleteResponse = AlinkRequest;
pub type SubDevAddTopologicalRelationResponse = AlinkResponse<Option<Vec<DeviceInfoId>>>;
pub type SubDevDeleteTopologicalRelationResponse = AlinkResponse<Option<Vec<DeviceInfoId>>>;
pub type SubDevGetTopologicalRelationResponse = AlinkResponse<Option<Vec<DeviceInfoId>>>;
pub type SubDevDeviceReportResponse = AlinkResponse;
pub type SubDevAddTopologicalRelationNotifyRequest = AlinkRequest<Option<Vec<DeviceInfoId>>>;
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SubDevChangeTopologicalRelationNotifyParams {
pub status: u32,
pub sub_list: Vec<DeviceInfoId>,
}
pub type SubDevChangeTopologicalRelationNotifyRequest =
AlinkRequest<SubDevChangeTopologicalRelationNotifyParams>;
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SubDevRegisterResult {
pub iot_id: String,
pub device_secret: String,
pub device_name: String,
pub product_key: String,
}
impl From<SubDevRegisterResult> for DeviceInfoWithSecret {
fn from(r: SubDevRegisterResult) -> Self {
DeviceInfoWithSecret {
device_name: r.device_name,
product_key: r.product_key,
device_secret: r.device_secret,
}
}
}
pub type SubDevRegisterResponse = AlinkResponse<Option<Vec<SubDevRegisterResult>>>;