package TPLinkClient import ( "bytes" "encoding/binary" "math/rand/v2" "net" ) func (client *TPLinkClient) QueryStats() []StatsData { // Send the request var payload = buildPayload(16384, []byte{}) response := doQuery(&client.header, payload) if len(response) == 0 { return []StatsData{} } // Decode the response responsePayload := decodePayload(response) portStats := make([]StatsData, 0) for _, port := range responsePayload { if port.Type.DataType != "stat" { continue } // Decode the port statistics var portData StatsData binary.Read(bytes.NewBuffer(port.Value), binary.BigEndian, &portData) portStats = append(portStats, portData) } return portStats } func (client *TPLinkClient) QueryInfo() InfoData { // Send the request var payload = buildPayload(2, []byte{}) response := doQuery(&client.header, payload) if len(response) == 0 { return InfoData{} } // Decode the response responsePayload := decodePayload(response) infoData := InfoData{} for _, line := range responsePayload { if line.Type.Id > 14 { continue } // Decode the info data switch lineType := line.Type.Name; lineType { case "type": infoData.Type = decodeString(line.Value) case "hostname": infoData.Hostname = decodeString(line.Value) case "mac": infoData.Mac = net.HardwareAddr(line.Value).String() case "ip_addr": infoData.IPAddr = bytesToIp(line.Value) case "ip_mask": infoData.IPMask = bytesToIp(line.Value) case "gateway": infoData.Gateway = bytesToIp(line.Value) case "firmware": infoData.Firmware = decodeString(line.Value) case "hardware": infoData.Hardware = decodeString(line.Value) case "dhcp": infoData.DHCP = line.Value[0] > 0 case "auto_save": infoData.AutoSave = line.Value[0] > 0 case "is_factory": infoData.IsFactory = line.Value[0] > 0 } } return infoData } func NewClient(switchMacAddr string) TPLinkClient { // Create client instance with default header client := TPLinkClient{ switchMacAddr: macToBytes(switchMacAddr), header: PacketHeader{ Version: 1, OpCode: 0, SwitchMAC: [6]byte{}, HostMAC: [6]byte{}, SequenceID: int16(rand.IntN(1000)), ErrorCode: 0, CheckLength: 0, FragmentOffset: 0, Flag: 0, TokenID: 0, Checksum: 0, }, } // Copy the Switch MAC address into the 6 byte array copy(client.header.SwitchMAC[:], client.switchMacAddr) return client }