Skip to content

Commit

Permalink
feat: add dewpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
wokim committed Apr 23, 2024
1 parent 74eb670 commit 0f16ece
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.vscode
grafana
.editorconfig
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18 as build
FROM node:18-alpine as build

WORKDIR /app
COPY package*.json ./
Expand All @@ -8,11 +8,11 @@ RUN npm ci
COPY . .
RUN npm run build

FROM node:18 as release
FROM node:18-alpine as release

WORKDIR /app
COPY --from=build /app/dist ./dist
COPY package.json package-lock.json ./
COPY package*.json ./
RUN npm ci --only=production

EXPOSE 3000
Expand Down
21 changes: 21 additions & 0 deletions src/dewPoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Function to calculate the dew point.
* @param temperature Current temperature (°C)
* @param relativeHumidity Relative humidity (%)
* @returns Dew point (°C)
*/
export function calculateDewPoint(
temperature: number,
relativeHumidity: number,
): number {
// Magnus formula constants
const a = 17.27;
const b = 237.7;

// Calculation of dew point using Magnus formula
const alpha =
(a * temperature) / (b + temperature) + Math.log(relativeHumidity / 100);
const dewPoint = (b * alpha) / (a - alpha);

return parseFloat(dewPoint.toFixed(1));
}
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
noxIndexGauge,
} from './metrics';
import { isUndefined } from 'lodash';
import { calculateDewPoint } from './dewPoint';

const app = express();

Expand Down Expand Up @@ -73,15 +74,20 @@ async function getStatus() {
const available = (await client.readDiscreteInputs(0x0040, 20)).data;
await new Promise<void>((resolve) => client.close(resolve));

const temperature = result.readInt16BE(12) / 100;
const humidity = result.readUInt16BE(14) / 100;
const dewPoint = calculateDewPoint(temperature, humidity);

return {
...(available[0] ? { tvoc: result.readUInt16BE(0) / 1000 } : {}),
pm1: result.readUInt16BE(2) / 10,
pm25: result.readUInt16BE(4) / 10,
pm4: result.readUInt16BE(6) / 10,
pm10: result.readUInt16BE(8) / 10,
co2: result.readUInt16BE(10),
temperature: result.readInt16BE(12) / 100,
humidity: result.readUInt16BE(14) / 100,
temperature,
humidity,
dewPoint,
abs_humidity: result.readUInt16BE(16),
pressure: result.readUInt16BE(18) / 10,
noise: result.readUInt16BE(20),
Expand Down

0 comments on commit 0f16ece

Please sign in to comment.