init
This commit is contained in:
21
controller/express.js
Normal file
21
controller/express.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import express from 'express';
|
||||
import { Config } from '../config/config.js';
|
||||
import { Storage } from './storage.js';
|
||||
|
||||
|
||||
|
||||
export class Web{
|
||||
constructor(){
|
||||
let app = express();
|
||||
|
||||
app.get('/temp',async (req,res)=>{
|
||||
if(Storage.hub==null){
|
||||
res.statusCode(500);
|
||||
res.send("err");
|
||||
}
|
||||
res.json(await Storage.hub.getTemp());
|
||||
})
|
||||
app.use(express.static('static'));
|
||||
app.listen(Config.WEB_SERVER.PORT);
|
||||
}
|
||||
}
|
||||
19
controller/sensorhub.js
Normal file
19
controller/sensorhub.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import SensorHub from 'dockerpi-sensorhub';
|
||||
|
||||
|
||||
|
||||
export class Hub {
|
||||
constructor() {
|
||||
let hub = new SensorHub();
|
||||
|
||||
if (!hub || !hub.read) throw new Error('Unable to init the hub.');
|
||||
this.hub = hub;
|
||||
}
|
||||
async getTemp() {
|
||||
let x = null;
|
||||
await this.hub.read().then(c=>{
|
||||
x = c;
|
||||
})
|
||||
return x;
|
||||
}
|
||||
}
|
||||
4
controller/storage.js
Normal file
4
controller/storage.js
Normal file
@@ -0,0 +1,4 @@
|
||||
export class Storage{
|
||||
static hub = null;
|
||||
static dir = null;
|
||||
}
|
||||
61
controller/weather_api.js
Normal file
61
controller/weather_api.js
Normal file
@@ -0,0 +1,61 @@
|
||||
import fs from 'fs';
|
||||
import fetch from 'node-fetch';
|
||||
import { Config } from '../config/config.js';
|
||||
import { Storage } from "./storage.js";
|
||||
|
||||
class WeatherData {
|
||||
timestamp = 0;
|
||||
humidity = 0;
|
||||
temp = 0;
|
||||
weather = [];
|
||||
wind_speed = 0;
|
||||
wind_deg = 0;
|
||||
}
|
||||
|
||||
export class Weather {
|
||||
constructor() {
|
||||
//let x = new WeatherData();
|
||||
//this.writeFile(x);
|
||||
Storage.weather = this;
|
||||
this.data = this.readFile();
|
||||
|
||||
this.readApi();
|
||||
this.inter = setInterval(this.readApi, 1000 * 60);
|
||||
}
|
||||
async readApi() {
|
||||
let me = Storage.weather;
|
||||
//console.log(`DIFF FROM TO ${(Math.floor((+ new Date) / 1000))} ${me.data.timestamp}`);
|
||||
if (((Math.floor((+ new Date) / 1000)) - me.data.timestamp) > Config.WEATHER_API.READING) {
|
||||
console.log("GET NEW DATA")
|
||||
|
||||
let d = Math.floor((+ new Date) / 1000)
|
||||
me.data.timestamp = d;
|
||||
|
||||
let url = `http://api.openweathermap.org/data/2.5/weather?q=${Config.WEATHER_API.CITY}&units=metric&appid=${Config.WEATHER_API.API_KEY}`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'get',
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
const data = await response.json();
|
||||
|
||||
data.timestamp = data.dt;
|
||||
me.data = data;
|
||||
me.writeFile(me.data);
|
||||
}
|
||||
}
|
||||
readFile() {
|
||||
let jso = {};
|
||||
try {
|
||||
let data = fs.readFileSync(`${Storage.dir}/data/weather.json`);
|
||||
jso = JSON.parse(data);
|
||||
} catch (e) {
|
||||
jso = {timestamp:0};
|
||||
}
|
||||
console.log(jso);
|
||||
return jso;
|
||||
}
|
||||
writeFile(data) {
|
||||
fs.writeFileSync(`${Storage.dir}/data/weather.json`, JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
26
controller/websocket.js
Normal file
26
controller/websocket.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { Config } from '../config/config.js';
|
||||
import { Storage } from './storage.js';
|
||||
export class WebSock {
|
||||
constructor() {
|
||||
let wss = new WebSocketServer({ port: Config.WEB_SERVER.WSPORT });
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
let sendData = setInterval(()=>{
|
||||
Storage.hub.getTemp().then(r=>{
|
||||
let data = {type:'temp',inside:r,outside:Storage.weather.data};
|
||||
ws.send(JSON.stringify(data));
|
||||
//console.log(ws)
|
||||
})
|
||||
},10000)
|
||||
ws.on('message', function message(data) {
|
||||
console.log('received: %s', data);
|
||||
});
|
||||
ws.on('close',()=>{
|
||||
clearInterval(sendData);
|
||||
})
|
||||
ws.send('something');
|
||||
});
|
||||
this.wss = wss;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user