-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardware.go
94 lines (84 loc) · 2.68 KB
/
hardware.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
package hardware
import (
"errors"
"fmt"
"github.com/sirupsen/logrus"
"github.com/smarthome-go/smarthome/core/database"
"github.com/smarthome-go/smarthome/core/event"
)
type PowerJob struct {
Id int64 `json:"id"`
Switch string `json:"switch"`
Power bool `json:"power"`
}
type JobResult struct {
Id int64 `json:"id"`
Error error `json:"error"`
}
var log *logrus.Logger
func InitLogger(logger *logrus.Logger) {
log = logger
}
// Returns the power state of a given switch
// Checks if the switch exists beforehand
func GetPowerState(switchId string) (bool, error) {
switchItem, switchExists, err := database.GetSwitchById(switchId)
if err != nil {
return false, err
}
if !switchExists {
return false, fmt.Errorf("Could not get power state of switch '%s': switch does not exists", switchId)
}
return switchItem.PowerOn, nil
}
// As setPower, just with additional logs
func SetPower(switchId string, powerOn bool) error {
err := setPower(switchId, powerOn)
if err != nil {
go event.Warn(
"Hardware Error",
fmt.Sprintf("The hardware failed while a user tried to interact with switch '%s': Error: %s",
switchId,
err.Error(),
),
)
return err
}
if powerOn {
go event.Info("Switch Activated", fmt.Sprintf("Switch '%s' was activated", switchId))
} else {
go event.Info("Switch Deactivated", fmt.Sprintf("Switch '%s' was deactivated", switchId))
}
return nil
}
// Sets the power-state of a specific switch
// Checks if the switch exists
// Checks if the user has all required permissions
// Sends a power request to all available nodes
func SetSwitchPowerAll(switchId string, powerOn bool, username string) error {
_, switchExists, err := database.GetSwitchById(switchId)
if err != nil {
return err
}
if !switchExists {
return fmt.Errorf("Failed to set power: switch '%s' does not exist", switchId)
}
userHasPowerPermission, err := database.UserHasPermission(username, database.PermissionPower)
if err != nil {
return fmt.Errorf("Failed to set power: could not check if user is allowed to interact with switches: %s", err.Error())
}
if !userHasPowerPermission {
return errors.New("Failed to set power: user is not allowed to interact with switches")
}
userHasSwitchPermission, err := database.UserHasSwitchPermission(username, switchId)
if err != nil {
return fmt.Errorf("Failed to set power: could not check if user is allowed to interact with this switch: %s", err.Error())
}
if !userHasSwitchPermission {
return fmt.Errorf("Failed to set power: user is not allowed to interact with switch '%s'", switchId)
}
if err := SetPower(switchId, powerOn); err != nil {
return fmt.Errorf("Failed to set power: hardware error: %s", err.Error())
}
return nil
}