/
report_persist.go
63 lines (55 loc) · 1.7 KB
/
report_persist.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
package server
// This file contains methods related to saving reports to disk and loading
// them off of disk.
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/glowlabs-org/gca-backend/glow"
)
// loadEquipmentReports will load all of the equipment reports that are saved
// to disk.
func (gcas *GCAServer) loadEquipmentReports() error {
filepath := filepath.Join(gcas.baseDir, "equipment-reports.dat")
rawData, err := ioutil.ReadFile(filepath)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("unable to open reports file: %v", err)
}
f, err := os.Create(filepath)
if err != nil {
return fmt.Errorf("unable to create reports file: %v", err)
}
f.Close()
return nil
}
// Check that the data is a sensisble length.
if len(rawData)%80 != 0 {
return fmt.Errorf("reports file has an unexpected size")
}
// Parse all of the reports and integrate them into the state.
for i := 0; i < len(rawData)/80; i++ {
report, err := gcas.parseReport(rawData[i*80 : i*80+80])
if err != nil {
return fmt.Errorf("corrupt report: %v", err)
}
gcas.integrateReport(report)
}
return nil
}
// saveEquipmentReport will save an equipment report to disk, so that the
// report will still be available after a restart.
func (gcas *GCAServer) saveEquipmentReport(ea glow.EquipmentReport) error {
filepath := filepath.Join(gcas.baseDir, "equipment-reports.dat")
file, err := os.OpenFile(filepath, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("unable to open file to save equipment report: %v", err)
}
defer file.Close()
_, err = file.Write(ea.Serialize())
if err != nil {
return fmt.Errorf("unable to write to file to save equipment report: %v", err)
}
return nil
}