Skip to content

Commit

Permalink
Merge pull request #4 from GDSC-HSU/feature/view-device
Browse files Browse the repository at this point in the history
Feature/view device
  • Loading branch information
truonghoangduy authored Mar 20, 2022
2 parents abbf0ba + 214fa86 commit b9709f5
Show file tree
Hide file tree
Showing 19 changed files with 353 additions and 88 deletions.
86 changes: 81 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@nebular/eva-icons": "^9.0.1",
"@nebular/theme": "^9.0.1",
"@ngrx/store": "^13.0.2",
"angularx-qrcode": "^13.0.3",
"eva-icons": "^1.1.3",
"firebase": "^9.4.0",
"lottie-web": "^5.9.1",
Expand Down
6 changes: 5 additions & 1 deletion src/app/models/device.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { DeviceStatus } from "./enums/device_status";

export interface Device {
id?: string,
accessKey?: string,
oid?: string,
name?: string,
location?: string,
hardwareInfo?: JSON,
tag?: Array<string>;
tags?: Array<string>;
status?: DeviceStatus;
timestampUpdate?: string;
}
4 changes: 0 additions & 4 deletions src/app/models/device_status.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,31 @@ export class DeviceCardDetailComponent implements OnInit {
@Input() device?: Device
status: DeviceStatus = DeviceStatus.disconnected;

constructor(private deviceService: DeviceService, private ref: ChangeDetectorRef) { }
constructor(private deviceService: DeviceService) { }

ngOnInit(): void {
this.listenStatus();
}

observerStatus = {
next: (value: DocumentSnapshot<DocumentData>) => {
let deviceStatus = value.data();
if (deviceStatus!["status"]) {
this.status = DeviceStatus.connected;
}
else {
this.status = DeviceStatus.disconnected;
}
this.ref.detectChanges();
},
error: (err: FirestoreError) => console.error(err),
complete: () => console.log('Observer got a complete notification'),
};

listenStatus() {
this.deviceService.checkCreated(this.device!.id!).then(value => {
if (value.data()) {
let dataSnapshot = this.deviceService.listenStatus(this.device!.id!, this.observerStatus);
} else {
this.status = DeviceStatus.created;
}
})
this.deviceService.checkCreated(this.device!.id!);
}

getTextStatus() {
if (this.status == DeviceStatus.connected) {
if (this.device?.status == DeviceStatus.connected) {
return "online";
}
else if (this.status == DeviceStatus.disconnected) {
else if (this.device?.status == DeviceStatus.disconnected) {
return "offline";
}
return "pending";
}

getStatus(){
if (this.status == DeviceStatus.connected) {
if (this.device?.status == DeviceStatus.connected) {
return "success";
}
else if (this.status == DeviceStatus.disconnected) {
else if (this.device?.status == DeviceStatus.disconnected) {
return "danger";
}
return "warning";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<nb-card class="main-card-device">
<nb-card [nbSpinner]="this.deviceService.isLoadingGetDevice" nbSpinnerStatus="primary" class="main-card-device">
<nb-card-header>
<div class="main-card-device-header">
<div class="title-card">
<b>
Devices
</b>
</div>
<button nbButton status="primary">
<button (click)="this.openAddNewDialog()" nbButton status="primary">
<nb-icon icon="plus-outline"></nb-icon>
new device
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Component, OnInit } from '@angular/core';
import { ChangeDetectorRef, Component, Input, OnInit } from '@angular/core';
import { NbDialogService } from '@nebular/theme';
import { Device } from 'src/app/models/device';
import { DeviceService } from 'src/app/services/device/device.service';
import { CreateDeviceDialogComponent } from '../../dialog/create-device-dialog/create-device-dialog.component';

@Component({
selector: 'app-device-card',
templateUrl: './device-card.component.html',
styleUrls: ['./device-card.component.scss']
})
export class DeviceCardComponent implements OnInit {
constructor(public deviceService: DeviceService) { }
@Input() loading!: boolean;
constructor(private dialogService: NbDialogService, public deviceService: DeviceService, private ref: ChangeDetectorRef) { }
ngOnInit(): void {
this.deviceService.renderUIDeviceStatus = () => { this.ref.detectChanges() }
}


openAddNewDialog() {
this.dialogService.open(CreateDeviceDialogComponent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ <h5>

<div class="header-total-text">
<h5>
{{this.totalConnect}} Devices
{{this.countDeviceConnected()}} Devices
</h5>
<p class="total-text">Connecting devices</p>
<p class="total-text">Connected devices</p>
</div>

</div>
Expand All @@ -48,9 +48,9 @@ <h5>

<div class="header-total-text">
<h5>
{{this.totalDisconnect}} Devices
{{this.countDeviceDisconnected()}} Devices
</h5>
<p class="total-text">Disconnect devices</p>
<p class="total-text">Disconnected devices</p>
</div>

</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { Firestore } from '@angular/fire/firestore';
import { collection, DocumentData, onSnapshot, QueryDocumentSnapshot, QuerySnapshot } from 'firebase/firestore';
import { DeviceStatus } from 'src/app/models/device_status';
import { DeviceStatus } from 'src/app/models/enums/device_status';
import { DeviceService } from 'src/app/services/device/device.service';

@Component({
Expand All @@ -13,32 +13,17 @@ export class HeaderTotalComponent implements OnInit {
totalConnect = 0;
totalDisconnect = 0;

constructor(public deviceService: DeviceService, private fireStore: Firestore) { }
constructor(public deviceService: DeviceService, private fireStore: Firestore, private ref: ChangeDetectorRef) { }

ngOnInit(): void {
this.listenDeviceCount();
this.deviceService.renderUIDeviceStatistics = () => { this.ref.detectChanges(); }
}

listenDeviceCount() {
onSnapshot(collection(this.fireStore, "device_status"), {
next: (value: QuerySnapshot<DocumentData>) => {
this.countConnect(value.docs);
},
})
countDeviceConnected() {
return this.deviceService.devices.filter(element => element.status == DeviceStatus.connected).length
}
countConnect(array: Array<QueryDocumentSnapshot<DocumentData>>) {
let countConnect = 0;
let countDisconnect = 0;
array.forEach(deviceStatusSnap => {
let deviceStatus = deviceStatusSnap.data() as DeviceStatus;
if (deviceStatus.status) {
countConnect++;
}
else {
countDisconnect++;
}
});
this.totalConnect = countConnect;
this.totalDisconnect = countDisconnect;

countDeviceDisconnected() {
return this.deviceService.devices.filter(element => element.status == DeviceStatus.disconnected).length
}
}
Loading

0 comments on commit b9709f5

Please sign in to comment.