Update to convert to TypeScript and use a few changes to get data from GraphQL
This commit is contained in:
parent
ea87ab6478
commit
a9d0fcf728
1242
package-lock.json
generated
1242
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
@ -2,9 +2,11 @@
|
||||
"name": "temperato",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"main": "src/index.ts",
|
||||
"module": "src/index.ts",
|
||||
"scripts": {
|
||||
"start": "ts-node src/index.ts",
|
||||
"ts-node": "ts-node",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
@ -14,9 +16,14 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@koa/router": "^10.1.1",
|
||||
"dotenv": "^16.0.1",
|
||||
"koa": "^2.13.4",
|
||||
"node-fetch": "^3.2.4"
|
||||
"graphql-request": "^4.2.0",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,8 @@
|
||||
|
||||
import 'dotenv/config';
|
||||
import fetch from 'node-fetch';
|
||||
import Koa from 'koa';
|
||||
import Router from '@koa/router';
|
||||
import { request } from 'graphql-request';
|
||||
import getSensorDataQuery from './queries/getSensorData';
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const SET_POINT = parseFloat(process.env.SET_POINT);
|
||||
@ -27,35 +27,14 @@ const USERNAME = process.env.USERNAME;
|
||||
const PASSWORD = process.env.PASSWORD;
|
||||
const NOTIF_URL = process.env.NOTIF_URL;
|
||||
const MESSAGE = process.env.MESSAGE;
|
||||
const SENSOR_URL = process.env.SENSOR_URL;
|
||||
const GRAPHQL_URL = process.env.GRAPHQL_URL;
|
||||
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() {
|
||||
try {
|
||||
const sensor = await fetch(SENSOR_URL);
|
||||
const reading = await sensor.json();
|
||||
currentSensorReading = reading.temperature;
|
||||
if (reading.temperature >= SET_POINT) {
|
||||
const response = await request(GRAPHQL_URL, getSensorDataQuery);
|
||||
|
||||
if (response.getSensorData.temperato >= SET_POINT) {
|
||||
const basicAuth = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64');
|
||||
await fetch(NOTIF_URL, {
|
||||
method: 'post',
|
11
src/queries/getSensorData.ts
Normal file
11
src/queries/getSensorData.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { gql } from 'graphql-request';
|
||||
|
||||
const getSensorDataQuery = gql`
|
||||
query Temperato {
|
||||
getSensorData {
|
||||
temperato
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export default getSensorDataQuery;
|
@ -2,8 +2,8 @@
|
||||
Description=Temperato Service
|
||||
|
||||
[Service]
|
||||
ExecStart=node index.js
|
||||
ExecReload=node index.js
|
||||
ExecStart=npm start
|
||||
ExecReload=npm start
|
||||
KillMode=process
|
||||
Restart=on-failure
|
||||
Type=simple
|
||||
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal 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"]
|
||||
}
|
Loading…
Reference in New Issue
Block a user