Skip to content
Permalink
d35dd797a9
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
55 lines (47 sloc) 1.4 KB
package main
import (
"math/rand"
"time"
)
// This file should only handle initialization of actors
func main() {
go dataReadLoop() // Start our async read loop
client.start() // Start our client
go dataSendLoop() // Start our loop to tell the client when to send
select {} // Immortality
}
func dataSendLoop() {
for {
time.Sleep(time.Second * dataSendPeriod)
client.sendData()
}
}
// TODO: when we have a real modbus connection the next two funcs will be moved into a modbus manager
func dataReadLoop() { // Read data all day every day
for {
dataBuffer := make(map[string][]StampedReading)
reading := readData() // TODO: error handling
for metric, value := range reading { // Put our data in the buffer
if record, found := dataBuffer[metric]; !found {
dataBuffer[metric] = []StampedReading{value}
} else {
dataBuffer[metric] = append(record, value)
}
}
client.send <- dataBuffer
time.Sleep(time.Second * dataReadPeriod)
}
}
var oldReadings = map[string]StampedReading{}
func readData() map[string]StampedReading {
newReadings := map[string]StampedReading{}
for _, element := range metrics {
newRead := StampedReading{
int(time.Now().Unix()),
0.875*oldReadings[element.Name].Value + 0.125*((rand.Float32()*(element.UBound-element.LBound))+element.LBound),
}
newReadings[element.Name] = newRead
}
oldReadings = newReadings
return newReadings
}