automatic-mouse-mover/pkg/mousemover/mouseMover.go

129 lines
3.0 KiB
Go
Raw Normal View History

package mousemover
import (
2019-03-28 19:52:25 +00:00
"sync"
"time"
2019-03-28 19:52:25 +00:00
log "github.com/sirupsen/logrus"
"github.com/go-vgo/robotgo"
"github.com/prashantgupta24/activity-tracker/pkg/tracker"
)
2019-03-27 23:10:07 +00:00
var instance *MouseMover
2019-03-27 23:10:07 +00:00
//MouseMover is the main struct for the app
type MouseMover struct {
2019-03-28 19:52:25 +00:00
quit chan struct{}
mutex sync.RWMutex
runningStatus bool
}
const (
timeout = 100 //ms
)
2019-03-27 23:10:07 +00:00
//Start the main app
func (m *MouseMover) Start() {
2019-03-28 19:52:25 +00:00
if m.isRunning() {
return
}
m.quit = make(chan struct{})
heartbeatInterval := 60 //value always in seconds
workerInterval := 15
activityTracker := &tracker.Instance{
HeartbeatInterval: heartbeatInterval,
WorkerInterval: workerInterval,
//LogLevel: "debug", //if we want verbose logging
}
heartbeatCh := activityTracker.Start()
2019-03-27 23:10:07 +00:00
go func(m *MouseMover) {
2019-03-28 19:52:25 +00:00
m.updateRunningStatus(true)
movePixel := 10
for {
select {
case heartbeat := <-heartbeatCh:
2019-03-28 20:38:01 +00:00
if !heartbeat.WasAnyActivity {
mouseMoveSuccessCh := make(chan bool)
go moveAndCheck(movePixel, mouseMoveSuccessCh)
select {
case wasMouseMoveSuccess := <-mouseMoveSuccessCh:
if wasMouseMoveSuccess {
log.Infof("moved mouse at : %v\n\n", time.Now())
movePixel *= -1
} else {
msg := "Mouse pointer cannot be moved. See README for details."
log.Errorf(msg)
2019-04-09 00:39:14 +00:00
robotgo.ShowAlert("Error with Automatic Mouse Mover", msg)
}
case <-time.After(timeout * time.Millisecond):
//timeout, do nothing
2019-03-28 19:52:25 +00:00
log.Errorf("timeout happened after %vms while trying to move mouse", timeout)
}
} else {
//uncomment just for reference
// log.Printf("activity detected in the last %v seconds.", int(heartbeatInterval))
// log.Printf("Activity type:\n")
// for activityType, times := range heartbeat.ActivityMap {
// log.Printf("activityType : %v times: %v\n", activityType, len(times))
// }
// log.Printf("\n\n\n")
}
case <-m.quit:
2019-03-28 19:52:25 +00:00
log.Infof("stopping mouse mover")
m.updateRunningStatus(false)
activityTracker.Quit()
return
}
}
}(m)
}
2019-03-28 19:52:25 +00:00
func (m *MouseMover) isRunning() bool {
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.runningStatus
}
func (m *MouseMover) updateRunningStatus(isRunning bool) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.runningStatus = isRunning
}
func moveAndCheck(movePixel int, mouseMoveSuccessCh chan bool) {
currentX, currentY := robotgo.GetMousePos()
moveToX := currentX + movePixel
moveToY := currentY + movePixel
robotgo.MoveMouse(moveToX, moveToY)
//check if mouse moved. Sometimes mac users need to give
//extra permission for controlling the mouse
movedX, movedY := robotgo.GetMousePos()
if movedX == currentX && movedY == currentY {
mouseMoveSuccessCh <- false
} else {
mouseMoveSuccessCh <- true
}
}
2019-03-27 23:10:07 +00:00
//Quit the app
func (m *MouseMover) Quit() {
//making it idempotent
2019-03-28 19:52:25 +00:00
if m != nil && m.isRunning() {
m.quit <- struct{}{}
}
}
//GetInstance gets the singleton instance for mouse mover app
2019-03-27 23:10:07 +00:00
func GetInstance() *MouseMover {
if instance == nil {
2019-03-27 23:10:07 +00:00
instance = &MouseMover{}
}
return instance
}