-
Notifications
You must be signed in to change notification settings - Fork 4
/
imx6d.go
102 lines (83 loc) · 1.86 KB
/
imx6d.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
// Copyright © 2015-2020 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
package imx6d
import (
"fmt"
"io/ioutil"
"strconv"
"syscall"
"time"
"github.com/platinasystems/goes"
"github.com/platinasystems/goes/cmd"
"github.com/platinasystems/goes/external/redis"
"github.com/platinasystems/goes/external/redis/publisher"
"github.com/platinasystems/goes/lang"
)
const ERRORMAX = 5
var readError int
type Command struct {
VpageByKey map[string]uint8
pub *publisher.Publisher
last map[string]float64
}
func (*Command) String() string { return "imx6d" }
func (*Command) Usage() string { return "imx6d" }
func (*Command) Apropos() lang.Alt {
return lang.Alt{
lang.EnUS: "ARM CPU temperature daemon, publishes to redis",
}
}
func (*Command) Kind() cmd.Kind { return cmd.Daemon }
func (c *Command) Main(...string) error {
var si syscall.Sysinfo_t
err := redis.IsReady()
if err != nil {
return err
}
c.last = make(map[string]float64)
if c.pub, err = publisher.New(); err != nil {
return err
}
if err = syscall.Sysinfo(&si); err != nil {
return err
}
t := time.NewTicker(10 * time.Second)
for {
select {
case <-goes.Stop:
return nil
case <-t.C:
if err = c.update(); err != nil {
}
}
}
}
func (c *Command) update() error {
for k, _ := range c.VpageByKey {
v, err := ReadTemp()
if err != nil {
if readError < ERRORMAX {
readError++
return nil
}
return err
}
readError = 0
if v != c.last[k] {
c.pub.Print(k, ": ", v)
c.last[k] = v
}
}
return nil
}
func ReadTemp() (float64, error) {
tmp, err := ioutil.ReadFile("/sys/class/thermal/thermal_zone0/temp")
if err != nil {
return 0, err
}
tmp2 := fmt.Sprintf("%.4s", string(tmp[:]))
tmp3, _ := strconv.Atoi(tmp2)
tmp4 := float64(tmp3)
return float64(tmp4 / 100.0), nil
}