Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Changed datatype of stampedreading to float32 for now, created far mo…
…re realistic data for readings, changed the rate at which data is polled and sent to server to be more realistic
  • Loading branch information
Evan Langlais committed Apr 8, 2019
1 parent 57b36ec commit d35dd79
Show file tree
Hide file tree
Showing 3 changed files with 235 additions and 14 deletions.
217 changes: 215 additions & 2 deletions constants.go
Expand Up @@ -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,
},
}
10 changes: 8 additions & 2 deletions dataTypes.go
@@ -1,12 +1,18 @@
package main

type StampedReading struct {
Timestamp int `json:"timestamp"`
Value interface{} `json:"value"`
Timestamp int `json:"timestamp"`
Value float32 `json:"value"`
}

type Packet struct {
ID int `json:"id"`
Kind string `json:"kind"`
Message []byte `json:"message"`
}

type Metric struct {
Name string
LBound float32
UBound float32
}
22 changes: 12 additions & 10 deletions server.go
Expand Up @@ -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}
Expand All @@ -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
}

0 comments on commit d35dd79

Please sign in to comment.