incorporating new activity tracker changes

This commit is contained in:
Prashant Gupta 2019-03-20 12:44:51 -07:00
parent 07596ffea1
commit de4a8882d9
7 changed files with 207 additions and 169 deletions

View File

@ -5,7 +5,7 @@ import (
"github.com/getlantern/systray" "github.com/getlantern/systray"
"github.com/getlantern/systray/example/icon" "github.com/getlantern/systray/example/icon"
"github.com/prashantgupta24/automatic-mouse-mover/src/mousemover" "github.com/prashantgupta24/automatic-mouse-mover/pkg/mousemover"
) )
func main() { func main() {
@ -17,29 +17,26 @@ func onReady() {
systray.SetIcon(icon.Data) systray.SetIcon(icon.Data)
systray.SetTitle("AMM") systray.SetTitle("AMM")
ammStart := systray.AddMenuItem("Start", "start the app") ammStart := systray.AddMenuItem("Start", "start the app")
ammPause := systray.AddMenuItem("Pause", "pause the app") ammStop := systray.AddMenuItem("Stop", "stop the app")
systray.AddSeparator() systray.AddSeparator()
mQuit := systray.AddMenuItem("Quit", "Quit the whole app") mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
// Sets the icon of a menu item. Only available on Mac. // Sets the icon of a menu item. Only available on Mac.
//mQuit.SetIcon(icon.Data) //mQuit.SetIcon(icon.Data)
var quit chan struct{} mouseMover := mousemover.GetInstance()
for { for {
select { select {
case <-ammStart.ClickedCh: case <-ammStart.ClickedCh:
fmt.Println("starting the app") fmt.Println("starting the app")
quit = mousemover.Start() mouseMover.Start()
//notify.SendMessage("starting the app") //notify.SendMessage("starting the app")
case <-ammPause.ClickedCh: case <-ammStop.ClickedCh:
fmt.Println("pausing the app") fmt.Println("pausing the app")
if quit != nil { mouseMover.Quit()
quit <- struct{}{}
} else {
fmt.Println("app is not started")
}
case <-mQuit.ClickedCh: case <-mQuit.ClickedCh:
fmt.Println("Requesting quit") fmt.Println("Requesting quit")
mouseMover.Quit()
systray.Quit() systray.Quit()
fmt.Println("Finished quitting") fmt.Println("Finished quitting")
return return

12
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: 0e9d554853b2c3ed23ba323715540d961c06465a1a125959a698d6162e17f91d hash: 0e9d554853b2c3ed23ba323715540d961c06465a1a125959a698d6162e17f91d
updated: 2019-03-13T16:48:18.629713-07:00 updated: 2019-03-20T12:29:04.478748-07:00
imports: imports:
- name: github.com/BurntSushi/xgb - name: github.com/BurntSushi/xgb
version: 27f122750802c950b2c869a5b63dafcf590ced95 version: 27f122750802c950b2c869a5b63dafcf590ced95
@ -44,18 +44,18 @@ imports:
- name: github.com/lxn/win - name: github.com/lxn/win
version: 0040366d745006bbf7528ac96de2de375846aa79 version: 0040366d745006bbf7528ac96de2de375846aa79
- name: github.com/otiai10/gosseract - name: github.com/otiai10/gosseract
version: ca64adaa320dc563d0966d58d3811512cb3362dc version: d442e510d0b37ec1f332a61a04e323ecee91d0cb
- name: github.com/oxtoacart/bpool - name: github.com/oxtoacart/bpool
version: 8c4636f812cc8920c26e2002b988c878b1fd6f5e version: 8c4636f812cc8920c26e2002b988c878b1fd6f5e
- name: github.com/prashantgupta24/activity-tracker - name: github.com/prashantgupta24/activity-tracker
version: d567f73735304c333bba0a151cf4aca8602c1ae2 version: 7330e5e8a948db11beb7aef23996d37ef26977f4
subpackages: subpackages:
- src/activity - src/activity
- src/mouse - src/mouse
- name: github.com/robotn/gohook - name: github.com/robotn/gohook
version: bbbbefb5ff8541a05870596ea081ad8581b5879f version: a7a38946710e764b00839160e13a92b09e40e96a
- name: github.com/shirou/gopsutil - name: github.com/shirou/gopsutil
version: ebc97eefea9b062f9f1624c042c98f196fc90248 version: 381f7cc0bf4ccb60157fd5c6eaabfdfc681e6c2f
subpackages: subpackages:
- cpu - cpu
- host - host
@ -74,7 +74,7 @@ imports:
subpackages: subpackages:
- bmp - bmp
- name: golang.org/x/sys - name: golang.org/x/sys
version: fead79001313d15903fb4605b4a1b781532cd93e version: 6c81ef8f67ca3f42fc9cd71dfbd5f35b0c4b5771
subpackages: subpackages:
- unix - unix
- windows - windows

109
pkg/mousemover/misc.go Normal file
View File

@ -0,0 +1,109 @@
package mousemover
// import (
// "fmt"
// "time"
// "github.com/go-vgo/robotgo"
// )
// // func isPointerIdle(comm chan bool) {
// // for {
// // x1, y1 := robotgo.GetMousePos()
// // time.Sleep(time.Second * 3)
// // x2, y2 := robotgo.GetMousePos()
// // if x1 == x2 && y1 == y2 {
// // fmt.Println("idle")
// // //comm <- true
// // } else {
// // fmt.Println("moving")
// // comm <- false
// // }
// // }
// // }
// func checkIfMouseMoved(x1, y1, x2, y2 int, comm chan bool) {
// if x1 == x2 && y1 == y2 {
// fmt.Println("idle")
// //return false
// //comm <- true
// } else {
// fmt.Println("moving")
// comm <- false
// //return true
// }
// }
// func isMouseClick(comm chan bool) {
// }
// func moveMouse(comm chan bool) {
// ticker := time.NewTicker(time.Second * 3)
// val := true
// movePixel := 10
// x1, y1 := robotgo.GetMousePos()
// for {
// select {
// case <-ticker.C:
// fmt.Println("ticked")
// x2, y2 := robotgo.GetMousePos()
// checkIfMouseMoved(x1, y1, x2, y2, comm)
// if val {
// fmt.Println("moving mouse because idle")
// //x1, y1 := robotgo.GetMousePos()
// robotgo.Move(x2+movePixel, y2+movePixel)
// movePixel *= -1
// } else {
// val = true
// }
// x1 = x2
// y1 = y2
// case val = <-comm:
// fmt.Println("val received: ", val)
// }
// }
// }
// func main() {
// // log.SetOutput(os.Stdout)
// // log.Println("starting")
// // log.Println("logging")
// // //robotgo.ScrollMouse(100, "up")
// // robotgo.Move(100, 100)
// // x, y := robotgo.GetMousePos()
// // fmt.Println("pos: ", x, y)
// comm := make(chan bool)
// moveMouse(comm)
// //isPointerIdle(comm)
// // for {
// // // wheelDown := robotgo.AddEvent("wheelDown")
// // // wheelRight := robotgo.AddEvent("wheelRight")
// // // fmt.Println("wheelDown : ", wheelDown)
// // // fmt.Println("wheelRight : ", wheelRight)
// // count := 0
// // go func(count *int) {
// // for {
// // mleft := robotgo.AddEvent("mleft")
// // if mleft == 0 {
// // *count++
// // fmt.Println("mleft : ", *count)
// // time.Sleep(time.Millisecond * 500)
// // }
// // }
// // }(&count)
// // mright := robotgo.AddEvent("mright")
// // fmt.Println("mright : ", mright)
// // // if mleft {
// // // fmt.Println("you press... ", "mouse left button")
// // // }
// // }
// }

View File

@ -0,0 +1,85 @@
package mousemover
import (
"fmt"
"log"
"time"
"github.com/go-vgo/robotgo"
"github.com/prashantgupta24/activity-tracker/pkg/tracker"
)
var instance *mouseMover
type mouseMover struct {
quit chan struct{}
isRunning bool
}
const (
timeout = 100 //ms
)
func (m *mouseMover) Start() {
m.quit = make(chan struct{})
frequency := 5 //value always in seconds
activityTracker := &tracker.Instance{
Frequency: frequency,
}
heartbeatCh := activityTracker.Start()
go func(m *mouseMover) {
m.isRunning = true
movePixel := 10
for {
select {
case heartbeat := <-heartbeatCh:
if !heartbeat.IsActivity {
commCh := make(chan bool)
go moveMouse(movePixel, commCh)
select {
case wasMouseMoveSuccess := <-commCh:
if wasMouseMoveSuccess {
fmt.Println("moving mouse at : \n", time.Now())
movePixel *= -1
}
case <-time.After(timeout * time.Millisecond):
//timeout, do nothing
log.Printf("timeout happened after %vms while trying to move mouse", timeout)
}
}
case <-m.quit:
fmt.Println("stopping mouse mover")
m.isRunning = false
activityTracker.Quit()
return
}
}
}(m)
}
func moveMouse(movePixel int, commCh chan bool) {
currentX, currentY := robotgo.GetMousePos()
moveToX := currentX + movePixel
moveToY := currentY + movePixel
robotgo.MoveMouse(moveToX, moveToY)
commCh <- true
}
func (m *mouseMover) Quit() {
//making it idempotent
if !m.isRunning {
m.quit <- struct{}{}
}
}
//GetInstance gets the singleton instance for mouse mover app
func GetInstance() *mouseMover {
if instance == nil {
instance = &mouseMover{}
}
return instance
}

View File

@ -1,109 +0,0 @@
package mousemover
import (
"fmt"
"time"
"github.com/go-vgo/robotgo"
)
// func isPointerIdle(comm chan bool) {
// for {
// x1, y1 := robotgo.GetMousePos()
// time.Sleep(time.Second * 3)
// x2, y2 := robotgo.GetMousePos()
// if x1 == x2 && y1 == y2 {
// fmt.Println("idle")
// //comm <- true
// } else {
// fmt.Println("moving")
// comm <- false
// }
// }
// }
func checkIfMouseMoved(x1, y1, x2, y2 int, comm chan bool) {
if x1 == x2 && y1 == y2 {
fmt.Println("idle")
//return false
//comm <- true
} else {
fmt.Println("moving")
comm <- false
//return true
}
}
func isMouseClick(comm chan bool) {
}
func moveMouse(comm chan bool) {
ticker := time.NewTicker(time.Second * 3)
val := true
movePixel := 10
x1, y1 := robotgo.GetMousePos()
for {
select {
case <-ticker.C:
fmt.Println("ticked")
x2, y2 := robotgo.GetMousePos()
checkIfMouseMoved(x1, y1, x2, y2, comm)
if val {
fmt.Println("moving mouse because idle")
//x1, y1 := robotgo.GetMousePos()
robotgo.Move(x2+movePixel, y2+movePixel)
movePixel *= -1
} else {
val = true
}
x1 = x2
y1 = y2
case val = <-comm:
fmt.Println("val received: ", val)
}
}
}
func main() {
// log.SetOutput(os.Stdout)
// log.Println("starting")
// log.Println("logging")
// //robotgo.ScrollMouse(100, "up")
// robotgo.Move(100, 100)
// x, y := robotgo.GetMousePos()
// fmt.Println("pos: ", x, y)
comm := make(chan bool)
moveMouse(comm)
//isPointerIdle(comm)
// for {
// // wheelDown := robotgo.AddEvent("wheelDown")
// // wheelRight := robotgo.AddEvent("wheelRight")
// // fmt.Println("wheelDown : ", wheelDown)
// // fmt.Println("wheelRight : ", wheelRight)
// count := 0
// go func(count *int) {
// for {
// mleft := robotgo.AddEvent("mleft")
// if mleft == 0 {
// *count++
// fmt.Println("mleft : ", *count)
// time.Sleep(time.Millisecond * 500)
// }
// }
// }(&count)
// mright := robotgo.AddEvent("mright")
// fmt.Println("mright : ", mright)
// // if mleft {
// // fmt.Println("you press... ", "mouse left button")
// // }
// }
}

View File

@ -1,44 +0,0 @@
package mousemover
import (
"fmt"
"time"
"github.com/go-vgo/robotgo"
"github.com/prashantgupta24/activity-tracker/src/activity"
"github.com/prashantgupta24/activity-tracker/src/mouse"
)
func Start() (quit chan struct{}) {
quit = make(chan struct{})
activityTracker := &activity.ActivityTracker{
TimeToCheck: 15,
}
heartbeatCh, quitActivityTracker := activityTracker.Start()
go func() {
movePixel := 10
for {
select {
case heartbeat := <-heartbeatCh:
if !heartbeat.IsActivity {
currentMousePos := mouse.GetPosition()
fmt.Println("moving mouse at : ", time.Now())
nextMouseMov := &mouse.Position{
MouseX: currentMousePos.MouseX + movePixel,
MouseY: currentMousePos.MouseY + movePixel,
}
robotgo.MoveMouse(nextMouseMov.MouseX, nextMouseMov.MouseY)
movePixel *= -1
}
case <-quit:
fmt.Println("stopping mouse mover")
quitActivityTracker <- struct{}{}
return
}
}
}()
return quit
}