fixed problems with AC protocol,
nodejs backend improved
This commit is contained in:
43
dht.js
43
dht.js
@@ -121,7 +121,7 @@ app.use(helmet());
|
||||
//app.use(bodyParser.text());
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(express.static('public'))
|
||||
app.use(express.static('public'));
|
||||
// enabling CORS for all requests
|
||||
app.use(cors());
|
||||
//Authentication ++
|
||||
@@ -175,6 +175,7 @@ app.get('/accontrol', ensureLoggedIn, function(req, res){
|
||||
});
|
||||
|
||||
var ac = require('./ac.js');
|
||||
|
||||
app.post('/accontrol', ensureLoggedIn, function(req, res){
|
||||
var model = { model: {user: req.user, data: req.body} };
|
||||
console.log("power:"+ req.body.power);
|
||||
@@ -251,25 +252,45 @@ app.post('/accontrol', ensureLoggedIn, function(req, res){
|
||||
const { parse } = require('querystring');
|
||||
app.post('/dht/ping', (req, res) => { (async (res) => {
|
||||
try {
|
||||
console.log("HEADERS:" + req.headers); res.sendStatus(200);
|
||||
console.log("HEADERS:" + req.headers);
|
||||
res.sendStatus(200);
|
||||
} catch (error) {
|
||||
console.log("PING Error:" + error); res.sendStatus(500);
|
||||
console.log("PING Error:" + error);
|
||||
res.sendStatus(500);
|
||||
} })(res);
|
||||
});
|
||||
|
||||
var moment = require('moment');
|
||||
|
||||
app.use('/setup', bodyParser.text(), function(req, res) {
|
||||
let resp = JSON.stringify(req.body);
|
||||
console.log(Jresp);
|
||||
console.log("/setup> Device is online:" + req.body.IP);
|
||||
res.setHeader('Content-Type', 'text/plain');
|
||||
//res.write('you posted:\n');
|
||||
res.write(resp);
|
||||
let body = '';
|
||||
req.on('data', chunk => {
|
||||
console.log("d>"+chunk);
|
||||
body += chunk.toString();
|
||||
});
|
||||
req.on('end', () => {
|
||||
console.log("ended>"+body);
|
||||
var ob = parse(body);
|
||||
console.log(ob);
|
||||
res.end('ok');
|
||||
});
|
||||
});
|
||||
|
||||
app.use('/ir', bodyParser.text(), function(rq, rs) {
|
||||
|
||||
console.log("");
|
||||
console.log("");
|
||||
console.log("");
|
||||
console.log("");
|
||||
console.log("IR request:"+rq.method );
|
||||
console.log("/ir>:"+rq.method );
|
||||
if(rq.method == "GET")
|
||||
{
|
||||
var cmd = rq.param('command');
|
||||
console.log("GET CMD:"+cmd );
|
||||
// console.log("GET CMD:"+cmd );
|
||||
switch(cmd)
|
||||
{
|
||||
case 'ping':
|
||||
@@ -291,7 +312,7 @@ app.use('/ir', bodyParser.text(), function(rq, rs) {
|
||||
body += chunk.toString();
|
||||
});
|
||||
rq.on('end', () => {
|
||||
console.log(">"+body);
|
||||
console.log("POSTED IR DATA>"+body);
|
||||
var ob = parse(body);
|
||||
console.log(ob);
|
||||
rs.end('ok');
|
||||
@@ -302,7 +323,7 @@ app.use('/ir', bodyParser.text(), function(rq, rs) {
|
||||
|
||||
app.get('/dht', (req, res) => { (async (res) => {
|
||||
try {
|
||||
const response = await got('http://192.168.1.126/json')
|
||||
const response = await got('http://192.168.1.126/json');
|
||||
res.send(response.body);
|
||||
} catch (error) {
|
||||
console.log("DHT Error:" + error); //..response.body);
|
||||
@@ -405,6 +426,7 @@ cron.schedule(' */30 * * * *', () => {//cron.schedule('*/5 * * * * *', () =>
|
||||
|
||||
async function StoreSensorReadingsAsync()
|
||||
{
|
||||
console.log("StoreSensorReadingsAsync");
|
||||
try {
|
||||
await new Promise(function(resolve, reject) {
|
||||
request('http://192.168.1.126/json', { json: true }, (err, res, body) => {
|
||||
@@ -434,7 +456,8 @@ async function StoreSensorReadingsAsync()
|
||||
|
||||
|
||||
function StoreSensorReadings()
|
||||
{
|
||||
{
|
||||
console.log("StoreSensorReadings");
|
||||
(async () => {
|
||||
try {
|
||||
const dht = await got('http://192.168.1.126/json')
|
||||
|
||||
Reference in New Issue
Block a user