using JT808.Protocol.Enums;
using JT808.Protocol.Extensions;
using JT808.Protocol.Formatters;
using JT808.Protocol.Interfaces;
using JT808.Protocol.MessagePack;
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
namespace JT808.Protocol.MessageBody.CarDVR
{
///
/// 采集指定的外部供电记录
/// 返回:符合条件的供电记录
///
public class JT808_CarDVR_Up_0x13 : JT808CarDVRUpBodies, IJT808MessagePackFormatter, IJT808Analyze
{
public override byte CommandId => JT808CarDVRCommandID.采集指定的外部供电记录.ToByteValue();
///
/// 请求发送指定的时间范围内 N 个单位数据块的数据(N≥1)
///
public List JT808_CarDVR_Up_0x13_ExternalPowerSupplys { get; set; }
public override string Description => "符合条件的供电记录";
public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config)
{
JT808_CarDVR_Up_0x13 value = new JT808_CarDVR_Up_0x13();
writer.WriteStartArray("请求发送指定的时间范围内 N 个单位数据块的数据");
var count = (reader.ReadCurrentRemainContentLength() - 1) / 7;//记录块个数, -1 去掉校验位
for (int i = 0; i < count; i++)
{
JT808_CarDVR_Up_0x13_ExternalPowerSupply jT808_CarDVR_Up_0x13_ExternalPowerSupply = new JT808_CarDVR_Up_0x13_ExternalPowerSupply();
writer.WriteStartObject();
writer.WriteStartObject($"从指定的结束时间之前最近的第{i+1}条外部电源记录");
var hex = reader.ReadVirtualArray(6);
jT808_CarDVR_Up_0x13_ExternalPowerSupply.EventTime = reader.ReadDateTime6();
writer.WriteString($"[{hex.ToArray().ToHexString()}]事件发生时间", jT808_CarDVR_Up_0x13_ExternalPowerSupply.EventTime);
jT808_CarDVR_Up_0x13_ExternalPowerSupply.EventType = reader.ReadByte();
writer.WriteString($"[{ jT808_CarDVR_Up_0x13_ExternalPowerSupply.EventType.ReadNumber()}]事件类型", EventTypeDisplay(jT808_CarDVR_Up_0x13_ExternalPowerSupply.EventType));
writer.WriteEndObject();
writer.WriteEndObject();
}
writer.WriteEndArray();
string EventTypeDisplay(byte eventType) {
if (eventType == 1)
{
return "供电";
}
else {
return "断电";
}
}
}
public void Serialize(ref JT808MessagePackWriter writer, JT808_CarDVR_Up_0x13 value, IJT808Config config)
{
foreach (var externalPowerSupply in value.JT808_CarDVR_Up_0x13_ExternalPowerSupplys)
{
writer.WriteDateTime6(externalPowerSupply.EventTime);
writer.WriteByte(externalPowerSupply.EventType);
}
}
public JT808_CarDVR_Up_0x13 Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
{
JT808_CarDVR_Up_0x13 value = new JT808_CarDVR_Up_0x13();
value.JT808_CarDVR_Up_0x13_ExternalPowerSupplys = new List();
var count = (reader.ReadCurrentRemainContentLength() - 1) / 7;//记录块个数, -1 去掉校验位
for (int i = 0; i < count; i++)
{
JT808_CarDVR_Up_0x13_ExternalPowerSupply jT808_CarDVR_Up_0x13_ExternalPowerSupply = new JT808_CarDVR_Up_0x13_ExternalPowerSupply();
jT808_CarDVR_Up_0x13_ExternalPowerSupply.EventTime = reader.ReadDateTime6();
jT808_CarDVR_Up_0x13_ExternalPowerSupply.EventType = reader.ReadByte();
value.JT808_CarDVR_Up_0x13_ExternalPowerSupplys.Add(jT808_CarDVR_Up_0x13_ExternalPowerSupply);
}
return value;
}
}
///
/// 单位记录仪外部供电记录数据块格式
///
public class JT808_CarDVR_Up_0x13_ExternalPowerSupply
{
///
/// 事件发生时间
///
public DateTime EventTime { get; set; }
///
/// 事件类型
///
public byte EventType { get; set; }
}
}