Merge pull request #12 from prashantgupta24/error-update

handle machine sleep
This commit is contained in:
Prashant Gupta 2019-04-23 17:28:07 -07:00 committed by GitHub
commit 39119e193b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 17 deletions

1
.gitignore vendored
View File

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

View File

@ -1,12 +1,16 @@
package mousemover package mousemover
import ( import (
"fmt"
"os"
"sync" "sync"
"time" "time"
"github.com/sirupsen/logrus"
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"
) )
@ -17,10 +21,13 @@ type MouseMover struct {
quit chan struct{} quit chan struct{}
mutex sync.RWMutex mutex sync.RWMutex
runningStatus bool runningStatus bool
logFile *os.File
} }
const ( const (
timeout = 100 //ms timeout = 100 //ms
logDir = "log"
logFileName = "logFile-amm-1"
) )
//Start the main app //Start the main app
@ -31,7 +38,7 @@ func (m *MouseMover) Start() {
m.quit = make(chan struct{}) m.quit = make(chan struct{})
heartbeatInterval := 60 //value always in seconds heartbeatInterval := 60 //value always in seconds
workerInterval := 15 workerInterval := 10
activityTracker := &tracker.Instance{ activityTracker := &tracker.Instance{
HeartbeatInterval: heartbeatInterval, HeartbeatInterval: heartbeatInterval,
@ -42,40 +49,59 @@ func (m *MouseMover) Start() {
heartbeatCh := activityTracker.Start() heartbeatCh := activityTracker.Start()
go func(m *MouseMover) { go func(m *MouseMover) {
logger := getLogger(m, false) //set writeToFile=true only for debugging
m.updateRunningStatus(true) m.updateRunningStatus(true)
movePixel := 10 movePixel := 10
var lastMoved time.Time
isSystemSleeping := false
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 {
case wasMouseMoveSuccess := <-mouseMoveSuccessCh: case wasMouseMoveSuccess := <-mouseMoveSuccessCh:
if wasMouseMoveSuccess { if wasMouseMoveSuccess {
log.Infof("moved mouse at : %v\n\n", time.Now()) lastMoved = time.Now()
logger.Infof("moved mouse at : %v\n\n", lastMoved)
movePixel *= -1 movePixel *= -1
didNotMoveTimes = 0
} else { } else {
msg := "Mouse pointer cannot be moved. See README for details." didNotMoveTimes++
log.Errorf(msg) msg := fmt.Sprintf("Mouse pointer cannot be moved at %v. Last moved at %v. Happened %v times. See README for details.",
robotgo.ShowAlert("Error with Automatic Mouse Mover", msg) time.Now(), lastMoved, didNotMoveTimes)
logger.Errorf(msg)
if didNotMoveTimes >= 3 {
go func() {
robotgo.ShowAlert("Error with Automatic Mouse Mover", msg)
}()
}
} }
case <-time.After(timeout * time.Millisecond): case <-time.After(timeout * time.Millisecond):
//timeout, do nothing //timeout, do nothing
log.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 just for reference logger.Infof("activity detected in the last %v seconds.", int(heartbeatInterval))
// log.Printf("activity detected in the last %v seconds.", int(heartbeatInterval)) logger.Infof("Activity type:\n")
// log.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))
// log.Printf("activityType : %v times: %v\n", activityType, len(times)) if activityType == activity.MachineSleep {
// } isSystemSleeping = true
// log.Printf("\n\n\n") } else if activityType == activity.MachineWake {
isSystemSleeping = false
}
}
logger.Infof("\n\n\n")
} }
case <-m.quit: case <-m.quit:
log.Infof("stopping mouse mover") logger.Infof("stopping mouse mover")
m.updateRunningStatus(false) m.updateRunningStatus(false)
activityTracker.Quit() activityTracker.Quit()
return return
@ -117,6 +143,9 @@ func (m *MouseMover) Quit() {
if m != nil && m.isRunning() { if m != nil && m.isRunning() {
m.quit <- struct{}{} m.quit <- struct{}{}
} }
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
@ -126,3 +155,31 @@ func GetInstance() *MouseMover {
} }
return instance return instance
} }
func getLogger(m *MouseMover, doWriteToFile bool) *log.Logger {
logger := log.New()
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}
if doWriteToFile {
_, err := os.Stat(logDir)
if err != nil {
if os.IsNotExist(err) {
err = os.Mkdir(logDir, os.ModePerm)
if err != nil {
log.Fatalf("error creating dir: %v", err)
}
}
}
logFile, err := os.OpenFile(logDir+"/"+logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
logger.SetOutput(logFile)
m.logFile = logFile
}
return logger
}