diff --git a/backend/api/services/ApiBase.ts b/backend/api/services/ApiBase.ts index aa69188..3d8fe56 100644 --- a/backend/api/services/ApiBase.ts +++ b/backend/api/services/ApiBase.ts @@ -1,5 +1,5 @@ import axios from 'axios'; -import type { AxiosResponse } from 'axios'; +import { AxiosError, AxiosResponse } from 'axios'; import xml2js from 'xml2js'; import errorLog from '../../functions/errorLog'; @@ -19,7 +19,7 @@ export default class ApiService { } } - protected static async get(baseURL: string, endpoint: string): Promise { + protected static async get(baseURL: string, endpoint: string): Promise { try { //console.info(`http://${baseURL}/${endpoint}`) const response = await this.instance.get(endpoint, { @@ -27,13 +27,17 @@ export default class ApiService { }); this.handleResponse(response, `get:${endpoint}`); return response.data; - } catch (error) { - console.error(`API Error on get:${endpoint}`, error); - throw new Error(`API Error on get:${endpoint}`); + } catch (error: unknown) { + errorLog( + `API Error on get:${endpoint} | ERR: ${ + error instanceof AxiosError ? error.message : 'Unexpected error type' + }` + ); + return null; } } - protected static async getXML2JSON(baseURL: string, endpoint: string): Promise { + protected static async getXML2JSON(baseURL: string, endpoint: string): Promise { try { const response: AxiosResponse = await this.instance.get(endpoint, { baseURL: `http://${baseURL}/`, @@ -50,7 +54,8 @@ export default class ApiService { } } catch (error) { errorLog(`API Error on getXML2JSON: ${endpoint} ${JSON.stringify(error, null, 4)}`); - throw new Error(`API Error on getXML2JSON: ${endpoint}`); + return null; + //throw new Error(`API Error on getXML2JSON: ${endpoint}`); } } diff --git a/backend/api/services/BatteryService.ts b/backend/api/services/BatteryService.ts index 9b79e49..ce15c48 100644 --- a/backend/api/services/BatteryService.ts +++ b/backend/api/services/BatteryService.ts @@ -3,7 +3,7 @@ import ConfigFile from '../../classes/ConfigFile.js'; import InterfaceBatData from '../../models/InterfaceBatData.js'; export default class extends ApiBase { - static async getEMSDATA(): Promise { + static async getEMSDATA(): Promise { return this.getXML2JSON(ConfigFile.read().BatteryHost, 'cgi/ems_data.xml'); } } diff --git a/backend/api/services/ChargerService.ts b/backend/api/services/ChargerService.ts index 0135f37..89f11a4 100644 --- a/backend/api/services/ChargerService.ts +++ b/backend/api/services/ChargerService.ts @@ -2,22 +2,22 @@ import ApiBase from './ApiBase.js'; import ConfigFile from '../../classes/ConfigFile.js'; import InterfaceChaStatus from '../../models/InterfaceChaStatus.js'; export default class extends ApiBase { - static async getChargeInfo(): Promise { + static async getChargeInfo(): Promise { return this.get( ConfigFile.read().ChargerHost, `api/status?filter=rssi,amp,ama,car,pha,wh,nrg,pnp,pwm,fsp,lccfi,psm` ); } - static async setChargeStart(): Promise { + static async setChargeStart(): Promise { return this.get(ConfigFile.read().ChargerHost, `api/set?frc=0`); } - static async setChargeStop(): Promise { + static async setChargeStop(): Promise { return this.get(ConfigFile.read().ChargerHost, `api/set?frc=1`); } - static async setChargeAmp(amp: number): Promise { + static async setChargeAmp(amp: number): Promise { return this.get(ConfigFile.read().ChargerHost, `api/set?amp=${amp}`); } - static async setChargePhase(phase: number): Promise { + static async setChargePhase(phase: number): Promise { return this.get(ConfigFile.read().ChargerHost, `api/set?psm=${phase}`); } } diff --git a/backend/api/services/InverterService.ts b/backend/api/services/InverterService.ts index 7b043d5..6a739fa 100644 --- a/backend/api/services/InverterService.ts +++ b/backend/api/services/InverterService.ts @@ -2,13 +2,13 @@ import ApiBase from './ApiBase.js'; import InterfaceInvPowerFlowRealtimeData from '../../models/InterfaceInvPowerFlowRealtimeData.js'; import InterfaceInvMeterRealtimeData from '../../models/InterfaceInvMeterRealtimeData.js'; export default class extends ApiBase { - static async getPowerFlowRealtimeData(host: string): Promise { + static async getPowerFlowRealtimeData(host: string): Promise { return this.get( host, 'solar_api/v1/GetPowerFlowRealtimeData.fcgi' ); } - static async getMeterRealtimeData(host: string): Promise { + static async getMeterRealtimeData(host: string): Promise { return this.get( host, 'solar_api/v1/GetMeterRealtimeData.cgi?Scope=Device&DeviceId=0&DataCollection=CumulationInvert' diff --git a/backend/loop.ts b/backend/loop.ts index acafda3..e88ebc7 100644 --- a/backend/loop.ts +++ b/backend/loop.ts @@ -118,7 +118,7 @@ export default async function (): Promise { if (chargerData.psm !== LiveData.data.Charger.PhaseModeCalc) { infoLog('Phase was corrected'); const response = await ChargerService.setChargePhase(LiveData.data.Charger.PhaseModeCalc); - if (response.psm === LiveData.data.Charger.PhaseModeCalc) { + if (response && response.psm === LiveData.data.Charger.PhaseModeCalc) { LiveData.data.Charger.PhaseMode = LiveData.data.Charger.PhaseModeCalc; } } @@ -126,7 +126,7 @@ export default async function (): Promise { if (chargerData.amp !== LiveData.data.Charger.AmpCalc) { infoLog('Amp was corrected'); const response = await ChargerService.setChargeAmp(LiveData.data.Charger.AmpCalc); - if (response.amp === true) { + if (response && response.amp === true) { LiveData.data.Charger.Amp = LiveData.data.Charger.AmpCalc; } } diff --git a/backend/routes/chargeRoutes.ts b/backend/routes/chargeRoutes.ts index 37ffaf0..343d5fb 100644 --- a/backend/routes/chargeRoutes.ts +++ b/backend/routes/chargeRoutes.ts @@ -27,7 +27,7 @@ const r = express.Router(); r.post('/charge-start', (_req, res) => { ChargerService.setChargeStart() .then((x) => { - if (x.frc === true) { + if (x && x.frc === true) { res.status(200).json({ msg: 'success' }); } else { throw new AxiosError(); @@ -61,7 +61,7 @@ r.post('/charge-start', (_req, res) => { r.post('/charge-stop', (_req, res) => { ChargerService.setChargeStop() .then((x) => { - if (x.frc === true) { + if (x && x.frc === true) { res.status(200).json({ msg: 'success' }); } else { throw new AxiosError();