Stefan Huber

api changes

export interface Node {
ip: string;
uuid: string;
couchPort: string;
webPort: string;
}
"use strict";
......@@ -6,10 +6,8 @@ export declare class Repository {
protected device: Device;
protected _db: any;
protected _params: any;
protected _localWebPort: number;
db: any;
params: any;
readonly localWebPort: number;
constructor(rest: Rest, device: Device);
findById(id: string): Promise<any>;
findByIds(ids: Array<string>): Promise<Array<any>>;
......
......@@ -15,7 +15,6 @@ var Repository = (function () {
function Repository(rest, device) {
this.rest = rest;
this.device = device;
this._localWebPort = 8320;
}
Object.defineProperty(Repository.prototype, "db", {
get: function () {
......@@ -37,13 +36,6 @@ var Repository = (function () {
enumerable: true,
configurable: true
});
Object.defineProperty(Repository.prototype, "localWebPort", {
get: function () {
return this._localWebPort;
},
enumerable: true,
configurable: true
});
Repository.prototype.findById = function (id) {
return this.db.get(id);
};
......@@ -112,7 +104,7 @@ var Repository = (function () {
_this.rest.scanEnvironment(response.local_ips)
.then(function (node) {
var res = _this.parseUrl(response.db_url);
resolve(_this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + node.IP + ':' + _this.localWebPort + '/' + res.orig_db_name));
resolve(_this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + node.ip + ':' + node.couchPort + '/' + res.orig_db_name));
}).catch(function () {
resolve(_this.prepare(response.db_url));
});
......
import { Http } from '@angular/http';
import { DeviceInfo } from './../api/device-info';
import { Node } from './../api/node';
export declare const ERROR_CODE_UNAUTHORIZED: string;
export declare const ERROR_CODE_NOT_FOUND: string;
export declare const ERROR_CODE_SERVER_ERROR: string;
export declare class Rest {
private http;
static localWebPort: number;
static serviceUrl: string;
constructor(http: Http);
prepareDeviceInfo(deviceInfo: DeviceInfo, prefixed?: boolean): string;
register(registerCode: string, deviceInfo: DeviceInfo): Promise<any>;
heartbeat(deviceInfo: DeviceInfo): Promise<any>;
scanNode(node: any): Promise<any>;
scanEnvironment(nodes: any[]): Promise<any>;
scanNode(node: any): Promise<Node>;
scanEnvironment(nodes: any[]): Promise<Node>;
}
......
......@@ -92,12 +92,17 @@ var Rest = Rest_1 = (function () {
var _this = this;
return new Promise(function (resolve, reject) {
if (node && node.IP) {
_this.http.get(node.IP + "/device")
_this.http.get("http://" + node.IP + ":" + Rest_1.localWebPort + "/device")
.subscribe(function (response) {
try {
var body = response.json();
if (body.device_id == node.UUID) {
resolve(body);
resolve({
ip: node.IP,
uuid: body.device_id,
couchPort: body.couch_port,
webPort: node.web_port
});
}
else {
reject();
......@@ -135,6 +140,7 @@ var Rest = Rest_1 = (function () {
};
return Rest;
}());
Rest.localWebPort = 8320;
Rest.serviceUrl = "http://someurl.com";
Rest = Rest_1 = __decorate([
core_1.Injectable(),
......
export interface Node {
ip : string;
uuid : string;
couchPort : string;
webPort : string;
}
\ No newline at end of file
......@@ -10,7 +10,6 @@ export class Repository {
protected _db:any;
protected _params:any;
protected _localWebPort:number = 8320;
get db() {
return this._db;
......@@ -28,10 +27,6 @@ export class Repository {
this._params = params;
}
get localWebPort() : number {
return this._localWebPort;
}
constructor(
protected rest:Rest ,
protected device:Device
......@@ -104,7 +99,7 @@ export class Repository {
this.rest.scanEnvironment(response.local_ips)
.then(node => {
let res = this.parseUrl(response.db_url);
resolve(this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + node.IP + ':' + this.localWebPort + '/' + res.orig_db_name));
resolve(this.prepare(res.protocol + '://' + res.user + ':' + res.pass + '@' + node.ip + ':' + node.couchPort + '/' + res.orig_db_name));
}).catch(() => {
resolve(this.prepare(response.db_url));
});
......
import {Injectable} from '@angular/core';
import {Http,Response,Headers} from '@angular/http';
import { DeviceInfo } from './../api/device-info';
import { Node } from './../api/node';
export const ERROR_CODE_UNAUTHORIZED:string = "unauthorized";
export const ERROR_CODE_NOT_FOUND:string = "not-found";
......@@ -9,6 +10,7 @@ export const ERROR_CODE_SERVER_ERROR:string = "server-error";
@Injectable()
export class Rest {
public static localWebPort:number = 8320;
public static serviceUrl: string = "http://someurl.com";
constructor(private http:Http) {}
......@@ -94,15 +96,20 @@ export class Rest {
});
}
scanNode(node:any) : Promise<any> {
return new Promise<any> ((resolve, reject) => {
scanNode(node:any) : Promise<Node> {
return new Promise<Node> ((resolve, reject) => {
if (node && node.IP) {
this.http.get(node.IP + "/device")
this.http.get("http://" + node.IP + ":" + Rest.localWebPort + "/device")
.subscribe(response => {
try {
let body = response.json();
if (body.device_id == node.UUID) {
resolve(body);
resolve({
ip : node.IP ,
uuid : body.device_id ,
couchPort : body.couch_port ,
webPort : node.web_port
});
} else {
reject();
}
......@@ -118,8 +125,8 @@ export class Rest {
});
}
scanEnvironment(nodes:any[]) : Promise<any> {
return new Promise<any>((resolve, reject) => {
scanEnvironment(nodes:any[]) : Promise<Node> {
return new Promise<Node>((resolve, reject) => {
let index = Math.floor(Math.random() * nodes.length);
let node = nodes.splice(index, 1)[0];
this.scanNode(node)
......