package protocol

import (
	"encoding/hex"
	"github.com/gogf/gf/os/glog"
)

type Dlt_0x93 struct {
	//接收表号
	DeviceAddress []byte
	//表号
	DeviceID string
	//当前视在功率
	ApparentPower float64
}

func (e *Dlt_0x93) MsgID() MsgID {
	return ReadAddress
}

//func (entity *Dlt_0x93) GetDeviceAddress() DeviceAddress {
//	return entity.DeviceAddress
//}

func (e *Dlt_0x93) Encode(ctx *PacketContext) ([]byte, error) {
	writer := NewWriter()
	// 接收符号
	writer.Write([]byte{0x68, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0x68, 0x13, 0x00, 0xDF, 0x16})
	return writer.Bytes(), nil
}

func (e *Dlt_0x93) Decode(ctx *PacketContext, dataByte []byte) (data Data, err error) {

	e.DeviceAddress = dataByte[1:7]
	ctx.SetReceiveAddress(dataByte[1:7])
	var bytea []byte
	bytea = append(bytea, dataByte[1:7]...)
	for i, j := 0, len(bytea)-1; i < j; i, j = i+1, j-1 {
		bytea[i], bytea[j] = bytea[j], bytea[i]
	}
	e.DeviceID = hex.EncodeToString(bytea)
	ctx.SetDeviceId(e.DeviceID)
	glog.Debugf("表号读取成功：%s", e.DeviceID)
	return data, nil
}
