This repository has been archived by the owner on May 18, 2023. It is now read-only.
/
client.go
159 lines (129 loc) · 4.02 KB
/
client.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
package baikal
import (
"context"
"fmt"
"strconv"
"github.com/ka2n/masminer/machine"
"github.com/ka2n/masminer/machine/asic/base"
"golang.org/x/crypto/ssh"
)
type Client struct {
base.Client
systemInfo *SystemInfo
}
func (c *Client) MineStop(ctx context.Context) error {
return base.RunRemoteShell(ctx, c.SSH, minerStopCMD)
}
func (c *Client) MineStart(ctx context.Context) error {
return base.RunRemoteShell(ctx, c.SSH, minerStartCMD)
}
func (c *Client) Restart(ctx context.Context) error {
_, err := base.OutputMinerRPC(ctx, c.SSH, "restart", "")
if err != nil {
return err
}
return nil
}
func (c *Client) Reboot(ctx context.Context) error {
return base.RunRemoteShell(ctx, c.SSH, "shutdown -r +5")
}
func (c *Client) SSHConfig(host string) (string, *ssh.ClientConfig) {
return host + ":22", &ssh.ClientConfig{
User: "baikal",
Auth: []ssh.AuthMethod{
ssh.Password("baikal"),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
}
func (c *Client) RigInfo(ctx context.Context) (machine.RigInfo, error) {
var info machine.RigInfo
si, err := c.GetSystemInfoContext(ctx)
if err != nil {
return info, err
}
info.Rig.IPAddr = si.IPAddr
info.Rig.Hostname = si.Hostname
info.Rig.Name = machine.ShortName(si.MACAddr)
info.Rig.MACAddr = si.MACAddr
info.Model = si.ProductType
info.Manufacture = manufactureName
info.HardwareVersion = si.ProductVersion
info.FirmwareVersion = si.FileSystemVersion
info.MinerVersion = si.MinerVersion
info.Algos = Algos(si.ProductType)
info.UptimeSeconds = si.UptimeSeconds
return info, nil
}
func (c *Client) RigStat(ctx context.Context) (machine.RigStat, error) {
var stat machine.RigStat
ms, err := c.GetStatsContext(ctx)
if err != nil {
return stat, err
}
stat.MHS5s = fmt.Sprintf("%.4f", ms.Summary.MHS5S)
stat.MHSAvarage = fmt.Sprintf("%.4f", ms.Summary.MHSAv)
stat.KHS5s = fmt.Sprintf("%.4f", ms.Summary.KHS5S)
stat.KHSAvarage = fmt.Sprintf("%.4f", ms.Summary.KHSAv)
stat.Accepted = strconv.Itoa(ms.Summary.Accepted)
stat.Rejected = strconv.Itoa(ms.Summary.Rejected)
stat.HardwareErrors = strconv.Itoa(ms.Summary.HardwareErrors)
stat.Utility = fmt.Sprintf("%.4f", ms.Summary.Utility)
stat.System.TempCPU = ms.System.TempCPU
if len(ms.Stats) != len(ms.Devs) {
return stat, fmt.Errorf("invalid stats/devs")
}
stat.Devices = make([]machine.DeviceStat, len(ms.Devs))
for i, ds := range ms.Devs {
st := ms.Stats[i]
var dev machine.DeviceStat
dev.Chips = st.ChipCount
dev.Frequency = strconv.Itoa(st.Clock)
dev.TempChip = fmt.Sprintf("%.1f", ds.Temperature)
dev.HardwareErrors = strconv.Itoa(ds.HardwareErrors)
dev.Hashrate = fmt.Sprintf("%.4f", ds.MHS5S)
stat.Devices[i] = dev
}
stat.Pools = make([]machine.PoolStat, len(ms.Pools))
for i, pl := range ms.Pools {
var p machine.PoolStat
p.URL = pl.URL
p.User = pl.User
p.Algo = pl.Algorithm
p.Status = pl.Status
p.StratumActive = pl.StratumActive
p.Priority = pl.Priority
p.Getworks = strconv.Itoa(pl.Getworks)
p.Accepted = strconv.Itoa(pl.Accepted)
p.Rejected = strconv.Itoa(pl.Rejected)
p.Discarded = strconv.Itoa(pl.Discarded)
p.Stale = strconv.Itoa(pl.Stale)
p.DifficultyAccepted = fmt.Sprintf("%.4f", pl.DifficultyAccepted)
p.DifficultyRejected = fmt.Sprintf("%.4f", pl.DifficultyRejected)
p.DifficultyStale = fmt.Sprintf("%.4f", pl.DifficultyStale)
p.LastShareDifficulty = fmt.Sprintf("%.4f", pl.LastShareDifficulty)
p.LastShareTime = strconv.Itoa(pl.LastShareTime)
stat.Pools[i] = p
}
return stat, nil
}
func (c *Client) MinerSetting(ctx context.Context) (machine.MinerSetting, error) {
var s machine.MinerSetting
ms, err := getMinerSetting(ctx, c.SSH)
if err != nil {
return s, err
}
ps, err := getMinerPools(ctx, c.SSH)
if err != nil {
return s, err
}
return getCommonMinerSetting(ms, ps), nil
}
func (c *Client) SetMinerSetting(ctx context.Context, setting machine.MinerSetting) error {
var ms MinerSetting
var ps []PoolSetting
if err := loadCommonMinerSetting(setting, &ms, &ps); err != nil {
return err
}
return writeMinerAndPoolSetting(ctx, c.SSH, ms, ps)
}