-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
185 lines (153 loc) · 4.87 KB
/
schedule.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package shelly
import (
"context"
"encoding/json"
"github.com/mongoose-os/mos/common/mgrpc"
"github.com/mongoose-os/mos/common/mgrpc/frame"
)
// Schedule describes a series of RPCs to be repeated on a schedule.
type Schedule struct {
// ID assigned to the job when it is created. This is used in subsequent Update / Delete calls.
// This should be nil for Schedule.Create, and MUST have a value for Schedule.Update.
ID *int `json:"id,omitempty"`
// Enable is true to enable the execution of this job, false otherwise. It is true by default.
Enable *bool `json:"enable,omitempty"`
// Timespec as defined by [cron](https://github.com/mongoose-os-libs/cron). Note that leading
// 0s are not supported (e.g.: for 8 AM you should set 8 instead of 08).
TimeSpec *string `json:"timespec,omitempty"`
// Calls is a list of RPC methods and arguments to be invoked when the job gets executed. It
// must contain at least one valid object. There is a limit of 5 calls per schedule job.
Calls []ScheduleCall `json:"calls,omitempty"`
}
// ScheduleCall describes a single RPC call to be initiated by the Schedule.
type ScheduleCall struct {
// Method is the name of the RPC method. Required
Method string `json:"method"`
// Params are the parameters used to invoke the RPC call. If the call requires no parameters
// params may be omitted.
Params *json.RawMessage `json:"params,omitempty"`
}
// NewScheduleCallRPCRequest creates a ScheduleCall object from a RPCRequestBody.
func NewScheduleCallWithRPCRequest(req RPCRequestBody) (ScheduleCall, error) {
b, err := json.Marshal(req)
if err != nil {
return ScheduleCall{}, err
}
asRaw := json.RawMessage(b)
return ScheduleCall{
Method: req.Method(),
Params: &asRaw,
}, nil
}
// ScheduleCreateRequest adds a new schedule to the shelly device.
type ScheduleCreateRequest Schedule
func (r *ScheduleCreateRequest) Method() string {
return "Schedule.Create"
}
func (r *ScheduleCreateRequest) NewTypedResponse() *ScheduleCreateResponse {
return &ScheduleCreateResponse{}
}
func (r *ScheduleCreateRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ScheduleCreateRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ScheduleCreateResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
// ScheduleCreateResponse is the RPC response to the ScheduleCreateRequest.
type ScheduleCreateResponse struct {
// ID assigned to the scheduled job.
ID *int `json:"id,omitempty"`
// Rev is the current revision number of the schedule instances.
Rev *int `json:"rev,omitempty"`
}
// ScheduleUpdateResponse is the response for Schedule.Update, Schedule.Delete,
// and Schedule.DeleteAll RPC requests.
type ScheduleUpdateResponse struct {
// Rev is the current revision number of the schedule instances.
Rev *int `json:"rev,omitempty"`
}
// ScheduleUpdateRequest modifies an existing schedule.
type ScheduleUpdateRequest Schedule
func (r *ScheduleUpdateRequest) Method() string {
return "Schedule.Update"
}
func (r *ScheduleUpdateRequest) NewTypedResponse() *ScheduleUpdateResponse {
return &ScheduleUpdateResponse{}
}
func (r *ScheduleUpdateRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ScheduleUpdateRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ScheduleUpdateResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
// ScheduleDeleteRequest deletes an existing schedule.
type ScheduleDeleteRequest struct {
// ID of the schedule to be deleted. Required.
ID int `json:"id"`
}
func (r *ScheduleDeleteRequest) Method() string {
return "Schedule.Delete"
}
func (r *ScheduleDeleteRequest) NewTypedResponse() *ScheduleUpdateResponse {
return &ScheduleUpdateResponse{}
}
func (r *ScheduleDeleteRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ScheduleDeleteRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ScheduleUpdateResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
// ScheduleDeleteAllRequest deletes all existing schedules.
type ScheduleDeleteAllRequest struct{}
func (r *ScheduleDeleteAllRequest) Method() string {
return "Schedule.DeleteAll"
}
func (r *ScheduleDeleteAllRequest) NewTypedResponse() *ScheduleUpdateResponse {
return &ScheduleUpdateResponse{}
}
func (r *ScheduleDeleteAllRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ScheduleDeleteAllRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ScheduleUpdateResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}