mirror of
https://github.com/lasthead0/yandex2mqtt.git
synced 2025-08-07 08:40:29 +03:00
Add logging
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const {logger, authl} = global;
|
||||
const loki = require('lokijs');
|
||||
|
||||
global.dbl = new loki('./loki.json', {
|
||||
@@ -7,46 +8,50 @@ global.dbl = new loki('./loki.json', {
|
||||
autosave: true,
|
||||
autosaveInterval: 5000,
|
||||
autoloadCallback() {
|
||||
global.authl = global.dbl.getCollection('tokens');
|
||||
if (global.authl === null) {
|
||||
global.authl = global.dbl.addCollection('tokens');
|
||||
authl = global.dbl.getCollection('tokens');
|
||||
if (authl === null) {
|
||||
authl = global.dbl.addCollection('tokens');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
module.exports.find = (key, done) => {
|
||||
const ltoken = global.authl.findOne({'token': key});
|
||||
const ltoken = authl.findOne({'token': key});
|
||||
if (ltoken){
|
||||
const {userId, clientId} = ltoken;
|
||||
return done(null, {userId, clientId})
|
||||
} else {
|
||||
global.logger.log('error', new Error('Token Not Found'));
|
||||
logger.log('error', new Error('Token Not Found'));
|
||||
return done();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.findByUserIdAndClientId = (userId, clientId, done) => {
|
||||
const ltoken = global.authl.findOne({'userId': userId});
|
||||
const ltoken = authl.findOne({'userId': userId});
|
||||
if (ltoken){
|
||||
global.logger.log('info', {message: `Load token by userId (${userId}): User found`});
|
||||
logger.log('info', {message: `Load token by userId (${userId}): User found`});
|
||||
const {token, userId: uid, clientId: cid} = ltoken;
|
||||
if (uid === userId && cid === clientId) return done(null, token);
|
||||
else return done(new Error('Token Not Found'));
|
||||
if (uid === userId && cid === clientId) {
|
||||
return done(null, token);
|
||||
} else {
|
||||
logger.log('error', new Error('Token Not Found'));
|
||||
return done();
|
||||
}
|
||||
} else {
|
||||
console.log('User not found');
|
||||
return done(new Error('User Not Found'));
|
||||
logger.log('error', new Error('User Not Found'));
|
||||
return done();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.save = (token, userId, clientId, done) => {
|
||||
global.logger.log('info', {message: `Start saving token`});
|
||||
const ltoken = global.authl.findOne({'userId': userId});
|
||||
logger.log('info', {message: `Start saving token`});
|
||||
const ltoken = authl.findOne({'userId': userId});
|
||||
if (ltoken){
|
||||
global.logger.log('info', {message: `User Updated`});
|
||||
global.authl.update(Object.assign({}, ltoken, {token, userId, clientId}));
|
||||
logger.log('info', {message: `User Updated`});
|
||||
authl.update(Object.assign({}, ltoken, {token, userId, clientId}));
|
||||
} else {
|
||||
global.logger.log('info', {message: `User not Found. Create new...`});
|
||||
global.authl.insert({'type': 'token', token, userId, clientId});
|
||||
logger.log('info', {message: `User not Found. Create new...`});
|
||||
authl.insert({'type': 'token', token, userId, clientId});
|
||||
}
|
||||
done();
|
||||
};
|
||||
|
@@ -1,10 +1,15 @@
|
||||
'use strict';
|
||||
|
||||
const {logger} = global;
|
||||
const codes = {};
|
||||
|
||||
module.exports.find = (key, done) => {
|
||||
if (codes[key]) return done(null, codes[key]);
|
||||
return done(new Error('Code Not Found'));
|
||||
if (codes[key]) {
|
||||
return done(null, codes[key]);
|
||||
} else {
|
||||
logger.log('error', new Error('Code Not Found'));
|
||||
return done();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.save = (code, clientId, redirectUri, userId, userName, done) => {
|
||||
|
@@ -1,17 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const {logger} = global;
|
||||
const {clients} = require('../config');
|
||||
|
||||
module.exports.findById = (id, done) => {
|
||||
for (const client of clients) {
|
||||
if (client.id === id) return done(null, client);
|
||||
}
|
||||
return done(new Error('Client Not Found'));
|
||||
logger.log('error', new Error('Client Not Found'));
|
||||
return done();
|
||||
};
|
||||
|
||||
module.exports.findByClientId = (clientId, done) => {
|
||||
for (const client of clients) {
|
||||
if (client.clientId === clientId) return done(null, client);
|
||||
}
|
||||
return done(new Error('Client Not Found'));
|
||||
logger.log('error', new Error('Client Not Found'));
|
||||
return done();
|
||||
};
|
||||
|
@@ -1,17 +1,20 @@
|
||||
'use strict';
|
||||
|
||||
const {logger} = global;
|
||||
const {users} = require('../config');
|
||||
|
||||
module.exports.findById = (id, done) => {
|
||||
for (const user of users) {
|
||||
if (user.id === id) return done(null, user);
|
||||
}
|
||||
return done(new Error('User Not Found'));
|
||||
logger.log('error', new Error('User Not Found'));
|
||||
return done();
|
||||
};
|
||||
|
||||
module.exports.findByUsername = (username, done) => {
|
||||
for (const user of users) {
|
||||
if (user.username === username) return done(null, user);
|
||||
}
|
||||
return done(new Error('User Not Found'));
|
||||
logger.log('error', new Error('User Not Found'));
|
||||
return done();
|
||||
};
|
||||
|
10
device.js
10
device.js
@@ -1,3 +1,5 @@
|
||||
const {logger} = global;
|
||||
|
||||
/* function for convert system values to Yandex (depends of capability or property type) */
|
||||
function convertToYandexValue(val, actType) {
|
||||
switch(actType) {
|
||||
@@ -8,7 +10,7 @@ function convertToYandexValue(val, actType) {
|
||||
const value = parseFloat(val);
|
||||
return isNaN(value) ? 0.0 : value;
|
||||
} catch(e) {
|
||||
console.error(`Can't parse to float: ${val}`);
|
||||
logger.log('error', {message: `Can't parse to float: ${val}`});
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
@@ -96,7 +98,7 @@ class Device {
|
||||
}
|
||||
}
|
||||
default: {
|
||||
console.error(`Unsupported capability type: ${type}`)
|
||||
logger.log('error', {message: `Unsupported capability type: ${type}`});
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -195,7 +197,7 @@ class Device {
|
||||
message = `${value}`;
|
||||
} catch(e) {
|
||||
topic = false;
|
||||
global.logger.log('error', {message: `${e}`});
|
||||
logger.log('error', {message: `${e}`});
|
||||
}
|
||||
|
||||
if (topic) {
|
||||
@@ -225,7 +227,7 @@ class Device {
|
||||
const value = this.getMappedValue(val, actType, false);
|
||||
cp.state = {instance, value: convertToYandexValue(value, actType)};
|
||||
} catch(e) {
|
||||
global.logger.log('error', {message: `${e}`});
|
||||
logger.log('error', {message: `${e}`});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user