-
Notifications
You must be signed in to change notification settings - Fork 246
/
pair_message.go
71 lines (62 loc) · 1.66 KB
/
pair_message.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package protocol
import (
"bytes"
"encoding/hex"
"encoding/json"
)
// PairMessage contains all message details.
type PairMessage struct {
InstallationID string `json:"installationId"`
// The type of the device
DeviceType string `json:"deviceType"`
// Name the user set name
Name string `json:"name"`
// The FCMToken for mobile platforms
FCMToken string `json:"fcmToken"`
// not protocol defined fields
ID []byte `json:"-"`
}
func (m *PairMessage) MarshalJSON() ([]byte, error) {
type PairMessageAlias PairMessage
item := struct {
*PairMessageAlias
ID string `json:"id"`
}{
PairMessageAlias: (*PairMessageAlias)(m),
ID: "0x" + hex.EncodeToString(m.ID),
}
return json.Marshal(item)
}
// CreatePairMessage creates a PairMessage which is used
// to pair devices.
func CreatePairMessage(installationID string, name string, deviceType string, fcmToken string) PairMessage {
return PairMessage{
InstallationID: installationID,
Name: name,
DeviceType: deviceType,
FCMToken: fcmToken,
}
}
// DecodePairMessage decodes a raw payload to Message struct.
func DecodePairMessage(data []byte) (message PairMessage, err error) {
buf := bytes.NewBuffer(data)
decoder := NewMessageDecoder(buf)
value, err := decoder.Decode()
if err != nil {
return
}
message, ok := value.(PairMessage)
if !ok {
return message, ErrInvalidDecodedValue
}
return
}
// EncodePairMessage encodes a PairMessage using Transit serialization.
func EncodePairMessage(value PairMessage) ([]byte, error) {
var buf bytes.Buffer
encoder := NewMessageEncoder(&buf)
if err := encoder.Encode(value); err != nil {
return nil, err
}
return buf.Bytes(), nil
}