Permalink
Cannot retrieve contributors at this time
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?
Generator-Server/server.go
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
96 lines (85 sloc)
2.41 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |