-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (113 loc) · 2.86 KB
/
main.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
package main
import (
"fmt"
// "flag"
"os"
"log"
"time"
"strings"
"strconv"
"regexp"
"github.com/tkanos/gonfig"
"github.com/stianeikeland/go-rpio/v4"
)
type pc struct {
Temp_Limit float64
GPIO_port string
GPIO int
Log_Path string
Hysteresys float64
Current_CPU_temp float64
Fan_Enable bool
CPU_Temp_Path string
}
var node pc = pc{}
var cfgFile string = "gtemp.conf"
func check(err error) {
if err != nil {
log.Fatal(err.Error())
}
}
func readCfg(cfg string) (n pc) {
err := gonfig.GetConf(cfg, &n)
check(err)
//рассчитываем номер порта GPIO
n.GPIO,_ = convertGpioPort(n.GPIO_port)
return n
}
func convertGpioPort(s string) (t int, err error) {
re, err := regexp.Compile(`^gpio\d_[a-z]\d`)
matched := re.MatchString(s)
if matched {
var s1 []string = strings.Split(s, "")
m := map[string]string {
"a": "0",
"b": "1",
"c": "2",
"d": "3",
}
t1,_ := strconv.Atoi(s1[4])
t2,_ := strconv.Atoi(m[s1[6]])
t3,_ := strconv.Atoi(s1[7])
t = t1*32 + t2*8 + t3
} else {
t,err = strconv.Atoi(s)
}
return t, err
}
func logConfigure() {
if node.Log_Path != ""{
file, err := os.OpenFile(node.Log_Path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
check(err)
log.SetOutput(file)
}
}
func doWork() {
for {
node.Current_CPU_temp = getCPUTemp(node.CPU_Temp_Path)
if node.Current_CPU_temp >= node.Temp_Limit {
fanControll(node.GPIO, true)
} else if node.Current_CPU_temp <= node.Temp_Limit - node.Hysteresys {
fanControll(node.GPIO, false)
}
time.Sleep(1 * time.Second)
}
}
func getCPUTemp(path string) (t float64) {
bytes, err := os.ReadFile(path);
check(err)
fileText := string(bytes[:]);
re, err := regexp.Compile(`^\d+`)
t,err = strconv.ParseFloat(re.FindString(fileText), 64)
t = t/1000
check(err)
return t
}
func fanControll(n int , command bool) {
m := map[string]bool {
"Low": false,
"High": true,
}
node.Fan_Enable = m[string(rpio.Pin(n).Read())]
if command && node.Fan_Enable {
rpio.Pin(n).High()
log.Printf("Current CPU Temperature: %4.2f(%4.2f), fan is ON",node.Current_CPU_temp, node.Temp_Limit)
}
if !command && !node.Fan_Enable {
rpio.Pin(n).Low()
log.Printf("Current CPU Temperature: %4.2f(%4.2f), fan is OFF",node.Current_CPU_temp, node.Temp_Limit)
}
}
func main() {
// err := rpio.Open()
//check(err)
//определяем модель и версию платы
// getHW()
// читаем конфиг из файла или из ключей
node = readCfg(cfgFile)
// настраиваем логирование
logConfigure()
// считываем температуру в цикле и управляем
doWork()
fmt.Println(node)
}