This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheps.go
69 lines (59 loc) · 1.72 KB
/
eps.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
package smd
import (
"errors"
"log"
"time"
)
// EPS defines the interface for an electrical power subsystem.
type EPS interface {
Drain(voltage, power uint, dt time.Time) error
}
/* Available EPS */
// UnlimitedEPS drain as much as you want, always.
type UnlimitedEPS struct{}
// Drain implements the interface.
func (e *UnlimitedEPS) Drain(voltage, power uint, dt time.Time) error {
return nil
}
// NewUnlimitedEPS returns a dream-like EPS.
func NewUnlimitedEPS() (e *UnlimitedEPS) {
e = new(UnlimitedEPS)
return
}
// TimedEPS sets a hard limit on how long (time-wise) the EPS can deliver any power.
type TimedEPS struct {
turnedOn bool // Stores whether on or off.
turnOnDT time.Time // Time at which it was turned on.
turnOffDT time.Time // Time at which it was turned off.
dischargeTime time.Duration // Max discharge duration.
chargeTime time.Duration // Charging duration.
}
// NewTimedEPS creates a new TimedEPS.
func NewTimedEPS(charge, discharge time.Duration) (t *TimedEPS) {
t = new(TimedEPS)
t.turnedOn = false
t.turnOnDT = time.Now()
t.turnOffDT = time.Now().Add(-charge) // Make the EPS available at start.
t.dischargeTime = discharge
t.chargeTime = charge
return
}
// Drain implements the EPS subsystem.
func (t *TimedEPS) Drain(voltage, power uint, dt time.Time) error {
if t.turnedOn {
if dt.Sub(t.turnOnDT) >= t.dischargeTime {
t.turnedOn = false
t.turnOffDT = dt
log.Println("EPS is off") // TODO: switch to context based logging...
return errors.New("power now exhausted")
}
return nil
}
if dt.Sub(t.turnOffDT) >= t.chargeTime {
t.turnedOn = true
t.turnOnDT = dt
log.Println("EPS is on")
return nil
}
return errors.New("charging incomplete")
}