Skip to content

Commit

Permalink
Add the device single to read from https (#226)
Browse files Browse the repository at this point in the history
* Add the device single to read from https

* Solve linting issue
  • Loading branch information
torabian authored Apr 27, 2018
1 parent f7b84e1 commit 40ea033
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 110 deletions.
41 changes: 22 additions & 19 deletions src/app/components/device-single/device-single.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { IResponse } from 'response-type';
import { error } from '@app/common';
import { NotificationService } from '@app/services/notification.service';
import { TranslateService } from '@ngx-translate/core';
import 'rxjs/add/observable/combineLatest';
import { Observable } from 'rxjs/Observable';

@Component({
selector: 'app-device-single',
Expand All @@ -17,7 +19,6 @@ import { TranslateService } from '@ngx-translate/core';
export class DeviceSingleComponent implements OnInit, OnDestroy {

public isRequesting = false;
private ref = null;
public response: IResponse<CloudDevice> = null;
public locations: Array<any> = [];
public form: CloudDevice = {
Expand All @@ -36,26 +37,28 @@ export class DeviceSingleComponent implements OnInit, OnDestroy {
) {
}

ngOnInit() {
this.route.params.subscribe((data: {id?: any, sourceId?: any}) => {
if (data.sourceId) {
this.form.datasource = data.sourceId;
async ngOnInit() {
Observable.combineLatest(
this.store.select('devices'),
this.route.params
).subscribe(([devices, params]) => {
if (params.sourceId) {
this.form.datasource = params.sourceId;
}
if ( ! data.id ) {
if ( ! params.id ) {
return ;
}
this.form.id = data.id;
this.ref = this.store.select('devices').subscribe((devices: Array<CloudDevice>) => {
/* tslint:disable */
const form = devices.find(dev => dev.id == data.id);
if ( ! form) {
return;
}
this.form = Object.assign({}, form);


}).unsubscribe();
this.form.id = params.id;
const form = devices.find(dev => dev.id === +params.id);
if (!form) {
return;
}
this.form = Object.assign({}, form);
}).unsubscribe();
const device = await this.requests.getDevice(this.form.id);
if ( device ) {
this.form = device;
}
}
ngOnDestroy () {

Expand Down Expand Up @@ -85,7 +88,7 @@ export class DeviceSingleComponent implements OnInit, OnDestroy {
} else {
this.notification.InvokeDeviceCreate(response.data.items[0]);
}

}
this.response = response;
} catch (error) {
Expand All @@ -107,5 +110,5 @@ export class DeviceSingleComponent implements OnInit, OnDestroy {
public DeviceCustomizationChange (value) {
this.form.preferences = value;
}

}
193 changes: 102 additions & 91 deletions src/app/services/mocks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,99 @@ import { IotSvgService } from '@services/iot-svg/iot-svg.service';
import { random, times } from '@lodash';
import { ILocation } from '@app/definitions';
import { TranslateService } from '@ngx-translate/core';
import 'rxjs/add/operator/delay';

const devices = [
{
id: 1,
name: 'Hall temperature',
type: CloudDeviceType.TemperatureSensor,
datasource: 'device-1',
value: random(10, 30),
location: 1,
preferences: {
DisplayRealTimeTemperatureInSidebar: true,
DisplayHistoryStatisticsInHome: true
}
},
{
id: 2,
name: 'Kitchen temperature',
type: CloudDeviceType.TemperatureSensor,
datasource: 'device-2',
value: random(10, 30),
location: 1,
preferences: {
DisplayRealTimeTemperatureInSidebar: true
}
},
{
id: 3,
name: 'Main Lamp',
type: CloudDeviceType.LampBridge,
datasource: 'device-4',
value: 1,
location: 1,
preferences: {
DisplayLampOnOffInHome: true
}
},
{
id: 4,
name: 'Lobby humidity',
type: CloudDeviceType.HumiditySensor,
datasource: 'device-5',
value: random(10, 60),
location: 2,
preferences: {
DisplayHumidityInHome: true,
}
},
{
id: 5,
name: 'CO2 sensor',
type: CloudDeviceType.CO2Sensor,
datasource: 'device-6',
value: random(100, 600),
location: 2,
preferences: {
DisplayCO2InHome: true
}
},
{
id: 6,
name: 'Magnet temperature',
type: CloudDeviceType.TemperatureSensor,
datasource: 'device-7',
value: random(10, 30),
location: 2,
preferences: {
DisplayRealTimeTemperatureInSidebar: true
}
},
{
id: 7,
name: 'Negative temperature',
type: CloudDeviceType.TemperatureSensor,
datasource: 'device-8',
value: random(10, 30),
location: 3,
preferences: {
DisplayRealTimeTemperatureInSidebar: true
}
},
{
id: 8,
name: 'Thermal temperature',
type: CloudDeviceType.TemperatureSensor,
datasource: 'device-9',
value: random(10, 30),
location: 3,
preferences: {
DisplayRealTimeTemperatureInSidebar: true
}
},
];
const validateLocation = (location: ILocation) => {
const errors: Array<IResponseErrorItem> = [];
if (!location.name) {
Expand Down Expand Up @@ -46,6 +138,7 @@ export class MockService {
'GET /api/devices/daily-history/:id': 'GetDeviceDailyHistory',
'GET /api/devices/token': 'getDevicesToken',
'GET /api/devices/day-history/:date/:id': 'GetDeviceDayHistory',
'GET /api/device/:id': 'getDevice',
'GET /api/devices': 'getDevices',
'GET /api/unconnected': 'getUnconnected',
'POST /api/device': 'postDevice',
Expand Down Expand Up @@ -305,97 +398,15 @@ export class MockService {
public getDevices (): IResponse<CloudDevice> {
return {
data: {
items: [
{
id: 1,
name: 'Hall temperature',
type: CloudDeviceType.TemperatureSensor,
datasource: 'device-1',
value: random(10, 30),
location: 1,
preferences: {
DisplayRealTimeTemperatureInSidebar: true,
DisplayHistoryStatisticsInHome: true
}
},
{
id: 2,
name: 'Kitchen temperature',
type: CloudDeviceType.TemperatureSensor,
datasource: 'device-2',
value: random(10, 30),
location: 1,
preferences: {
DisplayRealTimeTemperatureInSidebar: true
}
},
{
id: 3,
name: 'Main Lamp',
type: CloudDeviceType.LampBridge,
datasource: 'device-4',
value: 1,
location: 1,
preferences: {
DisplayLampOnOffInHome: true
}
},
{
id: 4,
name: 'Lobby humidity',
type: CloudDeviceType.HumiditySensor,
datasource: 'device-5',
value: random(10, 60),
location: 2,
preferences: {
DisplayHumidityInHome: true,
}
},
{
id: 5,
name: 'CO2 sensor',
type: CloudDeviceType.CO2Sensor,
datasource: 'device-6',
value: random(100, 600),
location: 2,
preferences: {
DisplayCO2InHome: true
}
},
{
id: 6,
name: 'Magnet temperature',
type: CloudDeviceType.TemperatureSensor,
datasource: 'device-7',
value: random(10, 30),
location: 2,
preferences: {
DisplayRealTimeTemperatureInSidebar: true
}
},
{
id: 7,
name: 'Negative temperature',
type: CloudDeviceType.TemperatureSensor,
datasource: 'device-8',
value: random(10, 30),
location: 3,
preferences: {
DisplayRealTimeTemperatureInSidebar: true
}
},
{
id: 8,
name: 'Thermal temperature',
type: CloudDeviceType.TemperatureSensor,
datasource: 'device-9',
value: random(10, 30),
location: 3,
preferences: {
DisplayRealTimeTemperatureInSidebar: true
}
},
]
items: devices
}
};
}
public getDevice (req: HttpRequest<any> , params): IResponse<CloudDevice> {
const id = req.url.split('/').reverse()[0];
return {
data: {
items: devices.filter(device => device.id === +id)
}
};
}
Expand Down
15 changes: 15 additions & 0 deletions src/app/services/requests.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ export class RequestsService {
);
}

public async getDevice (id: number) {
try {
const response: IResponse<CloudDevice> = await (this.http.get(environment.api + '/api/device/' + id).toPromise());
const collections = response.data.items;
for (const item of collections) {
this.store.dispatch({
type: 'UPDATE_DEVICE',
payload: item
});
}
return collections[0];
} catch (error) {

}
}
getDevices () {
this.http.get(environment.api + '/api/devices').subscribe(
(response: any) => {
Expand Down

0 comments on commit 40ea033

Please sign in to comment.