-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenergenie.go
215 lines (190 loc) · 5.39 KB
/
energenie.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2018, 2020 Andreas Signer <[email protected]>
*
* This file is part of energenie.
*
* energenie is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* energenie is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
"github.com/asig/energenie/pkg/energenie"
)
var (
client energenie.Client
flagAddress = flag.String("address", "192.168.3.200", "EnerGenie's address")
flagPort = flag.Int("port", 5000, "Port used for native protocol")
flagPassword = flag.String("password", "1", "password to log in to EnerGenie")
flagProtocol = flag.String("protocol", "native", "protocol to use. Possible values are 'native' and 'http'")
)
func init() {
var err error
flag.Parse()
switch strings.ToLower(*flagProtocol) {
case "native":
client, err = energenie.NewNativeClient(*flagAddress, *flagPort, *flagPassword)
case "http":
client, err = energenie.NewHttpClient(*flagAddress, *flagPassword)
default: log.Fatalf("Unsupported protocol %q. Valid values are \"native\" and \"http\"", *flagProtocol);
}
if err != nil {
fmt.Fprintf(os.Stderr, "Can't instantiate a client: %s", err)
os.Exit(1)
}
}
func usage() {
fmt.Fprintln(os.Stderr, `usage: energenie [flags] command [args]
flags:
--address: The EnerGenie's network address
--port: The port used in the native protocol
--password: The password used to log in
--protocol: What protocol to use: "http" or "native"
commands:
status [<socket-spec>]: Print the sockets' status.
on <socket-spec>: Turn sockets on that match <socket-spec>.
off <socket-spec>: Turn sockets off that match <socket-spec>.
<socket-spec> can be a comma-separated list of socket numbers (1 - 4),
ranges, or 'all' as a short cut for 1-4.
Example: 1,2-3 specifies sockets 1, 2, and 3.
`)
os.Exit(1)
}
func atoiOrDie(raw string) int {
i, err := strconv.Atoi(strings.TrimSpace(raw))
if err != nil {
fmt.Fprintf(os.Stderr, "%s is not a valid integer\n", raw)
usage()
}
return i
}
func addAll(set map[energenie.Socket]bool) map[energenie.Socket]bool {
for i := energenie.Socket_Min; i <= energenie.Socket_Max; i++ {
set[i] = true
}
return set
}
func checkSocket(socket energenie.Socket) {
if socket < energenie.Socket_Min || socket > energenie.Socket_Max {
fmt.Fprintf(os.Stderr, "Invalid socket number %d, must be between %d and %d.\n", socket, energenie.Socket_Min, energenie.Socket_Max)
os.Exit(1)
}
}
func addRange(r string, set map[energenie.Socket]bool) map[energenie.Socket]bool {
parts := strings.Split(r, "-")
low := energenie.Socket(atoiOrDie(parts[0]))
checkSocket(low)
hi := energenie.Socket(atoiOrDie(parts[1]))
checkSocket(hi)
if low > hi {
low, hi = hi, low
}
for i := low; i <= hi; i++ {
set[i] = true
}
return set
}
func addSingle(str string, set map[energenie.Socket]bool) map[energenie.Socket]bool {
s := energenie.Socket(atoiOrDie(strings.TrimSpace(str)))
checkSocket(s)
set[s] = true
return set
}
type socketSlice []energenie.Socket
func (p socketSlice) Len() int { return len(p) }
func (p socketSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p socketSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func parseSocketSpec(specString string) []energenie.Socket {
seen := make(map[energenie.Socket]bool)
parts := strings.Split(specString, ",")
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "all" {
seen = addAll(seen)
} else if strings.Index(part, "-") > 0 {
seen = addRange(part, seen)
} else {
seen = addSingle(part, seen)
}
}
var res []energenie.Socket
for key := range seen {
res = append(res, key)
}
sort.Sort(socketSlice(res))
return res
}
func showStatus(socketSpec string) {
sockets := parseSocketSpec(socketSpec)
state, names, err := client.Status()
hasNames := names != nil
if err != nil {
fmt.Fprintf(os.Stderr, "Can't get status: %s", err)
os.Exit(1)
}
for _, socket := range sockets {
status := "off"
if state[socket] {
status = "on"
}
socketName := fmt.Sprintf("%d", socket)
if hasNames {
socketName = socketName + " (" + names[socket] + ")"
}
fmt.Printf("Socket %s: %s\n", socketName, status)
}
}
func switchSockets(on bool, socketSpec string) {
sockets := parseSocketSpec(socketSpec)
states := make(map[energenie.Socket]bool)
for _, s := range sockets {
states[s] = on
}
err := client.Switch(states)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't switch sockets: %s", err)
os.Exit(1)
}
}
func main() {
args := flag.Args()
if len(args) == 0 || len(args) > 2 {
usage()
os.Exit(1)
}
switch args[0] {
case "status":
if len(args) > 2 {
usage()
}
s := "all"
if len(args) == 2 {
s = args[1]
}
showStatus(s)
case "on", "off":
if len(args) != 2 {
usage()
}
switchSockets(args[0] == "on", args[1])
default:
fmt.Fprintf(os.Stderr, "%s is not a valid command.\n", args[0])
usage()
}
}