gyrogpsc/core/service.go

150 lines
3.1 KiB
Go

package core
import (
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"time"
)
type OpMode uint8
const (
STOPPED OpMode = iota
LIVE
RECORDING
REPLAY
)
type trackingService struct {
current *Tracking
config *Configuration
pipe *pipeline
repo Repo
opMode OpMode
collectors []Collector
}
func TrackingService(r Repo, d Publisher, c *Configuration) *trackingService {
t := &Tracking{}
ts := &trackingService{
current: t,
opMode: STOPPED,
config: c,
repo: r,
pipe: NewPipeline(d, t, c),
collectors: nil,
}
//ts.pipe.Run()
return ts
}
//const(
// errA error = errors.New("A")
//)
func (t *trackingService) AllTrackings() {
panic("implement me")
}
func (t *trackingService) NewSetup(cols ...CollectorType) {
logrus.Info("SERVICE: NEW SETUP")
if t.opMode == RECORDING {
logrus.Println("trackingservice: no reset while recording")
return
}
if t.opMode == LIVE {
logrus.Println("trackingservice: stop currently running setup before creating new one")
t.StopAll()
}
logrus.Debug("new tracking:", cols)
t.opMode = LIVE
t.collectors = nil
for _, col := range cols {
t.collectors = append(t.collectors, NewCollector(col, t.pipe, t.config))
}
t.safelyReplaceTracking(emptyTracking())
t.current.Collectors = cols
for _, e := range t.collectors {
e.Collect()
}
t.pipe.Run()
//time.Sleep(3 * time.Second)
}
func (t *trackingService) StartRecord() {
logrus.Info("SERVICE: START RECORD")
if t.opMode != LIVE {
logrus.Println("trackingservice: wrong mode of operation")
return
}
t.opMode = RECORDING
t.current.TimeCreated = time.Now()
t.pipe.Record()
}
func (t *trackingService) StopRecord() {
logrus.Info("SERVICE: STOP RECORD")
if t.opMode != RECORDING {
logrus.Println("trackingservice: couldn't stop. not recording")
return
}
t.opMode = LIVE
t.pipe.StopRecord()
m1.Lock()
m2.Lock()
err := t.repo.Save(*t.current)
m2.Unlock()
m1.Unlock()
if err != nil {
logrus.Println(err)
}
t.safelyReplaceTracking(emptyTracking())
}
func (t *trackingService) StopAll() {
logrus.Info("SERVICE: STOP ALL")
if t.opMode == RECORDING {
logrus.Println("trackingservice: stop recording gracefully")
t.StopRecord()
}
t.opMode = STOPPED
t.pipe.Close()
for _, e := range t.collectors {
e.Close()
}
t.collectors = nil
t.safelyReplaceTracking(emptyTracking())
}
func (t *trackingService) DeleteTracking(trackingId uuid.UUID) {
panic("implement me")
}
func (t *trackingService) StartReplay() {
panic("implement me")
}
func (t *trackingService) PauseReplay() {
panic("implement me")
}
func (t *trackingService) StopReplay() {
panic("implement me")
}
func (t *trackingService) LoadTracking(trackingId uuid.UUID) {
}
func (t *trackingService) safelyReplaceTracking(tr Tracking) {
m1.Lock()
m2.Lock()
*t.current = tr
m2.Unlock()
m1.Unlock()
}