forked from crypt0train/go-cgminer-api
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.go
164 lines (137 loc) · 5.05 KB
/
api.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
package cgminer
import (
"context"
"errors"
"fmt"
"strconv"
)
// Version returns version information
//
// For context-based requests use `VersionContext()`
func (c *CGMiner) Version() (*Version, error) {
return c.VersionContext(context.Background())
}
// VersionContext returns version information using provided context
func (c *CGMiner) VersionContext(ctx context.Context) (*Version, error) {
resp := new(VersionResponse)
if err := c.CallContext(ctx, NewCommandWithoutParameter("version"), resp); err != nil {
return nil, err
}
if len(resp.Version) < 1 {
return nil, errors.New("no version in JSON response")
}
if len(resp.Version) > 1 {
return nil, errors.New("too many versions in JSON response")
}
return &resp.Version[0], nil
}
// Devs returns basic information on the miner. See the Devs struct.
func (c *CGMiner) Devs() (*[]Devs, error) {
return c.DevsContext(context.Background())
}
// DevsContext returns basic information on the miner. See the Devs struct.
func (c *CGMiner) DevsContext(ctx context.Context) (*[]Devs, error) {
resp := new(devsResponse)
if err := c.CallContext(ctx, NewCommandWithoutParameter("devs"), resp); err != nil {
return nil, err
}
return &resp.Devs, nil
}
// Summary returns basic information on the miner. See the Summary struct.
func (c *CGMiner) Summary() (*Summary, error) {
return c.SummaryContext(context.Background())
}
// SummaryContext returns basic information on the miner. See the Summary struct.
func (c *CGMiner) SummaryContext(ctx context.Context) (*Summary, error) {
resp := new(summaryResponse)
if err := c.CallContext(ctx, NewCommandWithoutParameter("summary"), resp); err != nil {
return nil, err
}
if len(resp.Summary) > 1 {
return nil, errors.New("Received multiple Summary objects")
}
if len(resp.Summary) < 1 {
return nil, errors.New("No summary info received")
}
return &resp.Summary[0], nil
}
// Stats returns basic information on the miner. See the Stats struct.
func (c *CGMiner) Stats() (Stats, error) {
return c.StatsContext(context.Background())
}
// StatsContext returns basic information on the miner. See the Stats struct.
func (c *CGMiner) StatsContext(ctx context.Context) (Stats, error) {
resp := new(statsResponse)
if err := c.CallContext(ctx, NewCommandWithoutParameter("stats"), resp); err != nil {
return nil, err
}
if len(resp.Stats) < 1 {
return nil, errors.New("no stats in JSON response")
}
if len(resp.Stats) > 1 {
return nil, errors.New("too many stats in JSON response")
}
return &resp.Stats[0], nil
}
// PoolsContext returns a slice of Pool structs, one per pool.
func (c *CGMiner) PoolsContext(ctx context.Context) ([]Pool, error) {
resp := new(poolsResponse)
if err := c.CallContext(ctx, NewCommandWithoutParameter("pools"), resp); err != nil {
return nil, err
}
return resp.Pools, nil
}
// Pools returns a slice of Pool structs, one per pool.
func (c *CGMiner) Pools() ([]Pool, error) {
return c.PoolsContext(context.Background())
}
// AddPool adds the given URL/username/password combination to the miner's
// pool list.
func (c *CGMiner) AddPool(url, username, password string) error {
return c.AddPoolContext(context.Background(), url, username, password)
}
// AddPoolContext adds the given URL/username/password combination to the miner's
// pool list with provided context.
func (c *CGMiner) AddPoolContext(ctx context.Context, url, username, password string) error {
// TODO: Don't allow adding a pool that's already in the pool list
// TODO: Escape commas in the URL, username, and password
resp := new(GenericResponse)
parameter := fmt.Sprintf("%s,%s,%s", url, username, password)
return c.CallContext(ctx, NewCommand("pools", parameter), resp)
}
func (c *CGMiner) EnablePool(pool *Pool) error {
return c.EnablePoolContext(context.Background(), pool)
}
func (c *CGMiner) DisablePool(pool *Pool) error {
return c.DisablePoolContext(context.Background(), pool)
}
func (c *CGMiner) EnablePoolContext(ctx context.Context, pool *Pool) error {
return c.CallContext(ctx, NewCommand("enablepool", strconv.FormatInt(pool.Pool, 10)), nil)
}
func (c *CGMiner) DisablePoolContext(ctx context.Context, pool *Pool) error {
return c.CallContext(ctx, NewCommand("disablepool", strconv.FormatInt(pool.Pool, 10)), nil)
}
func (c *CGMiner) RemovePool(pool *Pool) error {
return c.Call(NewCommand("removepool", strconv.FormatInt(pool.Pool, 10)), nil)
}
func (c *CGMiner) SwitchPool(pool *Pool) error {
return c.Call(NewCommand("switchpool", strconv.FormatInt(pool.Pool, 10)), nil)
}
func (c *CGMiner) Restart() error {
return c.Call(NewCommandWithoutParameter("restart"), nil)
}
func (c *CGMiner) Quit() error {
return c.Call(NewCommandWithoutParameter("quit"), nil)
}
// CheckAvailableCommands - check all commands, that supported by device
// func (miner *CGMiner) CheckAvailableCommands() {
// // TODO: add all commands, please note: your ip need to be in "api-allow" range
// commandsList := []string{"version"}
// for _, cmd := range commandsList {
// result, err := miner.runCommand("check", cmd)
// if err != nil {
// log.Println(err)
// }
// log.Println(result)
// }
// }