added system sleep check

This commit is contained in:
Prashant Gupta 2019-04-23 16:51:58 -07:00
parent 708f4623e5
commit f212967d71
2 changed files with 24 additions and 11 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
vendor/ vendor/
bin/ bin/
log/

View File

@ -10,6 +10,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/go-vgo/robotgo" "github.com/go-vgo/robotgo"
"github.com/prashantgupta24/activity-tracker/pkg/activity"
"github.com/prashantgupta24/activity-tracker/pkg/tracker" "github.com/prashantgupta24/activity-tracker/pkg/tracker"
) )
@ -25,7 +26,7 @@ type MouseMover struct {
const ( const (
timeout = 100 //ms timeout = 100 //ms
logFileName = "logFile" logFileName = "log/logFile-amm-1"
) )
//Start the main app //Start the main app
@ -51,11 +52,16 @@ func (m *MouseMover) Start() {
m.updateRunningStatus(true) m.updateRunningStatus(true)
movePixel := 10 movePixel := 10
var lastMoved time.Time var lastMoved time.Time
isSystemSleeping := false
didNotMoveTimes := 0 didNotMoveTimes := 0
for { for {
select { select {
case heartbeat := <-heartbeatCh: case heartbeat := <-heartbeatCh:
if !heartbeat.WasAnyActivity { if !heartbeat.WasAnyActivity {
if isSystemSleeping {
logger.Infof("system sleeping")
continue
}
mouseMoveSuccessCh := make(chan bool) mouseMoveSuccessCh := make(chan bool)
go moveAndCheck(movePixel, mouseMoveSuccessCh) go moveAndCheck(movePixel, mouseMoveSuccessCh)
select { select {
@ -80,14 +86,18 @@ func (m *MouseMover) Start() {
//timeout, do nothing //timeout, do nothing
logger.Errorf("timeout happened after %vms while trying to move mouse", timeout) logger.Errorf("timeout happened after %vms while trying to move mouse", timeout)
} }
} else {
} else { //uncomment to see all activities received logger.Infof("activity detected in the last %v seconds.", int(heartbeatInterval))
// logger.Printf("activity detected in the last %v seconds.", int(heartbeatInterval)) logger.Infof("Activity type:\n")
// logger.Printf("Activity type:\n") for activityType, times := range heartbeat.ActivityMap {
// for activityType, times := range heartbeat.ActivityMap { logger.Infof("activityType : %v times: %v\n", activityType, len(times))
// logger.Printf("activityType : %v times: %v\n", activityType, len(times)) if activityType == activity.MachineSleep {
// } isSystemSleeping = true
// logger.Printf("\n\n\n") } else if activityType == activity.MachineWake {
isSystemSleeping = false
}
}
logger.Infof("\n\n\n")
} }
case <-m.quit: case <-m.quit:
logger.Infof("stopping mouse mover") logger.Infof("stopping mouse mover")
@ -132,7 +142,9 @@ func (m *MouseMover) Quit() {
if m != nil && m.isRunning() { if m != nil && m.isRunning() {
m.quit <- struct{}{} m.quit <- struct{}{}
} }
m.logFile.Close() if m.logFile != nil {
m.logFile.Close()
}
} }
//GetInstance gets the singleton instance for mouse mover app //GetInstance gets the singleton instance for mouse mover app