forked from electricbubble/gidevice
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
packet_location.go
53 lines (42 loc) 路 1.35 KB
/
packet_location.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package libimobiledevice
import (
"bytes"
"encoding/binary"
"fmt"
"strconv"
)
var _ Packet = (*locationPacket)(nil)
type locationPacket struct {
lon float64
lat float64
}
func (l *locationPacket) Pack() ([]byte, error) {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.BigEndian, uint32(0)); err != nil {
return nil, fmt.Errorf("packet (location) pack: %w", err)
}
latS := []byte(strconv.FormatFloat(l.lat, 'E', -1, 64))
if err := binary.Write(buf, binary.BigEndian, uint32(len(latS))); err != nil {
return nil, fmt.Errorf("packet (location) pack: %w", err)
}
if err := binary.Write(buf, binary.BigEndian, latS); err != nil {
return nil, fmt.Errorf("packet (location) pack: %w", err)
}
lonS := []byte(strconv.FormatFloat(l.lon, 'E', -1, 64))
if err := binary.Write(buf, binary.BigEndian, uint32(len(lonS))); err != nil {
return nil, fmt.Errorf("packet (location) pack: %w", err)
}
if err := binary.Write(buf, binary.BigEndian, lonS); err != nil {
return nil, fmt.Errorf("packet (location) pack: %w", err)
}
return buf.Bytes(), nil
}
func (l *locationPacket) Unpack(buffer *bytes.Buffer) (Packet, error) {
panic("never use (location)")
}
func (l *locationPacket) Unmarshal(v interface{}) error {
panic("never use (location)")
}
func (l *locationPacket) String() string {
return fmt.Sprintf("lon: %v, lat: %v\n", l.lon, l.lat)
}