small changes in sensordata logic

This commit is contained in:
Timo Volkmann 2020-12-10 09:43:44 +01:00
parent f5f98ced5b
commit b03d7fcfa2
2 changed files with 30 additions and 32 deletions

View File

@ -90,12 +90,12 @@ type aggregator struct {
type UnixNanoTime int64 type UnixNanoTime int64
type synchronizer struct { type synchronizer struct {
tcpSerialDelayMs int64 tcpSerialDelayMs int64
//tcpBuffer map[UnixNanoTime]Sensordata //tcpBuffer map[UnixNanoTime]Sensordata
//serialBuffer map[UnixNanoTime]Sensordata //serialBuffer map[UnixNanoTime]Sensordata
//bufferSize int //bufferSize int
mutex *sync.Mutex mutex *sync.Mutex
updateTicker *time.Ticker updateTicker *time.Ticker
// should run concurrently // should run concurrently
// //
// Methods: // Methods:
@ -146,7 +146,7 @@ func (p *pipeline) Process(data *Sensordata) error {
case SOURCE_SERIAL: case SOURCE_SERIAL:
go p.pushSerialDataToBuffer(*data) go p.pushSerialDataToBuffer(*data)
default: default:
return errors.New("invalid data source") panic("pipeline: invalid data source")
} }
return nil return nil
} }
@ -156,7 +156,7 @@ func (p *pipeline) pushTcpDataToBuffer(data Sensordata) {
time.Sleep(time.Duration(p.syn.tcpSerialDelayMs) * time.Millisecond) time.Sleep(time.Duration(p.syn.tcpSerialDelayMs) * time.Millisecond)
} }
p.agr.tcpMutex.Lock() p.agr.tcpMutex.Lock()
p.agr.tcpSensorData = p.agr.tcpSensorData.Consolidate2(data) p.agr.tcpSensorData = p.agr.tcpSensorData.ConsolidateExTime(data)
p.agr.tcpMutex.Unlock() p.agr.tcpMutex.Unlock()
} }
func (p *pipeline) pushSerialDataToBuffer(data Sensordata) { func (p *pipeline) pushSerialDataToBuffer(data Sensordata) {
@ -164,6 +164,6 @@ func (p *pipeline) pushSerialDataToBuffer(data Sensordata) {
time.Sleep(time.Duration(-p.syn.tcpSerialDelayMs) * time.Millisecond) time.Sleep(time.Duration(-p.syn.tcpSerialDelayMs) * time.Millisecond)
} }
p.agr.serialMutex.Lock() p.agr.serialMutex.Lock()
p.agr.serialSensorData = p.agr.serialSensorData.Consolidate2(data) p.agr.serialSensorData = p.agr.serialSensorData.ConsolidateEpochsOnly(data)
p.agr.serialMutex.Unlock() p.agr.serialMutex.Unlock()
} }

View File

@ -31,40 +31,30 @@ func (s Sensordata) isSameEpoch(n Sensordata) bool {
return s.itow == n.itow return s.itow == n.itow
} }
func (s Sensordata) Consolidate(n Sensordata) Sensordata { // Consolidates two sensordata elements if they are in the same epoch
if (s.SourceId != n.SourceId && s != Sensordata{}) { func (s Sensordata) ConsolidateEpochsOnly(n Sensordata) Sensordata {
log.Println(s) s.checkSources(&n)
log.Println(n)
log.Fatalln("Do not consolidate Sensordata from different Sources")
}
if s.isSameEpoch(n) { if s.isSameEpoch(n) {
null := Sensordata{} null := Sensordata{}
//if s.Timestamp == null.Timestamp { s.Timestamp = n.Timestamp }
//if s.Position == null.Position { s.Position = n.Position }
//if s.Orientation == null.Orientation { s.Orientation = n.Orientation }
if n.Timestamp != null.Timestamp && s.Timestamp != n.Timestamp {
s.Timestamp = n.Timestamp
}
if n.Position != null.Position && s.Position != n.Position {
s.Position = n.Position
}
if n.Orientation != null.Orientation && s.Orientation != n.Orientation {
s.Orientation = n.Orientation
}
return s if n.Timestamp == null.Timestamp {
n.Timestamp = s.Timestamp
}
if n.Position == null.Position {
n.Position = s.Position
}
if n.Orientation == null.Orientation {
n.Orientation = s.Orientation
}
} }
return n return n
} }
func (s Sensordata) Consolidate2(n Sensordata) Sensordata { // Consolidates two sensordata elements but ignores timestamps
func (s Sensordata) ConsolidateExTime(n Sensordata) Sensordata {
s.checkSources(&n)
null := Sensordata{} null := Sensordata{}
//if s.Timestamp == null.Timestamp { s.Timestamp = n.Timestamp }
//if s.Position == null.Position { s.Position = n.Position }
//if s.Orientation == null.Orientation { s.Orientation = n.Orientation }
//if n.Timestamp == null.Timestamp {
// n.Timestamp = s.Timestamp
//}
if n.Position == null.Position { if n.Position == null.Position {
n.Position = s.Position n.Position = s.Position
} }
@ -74,6 +64,14 @@ func (s Sensordata) Consolidate2(n Sensordata) Sensordata {
return n return n
} }
func (s *Sensordata) checkSources(n *Sensordata) {
if (s.SourceId != n.SourceId && *s != Sensordata{}) {
log.Println(s)
log.Println(n)
log.Fatalln("Do not consolidate Sensordata from different Sources")
}
}
var ( var (
errNotImplemented = errors.New("message not implemented") errNotImplemented = errors.New("message not implemented")
errRawMessage = errors.New("raw message") errRawMessage = errors.New("raw message")