From d35dd797a9ad533f61988cf018286b57dcf9bf09 Mon Sep 17 00:00:00 2001 From: Evan Langlais Date: Mon, 8 Apr 2019 17:01:06 -0400 Subject: [PATCH] Changed datatype of stampedreading to float32 for now, created far more realistic data for readings, changed the rate at which data is polled and sent to server to be more realistic --- constants.go | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++- dataTypes.go | 10 ++- server.go | 22 +++--- 3 files changed, 235 insertions(+), 14 deletions(-) diff --git a/constants.go b/constants.go index 3b417a1..29ae098 100644 --- a/constants.go +++ b/constants.go @@ -17,7 +17,220 @@ const ( messageReceived = "received" // Time (in seconds) to wait between performing read/send operations - dataReadPeriod = 1 - dataSendPeriod = 10 + dataReadPeriod = 5 + dataSendPeriod = 30 heartbeatPeriod = 10 ) + +var metrics = [...]Metric{ + { + "l1.l2.voltage", + 0, + 300, + }, + { + "l2.l3.voltage", + 0, + 300, + }, + { + "l3.l1.voltage", + 0, + 300, + }, + { + "l1.l0.voltage", + 0, + 300, + }, + { + "l2.l0.voltage", + 0, + 300, + }, + { + "l3.l0.voltage", + 0, + 300, + }, + { + "l1.current", + 0, + 300, + }, + { + "l2.current", + 0, + 300, + }, + { + "l3.current", + 0, + 300, + }, + { + "frequency", + 0, + 3000, + }, + { + "total.kw", + 0, + 1000000, + }, + { + "rate.kw", + 0, + 100, + }, + { + "total.pf", + 0, + 100, + }, + { + "l1.kw", + 0, + 1000000, + }, + { + "l1.pf", + 0, + 100, + }, + { + "l2.kw", + 0, + 1000000, + }, + { + "l2.pf", + 0, + 100, + }, + { + "l3.kw", + 0, + 1000000, + }, + { + "l3.pf", + 0, + 100, + }, + { + "total.kvar", + 0, + 1000, + }, + { + "l1.kvar", + 0, + 1000, + }, + { + "l2.kvar", + 0, + 1000, + }, + { + "l3.kvar", + 0, + 1000, + }, + { + "total.kva", + 0, + 1000, + }, + { + "l1.kva", + 0, + 1000, + }, + { + "l2.kva", + 0, + 1000, + }, + { + "l3.kva", + 0, + 1000, + }, + { + "oil.pressure", + 0, + 200, + }, + { + "coolant.temp", + 0, + 120, + }, + { + "engine.rpm", + 0, + 3000, + }, + { + "battery.voltage", + 0, + 100, + }, + { + "fuel.pressure", + 0, + 1000, + }, + { + "fuel.temp", + 0, + 200, + }, + { + "fuel.rate", + 0, + 500, + }, + { + "coolant.pressure", + 0, + 200, + }, + { + "coolant.level", + 0, + 100, + }, + { + "oil.temp", + 0, + 200, + }, + { + "oil.level", + 0, + 100, + }, + { + "crankcase.pressure", + 0, + 200, + }, + { + "ambient.temp", + 0, + 200, + }, + { + "intake.temp", + 0, + 200, + }, + { + "intake.pressure", + 0, + 200, + }, +} diff --git a/dataTypes.go b/dataTypes.go index 93f9dc9..5e4369d 100644 --- a/dataTypes.go +++ b/dataTypes.go @@ -1,8 +1,8 @@ package main type StampedReading struct { - Timestamp int `json:"timestamp"` - Value interface{} `json:"value"` + Timestamp int `json:"timestamp"` + Value float32 `json:"value"` } type Packet struct { @@ -10,3 +10,9 @@ type Packet struct { Kind string `json:"kind"` Message []byte `json:"message"` } + +type Metric struct { + Name string + LBound float32 + UBound float32 +} diff --git a/server.go b/server.go index 060ff4b..a7ab522 100644 --- a/server.go +++ b/server.go @@ -26,7 +26,7 @@ func dataSendLoop() { func dataReadLoop() { // Read data all day every day for { dataBuffer := make(map[string][]StampedReading) - reading := readData() // TODO: error handling + 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} @@ -39,15 +39,17 @@ func dataReadLoop() { // Read data all day every day } } +var oldReadings = map[string]StampedReading{} + func readData() map[string]StampedReading { - return map[string]StampedReading{ - "oil.temp": { - Timestamp: int(time.Now().Unix()), - Value: rand.Intn(150), - }, - "fuel.temp": { - Timestamp: int(time.Now().Unix()), - Value: rand.Intn(150), - }, + 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 }