Update to convert to TypeScript and use a few changes to get data from GraphQL

This commit is contained in:
William Moore 2022-05-27 15:38:38 -05:00
parent ea87ab6478
commit a9d0fcf728
6 changed files with 483 additions and 786 deletions

1190
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,9 +2,11 @@
"name": "temperato", "name": "temperato",
"version": "1.0.0", "version": "1.0.0",
"description": "", "description": "",
"main": "index.js", "main": "src/index.ts",
"type": "module", "module": "src/index.ts",
"scripts": { "scripts": {
"start": "ts-node src/index.ts",
"ts-node": "ts-node",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
@ -14,9 +16,14 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@koa/router": "^10.1.1",
"dotenv": "^16.0.1", "dotenv": "^16.0.1",
"koa": "^2.13.4", "graphql-request": "^4.2.0",
"node-fetch": "^3.2.4" "node-fetch": "^2.6.1"
},
"devDependencies": {
"@types/node": "^17.0.35",
"@types/node-fetch": "^2.6.1",
"ts-node": "^10.8.0",
"typescript": "^4.7.2"
} }
} }

View File

@ -18,8 +18,8 @@
import 'dotenv/config'; import 'dotenv/config';
import fetch from 'node-fetch'; import fetch from 'node-fetch';
import Koa from 'koa'; import { request } from 'graphql-request';
import Router from '@koa/router'; import getSensorDataQuery from './queries/getSensorData';
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
const SET_POINT = parseFloat(process.env.SET_POINT); const SET_POINT = parseFloat(process.env.SET_POINT);
@ -27,35 +27,14 @@ const USERNAME = process.env.USERNAME;
const PASSWORD = process.env.PASSWORD; const PASSWORD = process.env.PASSWORD;
const NOTIF_URL = process.env.NOTIF_URL; const NOTIF_URL = process.env.NOTIF_URL;
const MESSAGE = process.env.MESSAGE; const MESSAGE = process.env.MESSAGE;
const SENSOR_URL = process.env.SENSOR_URL; const GRAPHQL_URL = process.env.GRAPHQL_URL;
const SLEEP_TIMER = 1000; const SLEEP_TIMER = 1000;
const app = new Koa();
const router = new Router();
router.get('/', async ctx => {
try {
ctx.body = `{"temperature": ${currentSensorReading}}`;
} catch (e) {
ctx.body = 'There was an error retrieving the sensor data.';
ctx.status = 500;
}
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(PORT);
let currentSensorReading=0.0
async function temperatoTick() { async function temperatoTick() {
try { try {
const sensor = await fetch(SENSOR_URL); const response = await request(GRAPHQL_URL, getSensorDataQuery);
const reading = await sensor.json();
currentSensorReading = reading.temperature; if (response.getSensorData.temperato >= SET_POINT) {
if (reading.temperature >= SET_POINT) {
const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64'); const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64');
await fetch(NOTIF_URL, { await fetch(NOTIF_URL, {
method: 'post', method: 'post',

View File

@ -0,0 +1,11 @@
import { gql } from 'graphql-request';
const getSensorDataQuery = gql`
query Temperato {
getSensorData {
temperato
}
}
`;
export default getSensorDataQuery;

View File

@ -2,8 +2,8 @@
Description=Temperato Service Description=Temperato Service
[Service] [Service]
ExecStart=node index.js ExecStart=npm start
ExecReload=node index.js ExecReload=npm start
KillMode=process KillMode=process
Restart=on-failure Restart=on-failure
Type=simple Type=simple

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"outDir": "./build",
"typeRoots": ["node_modules/@types", "node_modules/node-fetch/@types"],
"module": "commonjs",
"esModuleInterop": true,
"moduleResolution": "node",
"declaration": true,
"noImplicitAny": true,
"skipLibCheck": true
},
"include": ["./src/**/*.ts"],
"exclude": ["node_modules"]
}