import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { createAgrarmonitorClient, FileCookieStore, AesGcmCookieEncryptor, type AgrarmonitorConnectorResult, } from 'agrarmonitor-connector'; export interface AgrarmonitorStatusDto { connected: boolean; registriert: boolean | null; freigeschaltet: boolean | null; error?: string; } export interface AgrarmonitorRegisterResultDto { success: boolean; message: string; } @Injectable() export class AgrarmonitorService { private readonly logger = new Logger(AgrarmonitorService.name); private client: AgrarmonitorConnectorResult | null = null; constructor(private readonly configService: ConfigService) {} async getClient(): Promise { if (this.client) return this.client; const username = this.configService.get( 'AGRARMONITOR_USERNAME', '', ); const password = this.configService.get( 'AGRARMONITOR_PASSWORD', '', ); const baseUrl = this.configService.get( 'AGRARMONITOR_BASE_URL', 'https://admin7.agrarmonitor.de', ); const apiBaseUrl = this.configService.get( 'AGRARMONITOR_API_BASE_URL', 'https://api.agrarmonitor.de', ); const apiToken = this.configService.get('AGRARMONITOR_API_TOKEN'); const cookiePath = this.configService.get( 'AGRARMONITOR_COOKIE_PATH', './data/agrarmonitor-cookies.json', ); const encryptionKey = this.configService.get( 'AGRARMONITOR_ENCRYPTION_KEY', ); const encryptor = encryptionKey ? new AesGcmCookieEncryptor(encryptionKey) : undefined; const cookieStore = new FileCookieStore(cookiePath, { encryptor, logger: this.logger, }); this.client = await createAgrarmonitorClient({ baseUrl, apiBaseUrl, apiToken, username, password, cookieStore, autoLogin: true, autoRetry: false, timeoutMs: 10000, loginStrategy: 'redirect', logger: this.logger, }); return this.client; } clearClient(): void { this.client = null; } async getStatus(): Promise { try { const client = await this.getClient(); const [registrierungStatus, freigeschaltetStatus] = await Promise.all([ client.checkRegistriert(), client.checkFreigeschaltet(), ]); return { connected: true, registriert: registrierungStatus.registriert, freigeschaltet: freigeschaltetStatus.freigeschaltet, }; } catch (err: any) { this.client = null; return { connected: false, registriert: null, freigeschaltet: null, error: err?.message ?? 'Verbindung fehlgeschlagen', }; } } async registerDevice( pcName: string, agrarmonitorId: string, ): Promise { const client = await this.getClient(); const result = await client.registerDevice({ agrarmonitorId, pcName }); return { success: result.success, message: result.message }; } }