Skip to content
Permalink
demo-day
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
package main
import (
"math"
"math/rand"
"time"
)
// This file should only handle initialization of actors
func main() {
go dataReadLoop() // Start our async read loop
socketClient.start() // Start our client
modbusClient.start()
go dataSendLoop() // Start our loop to tell the client when to send
go statusReadLoop() // Start our loop to read status updates
select {} // Immortality
}
func dataSendLoop() {
for {
time.Sleep(time.Second * dataSendPeriod)
socketClient.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 := readDummyData() // 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)
}
}
socketClient.dataSend <- dataBuffer
time.Sleep(time.Second * dataReadPeriod)
}
}
func statusReadLoop() { //
for {
updates := modbusClient.getStatusUpdates()
digitalUpdates := modbusClient.getDigitalUpdates()
for k, v := range digitalUpdates {
updates[k] = v
}
if len(updates) > 0 {
packetData := StatusUpdates{int(time.Now().Unix()), updates}
go socketClient.sendStatusUpdates(packetData) // Sends statuses immediately
}
time.Sleep(time.Second * statusReadPeriod)
}
}
var oldReadings = map[string]StampedReading{}
func readDummyData() map[string]StampedReading {
newReadings := map[string]StampedReading{}
for _, element := range metrics {
newRead := StampedReading{
int(time.Now().Unix()),
int32(math.Round(0.875*float64(oldReadings[element.Name].Value) + 0.125*float64((rand.Float32()*(element.UBound-element.LBound))+element.LBound))),
}
newReadings[element.Name] = newRead
}
oldReadings = newReadings
return newReadings
}
var oldStatuses = map[string]int{}
func readDummyStatuses() map[string]int {
newStatuses := make(map[string]int)
for _, status := range _statuses {
newStatuses[status] = rand.Intn(2)
}
updateMap := make(map[string]int)
if len(oldStatuses) == 0 {
oldStatuses = newStatuses
updateMap = newStatuses
} else {
for k, v := range oldStatuses {
newv := newStatuses[k]
if newv != v {
updateMap[k] = newv
}
}
oldStatuses = newStatuses
}
return updateMap
}