Skip to content

Commit e964e90

Browse files
committed
list all connected devices ok
1 parent 051cded commit e964e90

File tree

10 files changed

+117
-126
lines changed

10 files changed

+117
-126
lines changed

run.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function usage ()
66
{
77
echo ""
88
echo "USAGE: "
9-
echo " run.sh [-d] [-c] [-h]"
9+
echo " run.sh [-c] [-d] [-h]"
1010
echo ""
1111
echo "OPTIONS:"
1212
echo " -d enable debug mode"
@@ -17,7 +17,7 @@ function usage ()
1717
exit $E_OPTERROR # Exit and explain usage, if no argument(s) given.
1818
}
1919

20-
while getopts ":dch" Option
20+
while getopts ":cdh" Option
2121
do
2222
case $Option in
2323
d) export GRADLE_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

src/main/groovy/com/samantha/vertx/MobileServerVerticle.groovy

+32-5
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ class MobileServerVerticle extends Verticle {
1717
OBJECT_MAPPER.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
1818
}
1919

20+
def connectionMap = [:]
21+
def deviceMap = [:]
22+
def sockets = []
23+
2024
def server
21-
NetSocket socket
2225

2326

2427
@Override
@@ -36,20 +39,32 @@ class MobileServerVerticle extends Verticle {
3639
vertx.eventBus
3740
.registerHandler("vertx.apps.get", this.&handleListAppRequest)
3841
.registerHandler("vertx.app.start", this.&startApplication)
42+
.registerHandler("vertx.devices.get", this.&getDevices)
3943
}
4044

4145
def handleListAppRequest(Message message) {
46+
println "listing applications"
47+
def socket = deviceMap.get(message.body().deviceId)
4248
socket?.write(OBJECT_MAPPER.writeValueAsString([address: "android.apps.get"]))
4349
}
4450

4551
def startApplication(Message message) {
52+
println "starting application $message.body()"
53+
def socket = deviceMap.get(message.body().deviceId)
4654
socket?.write(OBJECT_MAPPER.writeValueAsString([address: "android.monitoring.start", body: message.body()]))
4755
}
4856

57+
def getDevices(Message message) {
58+
println "list devices"
59+
message.reply(new ArrayList(connectionMap.values()))
60+
}
61+
4962
@Override
5063
def stop() {
5164
println "Closing Mobile Server..."
52-
socket?.close()
65+
sockets.each { socket ->
66+
socket?.close()
67+
}
5368
server?.close { asyncResult ->
5469
if (asyncResult.succeeded) {
5570
println "Mobile Server closed"
@@ -63,8 +78,7 @@ class MobileServerVerticle extends Verticle {
6378
def createSocketServer(config, closure) {
6479

6580
vertx.createNetServer().connectHandler { NetSocket socket ->
66-
67-
this.socket = socket
81+
sockets.add(socket)
6882
Buffer body = new Buffer(0)
6983

7084
socket.dataHandler { Buffer buffer ->
@@ -75,6 +89,11 @@ class MobileServerVerticle extends Verticle {
7589
jsonMessages.eachWithIndex { messageJson, i ->
7690
try {
7791
def message = OBJECT_MAPPER.readValue(messageJson, Map.class)
92+
if (message.address == "device.connect") {
93+
connectionMap.put(socket, message.body)
94+
deviceMap.put(message.body.imei, socket)
95+
message.body.connected = true
96+
}
7897
vertx.eventBus.publish(message.address, message.body)
7998
body = new Buffer(0)
8099
} catch (Exception e) {
@@ -87,7 +106,15 @@ class MobileServerVerticle extends Verticle {
87106
}
88107

89108
socket.endHandler {
90-
//TODO publish client disconnected
109+
def device = connectionMap.get(socket)
110+
device.connected = false
111+
112+
vertx.eventBus.publish("device.disconnect", device)
113+
sockets.remove(socket)
114+
connectionMap.remove(socket)
115+
deviceMap.remove(device.imei)
116+
117+
println "client disconnected"
91118
}
92119

93120
}

src/main/groovy/com/samantha/vertx/WebServerVerticle.groovy

-6
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ class WebServerVerticle extends Verticle {
2525
server = createHttpServer(container.config) { asyncResult, host, port ->
2626
if (asyncResult.succeeded) {
2727
println "Web Server started. Listening on ${host}:${port}"
28-
29-
vertx.eventBus
30-
// .registerHandler("vertx.app.post", this.&handleAppResponse)
31-
// .registerHandler("vertx.apps.get", this.&handleListAppRequest)
3228
} else {
3329
println "Starting Web Server failed -> ${asyncResult.cause}"
3430
}
@@ -81,11 +77,9 @@ class WebServerVerticle extends Verticle {
8177

8278
matcher.get("/") { HttpServerRequest req ->
8379
req.response.sendFile("${webRoot}/${config.get("index_page", DEFAULT_INDEX_PAGE)}")
84-
// handleListAppRequest()
8580
}
8681

8782
matcher.getWithRegEx("^\\/(bower_components|libs|images|partials|scripts|styles)\\/.*") { HttpServerRequest req ->
88-
println "${req.path.substring(1)}"
8983
req.response.sendFile("${webRoot}/${req.path.substring(1)}")
9084
}
9185

src/main/webapp/bower.json

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
{
2-
"name": "samantha",
3-
"version": "0.0.0",
4-
"dependencies": {
5-
"angular": ">=1.2.*",
6-
"json3": "~3.2.6",
7-
"es5-shim": "~2.1.0",
8-
"angular-resource": ">=1.2.*",
9-
"angular-cookies": ">=1.2.*",
10-
"angular-sanitize": ">=1.2.*",
11-
"angular-route": ">=1.2.*",
12-
"angular-vertxbus": "~0.6.0",
13-
"sockjs": "~0.3.4",
14-
"angular-truncate": "*",
15-
"angular-local-storage": "~0.0.5",
16-
"angular-qrcode": "~3.1.0",
17-
"normalize-css": "~3.0.1"
18-
},
19-
"devDependencies": {
20-
"angular-mocks": ">=1.2.*",
21-
"angular-scenario": ">=1.2.*"
22-
}
2+
"name": "samantha",
3+
"version": "0.0.0",
4+
"dependencies": {
5+
"angular": ">=1.2.*",
6+
"json3": "~3.2.6",
7+
"es5-shim": "~2.1.0",
8+
"angular-resource": ">=1.2.*",
9+
"angular-cookies": ">=1.2.*",
10+
"angular-sanitize": ">=1.2.*",
11+
"angular-route": ">=1.2.*",
12+
"angular-vertxbus": "~0.6.0",
13+
"sockjs": "~0.3.4",
14+
"angular-truncate": "*",
15+
"angular-local-storage": "~0.0.5",
16+
"angular-qrcode": "~3.1.0",
17+
"normalize-css": "~3.0.1",
18+
"lodash": "~2.4.*"
19+
},
20+
"devDependencies": {
21+
"angular-mocks": ">=1.2.*",
22+
"angular-scenario": ">=1.2.*"
23+
}
2324
}

src/main/webapp/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<script src="bower_components/angular-local-storage/angular-local-storage.js"></script>
5454
<script src="bower_components/qrcode-generator/js/qrcode.js"></script>
5555
<script src="bower_components/angular-qrcode/qrcode.js"></script>
56+
<script src="bower_components/lodash/dist/lodash.compat.min.js"></script>
5657
<!-- endbower -->
5758
<!-- endbuild -->
5859

src/main/webapp/partials/devices.html

+4-10
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,16 @@
44
<section class="devices-section">
55

66
<article class="tile device-article" ng-repeat="device in devices"
7-
ng-click="showApps(device)" ng-class="{ true: 'connected', false:'disconnected'}[device.connected]"
8-
ng-init="z=0" ng-mouseover="z= { true: '3', false:'0'}[device.connected]"
9-
ng-mouseleave="z=0">
10-
<paper-shadow z="{{z}}"></paper-shadow>
7+
ng-click="showApps(device)" ng-class="{ true: 'connected', false:'disconnected', }[device.connected]">
118

12-
13-
<div class="title">{{device.brand}} {{device.model}}</div>
9+
<div class="title">{{device.manufacturer}} {{device.model}}</div>
1410
<img class="device-logo" src="../images/device.svg" width="auto" height="48"/>
1511

1612
<div>
1713
<ul class="detail">
1814
<li>IMEI {{device.imei}}</li>
19-
<li>{{device.version}}</li>
20-
<li>API {{device.api}}</li>
21-
<li>Resolution {{device.resolution}}</li>
22-
<li>156 Applications</li>
15+
<li>Android {{device.versionName}}</li>
16+
<li>{{device.dimension.x}}x{{device.dimension.y}}</li>
2317
</ul>
2418
</div>
2519

src/main/webapp/scripts/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ angular.module('samanthaApp', [
1616
templateUrl: 'partials/devices.html',
1717
controller: 'DevicesCtrl'
1818
})
19-
.when('/device/:id/apps', {
19+
.when('/device/:deviceId/apps', {
2020
templateUrl: 'partials/applications.html',
2121
controller: 'ApplicationsCtrl'
2222
})

src/main/webapp/scripts/controllers/ApplicationsController.js

+6-21
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
11
'use strict';
22

33
angular.module('samanthaApp')
4-
.controller('ApplicationsCtrl', ['$scope', '$rootScope', 'vertxEventBusService', 'localStorageService', '$location',
5-
function ($scope, $rootScope, vertxEventBusService, localStorageService, $location) {
4+
.controller('ApplicationsCtrl', ['$scope', '$rootScope', 'vertxEventBusService', '$routeParams',
5+
function ($scope, $rootScope, vertxEventBusService, $routeParams) {
66

77
$scope.applications = [];
8-
$scope.qrCodeData = $location.host();
8+
var appList = [];
99

10-
var appList = localStorageService.get("appList");
10+
var deviceId = $routeParams['deviceId'];
1111

1212
vertxEventBusService.on('vertx.app.post', function (application) {
1313
$scope.applications.push(application);
14-
15-
appList = localStorageService.get("appList");
16-
17-
if (!appList) {
18-
appList = [];
19-
}
20-
2114
appList.push(application);
22-
localStorageService.set("appList", appList);
2315
});
2416

25-
2617
vertxEventBusService.on('android.monitoring.progress', function (sysdump) {
2718
console.log(sysdump);
2819
});
2920

3021

3122
$scope.startApplication = function (application) {
32-
vertxEventBusService.send("vertx");
33-
vertxEventBusService.send("vertx.app.start", {packageName: application.packageName})
23+
vertxEventBusService.send("vertx.app.start", {deviceId: deviceId, packageName: application.packageName})
3424
}
3525

3626
function retrieveApplications() {
37-
if (localStorageService.isSupported && appList) {
38-
$scope.applications = appList;
39-
} else {
40-
localStorageService.remove(appList);
41-
vertxEventBusService.send("vertx.apps.get", {message: ""});
42-
}
27+
vertxEventBusService.send("vertx.apps.get", {deviceId: deviceId});
4328
}
4429

4530

src/main/webapp/scripts/controllers/DevicesController.js

+48-58
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,55 @@ angular.module('samanthaApp')
44
.controller('DevicesCtrl', ['$scope', '$rootScope', 'vertxEventBusService', '$location',
55
function ($scope, $rootScope, vertxEventBusService, $location) {
66

7-
$scope.devices = [
8-
{
9-
imei: 123344,
10-
brand: "Google",
11-
model: "Nexus 5",
12-
resolution: "1920x1080",
13-
api: "20",
14-
version: "Android L",
15-
connected: false
16-
},
17-
{
18-
imei: 123345,
19-
brand: "HTC",
20-
model: "One",
21-
resolution: "1920x1080",
22-
api: "19",
23-
version: "Android 4.4",
24-
connected: true
25-
},
26-
{
27-
imei: 123346,
28-
brand: "Samsung",
29-
model: "Galaxy S3",
30-
resolution: "1920x1080",
31-
api: "17",
32-
version: "Android 4.2.2",
33-
connected: false
34-
},
35-
{
36-
imei: 123347,
37-
brand: "Google",
38-
model: "Nexus 4",
39-
resolution: "1920x1080",
40-
api: "19",
41-
version: "Android 4.4",
42-
connected: false
43-
},
44-
{
45-
imei: 123348,
46-
brand: "Motorola",
47-
model: "Moto X",
48-
resolution: "1920x1080",
49-
api: "19",
50-
version: "Android 4.4",
51-
connected: false
52-
},
53-
{
54-
imei: 123349,
55-
brand: "Samsung",
56-
model: "Galaxy S4",
57-
resolution: "1920x1080",
58-
api: "19",
59-
version: "Android 4.2.2",
60-
connected: false
7+
var imeis = [];
8+
$scope.devices = [];
9+
10+
$scope.showApps = function (device) {
11+
$location.path("device/" + device.imei + "/apps");
12+
}
13+
14+
vertxEventBusService.on('device.connect', function (device) {
15+
addDevice(device);
16+
});
17+
18+
vertxEventBusService.on('device.disconnect', function (device) {
19+
removeDevice(device);
20+
});
21+
22+
23+
$scope.$on('vertx-eventbus.system.connected', function () {
24+
vertxEventBusService.send('vertx.devices.get', null, true).then(function (reply) {
25+
_(reply).each(function(device){
26+
addDevice(device);
27+
});
28+
});
29+
});
30+
31+
32+
33+
function addDevice(device) {
34+
if(!device){
35+
return;
6136
}
62-
]
6337

64-
$scope.showApps = function(device){
65-
$location.path( "device/"+device.imei+"/apps" );
38+
var index = imeis.indexOf(device.imei);
39+
if (index != -1) {
40+
$scope.devices.splice(index, 1, device);
41+
} else {
42+
imeis.push(device.imei)
43+
$scope.devices.push(device);
44+
}
45+
}
46+
function removeDevice(device){
47+
if(!device){
48+
return;
49+
}
50+
51+
var index = imeis.indexOf(device.imei);
52+
if (index != -1) {
53+
$scope.devices.splice(index, 1, device);
54+
}
6655
}
6756

68-
}]);
57+
}])
58+
;

src/main/webapp/styles/_welcome.scss

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ body {
3232
cursor: pointer;
3333

3434
&:hover {
35-
transform: translate3d(0, -2px, 0);
36-
-webkit-transform: translate3d(0, -2px, 0);
35+
@include transform(translate3d(0, -2px, 0));
3736
}
3837

3938
}

0 commit comments

Comments
 (0)