Skip to content

Commit

Permalink
Update airqualitysensor.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
donavanbecker committed Sep 23, 2024
1 parent 2f52e6d commit cfa6c95
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/devices/airqualitysensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,30 @@ export class AirQualitySensor extends deviceBase {
}
const url = providerUrls[this.device.provider]
if (url) {
const { body, statusCode } = await request(url)
const response = await body.json()
await this.debugWarnLog(`statusCode: ${JSON.stringify(statusCode)}`)
await this.debugLog(`response: ${JSON.stringify(response)}`)

if (statusCode !== 200) {
this.errorLog(`${this.device.provider === 'airnow' ? 'AirNow' : 'World Air Quality Index'} air quality Network or Unknown Error from %s.`, this.device.provider)
this.AirQualitySensor.StatusFault = this.hap.Characteristic.StatusFault.GENERAL_FAULT
await this.debugLog(`Error: ${JSON.stringify(response)}`)
await this.apiError(response)
} else {
this.deviceStatus = this.device.provider === 'aqicn' ? (response as AqicnData).data : response as AirNowAirQualityDataArray
await this.parseStatus()
try {
const { body, statusCode } = await request(url)
let response: any
try {
response = await body.json()
} catch (jsonError: any) {
this.errorLog(`Failed to parse JSON response: ${jsonError.message}`)
this.errorLog(`Raw response body: ${await body.text()}`)
throw jsonError
}
await this.debugWarnLog(`statusCode: ${JSON.stringify(statusCode)}`)
await this.debugLog(`response: ${JSON.stringify(response)}`)

if (statusCode !== 200) {
this.errorLog(`${this.device.provider === 'airnow' ? 'AirNow' : 'World Air Quality Index'} air quality Network or Unknown Error from %s.`, this.device.provider)
this.AirQualitySensor.StatusFault = this.hap.Characteristic.StatusFault.GENERAL_FAULT
await this.debugLog(`Error: ${JSON.stringify(response)}`)
await this.apiError(response)
} else {
this.deviceStatus = this.device.provider === 'aqicn' ? (response as AqicnData).data : response as AirNowAirQualityDataArray
await this.parseStatus()
}
} catch (error: any) {
this.errorLog(`Request failed: ${error.message}`)
}
} else {
await this.errorLog('Unknown air quality provider: %s.', this.device.provider)
Expand Down

0 comments on commit cfa6c95

Please sign in to comment.