Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace axios with fetch, add option to enable debug logging #68

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '18.x'
node-version: '22.x'
- name: Install dependencies
run: npm ci
- name: Check formatting
Expand All @@ -30,7 +30,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '18.x'
node-version: '22.x'
- name: Install dependencies
run: npm ci
- name: Run eslint
Expand All @@ -43,7 +43,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '18.x'
node-version: '22.x'
- name: Install dependencies
run: npm ci
- name: Run tests
Expand All @@ -59,7 +59,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '18.x'
node-version: '22.x'
- name: Install dependencies
run: npm ci
- name: Check formatting
Expand All @@ -75,7 +75,7 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: '18.x'
node-version: '22.x'
- name: Install dependencies
run: npm ci
- name: Run eslint
Expand Down
111 changes: 15 additions & 96 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
"test": "jest"
},
"engines": {
"node": ">= 18"
"node": ">= 22"
},
"devDependencies": {
"@tsconfig/node18": "^18.2.2",
"@types/jest": "^29.5.6",
"@types/node": "^20.5.9",
"@types/node": "^22.1.0",
"@types/ws": "^8.5.5",
"@types/yargs": "^17.0.24",
"@typescript-eslint/eslint-plugin": "^6.7.2",
Expand All @@ -32,7 +32,6 @@
},
"dependencies": {
"@influxdata/influxdb-client": "^1.33.2",
"axios": "^1.6.0",
"modbus-serial": "^8.0.16",
"mqtt": "^5.1.2",
"slugify": "^1.6.6",
Expand Down
10 changes: 9 additions & 1 deletion src/eachwatt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { httpRequestHandler } from './http/server'
import { WebSocketPublisherImpl } from './publisher/websocket'
import { PublisherType } from './publisher'
import { pollCharacteristicsSensors } from './characteristics'
import { createLogger } from './logger'
import { createLogger, LogLevel, setLogLevel } from './logger'
import { setRequestTimeout as setHttpRequestTimeout } from './http/client'
import { setRequestTimeout as setModbusRequestTimeout } from './modbus/client'
import { applyFilters } from './filter/filter'
Expand All @@ -26,10 +26,18 @@ const argv = yargs(process.argv.slice(2))
demandOption: true,
alias: 'c',
},
'verbose': {
description: 'Enable verbose logging',
alias: 'v',
},
})
.parseSync()

const logger = createLogger('main')
if (argv.verbose) {
logger.info('Setting log level to DEBUG')
setLogLevel(LogLevel.DEBUG)
}

const mainPollerFunc = async (config: Config) => {
const now = Date.now()
Expand Down
25 changes: 15 additions & 10 deletions src/http/client.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import axios, { AxiosResponse } from 'axios'
import http from 'http'
import { createLogger } from '../logger'

const logger = createLogger('http')

const httpClient = axios.create({
// We keep polling the same hosts over and over so keep-alive is essential
httpAgent: new http.Agent({ keepAlive: true }),
})

let requestTimeout = 0
let lastTimestamp = 0
const promiseCache = new Map()

const createRequestParams = (): RequestInit => {
return {
// We keep polling the same hosts over and over so keep-alive is essential
keepalive: true,
// Use the configured timeout
signal: AbortSignal.timeout(requestTimeout),
}
}

export const setRequestTimeout = (timeoutMs: number) => {
httpClient.defaults.timeout = timeoutMs
requestTimeout = timeoutMs
logger.info(`Using ${timeoutMs} millisecond timeout for HTTP requests`)
}

export const getDedupedResponse = async (timestamp: number, url: string): Promise<AxiosResponse> => {
export const getDedupedResponse = async (timestamp: number, url: string): Promise<Response> => {
// Clear the cache whenever the timestamp changes
if (timestamp !== lastTimestamp) {
lastTimestamp = timestamp
Expand All @@ -30,7 +33,9 @@ export const getDedupedResponse = async (timestamp: number, url: string): Promis
return promiseCache.get(key)
}

const promise = httpClient.get(url)
const request = new Request(url, createRequestParams())
logger.debug(`GET ${url}`)
const promise = fetch(request)
promiseCache.set(key, promise)

return promise
Expand Down
14 changes: 8 additions & 6 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import winston, { Logger } from 'winston'

const DEFAULT_LOG_LEVEL = 'info'
export enum LogLevel {
INFO = 'info',
DEBUG = 'debug',
}

// Define log transports here, so we can change the log level later
const transports = [new winston.transports.Console()]
Expand All @@ -9,14 +12,13 @@ const logFormat = winston.format.printf(({ level, message, label, timestamp }) =
return `${timestamp} [${label}] ${level}: ${message}`
})

// export const setLogLevel = (logger: Logger, level: string) => {
// logger.info(`Setting log level to ${level}`)
// transports[0].level = level
// }
export const setLogLevel = (level: LogLevel) => {
transports[0].level = level
}

export const createLogger = (module: string): Logger => {
return winston.createLogger({
'level': DEFAULT_LOG_LEVEL,
'level': LogLevel.INFO,
'format': winston.format.combine(winston.format.label({ label: module }), winston.format.timestamp(), logFormat),
'transports': transports,
})
Expand Down
12 changes: 6 additions & 6 deletions src/sensor/iotawatt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ export const getSensorData: PowerSensorPollFunction = async (
const sensor = circuit.sensor as IotawattSensor

try {
const configurationResult = await getDedupedResponse(timestamp, getConfigurationUrl(sensor))
const configuration = configurationResult.data as IotawattConfiguration
const statusResult = await getDedupedResponse(timestamp, getStatusUrl(sensor))
const status = statusResult.data as IotawattStatus
const configurationResult = (await getDedupedResponse(timestamp, getConfigurationUrl(sensor))).clone()
const configuration = (await configurationResult.json()) as IotawattConfiguration
const statusResult = (await getDedupedResponse(timestamp, getStatusUrl(sensor))).clone()
const status = (await statusResult.json()) as IotawattStatus

return {
timestamp: timestamp,
Expand All @@ -142,8 +142,8 @@ export const getCharacteristicsSensorData: CharacteristicsSensorPollFunction = a
const sensor = characteristics.sensor as IotawattCharacteristicsSensor

try {
const queryResult = await getDedupedResponse(timestamp, getQueryUrl(sensor))
const query = queryResult.data as IotawattCharacteristicsQuery
const queryResult = (await getDedupedResponse(timestamp, getQueryUrl(sensor))).clone()
const query = (await queryResult.json()) as IotawattCharacteristicsQuery

return {
timestamp: timestamp,
Expand Down
1 change: 1 addition & 0 deletions src/sensor/modbus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const getSensorData: PowerSensorPollFunction = async (
}

// Read the register and parse it accordingly
logger.debug(`Reading holding register ${sensorSettings.register}`)
const readRegisterResult = await client.readHoldingRegisters(sensorSettings.register, 1)

return {
Expand Down
Loading
Loading