-
Notifications
You must be signed in to change notification settings - Fork 9
/
snap.go
106 lines (94 loc) · 3.16 KB
/
snap.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* This file is part of the IoT Device Twin Service
* Copyright 2019 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License version 3, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package controller
import (
"fmt"
"github.com/CanonicalLtd/iot-devicetwin/domain"
"time"
)
// DeviceSnaps gets the device's snaps from the database cache
func (srv *Service) DeviceSnaps(orgID, clientID string) ([]domain.DeviceSnap, error) {
return srv.DeviceTwin.DeviceSnaps(orgID, clientID)
}
// DeviceSnapList triggers listing snaps on a device
func (srv *Service) DeviceSnapList(orgID, clientID string) error {
act := domain.SubscribeAction{
Action: "list",
}
return srv.deviceSnapAction(orgID, clientID, act)
}
// DeviceSnapInstall triggers installing a snap on a device
func (srv *Service) DeviceSnapInstall(orgID, clientID, snap string) error {
act := domain.SubscribeAction{
Action: "install",
Snap: snap,
}
return srv.deviceSnapAction(orgID, clientID, act)
}
// DeviceSnapRemove triggers uninstalling a snap on a device
func (srv *Service) DeviceSnapRemove(orgID, clientID, snap string) error {
act := domain.SubscribeAction{
Action: "remove",
Snap: snap,
}
return srv.deviceSnapAction(orgID, clientID, act)
}
// DeviceSnapUpdate triggers a snap update on a device
func (srv *Service) DeviceSnapUpdate(orgID, clientID, snap, action string) error {
switch action {
case "enable", "disable", "refresh":
act := domain.SubscribeAction{
Action: action,
Snap: snap,
}
return srv.deviceSnapAction(orgID, clientID, act)
default:
return fmt.Errorf("invalid update action `%s`", action)
}
}
// DeviceSnapConf triggers a snap settings update on a device
func (srv *Service) DeviceSnapConf(orgID, clientID, snap, settings string) error {
// Trigger the update settings action on the device
act := domain.SubscribeAction{
Action: "setconf",
Snap: snap,
Data: settings,
}
return srv.deviceSnapAction(orgID, clientID, act)
}
// deviceSnapAction triggers a snap action on a device
func (srv *Service) deviceSnapAction(orgID, clientID string, action domain.SubscribeAction) error {
// Validate the org and device ID
device, err := srv.DeviceTwin.DeviceGet(orgID, clientID)
if err != nil {
return err
}
// Trigger the action on the device
err = srv.triggerActionOnDevice(device.OrganizationID, device.DeviceID, action)
if err != nil {
return err
}
// State of the snaps has changed, so request a snap list
if action.Action != "list" {
// Request the list action after a few seconds
time.AfterFunc(10*time.Second, func() {
_ = srv.DeviceSnapList(orgID, clientID)
})
}
return err
}