forked from Zeukkari/leapgim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionController.coffee
224 lines (204 loc) · 8.5 KB
/
ActionController.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#
# Action Controller
#
# Triggers mouse and keyboard actions based on configured recipes. Actions are idempotent operations.
#
execSh = require 'exec-sh'
class ActionController
constructor:(config, feedback) ->
@config = config
@feedback = feedback
@actions = config.actions
@recipes = config.recipes
@robot = require 'robotjs'
@mouseState = 'free' # free|frozen
@position =
x: 0
y: 0
@freezePosition =
x: 0
y: 0
@unfreezePosition =
x: 0
y: 0
@keyboardModel =
test: false
@recipeState = {}
for name, recipe of @recipes
@recipeState[name] =
status: 'inactive'
timerID: null
if(recipe.tearDownDelay)
@recipeState[name].tearDownDelay = recipe.tearDownDelay
freezeMouse: (handPosition) =>
@freezePosition = @robot.getMousePos()
@mouseState = 'frozen'
unfreezeMouse: (handPosition) =>
screenSize = @robot.getScreenSize()
normalizedHandPosition =
x: handPosition.x * screenSize.width
y: handPosition.y * screenSize.height
@unfreezePosition = normalizedHandPosition
console.log "Unfreeze mouse", @unfreezePosition
@mouseState = 'free'
toggleMouseFreeze: (handPosition) =>
if (@mouseState == 'frozen')
@unfreezeMouse handPosition
else if(@mouseState == 'free')
@freezeMouse handPosition
mouseMove: (handPosition) =>
if(@mouseState == 'free')
screenSize = @robot.getScreenSize()
normalizedHandPosition =
x: handPosition.x * screenSize.width
y: handPosition.y * screenSize.height
offsetMapping =
x: @freezePosition.x - @unfreezePosition.x
y: @freezePosition.y - @unfreezePosition.y
moveTo =
x: normalizedHandPosition.x + offsetMapping.x
y: normalizedHandPosition.y + offsetMapping.y
@robot.moveMouse(moveTo.x, moveTo.y)
# buttonAction: up|down|click|doubleClick, button: left|right
mouseButton: (buttonAction, button) =>
if(buttonAction == 'up')
@robot.mouseToggle buttonAction, button
else if(buttonAction == 'down')
@robot.mouseToggle buttonAction, button
else if(buttonAction == 'click')
@robot.mouseClick button, false
else if(buttonAction == 'doubleClick')
@robot.mouseClick button, true
# action: up|down|tap
keyboard: (action, button) =>
if(action == 'up')
@robot.keyToggle button, action
else if(action == 'down')
@robot.keyToggle button, action
else if(action == 'tap')
@robot.keyTap button
return
scrollMouse: (direction, magnitude) =>
if(direction == 'up' or direction == 'down')
@robot.scrollMouse(magnitude, direction)
else
console.log 'This aint 3d, man!'
delayMouse: (delay) =>
@robot.delayMouse(delay)
execSh: (cmd, options, callback) =>
execSh cmd, options, callback
loadProfile: (profile) ->
console.log "Load profile #{profile}"
throw "LOAD PROFILE NOT IMPLEMENTED!"
executeAction: (action) =>
cmd = @actions[action]
# console.log "cmd: ", cmd
screenSize = @robot.getScreenSize()
if(cmd.feedback?)
if(cmd.feedback.audio?)
@feedback.audioNotification cmd.feedback.audio
if(cmd.feedback.visual?)
options = cmd.feedback.visual
@feedback.visualNotification options.id, options.msg
if(cmd.type == 'mouse')
if(cmd.action == 'freeze')
@freezeMouse(@position)
if(cmd.action == 'unfreeze')
@unfreezeMouse(@position)
if(cmd.action == 'toggleFreeze')
@toggleMouseFreeze(@position)
if(cmd.action in ['up', 'down', 'click', 'doubleClick'])
@mouseButton cmd.action, cmd.target
if(cmd.action == 'move')
@mouseMove(@position)
if(cmd.action == 'scroll')
@scrollMouse cmd.direction, cmd.magnitude
if(cmd.action == 'delay')
@delayMouse cmd.delay
if(cmd.type == 'keyboard')
if(cmd.action in ['up', 'down', 'tap'])
@keyboard cmd.action, cmd.button
if(cmd.type == 'compound')
@executeAction action for action in cmd.actions
if(cmd.type == 'exec')
@execSh cmd.cmd, cmd.options, (err)->
if(err)
console.log "Exec error", err
if(cmd.type == 'profile')
if(cmd.action == 'load')
@loadProfile(cmd.target)
activateRecipe: (recipeName) =>
recipe = @recipes[recipeName]
#console.log "recipe data:", recipe
actionName = recipe.action
# Skip activation if charging
if(!@recipeState[recipeName].timerID)
if(recipe.continuous)
if(@recipeState[recipeName].status != 'sleeping')
if(recipe.chargeDelay)
chargeDelay = recipe.chargeDelay
#console.log "Recipe #{recipeName} sleeping for #{chargeDelay}"
callback = () =>
#console.log "Recipe #{recipeName} is awake!"
@recipeState[recipeName].status = 'inactive'
@recipeState[recipeName].timerID = null
@recipeState[recipeName].status = 'sleeping'
@recipeState[recipeName].timerID = setTimeout callback, chargeDelay
#console.log "activate continuous recipe: ", recipeName
@executeAction(actionName)
return true
else
#console.log "Recipe #{recipeName} is sleeping..."
return false
else if(@recipeState[recipeName].status == 'inactive')
if(!@recipeState[recipeName].timerID)
#console.log "Activate recipe #{recipeName}"
@recipeState[recipeName].status = 'active'
@executeAction(actionName)
return true
return false
tearDownRecipe: (recipeName) =>
recipe = @recipes[recipeName]
return unless recipe
actionName = recipe.tearDown
# Apathy!
if(!actionName)
@recipeState[recipeName].status = 'inactive'
@recipeState[recipeName].timerID = null
return false
if(@recipeState[recipeName].status == 'active')
if(!@recipeState[recipeName].timerID)
#console.log "Tear down delay for #{recipeName} is " + @recipeState[recipeName].tearDownDelay
if(@recipeState[recipeName].tearDownDelay)
callback = () =>
console.log "Tear down timed recipe #{recipeName}"
@executeAction(actionName)
@recipeState[recipeName].status = 'inactive'
@recipeState[recipeName].timerID = null
@recipeState[recipeName].timerID = setTimeout callback, @recipeState[recipeName].tearDownDelay
return true
else
console.log "Tear down non-timed recipe #{recipeName}"
@recipeState[recipeName].status = 'inactive'
@recipeState[recipeName].timerID = null
@executeAction(actionName)
return true
else
#console.log "Tear down timer already triggered for #{recipeName}"
return false
else
#console.log "Recipe status is inactive for #{recipeName}"
return false
#console.log "How the fuck did I get in here?"
getActiveRecipes: (filter) =>
#console.log "get Active recipes.."
recipeList = []
for recipeName, recipeState of @recipeState
recipeStatus = recipeState.status
#console.log "recipeName: #{recipeName}, recipeState: #{recipeStatus}"
if(recipeState is 'active')
if(typeof filter != 'function' or filter(recipeName))
recipeList.push recipeName
return recipeList
if(window)
window.ActionController = ActionController