-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpayload.go
133 lines (114 loc) · 3.52 KB
/
payload.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package ewelink
import (
"encoding/json"
)
const (
on = "on"
off = "off"
)
// payload is the interface implemented by types that holds the fields to be delivered to the API.
type payload interface{}
type parameters interface{}
// emailAuthenticationPayload holds the required and optional fields of the payment httpRequest.
type emailAuthenticationPayload struct {
payload `json:"-"` // nolint:unused
Email string `json:"email"`
Password string `json:"password"`
Version string `json:"version"`
Ts int64 `json:"ts"`
Nonce string `json:"nonce"`
AppID string `json:"appid"`
AppSecret string `json:"appsecret"`
Imei string `json:"imei"`
Os string `json:"os"`
Model string `json:"model"`
RomVersion string `json:"romversion"`
AppVersion string `json:"appversion"`
}
// DevicePowerStateParameters for devices with only one outlet.
type DevicePowerStateParameters struct {
parameters `json:"-"` // nolint:unused
PowerOn bool `json:"switch"`
}
// MarshalJSON returns a JSON encoded 'DevicePowerStateParameters'.
func (d DevicePowerStateParameters) MarshalJSON() ([]byte, error) {
power := off
if d.PowerOn {
power = on
}
return json.Marshal(&struct {
Switch string `json:"switch"`
}{Switch: power})
}
// DeviceOutletPowerStateAction device with multiple outlets.
type DeviceOutletPowerStateAction struct {
parameters `json:"-"` // nolint:unused
PowerOn []bool `json:"switches"`
}
// MarshalJSON returns a JSON encoded 'DeviceOutletPowerStateAction'.
func (d DeviceOutletPowerStateAction) MarshalJSON() ([]byte, error) {
outlets := make([]string, len(d.PowerOn))
for i, v := range d.PowerOn {
state := on
// nolint:gosimple
if v == false {
state = off
}
outlets[i] = state
}
return json.Marshal(&struct {
Switch []string `json:"switches"`
}{Switch: outlets})
}
// ActionPayload struct.
type ActionPayload struct {
Action string `json:"action"`
UserAgent string `json:"userAgent"`
Parameters parameters `json:"params"`
APIKey string `json:"apikey"`
DeviceID string `json:"deviceid"`
Sequence int64 `json:"sequence"`
}
// AuthenticateActionPayload struct.
type AuthenticateActionPayload struct {
Action string `json:"action"`
APIKey string `json:"apikey"`
AppID string `json:"appid"`
UserAgent string `json:"userAgent"`
Version string `json:"version"`
Nonce string `json:"nonce"`
ApkVersion string `json:"apkVersion"`
Os string `json:"os"`
At string `json:"at"`
Ts string `json:"ts"`
Model string `json:"model"`
RomVersion string `json:"romVersion"`
Sequence int64 `json:"sequence"`
}
func createAuthenticateActionPayload(session *Session) *AuthenticateActionPayload {
return &AuthenticateActionPayload{
Action: "userOnline",
UserAgent: "app",
Version: session.Application.Version,
Nonce: generateNonce(),
ApkVersion: session.Application.ApkVersion,
Os: session.MobileDevice.Os(),
At: session.AuthenticationToken,
APIKey: session.User.APIKey,
AppID: session.User.AppID,
Ts: "1",
Model: session.MobileDevice.Model(),
RomVersion: session.MobileDevice.RomVersion(),
Sequence: 1,
}
}
func createUpdateActionPayload(parameters parameters, apiKey string, deviceID string) *ActionPayload {
return &ActionPayload{
Action: "update",
UserAgent: "app",
Parameters: parameters,
APIKey: apiKey,
DeviceID: deviceID,
Sequence: 2,
}
}