-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatastore.go
More file actions
52 lines (45 loc) · 975 Bytes
/
datastore.go
File metadata and controls
52 lines (45 loc) · 975 Bytes
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
package main
import (
"fmt"
"sync"
"time"
)
type CurrentData struct {
Weight float64
TimeStamp time.Time
// We'll calculate this when setting so the
// bot doesn't have to
Remaining float64
}
type Datastore struct {
data CurrentData
lock *sync.RWMutex
}
func NewDatastore() *Datastore {
return &Datastore{
data: CurrentData{},
lock: &sync.RWMutex{},
}
}
func (d *Datastore) Get() CurrentData {
d.lock.RLock()
defer d.lock.RUnlock()
return d.data
}
func (d *Datastore) GetString() string {
d.lock.RLock()
defer d.lock.RUnlock()
return fmt.Sprintf(
"Well, as of %s the cylinder weighs %.0f lbs which kinda translates into %.0f%% remaining",
d.data.TimeStamp.Format("Mon Jan _2 03:04PM 2006"),
d.data.Weight,
d.data.Remaining,
)
}
func (d *Datastore) Set(weight float64, timestamp time.Time, remaining float64) {
d.lock.Lock()
defer d.lock.Unlock()
d.data.Weight = weight
d.data.TimeStamp = timestamp
d.data.Remaining = remaining
}