-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
172 lines (152 loc) · 4.17 KB
/
sql.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v2"
)
// CreateTableStmt does not need no stinking migrations
const CreateTableStmt string = `
CREATE TABLE batteryinfo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
charge_now INTEGER NULL,
charge_full INTEGER NULL,
charge_design INTEGER NULL,
current_now INTEGER NULL,
voltage_now INTEGER NULL,
charging INTEGER NULL,
timestamp INTEGER NULL,
cycle_count INTEGER NULL
);`
//BatteryDataRow holds a measurement
type BatteryDataRow struct {
Id int64
ChargeNow int64
ChargeFull int64
ChargeFullDesign int64
CurrentNow int64
VoltageNow int64
Charging int64
Timestamp int64
Cycles int64
}
func (bdw *BatteryDataRow) GetPower() float64 {
return float64(bdw.VoltageNow) / 1000000.0 * float64(bdw.CurrentNow) / 1000000.0
}
func (bdw *BatteryDataRow) GetCurrentNow() float64 {
return float64(bdw.CurrentNow) / 1000.0
}
func (bdw *BatteryDataRow) GetVoltageNow() float64 {
return float64(bdw.VoltageNow) / 1000.0
}
func (bdw *BatteryDataRow) GetCharging() bool {
if bdw.Charging == 0 {
return false
} else {
return true
}
}
func (bdw *BatteryDataRow) GetChargeNow() float64 {
return float64(bdw.ChargeNow) / 1000.0
}
func (bdw *BatteryDataRow) GetChargeFull() float64 {
return float64(bdw.ChargeFull) / 1000.0
}
func (bdw *BatteryDataRow) GetChargeFullDesign() float64 {
return float64(bdw.ChargeFullDesign) / 1000.0
}
func (bdw *BatteryDataRow) GetCapacityPermille() int64 {
return 1000.0 * bdw.ChargeNow / bdw.ChargeFull
}
func (bdw *BatteryDataRow) GetCapacityDegradation() int64 {
return -1000.0*bdw.ChargeFull/bdw.ChargeFullDesign + 1000
}
func (bdw *BatteryDataRow) Compute() *BatteryDataComputed {
return &BatteryDataComputed{
bdw.Id,
bdw.Timestamp,
bdw.GetCharging(),
bdw.GetPower(),
bdw.GetCurrentNow(),
bdw.GetVoltageNow(),
bdw.GetChargeNow(),
bdw.GetChargeFull(),
bdw.GetChargeFullDesign(),
bdw.GetCapacityPermille(),
bdw.GetCapacityDegradation(),
bdw.Cycles,
}
}
type BatteryDataComputed struct {
Id int64 `json:"id" yaml:"id"`
Timestamp int64 `json:"timestamp" yaml:"timestamp"`
Charging bool `json:"charging" yaml:"charging"`
Power float64 `json:"power_W" yaml:"power_W"`
CurrentNow float64 `json:"current_now_mA" yaml:"current_now_mA"`
VoltageNow float64 `json:"voltage_now_mV" yaml:"voltage_now_mV"`
ChargeNow float64 `json:"charge_now_mAh" yaml:"charge_now_mAh"`
ChargeFull float64 `json:"charge_full_mAh" yaml:"charge_full_mAh"`
ChargeFullDesign float64 `json:"charge_full_design_mAh" yaml:"charge_full_design_mAh"`
CapacityPermille int64 `json:"capacity_permille" yaml:"capacity_permille"`
CapacityDegradation int64 `json:"capacity_degration_permille" yaml:"capacity_degration_permille"`
Cycles int64 `json:"cycles" yaml:"cycles"`
}
func (bdc *BatteryDataComputed) String() string {
switch *Cli.OutputFormat {
case "yaml":
return bdc.Yaml()
case "json":
return bdc.Json()
default:
return bdc.Plaintext()
}
}
func (bdc *BatteryDataComputed) Plaintext() string {
fString := ` Timestamp: %10d
Charging: %10t
Power: %10.2f W
CurrentNow: %10.1f mA
VoltageNow: %10.1f mV
ChargeNow: %10.1f mAh
ChargeFull: %10.1f mAh
ChargeFullDesign: %10.1f mAh
CapacityPermille: %10d ‰
CapacityDegration: %10d ‰
Cycles: %10d
`
return fmt.Sprintf(
fString,
bdc.Timestamp,
bdc.Charging,
bdc.Power,
bdc.CurrentNow,
bdc.VoltageNow,
bdc.ChargeNow,
bdc.ChargeFull,
bdc.ChargeFullDesign,
bdc.CapacityPermille,
bdc.CapacityDegradation,
bdc.Cycles,
)
}
func (bdc *BatteryDataComputed) Json() string {
asBytes, err := json.Marshal(bdc)
checkErr(err)
return string(asBytes)
}
func (bdc *BatteryDataComputed) Yaml() string {
asBytes, err := yaml.Marshal(bdc)
checkErr(err)
return string(asBytes)
}
// CreateDatabaseAndTables or bust!
func CreateDatabaseAndTables(db *sql.DB) {
_, err := db.Exec(CreateTableStmt)
checkErr(err)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}