Antworten
Jun 13, 2020 - 04:03
Sorry, hat sich erledigt - Fehler sitzt for dem Bildschirm.
Ciao Walter
Ciao Walter
//
// Variables
//
var db_host = "192.168.178.202"; // localhost oder IP der DB-Servers
var db_user = "hadb-user";
var db_userpw = "";
var db_name = "hadb";
var sql = "";
//
// on OSX
var mysql = require('/System/Volumes/Data/Applications/AIO CREATOR NEO.app/Contents/Resources/app.nw/node_modules/mysql2');
// on RaspBerryMatic
// var mysql = require('/usr/local/addons/mediola/neo_server/node_modules/mysql2');
//
// Functions
//
const createTable = function() {
db = mysql.createConnection({
host: db_host,
user: db_user,
password: db_userpw,
database: db_name,
multipleStatements: true // this allow you to run multiple queries at once.
});
db.connect(function(err) {
if (err) {
console.log("Error: " + err.message);
throw err;
}
console.log("database connected");
});
sql = "CREATE TABLE history (TIMESTAMP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, DEVICE VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL, TYPE VARCHAR(64), READING VARCHAR(64) CHARACTER SET 'utf8' COLLATE 'utf8_bin' NOT NULL, VALUE VARCHAR(128), UNIT VARCHAR(32), PRIMARY KEY (DEVICE, READING))";
db.query(sql, function (err, results) {
if (err) {
console.log("Error: " + err.message);
throw err;
}
console.log("Table created");
});
sql = "CREATE INDEX searchIndex on history (DEVICE, READING, TIMESTAMP)";
db.query(sql, function (err, results) {
if (err) {
console.log("Error: " + err.message);
throw err;
}
console.log("Index created");
});
db.end(function (err) {
if (err) {
console.log("Error: " + err.message);
throw err;
}
console.log("database close");
});
};
//
// Main
//
createTable();
//
// End
//
//
// scans for devices to log data
//
let url = "http://htmluser:sassicaja@192.168.178.201:8181/egal.exe?x=dom.GetObject(ID_DEV
ICES).EnumUsedIDs()";
// let url = "http://htmluser:sassicaja@192.168.178.201:8181/egal.exe?x=dom.GetObject(ID_CHA
NNELS).EnumUsedIDs()";
// let url = "http://htmluser:sassicaja@192.168.178.201:8181/egal.exe?x=dom.GetObject(ID_DAT
APOINTS).EnumUsedIDs()";
//
// database definitions
//
let db_host = "192.168.178.202"; // localhost oder IP der DB-Servers
let db_user = "hadb-user";
let db_userpw = "let db_name = "hadb";
let sql = "";
//
// on OSX
// var mysql = require('/System/Volumes/Data/Applications/AIO CREATOR NEO.app/Contents/Resources/app.nw/node_modules/mysql2');
// on RaspBerryMatic
var mysql = require('/usr/local/addons/mediola/neo_server/node_modules/mysql2');
//
//
// device and value lookup array (key:value)
//
const arrData2Log = new Array (
{ dev: "TempHum_Pool_STHO", val: ["ACTUAL_TEMPERATURE", "HUMIDITY"] },
{ dev: "Lux_Pool_SLO", val: ["CURRENT_ILLUMINATION"] }
);
//
// function for id numbers
//
const getObj = function(objurl) {
// return new pending promise
return new Promise((resolve, reject) => {
// select http or https module, depending on reqested url
const lib = url.startsWith('https') ? require('https') : require('http');
// const lib = require('http');
const request = lib.get(objurl, (response) => {
// handle http errors
if (response.statusCode < 200 || response.statusCode > 299) {
reject(new Error('Failed to load page, status code: ' + response.statusCode));
}
// temporary data holder
const body = [];
// on every content chunk, push it to the data array
response.on('data', (chunk) => {
body.push(chunk);
});
// we are done, resolve promise with those joined chunks
response.on('end', () => {
resolve(body.join(''));
});
// handle connection errors of the request
request.on('error', (err) => reject(err));
});
});
};
// check if device name is in array keys
const isDevNameInKeys = (a, d) => {
let k = "";
let r = false;
//
for (k of a) {
// console.log(k.dev);
if (k.dev === d) {
r = true;
break;
}
}
return r;
};
//
const writeData2DB = function(dn, dt, dpn, dpv, dpu) {
db = mysql.createConnection({
host: db_host,
user: db_user,
password: db_userpw,
database: db_name,
multipleStatements: true // this allow you to run multiple queries at once.
});
db.connect(function(err) {
if (err) {
console.log("Error: " + err.message);
throw err;
}
console.log("database connected");
});
sql = "INSERT INTO history (DEVICE,TYPE,READING,VALUE,UNIT) VALUES ('" + dn + "','" + dt + "','" + dpn + "','" + dpv + "','" + dpu + "')";
// console.log(sql + "\n\r");
db.execute(sql, function (err, results) {
if (err) {
console.log("Error: " + err.message);
throw err;
}
console.log("data written");
});
db.end(function (err) {
if (err) {
console.log("Error: " + err.message);
throw err;
}
console.log("database close");
});
};
//
// main call
//
getObj(url)
.then((devids) => {
// console.log(devids + "\n\r");
devids = devids.match(/[0-9]+/g);
// console.log(devids + "\n\r");
devids.forEach(devid => {
// get device name
getObj("http://htmluser:sassicaja@192.168.178.201:8181/egal.exe?x=dom.GetObject
(" + devid + ").Name()")
.then((devname) => {
// console.log(devname + "\n\r");
devname = devname.match(/([(A-Z)(a-z)(0-9)(_-\s.)]*?\b)(?=<\/x>)/g);
// console.log(devname + "\n\r");
if (devname !== null && isDevNameInKeys(arrData2Log, devname[0])) {
// console.log(devname + "\n\r");
getObj("http://htmluser:sassicaja@192.168.178.201:8181/egal.exe?x=dom.GetObject
(" + devid + ").HssType()")
.then((devtype) => {
// console.log(devtype + "\n\r");
devtype = devtype.match(/([(A-Z)(a-z)(0-9)(_-\s.)]*?\b)(?=<\/x>)/g);
if (devtype !== null) {
// console.log(devtype + "\n\r");
getObj("http://htmluser:sassicaja@192.168.178.201:8181/egal.exe?x=dom.GetObject
(" + devid + ").Channels()")
.then((chnids) => {
// console.log(chnid + "\n\r");
chnids = chnids.match(/[0-9]+/g);
if (chnids !== null) {
chnids.forEach(chnid => {
// get channel name
getObj("http://htmluser:sassicaja@192.168.178.201:8181/egal.exe?x=dom.GetObject
(" + chnid + ").DPs()")
.then((dpids) => {
dpids = dpids.match(/[0-9]+/g);
if (dpids !== null) {
// console.log(dpids + "\n\r");
dpids.forEach(dpid => {
getObj("http://htmluser:sassicaja@192.168.178.201:8181/egal.exe?x=dom.GetObject
(" + dpid + ").Name()")
.then((dpname) => {
// console.log(dpname);
dpname = dpname.match(/([(A-Z)(a-z)(0-9)(_-\s)]*?\b)(?=<\/x>)/g);
// console.log(dpname + "\n\r");
arrData2Log.forEach(arr => {
arr.val.forEach(val => {
if (val === dpname[0]) {
// console.log(val + "\n\r")
getObj("http://htmluser:sassicaja@192.168.178.201:8181/egal.exe?x=dom.GetObject
(" + dpid + ").Value()")
.then((dpvalue) => {
dpvalue = dpvalue.match(/([(A-Z)(a-z)(0-9)(_-\s.)]*?\b)(?=<\/x>)/g);
// console.log(dpvalue + "\n\r");
// console.log(devname[0] + "." + devtype[0] + "." + dpname[0] + ": " + dpvalue[0] + "\n\r");
writeData2DB(devname[0],devtype[0],dpname[0],dpvalue[0],"");
});
}
});
});
});
});
}
});
});
}
});
}
});
}
});
});
})
.catch((err) => console.log(err));
//
// on OSX
// var mysql = require('/System/Volumes/Data/Applications/AIO CREATOR NEO.app/Contents/Resources/app.nw/node_modules/mysql2');
// on RaspBerryMatic
var mysql = require('/usr/local/addons/mediola/neo_server/node_modules/mysql2');
//
Haben Sie die gleiche Frage? Dieser Frage folgen
Jun 13, 2020 - 04:03
Neuen Kommentar hinzufügen