diff --git a/internal/pkg/service/mouseClickHandler.go b/internal/pkg/service/mouseClickHandler.go index f8bee33..1994828 100644 --- a/internal/pkg/service/mouseClickHandler.go +++ b/internal/pkg/service/mouseClickHandler.go @@ -8,15 +8,17 @@ import ( "github.com/prashantgupta24/activity-tracker/pkg/activity" ) -type MouseClickHandler struct{} +type MouseClickHandler struct { + tickerCh chan struct{} +} -func (m *MouseClickHandler) Start(activityCh chan *activity.Type) (tickerCh chan struct{}) { - tickerCh = make(chan struct{}) +func (m *MouseClickHandler) Start(activityCh chan *activity.Type) { + m.tickerCh = make(chan struct{}) registrationFree := make(chan struct{}) go func() { go addMouseClickRegistration(activityCh, registrationFree) //run once before first check - for range tickerCh { + for range m.tickerCh { log.Printf("mouse clicker checked at : %v\n", time.Now()) select { case _, ok := <-registrationFree: @@ -34,8 +36,18 @@ func (m *MouseClickHandler) Start(activityCh chan *activity.Type) (tickerCh chan log.Printf("stopping click handler") return }() +} - return tickerCh +func (m *MouseClickHandler) Trigger() { + //doing it the non-blocking sender way + select { + case m.tickerCh <- struct{}{}: + default: + //service is blocked, handle it somehow? + } +} +func (m *MouseClickHandler) Close() { + close(m.tickerCh) } func addMouseClickRegistration(activityCh chan *activity.Type, registrationFree chan struct{}) { diff --git a/internal/pkg/service/mouseCursorHandler.go b/internal/pkg/service/mouseCursorHandler.go index 8a4845e..abba577 100644 --- a/internal/pkg/service/mouseCursorHandler.go +++ b/internal/pkg/service/mouseCursorHandler.go @@ -8,20 +8,22 @@ import ( "github.com/prashantgupta24/activity-tracker/pkg/activity" ) -type MouseCursorHandler struct{} +type MouseCursorHandler struct { + tickerCh chan struct{} +} type cursorInfo struct { didCursorMove bool currentMousePos *mouse.Position } -func (m *MouseCursorHandler) Start(activityCh chan *activity.Type) (tickerCh chan struct{}) { +func (m *MouseCursorHandler) Start(activityCh chan *activity.Type) { - tickerCh = make(chan struct{}) + m.tickerCh = make(chan struct{}) go func() { lastMousePos := mouse.GetPosition() - for range tickerCh { + for range m.tickerCh { log.Printf("mouse cursor checked at : %v\n", time.Now()) commCh := make(chan *cursorInfo) go checkCursorChange(commCh, lastMousePos) @@ -41,8 +43,18 @@ func (m *MouseCursorHandler) Start(activityCh chan *activity.Type) (tickerCh cha log.Printf("stopping cursor handler") return }() +} - return tickerCh +func (m *MouseCursorHandler) Trigger() { + //doing it the non-blocking sender way + select { + case m.tickerCh <- struct{}{}: + default: + //service is blocked, handle it somehow? + } +} +func (m *MouseCursorHandler) Close() { + close(m.tickerCh) } func checkCursorChange(commCh chan *cursorInfo, lastMousePos *mouse.Position) { diff --git a/internal/pkg/service/screenChangeHandler.go b/internal/pkg/service/screenChangeHandler.go index a6a89af..788e735 100644 --- a/internal/pkg/service/screenChangeHandler.go +++ b/internal/pkg/service/screenChangeHandler.go @@ -8,23 +8,25 @@ import ( "github.com/prashantgupta24/activity-tracker/pkg/activity" ) -type ScreenChangeHandler struct{} +type ScreenChangeHandler struct { + tickerCh chan struct{} +} type screenInfo struct { didScreenChange bool currentPixelColor string } -func (s *ScreenChangeHandler) Start(activityCh chan *activity.Type) (tickerCh chan struct{}) { +func (s *ScreenChangeHandler) Start(activityCh chan *activity.Type) { - tickerCh = make(chan struct{}) + s.tickerCh = make(chan struct{}) go func() { screenSizeX, screenSizeY := robotgo.GetScreenSize() pixelPointX := int(screenSizeX / 2) pixelPointY := int(screenSizeY / 2) lastPixelColor := robotgo.GetPixelColor(pixelPointX, pixelPointY) - for range tickerCh { + for range s.tickerCh { log.Printf("screen change checked at : %v\n", time.Now()) commCh := make(chan *screenInfo) go checkScreenChange(commCh, lastPixelColor, pixelPointX, pixelPointY) @@ -44,14 +46,25 @@ func (s *ScreenChangeHandler) Start(activityCh chan *activity.Type) (tickerCh ch log.Printf("stopping screen change handler") return }() +} - return tickerCh +func (s *ScreenChangeHandler) Trigger() { + //doing it the non-blocking sender way + select { + case s.tickerCh <- struct{}{}: + default: + //service is blocked, handle it somehow? + } +} +func (s *ScreenChangeHandler) Close() { + close(s.tickerCh) } func checkScreenChange(commCh chan *screenInfo, lastPixelColor string, pixelPointX, pixelPointY int) { currentPixelColor := robotgo.GetPixelColor(pixelPointX, pixelPointY) // log.Printf("current pixel color: %v\n", currentPixelColor) // log.Printf("last pixel color: %v\n", lastPixelColor) + //robotgo.MoveMouse(pixelPointX, pixelPointY) if lastPixelColor != currentPixelColor { commCh <- &screenInfo{ didScreenChange: true, diff --git a/internal/pkg/service/service.go b/internal/pkg/service/service.go index 58efb2b..d8890c5 100644 --- a/internal/pkg/service/service.go +++ b/internal/pkg/service/service.go @@ -7,5 +7,7 @@ const ( ) type Instance interface { - Start(chan *activity.Type) chan struct{} + Start(chan *activity.Type) + Trigger() + Close() } diff --git a/pkg/tracker/tracker.go b/pkg/tracker/tracker.go index 6614d27..dc8c8cd 100644 --- a/pkg/tracker/tracker.go +++ b/pkg/tracker/tracker.go @@ -34,14 +34,9 @@ func (tracker *Instance) Start() (heartbeatCh chan *Heartbeat, quit chan struct{ select { case <-tickerWorker.C: log.Printf("tracker worker working at %v\n", time.Now()) - //time to ping all registered service handlers - //doing it the non-blocking sender way - for _, serviceHandler := range tracker.serviceHandlers { - select { - case serviceHandler <- struct{}{}: - default: - //service is blocked - } + //time to trigger all registered services + for service := range tracker.services { + service.Trigger() } case <-tickerHeartbeat.C: log.Printf("tracker heartbeat checking at %v\n", time.Now()) @@ -69,9 +64,9 @@ func (tracker *Instance) Start() (heartbeatCh chan *Heartbeat, quit chan struct{ //log.Printf("activity received: %#v\n", activity) case <-quit: log.Printf("stopping activity tracker\n") - //close all service handlers for a clean exit - for _, serviceHandler := range tracker.serviceHandlers { - close(serviceHandler) + //close all services for a clean exit + for service := range tracker.services { + service.Close() } return } @@ -88,12 +83,14 @@ func makeActivityMap() map[*activity.Type]time.Time { func (tracker *Instance) registerHandlers(services ...service.Instance) { - if len(tracker.serviceHandlers) == 0 { //checking for multiple registration attempts - tracker.activityCh = make(chan *activity.Type, len(services)) // number based on types of activities being tracked + tracker.services = make(map[service.Instance]bool) + tracker.activityCh = make(chan *activity.Type, len(services)) // number based on types of activities being tracked - for _, service := range services { - tickerCh := service.Start(tracker.activityCh) - tracker.serviceHandlers = append(tracker.serviceHandlers, tickerCh) + for _, service := range services { + service.Start(tracker.activityCh) + if _, ok := tracker.services[service]; !ok { //duplicate registration prevention + tracker.services[service] = true } + } } diff --git a/pkg/tracker/types.go b/pkg/tracker/types.go index 03d26d1..dd46178 100644 --- a/pkg/tracker/types.go +++ b/pkg/tracker/types.go @@ -3,13 +3,14 @@ package tracker import ( "time" + "github.com/prashantgupta24/activity-tracker/internal/pkg/service" "github.com/prashantgupta24/activity-tracker/pkg/activity" ) type Instance struct { - TimeToCheck time.Duration - activityCh chan *activity.Type - serviceHandlers []chan struct{} + TimeToCheck time.Duration + activityCh chan *activity.Type + services map[service.Instance]bool } type Heartbeat struct {