homeweatherapi/src/app.service.ts

74 lines
1.8 KiB
TypeScript

import {
Injectable,
InternalServerErrorException,
Logger,
OnApplicationShutdown,
} from '@nestjs/common';
import WS1080 from './module/ws1080';
import { Repository } from 'typeorm';
import { WeatherEntity } from './entities/weather.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Cron } from '@nestjs/schedule';
@Injectable()
export class AppService implements OnApplicationShutdown {
private logger = new Logger(AppService.name);
public station?: WS1080;
constructor(
@InjectRepository(WeatherEntity)
private readonly weatherRepository: Repository<WeatherEntity>,
) {}
async getWeather() {
try {
// Retrieve from USB
if (!this.station) this.station = WS1080.fromDevice();
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;
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
const [previous] = await this.weatherRepository.find({
order: { date: -1 },
take: 1,
});
if (!previous) throw new InternalServerErrorException();
previous.fresh = false;
return previous;
}
}
@Cron('0 * * * *')
scheduledPulls() {
this.getWeather().catch(() => {
// do nothing
});
}
onApplicationShutdown() {
this.station?.close();
}
}