use super::base::{LogConfig, LogItem};
use crate::alink::aiot_module::{get_aiot_json, ModuleRecvKind};
use crate::alink::alink_topic::ALinkSubscribeTopic;
use crate::alink::{AlinkRequest, AlinkResponse, SimpleResponse};
use crate::{Error, Result, ThreeTuple};
use enum_iterator::IntoEnumIterator;
use enum_kinds::EnumKind;
use regex::Regex;
use rumqttc::{AsyncClient, QoS};
use serde::{Deserialize, Serialize};
use std::any::TypeId;
use std::sync::Arc;
use tokio::sync::mpsc;
use tokio::sync::mpsc::{Receiver, Sender};
pub type ConfigLogGetReply = AlinkResponse<LogConfig>;
pub type ConfigLogPush = AlinkRequest<LogConfig>;
pub type LogPostReply = SimpleResponse;
#[derive(Debug, EnumKind)]
#[enum_kind(LogPostRecvKind, derive(Serialize, IntoEnumIterator, Deserialize))]
pub enum LogPostRecv {
ConfigLogGetReply(ConfigLogGetReply),
ConfigLogPush(ConfigLogPush),
LogPostReply(LogPostReply),
}
impl ModuleRecvKind for super::RecvKind {
type Recv = super::Recv;
fn to_payload(&self, payload: &[u8], _: &Vec<String>) -> crate::Result<LogPostRecv> {
let s = get_aiot_json(payload);
match *self {
Self::ConfigLogGetReply => Ok(Self::Recv::ConfigLogGetReply(serde_json::from_str(&s)?)),
Self::ConfigLogPush => Ok(Self::Recv::ConfigLogPush(serde_json::from_str(&s)?)),
Self::LogPostReply => Ok(Self::Recv::LogPostReply(serde_json::from_str(&s)?)),
}
}
fn get_topic(&self) -> ALinkSubscribeTopic {
match *self {
Self::ConfigLogGetReply => {
ALinkSubscribeTopic::new("/sys/+/+/thing/config/log/get_reply")
}
Self::ConfigLogPush => ALinkSubscribeTopic::new("/sys/+/+/thing/config/log/push"),
Self::LogPostReply => ALinkSubscribeTopic::new("/sys/+/+/thing/log/post_reply"),
}
}
}