homeweatherapi/src/app.service.ts

199 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-09-17 06:54:01 +00:00
import {
Injectable,
InternalServerErrorException,
Logger,
OnApplicationShutdown,
} from '@nestjs/common';
2023-09-16 19:45:26 +00:00
import WS1080 from './module/ws1080';
2023-09-19 17:06:34 +00:00
import { Between, LessThan, MoreThan, Repository } from 'typeorm';
2023-09-17 06:54:01 +00:00
import { WeatherEntity } from './entities/weather.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Cron } from '@nestjs/schedule';
2023-09-17 07:05:52 +00:00
import { HistoryQueryDto } from './dtos/history-query.dto';
2023-09-19 17:06:34 +00:00
import { GraphQueryDto } from './dtos/graph-query.dto';
2023-09-16 19:45:26 +00:00
@Injectable()
export class AppService implements OnApplicationShutdown {
2023-09-17 06:54:01 +00:00
private logger = new Logger(AppService.name);
2023-09-16 19:45:26 +00:00
public station?: WS1080;
2023-09-17 06:54:01 +00:00
constructor(
@InjectRepository(WeatherEntity)
private readonly weatherRepository: Repository<WeatherEntity>,
) {}
2023-09-19 17:06:34 +00:00
/**
* Get current weather information.
* @returns Weather data
*/
2023-09-16 19:45:26 +00:00
async getWeather() {
2023-09-17 06:54:01 +00:00
try {
// Retrieve from USB
2023-09-19 17:06:34 +00:00
await this.createStation();
2023-09-17 06:54:01 +00:00
const data = await this.station.read();
// Save weather data to database
const entity = this.weatherRepository.create({
date: new Date(),
...data,
});
await this.weatherRepository.save(entity);
entity.fresh = true;
2023-09-17 17:49:17 +00:00
entity.rain24h = await this.rainFall24h();
2023-09-17 06:54:01 +00:00
return entity;
} catch (error) {
// USB errors likely mean we are not connected anymore
if (error.message?.includes('USB')) {
try {
this.station?.close();
} catch {}
this.station = null;
}
this.logger.error('Failed to retrieve weather data:', error.stack);
// Retrieve previous entry on error
2023-09-19 17:06:34 +00:00
const previous = await this.getPreviousEntry();
2023-09-17 06:54:01 +00:00
if (!previous) throw new InternalServerErrorException();
return previous;
}
}
2023-09-19 17:06:34 +00:00
/**
* Get previous weather entry.
* @returns Previous weather data entry
*/
async getPreviousEntry() {
const [previous] = await this.weatherRepository.find({
order: { date: -1 },
take: 1,
});
if (!previous) return null;
previous.rain24h = await this.rainFall24h(previous.date);
previous.fresh = false;
return previous;
}
/**
* Get weather history.
* @param query Weather history search
* @returns Paginated history list
*/
async getWeatherHistory(
query: HistoryQueryDto,
select?: (keyof WeatherEntity)[],
) {
2023-09-17 07:05:52 +00:00
const pageSize = Number(query.pageSize) || 100;
const page = Number(query.page) || 1;
const [list, rowCount] = await this.weatherRepository.findAndCount({
2023-09-19 17:06:34 +00:00
select,
where:
query.since || query.until
? {
date:
query.since && query.until
? Between(new Date(query.since), new Date(query.until))
: query.since
? MoreThan(new Date(query.since))
: LessThan(new Date(query.until)),
}
: undefined,
2023-09-17 07:05:52 +00:00
order: { date: -1 },
take: pageSize,
skip: (page - 1) * pageSize,
});
const pageCount = Math.ceil(rowCount / pageSize);
return {
list,
pagination: {
page,
pageSize,
pageCount,
rowCount,
},
};
}
2023-09-19 17:06:34 +00:00
/**
* Get graph datasets for weather.
* @param query Weather history search
* @returns Paginated graph dataset
*/
async graphWeatherHistory(query: GraphQueryDto) {
const { list, pagination } = await this.getWeatherHistory(query, [
...(query.columns as (keyof WeatherEntity)[]),
'date',
]);
const datasets = query.columns.reduce(
(mass, key: keyof WeatherEntity) => ({
...mass,
[key]: { label: String(key), data: [] },
}),
{},
);
list.forEach((entry) => {
Object.keys(entry)
.filter((key) => !['id', 'date'].includes(key))
.forEach((key) => {
datasets[key].data.push({
x: entry.date.getTime(),
y: entry[key],
});
});
});
return {
list: Object.values(datasets),
pagination,
};
}
2023-09-17 17:49:17 +00:00
/**
* Get rainfall in the last 24h since `since` start point.
* @param since Time start point
* @returns Rainfall in mm
*/
async rainFall24h(since = new Date()) {
const { rainfall } = await this.weatherRepository
.createQueryBuilder('weather')
.select('SUM(rainDiff)', 'rainfall')
.where('date >= :date', {
date: new Date(since.getTime() - 24 * 60 * 60 * 1000),
})
.getRawOne();
return Number(rainfall) || 0;
}
2023-09-19 17:06:34 +00:00
/**
* Create station instance.
*/
async createStation() {
if (this.station) return;
this.station = WS1080.fromDevice();
const previous = await this.getPreviousEntry();
if (previous) {
this.station.previousRain = previous.totalRain;
}
}
2023-09-17 06:54:01 +00:00
@Cron('0 * * * *')
scheduledPulls() {
this.getWeather().catch(() => {
// do nothing
});
2023-09-16 19:45:26 +00:00
}
onApplicationShutdown() {
this.station?.close();
}
}