Stefan Huber

api changes

1 +export interface Node {
2 + ip: string;
3 + uuid: string;
4 + couchPort: string;
5 + webPort: string;
6 +}
1 +"use strict";
...@@ -6,10 +6,8 @@ export declare class Repository { ...@@ -6,10 +6,8 @@ export declare class Repository {
6 protected device: Device; 6 protected device: Device;
7 protected _db: any; 7 protected _db: any;
8 protected _params: any; 8 protected _params: any;
9 - protected _localWebPort: number;
10 db: any; 9 db: any;
11 params: any; 10 params: any;
12 - readonly localWebPort: number;
13 constructor(rest: Rest, device: Device); 11 constructor(rest: Rest, device: Device);
14 findById(id: string): Promise<any>; 12 findById(id: string): Promise<any>;
15 findByIds(ids: Array<string>): Promise<Array<any>>; 13 findByIds(ids: Array<string>): Promise<Array<any>>;
......
...@@ -15,7 +15,6 @@ var Repository = (function () { ...@@ -15,7 +15,6 @@ var Repository = (function () {
15 function Repository(rest, device) { 15 function Repository(rest, device) {
16 this.rest = rest; 16 this.rest = rest;
17 this.device = device; 17 this.device = device;
18 - this._localWebPort = 8320;
19 } 18 }
20 Object.defineProperty(Repository.prototype, "db", { 19 Object.defineProperty(Repository.prototype, "db", {
21 get: function () { 20 get: function () {
...@@ -37,13 +36,6 @@ var Repository = (function () { ...@@ -37,13 +36,6 @@ var Repository = (function () {
37 enumerable: true, 36 enumerable: true,
38 configurable: true 37 configurable: true
39 }); 38 });
40 - Object.defineProperty(Repository.prototype, "localWebPort", {
41 - get: function () {
42 - return this._localWebPort;
43 - },
44 - enumerable: true,
45 - configurable: true
46 - });
47 Repository.prototype.findById = function (id) { 39 Repository.prototype.findById = function (id) {
48 return this.db.get(id); 40 return this.db.get(id);
49 }; 41 };
...@@ -112,7 +104,7 @@ var Repository = (function () { ...@@ -112,7 +104,7 @@ var Repository = (function () {
112 _this.rest.scanEnvironment(response.local_ips) 104 _this.rest.scanEnvironment(response.local_ips)
113 .then(function (node) { 105 .then(function (node) {
114 var res = _this.parseUrl(response.db_url); 106 var res = _this.parseUrl(response.db_url);
115 - resolve(_this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + node.IP + ':' + _this.localWebPort + '/' + res.orig_db_name)); 107 + resolve(_this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + node.ip + ':' + node.couchPort + '/' + res.orig_db_name));
116 }).catch(function () { 108 }).catch(function () {
117 resolve(_this.prepare(response.db_url)); 109 resolve(_this.prepare(response.db_url));
118 }); 110 });
......
1 import { Http } from '@angular/http'; 1 import { Http } from '@angular/http';
2 import { DeviceInfo } from './../api/device-info'; 2 import { DeviceInfo } from './../api/device-info';
3 +import { Node } from './../api/node';
3 export declare const ERROR_CODE_UNAUTHORIZED: string; 4 export declare const ERROR_CODE_UNAUTHORIZED: string;
4 export declare const ERROR_CODE_NOT_FOUND: string; 5 export declare const ERROR_CODE_NOT_FOUND: string;
5 export declare const ERROR_CODE_SERVER_ERROR: string; 6 export declare const ERROR_CODE_SERVER_ERROR: string;
6 export declare class Rest { 7 export declare class Rest {
7 private http; 8 private http;
9 + static localWebPort: number;
8 static serviceUrl: string; 10 static serviceUrl: string;
9 constructor(http: Http); 11 constructor(http: Http);
10 prepareDeviceInfo(deviceInfo: DeviceInfo, prefixed?: boolean): string; 12 prepareDeviceInfo(deviceInfo: DeviceInfo, prefixed?: boolean): string;
11 register(registerCode: string, deviceInfo: DeviceInfo): Promise<any>; 13 register(registerCode: string, deviceInfo: DeviceInfo): Promise<any>;
12 heartbeat(deviceInfo: DeviceInfo): Promise<any>; 14 heartbeat(deviceInfo: DeviceInfo): Promise<any>;
13 - scanNode(node: any): Promise<any>; 15 + scanNode(node: any): Promise<Node>;
14 - scanEnvironment(nodes: any[]): Promise<any>; 16 + scanEnvironment(nodes: any[]): Promise<Node>;
15 } 17 }
......
...@@ -92,12 +92,17 @@ var Rest = Rest_1 = (function () { ...@@ -92,12 +92,17 @@ var Rest = Rest_1 = (function () {
92 var _this = this; 92 var _this = this;
93 return new Promise(function (resolve, reject) { 93 return new Promise(function (resolve, reject) {
94 if (node && node.IP) { 94 if (node && node.IP) {
95 - _this.http.get(node.IP + "/device") 95 + _this.http.get("http://" + node.IP + ":" + Rest_1.localWebPort + "/device")
96 .subscribe(function (response) { 96 .subscribe(function (response) {
97 try { 97 try {
98 var body = response.json(); 98 var body = response.json();
99 if (body.device_id == node.UUID) { 99 if (body.device_id == node.UUID) {
100 - resolve(body); 100 + resolve({
101 + ip: node.IP,
102 + uuid: body.device_id,
103 + couchPort: body.couch_port,
104 + webPort: node.web_port
105 + });
101 } 106 }
102 else { 107 else {
103 reject(); 108 reject();
...@@ -135,6 +140,7 @@ var Rest = Rest_1 = (function () { ...@@ -135,6 +140,7 @@ var Rest = Rest_1 = (function () {
135 }; 140 };
136 return Rest; 141 return Rest;
137 }()); 142 }());
143 +Rest.localWebPort = 8320;
138 Rest.serviceUrl = "http://someurl.com"; 144 Rest.serviceUrl = "http://someurl.com";
139 Rest = Rest_1 = __decorate([ 145 Rest = Rest_1 = __decorate([
140 core_1.Injectable(), 146 core_1.Injectable(),
......
1 +export interface Node {
2 + ip : string;
3 + uuid : string;
4 + couchPort : string;
5 + webPort : string;
6 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -10,7 +10,6 @@ export class Repository { ...@@ -10,7 +10,6 @@ export class Repository {
10 10
11 protected _db:any; 11 protected _db:any;
12 protected _params:any; 12 protected _params:any;
13 - protected _localWebPort:number = 8320;
14 13
15 get db() { 14 get db() {
16 return this._db; 15 return this._db;
...@@ -28,10 +27,6 @@ export class Repository { ...@@ -28,10 +27,6 @@ export class Repository {
28 this._params = params; 27 this._params = params;
29 } 28 }
30 29
31 - get localWebPort() : number {
32 - return this._localWebPort;
33 - }
34 -
35 constructor( 30 constructor(
36 protected rest:Rest , 31 protected rest:Rest ,
37 protected device:Device 32 protected device:Device
...@@ -104,7 +99,7 @@ export class Repository { ...@@ -104,7 +99,7 @@ export class Repository {
104 this.rest.scanEnvironment(response.local_ips) 99 this.rest.scanEnvironment(response.local_ips)
105 .then(node => { 100 .then(node => {
106 let res = this.parseUrl(response.db_url); 101 let res = this.parseUrl(response.db_url);
107 - resolve(this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + node.IP + ':' + this.localWebPort + '/' + res.orig_db_name)); 102 + resolve(this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + node.ip + ':' + node.couchPort + '/' + res.orig_db_name));
108 }).catch(() => { 103 }).catch(() => {
109 resolve(this.prepare(response.db_url)); 104 resolve(this.prepare(response.db_url));
110 }); 105 });
......
1 import {Injectable} from '@angular/core'; 1 import {Injectable} from '@angular/core';
2 import {Http,Response,Headers} from '@angular/http'; 2 import {Http,Response,Headers} from '@angular/http';
3 import { DeviceInfo } from './../api/device-info'; 3 import { DeviceInfo } from './../api/device-info';
4 +import { Node } from './../api/node';
4 5
5 export const ERROR_CODE_UNAUTHORIZED:string = "unauthorized"; 6 export const ERROR_CODE_UNAUTHORIZED:string = "unauthorized";
6 export const ERROR_CODE_NOT_FOUND:string = "not-found"; 7 export const ERROR_CODE_NOT_FOUND:string = "not-found";
...@@ -9,6 +10,7 @@ export const ERROR_CODE_SERVER_ERROR:string = "server-error"; ...@@ -9,6 +10,7 @@ export const ERROR_CODE_SERVER_ERROR:string = "server-error";
9 @Injectable() 10 @Injectable()
10 export class Rest { 11 export class Rest {
11 12
13 + public static localWebPort:number = 8320;
12 public static serviceUrl: string = "http://someurl.com"; 14 public static serviceUrl: string = "http://someurl.com";
13 15
14 constructor(private http:Http) {} 16 constructor(private http:Http) {}
...@@ -94,15 +96,20 @@ export class Rest { ...@@ -94,15 +96,20 @@ export class Rest {
94 }); 96 });
95 } 97 }
96 98
97 - scanNode(node:any) : Promise<any> { 99 + scanNode(node:any) : Promise<Node> {
98 - return new Promise<any> ((resolve, reject) => { 100 + return new Promise<Node> ((resolve, reject) => {
99 if (node && node.IP) { 101 if (node && node.IP) {
100 - this.http.get(node.IP + "/device") 102 + this.http.get("http://" + node.IP + ":" + Rest.localWebPort + "/device")
101 .subscribe(response => { 103 .subscribe(response => {
102 try { 104 try {
103 let body = response.json(); 105 let body = response.json();
104 if (body.device_id == node.UUID) { 106 if (body.device_id == node.UUID) {
105 - resolve(body); 107 + resolve({
108 + ip : node.IP ,
109 + uuid : body.device_id ,
110 + couchPort : body.couch_port ,
111 + webPort : node.web_port
112 + });
106 } else { 113 } else {
107 reject(); 114 reject();
108 } 115 }
...@@ -118,8 +125,8 @@ export class Rest { ...@@ -118,8 +125,8 @@ export class Rest {
118 }); 125 });
119 } 126 }
120 127
121 - scanEnvironment(nodes:any[]) : Promise<any> { 128 + scanEnvironment(nodes:any[]) : Promise<Node> {
122 - return new Promise<any>((resolve, reject) => { 129 + return new Promise<Node>((resolve, reject) => {
123 let index = Math.floor(Math.random() * nodes.length); 130 let index = Math.floor(Math.random() * nodes.length);
124 let node = nodes.splice(index, 1)[0]; 131 let node = nodes.splice(index, 1)[0];
125 this.scanNode(node) 132 this.scanNode(node)
......