dev smartfarming #1
|
|
@ -27,9 +27,28 @@ export class SensorService extends ApiService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getSensorData(sensor: string, metric: string, startEnd: string, timeRange: string): Observable<ApiResponse> {
|
getSensorDataHourly(sensor: string, metric: string, start: string, end: string, timeRange: string): Observable<ApiResponse> {
|
||||||
const params = new HttpParams()
|
const params = new HttpParams()
|
||||||
.set('range[end]', startEnd)
|
.set('range[start]', start)
|
||||||
|
.set('range[end]', end)
|
||||||
|
.set('range[time_range]', timeRange)
|
||||||
|
.set('sensor', sensor)
|
||||||
|
.set('metric', metric);
|
||||||
|
|
||||||
|
const headers= this.createAuthHeaders();
|
||||||
|
|
||||||
|
return this.http.get<ApiResponse>(this.getDataUrl, { params, headers }).pipe(
|
||||||
|
catchError(error => {
|
||||||
|
// this.toast.error('Failed to get sensor data for graphic, please try again');
|
||||||
|
return throwError(error);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
getSensorDataDaily(sensor: string, metric: string, start: string, end: string, timeRange: string): Observable<ApiResponse> {
|
||||||
|
const params = new HttpParams()
|
||||||
|
.set('range[start]', start)
|
||||||
|
.set('range[end]', end)
|
||||||
.set('range[time_range]', timeRange)
|
.set('range[time_range]', timeRange)
|
||||||
.set('sensor', sensor)
|
.set('sensor', sensor)
|
||||||
.set('metric', metric);
|
.set('metric', metric);
|
||||||
|
|
|
||||||
|
|
@ -114,62 +114,138 @@ export class GraphComponent implements OnInit, AfterViewInit, OnDestroy, OnChang
|
||||||
return `${year}-${month}-${day}`;
|
return `${year}-${month}-${day}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getDateAgo():string{
|
||||||
|
const today = new Date();
|
||||||
|
today.setDate(today.getDate() - 7);
|
||||||
|
|
||||||
|
const year = today.getFullYear();
|
||||||
|
const month = String(today.getMonth() + 1).padStart(2, '0');
|
||||||
|
const day = String(today.getDate()).padStart(2, '0');
|
||||||
|
|
||||||
|
return `${year}-${month}-${day}`;
|
||||||
|
}
|
||||||
|
|
||||||
fetchDHTData(timeRange: string): void {
|
fetchDHTData(timeRange: string): void {
|
||||||
const startEnd = this.getDate();
|
const hEnd = this.getDate();
|
||||||
this.sensorService.getSensorData('dht', 'npk', startEnd, timeRange).subscribe({
|
const hStart = this.getDate();
|
||||||
next: (response) => {
|
const dEnd = this.getDate();
|
||||||
this.isLoadingDHT = false;
|
const dStart = this.getDateAgo();
|
||||||
if (response.statusCode === 200 && response.data.dht?.length > 0) {
|
|
||||||
this.createChart(this.dhtChartElement.nativeElement, response, 'dht', 'npk');
|
if(timeRange === 'HOURLY'){
|
||||||
this.isNoDataDHT = false;
|
this.sensorService.getSensorDataHourly('dht', 'npk', hStart, hEnd, timeRange).subscribe({
|
||||||
} else {
|
next: (response) => {
|
||||||
|
this.isLoadingDHT = false;
|
||||||
|
if (response.statusCode === 200 && response.data.dht?.length > 0) {
|
||||||
|
this.createChart(this.dhtChartElement.nativeElement, response, 'dht', 'npk');
|
||||||
|
this.isNoDataDHT = false;
|
||||||
|
} else {
|
||||||
|
this.isNoDataDHT = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: () => {
|
||||||
|
this.isLoadingDHT = false;
|
||||||
this.isNoDataDHT = true;
|
this.isNoDataDHT = true;
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
error: () => {
|
} else if(timeRange === 'DAILY'){
|
||||||
this.isLoadingDHT = false;
|
this.sensorService.getSensorDataDaily('dht', 'npk', dStart, dEnd, timeRange).subscribe({
|
||||||
this.isNoDataDHT = true;
|
next: (response) => {
|
||||||
}
|
this.isLoadingDHT = false;
|
||||||
});
|
if (response.statusCode === 200 && response.data.dht?.length > 0) {
|
||||||
|
this.createChart(this.dhtChartElement.nativeElement, response, 'dht', 'npk');
|
||||||
|
this.isNoDataDHT = false;
|
||||||
|
} else {
|
||||||
|
this.isNoDataDHT = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: () => {
|
||||||
|
this.isLoadingDHT = false;
|
||||||
|
this.isNoDataDHT = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchNPK1Data(timeRange: string): void {
|
fetchNPK1Data(timeRange: string): void {
|
||||||
const startEnd = this.getDate();
|
const hEnd = this.getDate();
|
||||||
this.sensorService.getSensorData('npk1', this.selectedNPK1, startEnd, timeRange).subscribe({
|
const hStart = this.getDate();
|
||||||
next: (response) => {
|
const dEnd = this.getDate();
|
||||||
this.isLoadingNPK1 = false;
|
const dStart = this.getDateAgo();
|
||||||
if (response.statusCode === 200 && response.data.npk1?.length > 0) {
|
|
||||||
this.createChart(this.npk1ChartElement.nativeElement, response, 'npk1', this.selectedNPK1);
|
if(timeRange === 'HOURLY'){
|
||||||
this.isNoDataNPK1 = false;
|
this.sensorService.getSensorDataHourly('npk1', this.selectedNPK1, hEnd, hStart, timeRange).subscribe({
|
||||||
} else {
|
next: (response) => {
|
||||||
|
this.isLoadingNPK1 = false;
|
||||||
|
if (response.statusCode === 200 && response.data.npk1?.length > 0) {
|
||||||
|
this.createChart(this.npk1ChartElement.nativeElement, response, 'npk1', this.selectedNPK1);
|
||||||
|
this.isNoDataNPK1 = false;
|
||||||
|
} else {
|
||||||
|
this.isNoDataNPK1 = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: () => {
|
||||||
|
this.isLoadingNPK1 = false;
|
||||||
this.isNoDataNPK1 = true;
|
this.isNoDataNPK1 = true;
|
||||||
}
|
}
|
||||||
},
|
})
|
||||||
error: () => {
|
}else if(timeRange === 'DAILY'){
|
||||||
this.isLoadingNPK1 = false;
|
this.sensorService.getSensorDataDaily('npk1', this.selectedNPK1, dStart, dEnd, timeRange).subscribe({
|
||||||
this.isNoDataNPK1 = true;
|
next: (response) => {
|
||||||
}
|
this.isLoadingNPK1 = false;
|
||||||
});
|
if (response.statusCode === 200 && response.data.npk1?.length > 0) {
|
||||||
|
this.createChart(this.npk1ChartElement.nativeElement, response, 'npk1', this.selectedNPK1);
|
||||||
|
this.isNoDataNPK1 = false;
|
||||||
|
} else {
|
||||||
|
this.isNoDataNPK1 = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: () => {
|
||||||
|
this.isLoadingNPK1 = false;
|
||||||
|
this.isNoDataNPK1 = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchNPK2Data(savedTimeRange: string): void {
|
fetchNPK2Data(savedTimeRange: string): void {
|
||||||
const startEnd = this.getDate();
|
const hEnd = this.getDate();
|
||||||
this.sensorService.getSensorData('npk2', this.selectedNPK2, startEnd, savedTimeRange).subscribe({
|
const hStart = this.getDate();
|
||||||
next: (response) => {
|
const dEnd = this.getDate();
|
||||||
console.log(savedTimeRange);
|
const dStart = this.getDateAgo();
|
||||||
this.isLoadingNPK2 = false;
|
|
||||||
if (response.statusCode === 200 && response.data.npk2?.length > 0) {
|
if(savedTimeRange === 'HOURLY'){
|
||||||
this.createChart(this.npk2ChartElement.nativeElement, response, 'npk2', this.selectedNPK2);
|
this.sensorService.getSensorDataHourly('npk2', this.selectedNPK2, hStart, hEnd, savedTimeRange).subscribe({
|
||||||
this.isNoDataNPK2 = false;
|
next: (response) => {
|
||||||
} else {
|
this.isLoadingNPK2 = false;
|
||||||
|
if (response.statusCode === 200 && response.data.npk2?.length > 0) {
|
||||||
|
this.createChart(this.npk2ChartElement.nativeElement, response, 'npk2', this.selectedNPK2);
|
||||||
|
this.isNoDataNPK2 = false;
|
||||||
|
} else {
|
||||||
|
this.isNoDataNPK2 = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: () => {
|
||||||
|
this.isLoadingNPK2 = false;
|
||||||
this.isNoDataNPK2 = true;
|
this.isNoDataNPK2 = true;
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
error: () => {
|
} else if(savedTimeRange === 'DAILY'){
|
||||||
this.isLoadingNPK2 = false;
|
this.sensorService.getSensorDataDaily('npk2', this.selectedNPK2, dStart, dEnd, savedTimeRange).subscribe({
|
||||||
this.isNoDataNPK2 = true;
|
next: (response) => {
|
||||||
}
|
this.isLoadingNPK2 = false;
|
||||||
});
|
if (response.statusCode === 200 && response.data.npk2?.length > 0) {
|
||||||
|
this.createChart(this.npk2ChartElement.nativeElement, response, 'npk2', this.selectedNPK2);
|
||||||
|
this.isNoDataNPK2 = false;
|
||||||
|
} else {
|
||||||
|
this.isNoDataNPK2 = true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: () => {
|
||||||
|
this.isLoadingNPK2 = false;
|
||||||
|
this.isNoDataNPK2 = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCharts(): void {
|
updateCharts(): void {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user