source now in separate modules;
Implemented MQTT coms. Using Tasmota for ESP8266; IR working reliably
This commit is contained in:
56
src/db/devicemessages.js
Normal file
56
src/db/devicemessages.js
Normal file
@@ -0,0 +1,56 @@
|
||||
//devicemessages
|
||||
var mysql = require('mysql');
|
||||
var con = mysql.createConnection({
|
||||
host : 'localhost',
|
||||
user : 'iot',
|
||||
password : '!iot_popovi',
|
||||
database : 'iot'
|
||||
});
|
||||
|
||||
const got = require('got');
|
||||
|
||||
exports.findByName = function(fieldName, cb) {
|
||||
process.nextTick(function() {
|
||||
con.query("SELECT * FROM devicemessages WHERE field_name=? OR ? IS NULL",
|
||||
[fieldName, fieldName], (err, data) => {
|
||||
if (!err) {
|
||||
cb(null, data);
|
||||
} else {
|
||||
cb(new Error('SQL Error: ' + err));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
exports.insert = function(device_id, field_name, value, cb){
|
||||
var params = [device_id, field_name,value];
|
||||
let sql = `INSERT INTO devicemessages(device_id,field_name,field_value,timestamp)
|
||||
VALUES (?,?,?,NOW());`;
|
||||
con.query(sql,params,(err, r) => {
|
||||
//if(!cb) {return;}
|
||||
if (err) {
|
||||
console.log("error: ", err);
|
||||
cb && cb(new Error('SQL Error: ' + err));
|
||||
}else{
|
||||
console.log("inserted record: ", { id: r.insertId});
|
||||
cb && cb(null, { id: r.insertId, ...params });
|
||||
}
|
||||
});
|
||||
}
|
||||
exports.getFromDht = function(url, cb)
|
||||
{
|
||||
(async () => {
|
||||
try {
|
||||
const dht = await got('http://192.168.1.126/json')
|
||||
var j = JSON.parse(body);
|
||||
if(j.dht && j.dht.hum <= 100 && j.dht.hum >= 0){
|
||||
exports.insert(0, "A23_DHT", dht.body, cb);
|
||||
}else {
|
||||
console.log("Skip! Got invalid data from DHT: " + dht);
|
||||
}
|
||||
} catch (ex) {
|
||||
console.log("DHT exception:" + ex);
|
||||
cb && cb(new Error('SQL Error: ' + ex));
|
||||
}
|
||||
})();
|
||||
}
|
||||
Reference in New Issue
Block a user