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

127 lines
3.3 KiB
Go
Raw Normal View History

package mousemover
import (
2019-04-19 22:52:37 +00:00
"fmt"
"time"
"github.com/go-vgo/robotgo"
2019-04-23 23:51:58 +00:00
"github.com/prashantgupta24/activity-tracker/pkg/activity"
"github.com/prashantgupta24/activity-tracker/pkg/tracker"
)
2019-03-27 23:10:07 +00:00
var instance *MouseMover
const (
2019-04-19 22:52:37 +00:00
timeout = 100 //ms
2019-04-24 00:26:56 +00:00
logDir = "log"
2019-04-27 01:05:16 +00:00
logFileName = "logFile-amm-2"
)
2019-03-27 23:10:07 +00:00
//Start the main app
func (m *MouseMover) Start() {
2019-05-01 01:53:53 +00:00
if m.state.isRunning() {
2019-03-28 19:52:25 +00:00
return
}
2019-05-01 01:53:53 +00:00
m.state = &state{}
m.quit = make(chan struct{})
heartbeatInterval := 60 //value always in seconds
2019-04-19 22:52:37 +00:00
workerInterval := 10
activityTracker := &tracker.Instance{
HeartbeatInterval: heartbeatInterval,
WorkerInterval: workerInterval,
//LogLevel: "debug", //if we want verbose logging
}
heartbeatCh := activityTracker.Start()
2019-04-27 01:05:16 +00:00
m.run(heartbeatCh, activityTracker)
2019-04-24 00:41:01 +00:00
}
2019-04-27 01:05:16 +00:00
func (m *MouseMover) run(heartbeatCh chan *tracker.Heartbeat, activityTracker *tracker.Instance) {
go func() {
2019-05-01 01:53:53 +00:00
state := m.state
if state != nil && state.isRunning() {
2019-04-27 01:05:16 +00:00
return
}
2019-05-01 01:53:53 +00:00
state.updateRunningStatus(true)
2019-05-03 00:58:01 +00:00
logger := getLogger(m, false, logFileName) //set writeToFile=true only for debugging
movePixel := 10
for {
select {
case heartbeat := <-heartbeatCh:
2019-03-28 20:38:01 +00:00
if !heartbeat.WasAnyActivity {
2019-05-01 01:53:53 +00:00
if state.isSystemSleeping() {
2019-04-23 23:51:58 +00:00
logger.Infof("system sleeping")
continue
}
mouseMoveSuccessCh := make(chan bool)
2019-05-01 01:53:53 +00:00
go moveAndCheck(state, movePixel, mouseMoveSuccessCh)
select {
case wasMouseMoveSuccess := <-mouseMoveSuccessCh:
if wasMouseMoveSuccess {
2019-05-01 01:53:53 +00:00
state.updateLastMouseMovedTime(time.Now())
logger.Infof("moved mouse at : %v\n\n", state.getLastMouseMovedTime())
movePixel *= -1
2019-05-01 01:53:53 +00:00
state.updateDidNotMoveCount(0)
} else {
2019-05-01 01:53:53 +00:00
didNotMoveCount := state.getDidNotMoveCount()
state.updateDidNotMoveCount(didNotMoveCount + 1)
2019-04-19 22:52:37 +00:00
msg := fmt.Sprintf("Mouse pointer cannot be moved at %v. Last moved at %v. Happened %v times. See README for details.",
2019-05-01 01:53:53 +00:00
time.Now(), state.getLastMouseMovedTime(), state.getDidNotMoveCount())
2019-04-19 22:52:37 +00:00
logger.Errorf(msg)
2019-05-01 01:53:53 +00:00
if state.getDidNotMoveCount() >= 3 {
2019-04-19 22:52:37 +00:00
go func() {
robotgo.ShowAlert("Error with Automatic Mouse Mover", msg)
}()
}
}
case <-time.After(timeout * time.Millisecond):
//timeout, do nothing
2019-04-19 22:52:37 +00:00
logger.Errorf("timeout happened after %vms while trying to move mouse", timeout)
}
2019-04-23 23:51:58 +00:00
} else {
2019-04-27 01:05:16 +00:00
logger.Infof("activity detected in the last %v seconds.", int(activityTracker.HeartbeatInterval))
2019-04-23 23:51:58 +00:00
logger.Infof("Activity type:\n")
for activityType, times := range heartbeat.ActivityMap {
logger.Infof("activityType : %v times: %v\n", activityType, len(times))
if activityType == activity.MachineSleep {
2019-05-01 01:53:53 +00:00
state.updateMachineSleepStatus(true)
2019-04-23 23:51:58 +00:00
} else if activityType == activity.MachineWake {
2019-05-01 01:53:53 +00:00
state.updateMachineSleepStatus(false)
2019-04-23 23:51:58 +00:00
}
}
logger.Infof("\n\n\n")
}
case <-m.quit:
2019-04-19 22:52:37 +00:00
logger.Infof("stopping mouse mover")
2019-05-01 01:53:53 +00:00
state.updateRunningStatus(false)
activityTracker.Quit()
return
}
}
2019-04-27 01:05:16 +00:00
}()
}
2019-03-27 23:10:07 +00:00
//Quit the app
func (m *MouseMover) Quit() {
//making it idempotent
2019-05-01 01:53:53 +00:00
if m != nil && m.state.isRunning() {
m.quit <- struct{}{}
}
2019-04-23 23:51:58 +00:00
if m.logFile != nil {
m.logFile.Close()
}
}
//GetInstance gets the singleton instance for mouse mover app
2019-03-27 23:10:07 +00:00
func GetInstance() *MouseMover {
if instance == nil {
2019-05-01 01:53:53 +00:00
instance = &MouseMover{
state: &state{},
}
}
return instance
}