Skip to content

Commit

Permalink
Merge pull request #63 from ilfreedom/one-field-one-crop
Browse files Browse the repository at this point in the history
add crop facts to field crop & remove my crops & list by field
  • Loading branch information
ilfreedom authored Apr 24, 2024
2 parents 19d67f2 + 8a3c4dc commit fb7bff5
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { FieldDataService } from 'src/app/service/FieldDataService';
import { Guid } from 'guid-typescript';
import { Field } from 'src/app/models/Field';
import { MatDialog } from '@angular/material/dialog';
import { SENSORS_MOCK_DATA } from './../../sensors/sensor-data';
import { SensorListComponent } from './../sensor-list/sensor-list.component';
import { SENSORS_MOCK_DATA } from '../../sensors/sensor-data';
import { SensorListComponent } from '../sensor-list/sensor-list.component';
import { CropDataService } from 'src/app/service/CropDataService';
import { forkJoin, from } from 'rxjs';
import {CropInfoResp} from "../../../../models/api/CropInfoResp";

@Component({
selector: 'app-add-field',
Expand Down Expand Up @@ -130,6 +131,8 @@ export class AddFieldComponent implements OnInit {
}
const sensorList = this.sensors;
const id = Guid.create().toString();
this.cropValue.seedingDate = new Date(formattedDate);
this.cropValue.waterDate = new Date(formattedDate);//Assume that each time a crop is planted, it will be watered.
const params: Field = {
id,
fieldName: name,
Expand All @@ -144,13 +147,24 @@ export class AddFieldComponent implements OnInit {

openDialog(dialogTemplate: TemplateRef<any>): void {
this.dialog.open(dialogTemplate, {
height: '300px',
height: '430px',
width: '400px',
});
}

clickCropNext() {
this.cropValue = this.fieldForm.get('cropSelect').value;
this.cropService.getCropInfo(this.cropValue.id).subscribe(
(resp: CropInfoResp)=>{
this.cropValue.id = resp.data.docs[0]._id;
this.cropValue.cropName = resp.data.docs[0].cropName;
this.cropValue.facts = resp.data.docs[0];
},
(error) =>{
alert('clickCropNext Could not get crop info: ' + error);
console.error('clickCropNext Error getting CropInfo:', error);
}
);
this.fieldForm.patchValue({
crop: this.fieldForm.get('cropSelect').value.cropName,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { MatSnackBar } from '@angular/material/snack-bar';
import { SensorListComponent } from './../sensor-list/sensor-list.component';
import { CropDataService } from 'src/app/service/CropDataService';
import { forkJoin, from } from 'rxjs';
import {CropInfoResp} from "../../../../models/api/CropInfoResp";

@Component({
selector: 'app-edit-field',
Expand Down Expand Up @@ -113,12 +114,23 @@ export class EditFieldComponent implements OnInit {

openCropDialog(dialogTemplate: TemplateRef<any>): void {
this.dialog.open(dialogTemplate, {
height: '300px',
height: '430px',
width: '400px',
});
}

clickCropNext() {
this.cropService.getCropInfo(this.cropValue.id).subscribe(
(resp: CropInfoResp)=>{
this.cropValue.id = resp.data.docs[0]._id;
this.cropValue.cropName = resp.data.docs[0].cropName;
this.cropValue.facts = resp.data.docs[0];
},
(error) =>{
alert('clickCropNext Could not get crop info: ' + error);
console.error('clickCropNext Error getting CropInfo:', error);
}
);
this.fieldForm.patchValue({
crop: this.fieldForm.get('cropSelect').value.cropName,
});
Expand Down Expand Up @@ -183,6 +195,7 @@ export class EditFieldComponent implements OnInit {
formattedDate = formatDate(plantDateValue, 'yyyy-MM-dd', 'en-US');
const sensorList = this.sensors;
const id = this.id;
this.cropValue.seedingDate = new Date(formattedDate);
const params: Field = {
id,
fieldName: name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@
</div>
</ng-template>
<mat-list>
<ng-container *ngFor="let crop of needsWateringCrops">
<ng-container *ngFor="let field of needsWateringFields">
<mat-list-item>
<div fxLayout="row" fxFlexFill fxLayoutAlign="space-between center" class="item-container">
<div fxFlex="nogrow">
<img src="{{ crop.url }}" alt="{{ crop.cropName }}" class="square-image">
<img src="{{ field.crop.url }}" alt="{{ field.crop.cropName }}" class="square-image">
</div>
<div fxFlex class="item-content">
<h3 matLine> Northwest Field 3 </h3>
<h3 matLine> {{field.fieldName}} </h3>
<p matLine>
<span> {{crop.cropName}} </span>
<span> {{field.crop.cropName}} </span>
</p>
</div>
<div class="item-actions" fxFlex="none">
<button mat-mini-fab color="primary" (click)="onWaterClick(crop)">
<button mat-mini-fab color="primary" (click)="onWaterClick(field)">
<mat-icon class="material-symbols-outlined">sprinkler</mat-icon>
</button>
</div>
Expand All @@ -64,16 +64,16 @@ <h3 matLine> Northwest Field 3 </h3>
</div>
</ng-template>
<mat-list>
<ng-container *ngFor="let crop of recentlyWateredCrops">
<ng-container *ngFor="let field of recentlyWateredFields">
<mat-list-item>
<div fxLayout="row" fxFlexFill fxLayoutAlign="space-between center" class="item-container">
<div fxFlex="nogrow">
<img src="{{ crop.url }}" alt="{{ crop.cropName }}" class="square-image">
<img src="{{ field.crop.url }}" alt="{{ field.crop.cropName }}" class="square-image">
</div>
<div fxFlex class="item-content">
<h3 matLine> Northwest Field 3 </h3>
<h3 matLine> {{field.fieldName}} </h3>
<p matLine>
<span> {{crop.cropName}} </span>
<span> {{ field.crop.cropName}} </span>
</p>
</div>
</div>
Expand Down
78 changes: 39 additions & 39 deletions liquid-prep-app/src/app/components/dashboard/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import { TodayWeather } from '../../../models/TodayWeather';
import { formatDate } from '@angular/common';
import { WeatherDataService } from '../../../service/WeatherDataService';
import {Crop} from '../../../models/Crop';
import {CropDataService} from '../../../service/CropDataService';
import {MAT_DIALOG_DATA, MatDialog} from '@angular/material/dialog';
import {WaterConfirmDialogComponent} from './water-confirm-dialog.component';
import {DateTimeUtil} from '../../../utility/DateTimeUtil';
import {WaterAdviceService} from '../../../service/WaterAdviceService';
import {Field} from "../../../models/Field";
import {FieldDataService} from "../../../service/FieldDataService";

const RECENTLY = 48;

Expand Down Expand Up @@ -44,59 +45,57 @@ export class HomeComponent implements OnInit {
public nextDayTemperatureMin: number;
public currentDate = '';
public location: string;
public myCrops: Crop[];
public needsWateringCrops: Crop[];
public recentlyWateredCrops: Crop[];
public nonRecentlyWateredCrops: Crop[];
public myCropStatus: 'no-crop' | 'crop-selected' = 'no-crop';
public myFields: Field[]
public needsWateringFields: Field[];
public recentlyWateredFields: Field[];
public nonRecentlyWateredFields: Field[];

constructor(
private waterAdviceService: WaterAdviceService,
private weatherService: WeatherDataService,
private headerService: HeaderService,
private geoLocationService: GeoLocationService,
private cropDataService: CropDataService,
private fieldDataService: FieldDataService,
private dialog: MatDialog,
@Inject(MAT_DIALOG_DATA) public data: { crop: Crop }
) {}
ngOnInit(): void {
this.headerService.updateHeader(this.headerConfig);
this.updateWeatherInfo();
this.getLocation();
this.cropDataService.getLocalStorageMyCrops().then(
(myCrops) => {
this.myCrops = myCrops;
if (this.myCrops.length > 0){
this.myCropStatus = 'crop-selected';
this.FilterWateredCrops(this.myCrops);
this.FilterNeedsWaterCrop(this.nonRecentlyWateredCrops);
}
}
);
this.fieldDataService.getLocalStorageMyFields()
.then((fields: Field[]) => {
this.myFields = fields;
this.FilterWateredFields(this.myFields);
this.FilterNeedsWaterField(this.nonRecentlyWateredFields)
})
.catch((error) => {
console.error('Error loading fields:', error);
});
}

private FilterWateredCrops(myCrops: Crop[]){
this.recentlyWateredCrops = [];
this.nonRecentlyWateredCrops = [];
private FilterWateredFields(MyFields: Field[]){
this.recentlyWateredFields = [];
this.nonRecentlyWateredFields=[];
const now = new Date();
const recentlyAgo = new Date(now.getTime() - RECENTLY * 60 * 60 * 1000);
myCrops.forEach((cropItem) => {
const waterDate = new Date(cropItem.waterDate);
MyFields.forEach((item:Field)=>{
const waterDate = new Date(item.crop.waterDate);
if (waterDate >= recentlyAgo && waterDate <= now) {
this.recentlyWateredCrops.push(cropItem);
this.recentlyWateredFields.push(item);
} else {
this.nonRecentlyWateredCrops.push(cropItem);
this.nonRecentlyWateredFields.push(item);
}
});
})
}

private FilterNeedsWaterCrop(crops: Crop[]){
this.needsWateringCrops = [];
this.nonRecentlyWateredCrops.forEach((item) => {
this.waterAdviceService.getWaterAdviceByCrop(item).subscribe(advice => {
console.log('water advice:', item.cropName, advice.wateringDecision, advice.waterRecommended);
private FilterNeedsWaterField(fields: Field[]){
this.needsWateringFields = [];
this.nonRecentlyWateredFields.forEach((item:Field)=>{
this.waterAdviceService.getWaterAdviceByCrop(item.crop).subscribe(advice => {
console.log('water advice:', item.crop.cropName, advice.wateringDecision, advice.waterRecommended);
if (advice.wateringDecision !== 'None'){
this.needsWateringCrops.push(item);
this.needsWateringFields.push(item);
}
});
});
Expand Down Expand Up @@ -157,25 +156,26 @@ export class HomeComponent implements OnInit {
);
}

onWaterClick(crop: Crop) {
onWaterClick(field: Field) {
const dialogRef = this.dialog.open(WaterConfirmDialogComponent, {
width: '500px',
panelClass: ['form-dialog'],
data: { crop },
data: { field },
});

dialogRef.afterClosed().subscribe(result => {
if (result === 'confirm') {
crop.waterDate = new DateTimeUtil().getTodayDate();
this.cropDataService.storeMyCropsInLocalStorage(crop).then(
field.crop.waterDate = new DateTimeUtil().getTodayDate();
this.fieldDataService.storeFieldsInLocalStorage(field).then(
(r) => {
console.log(`water ${crop.cropName} onConfirm water date ${crop.waterDate}`);
this.needsWateringCrops = this.needsWateringCrops.filter(item => item !== crop);
this.recentlyWateredCrops.push(crop);
console.log(`water ${field.crop.cropName} onConfirm water date ${field.crop.waterDate}`);
this.needsWateringFields = this.needsWateringFields.filter(item => item !== field);
this.recentlyWateredFields.push(field);
},
(e) => { console.error('save water data fail:', crop.cropName, e); }
(e) => { console.error('save water data fail:', field.fieldName,field.crop.cropName, e); }
);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {Component, Inject, OnInit} from '@angular/core';
import {Crop} from '../../../models/Crop';
import {MAT_DIALOG_DATA} from '@angular/material/dialog';
import {Field} from "../../../models/Field";

@Component({
selector: 'app-water-confirm-dialog',
template: `
<h1>Confirm Irrigation Status</h1>
<p>
Would you like to mark {{ data.crop.cropName }} as watered?
Would you like to mark {{ data.field.crop.cropName }} as watered?
<br />
<br />
The updated task will then appear in the "Recently Watered" list
Expand All @@ -29,7 +29,7 @@ import {MAT_DIALOG_DATA} from '@angular/material/dialog';
})
export class WaterConfirmDialogComponent implements OnInit {

constructor(@Inject(MAT_DIALOG_DATA) public data: { crop: Crop }) { }
constructor(@Inject(MAT_DIALOG_DATA) public data: { field: Field }) { }

ngOnInit(): void {
}
Expand Down
20 changes: 0 additions & 20 deletions liquid-prep-app/src/app/models/Crop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export class Crop {
id: string;
cropName: string;
type: string;
cropGrowthStage: CropGrowthStage;
url: string; // crop image mapping url
facts: CropFacts;
seedingDate: Date;
Expand All @@ -18,22 +17,3 @@ export class Measure{
measureValue: number;
}

export class CropGrowthStage {
numberOfStages: number;
waterMeasurementMetric: string; // cm
waterUsage: string; // daily
growthStageLengthMeasure: string;
totalGrowthStageLength: number;
rootDepthMetric: string;
stages: Stage[];
}

export class Stage {
stageNumber: number;
stage: string;
waterUse: number;
stageLength: number;
rootDepth: number;
url: string;
age: number; // the days since the crop was planted
}
2 changes: 1 addition & 1 deletion liquid-prep-app/src/app/models/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class Field {
id: string;
fieldName: string;
description?: string;
crop: Crop; //Get Crop Data
crop: Crop; //Get Crop Data, one field-one crop
plantDate: Date;
sensors?: any; // Get Sensor Data
}
Loading

0 comments on commit fb7bff5

Please sign in to comment.