blob: e275c658406bce9a4f553ae27845e4c052d65ef9 (
plain) (
blame)
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
|
package main
import "log"
type Building map[RoomID]Record[Humidity]
func newBuilding(roomIDs []RoomID) Building {
b := make(Building)
for _, id := range roomIDs {
b[id] = newRecord[Humidity](historySize)
}
return b
}
func (b Building) Close() {
for _, record := range b {
record.Close()
}
}
// Calculate the average humidity in the building. Returns false if there is not enough data available.
func (b Building) average() (Humidity, bool) {
var sum Humidity = 0
nRooms := 0
for room, record := range b {
c := make(chan Entry[Humidity])
record.getRecent <- c
if e, ok := <-c; ok {
sum += e.v
nRooms++
} else {
log.Printf("Warning: no humidity for room '%s'\n", room)
}
}
if nRooms == 0 {
log.Println("Warning: not enough data to calculate average humidity")
return -1.0, false
}
return sum / Humidity(nRooms), true
}
|