use jt808::models::{Jt808, Jt808BodySerialize, Jt808BodyTrans, Ver808};
use jt_util::{bytes::{IBuffRead, IBuffWrite}, bytes_gbk::BytesGBK};
use chrono::{DateTime, Local, TimeZone};
#[derive(Debug, Default)]
pub struct Jt0x9201 {
pub ipaddress: BytesGBK,
pub tcp_port: u16,
pub udp_port: u16,
pub channel: u8,
pub media_type: u8,
pub stream_type: u8,
pub storage_type: u8,
pub playback_mode: u8,
pub multiple: u8,
pub starttime: i64,
pub endtime: i64,
}
lazy_static::lazy_static! {
static ref ZERO_TIME:DateTime<Local> = Local.ymd(2022, 1, 1).and_hms(1, 1, 1);
}
impl Jt808BodySerialize for Jt0x9201 {
fn write(&mut self, _ver: &Ver808, buf: &mut dyn IBuffWrite) {
buf.put_u8(self.ipaddress.bytes_len() as u8);
buf.put(self.ipaddress.get_bytes());
buf.put_u16(self.tcp_port);
buf.put_u16(self.udp_port);
buf.put_u8(self.channel);
buf.put_u8(self.media_type);
buf.put_u8(self.stream_type);
buf.put_u8(self.storage_type);
buf.put_u8(self.playback_mode);
buf.put_u8(self.multiple);
buf.put_dt_bcd6(Local.timestamp(self.starttime, 0));
if self.endtime == 0 {
buf.put_slice(&[0; 6]);
} else {
buf.put_dt_bcd6(Local.timestamp(self.endtime, 0));
}
}
fn len(&self, _ver: &Ver808) -> usize {
self.ipaddress.bytes_len() + 23
}
}
impl Jt808BodyTrans for Jt0x9201 {
fn fill_new<T>(buf: &mut T, _jt808: &Jt808) -> Self
where
T: IBuffRead,
{
let len = buf.get_u8();
Self {
ipaddress: BytesGBK::new_with_bytes(buf.split_to(len as usize)),
tcp_port: buf.get_u16(),
udp_port: buf.get_u16(),
channel: buf.get_u8(),
media_type: buf.get_u8(),
stream_type: buf.get_u8(),
storage_type: buf.get_u8(),
playback_mode: buf.get_u8(),
multiple: buf.get_u8(),
starttime: buf.get_dt_bcd6_timestamp(),
endtime:buf.get_dt_bcd6_timestamp()
}
}
}