Skip to content

Commit

Permalink
Split configuration parsing and resolving/validation
Browse files Browse the repository at this point in the history
Easier to test this way
  • Loading branch information
Jalle19 committed Oct 18, 2023
1 parent deac8d0 commit 558a865
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export interface Config {
}

export const parseConfig = (configFileContents: string): Config => {
const config = YAML.parse(configFileContents) as Config
return YAML.parse(configFileContents) as Config
}

export const resolveAndValidateConfig = (config: Config): Config => {
// Set various defaults
for (const circuit of config.circuits) {
// Use Circuit as default circuit type
Expand Down
5 changes: 3 additions & 2 deletions src/eachwatt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import http from 'http'
import yargs from 'yargs'
import fs from 'fs'
import { Config, parseConfig } from './config'
import { Config, parseConfig, resolveAndValidateConfig } from './config'
import { SensorType } from './sensor'
import { pollPowerSensors } from './circuit'
import { httpRequestHandler } from './http/server'
Expand Down Expand Up @@ -71,7 +71,8 @@ const mainPollerFunc = async (config: Config) => {
}

const configFileContents = fs.readFileSync(configFile, 'utf8')
const config = parseConfig(configFileContents)
const rawConfig = parseConfig(configFileContents)
const config = resolveAndValidateConfig(rawConfig)

// Create and start HTTP server
const httpServer = http.createServer(httpRequestHandler)
Expand Down

0 comments on commit 558a865

Please sign in to comment.