Skip to content

Instantly share code, notes, and snippets.

@LieutenantChips
Last active May 28, 2018 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LieutenantChips/2b1c313265cd528965029b131142f6a3 to your computer and use it in GitHub Desktop.
Save LieutenantChips/2b1c313265cd528965029b131142f6a3 to your computer and use it in GitHub Desktop.
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import sys
import time
import subprocess
import os
sys.path.append('./')
import sim_start
###1:change this for different tablets ___________________________
recordDataValues = [1224, 334]
###1_end:_________________________________________________________
num_touch = 0
oldTime = 0
image_count = 0
point_index = 0
def printTime():
global oldTime
global image_count
newTime = int(round(time.time() * 1000))
diff = newTime - oldTime
oldTime = newTime
print "imageNum : " + str(image_count) + " ::: time : " + str(diff)
#tags : "monkeyrunner_locations:D"
def readLogcat():
p = subprocess.Popen([ "adb", "logcat", "monkeyrunner_locations:D", "*:S"], shell=False, stdout=subprocess.PIPE)
i = 0
open('logs.txt', 'w').close()
while True:
#device.shell("log -t TEST This is line %d" % i)
i += 1
line = p.stdout.readline().rstrip()
found = line.find('monkeyrunner_locations')
if(found != -1):
startSub = line.find('ID:') + len('ID:')
endSub = line.find(':IDend')
viewID = line[startSub:endSub]
startSub = line.find('Xval:') + len('Xval:')
endSub = line.find(':Xend')
X = line[startSub:endSub]
startSub = line.find('Yval:') + len('Yval:')
endSub = line.find(':Yend')
Y = line[startSub: endSub]
rf = open('logs.txt', 'r')
if not any(s == (viewID + " X : " + X + " | Y : " + Y + '\n') for s in rf):
f = open('logs.txt', 'a')
f.write(viewID + " X : " + X + " | Y : " + Y + '\n')
print viewID + " X : " + X + " | Y : " + Y + '\n'
f.close()
rf.close()
time.sleep(1)
if line == "DONEPARSING":
break
f.close()
def waitRobotReady():
p = subprocess.Popen('adb logcat -s "robot_ready:D" *:S', shell=False, stdout=subprocess.PIPE)
while True:
print "in while loop"
line = p.stdout.readline()
print line
if line.find("1"):
print "found ?"
break
time.sleep(1)
print "Robot Ready!"
#p.send_signal(signal.CTRL_BREAK_EVENT)
def sim_drag(tupleStart, tupleEnd, duration, steps):
#duration is in floats -> 0.2 = 200 ms
#steps is in int -> 50 is a good number.
global device
device.drag(tubleStart, tupleEnd, duration, steps)
monkeySleep(duration*1000 + 100)
def findLocationFromId(id_val):
rf = open("logs.txt", "r")
for num, line in enumerate(rf):
if id_val in line:
print str(num) + " : " + line
startSub = line.find("X :") + len("X :")
endSub = line.find(" |")
X = int(float(line[startSub:endSub]))
startSub = line.find("Y :") + len("Y :")
Y = int(float(line[startSub:]))
touch(X, Y)
def sim_sys():
parser = 0
for arg in sys.argv[1:]:
if parser%2 == 0:
#ID
if arg == "BACK":
device.press("KEYCODE_BACK", MonkeyDevice.DOWN_AND_UP)
parser += 1
continue
findLocationFromId(arg)
else:
#wait time
monkeySleep(arg)
parser += 1
parser = parser%2
def monkeySleep(msec):
MonkeyRunner.sleep(int(msec)/1000)
def touch(x,y):
device.touch(x, y, MonkeyDevice.DOWN_AND_UP)
def screenshot():
global image_count
global collection_size
result = device.takeSnapshot()
result.writeToFile('C:\\Users\\vkaraku2\\Documents\\snapshots\\shot_' + str(collection_size).rstrip() + '_' + str(image_count).rstrip() + ".png", 'png')
image_count = image_count + 1
def touch_img(x,y):
monkeySleep(100)
device.touch(x,y, MonkeyDevice.DOWN_AND_UP)
screenshot()
device = MonkeyRunner.waitForConnection()
#device.installPackage('C:\\Users\\vkaraku2\\AndroidStudioProjects\\terra-operator-android\\app\\build\\outputs\\apk\\debug\\app-debug.apk')
#package = 'com.daslab.swarm'
#activity = 'com.daslab.root.swarm.MainActivity'
#runComponent = package + '/' + activity
#device.startActivity(component=runComponent)
#readLogcat()
#MonkeyRunner.sleep(5)
p = subprocess.Popen('monkeyrunner.bat sim_start.py', shell=False, stdout=subprocess.PIPE)
device = sim_start.device
mfile = open('collection.txt', 'r+')
collection_size = mfile.read(10)
mfile.seek(0)
mfile.write(str(int(collection_size) + 1))
mfile.truncate()
mfile.close()
#waitRobotReady()
sim_sys()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment