added database updated notification
fixed notification tests fixed datetime notification id bump api version
This commit is contained in:
parent
fb4f1cf1a2
commit
1688a6b24c
@ -118,7 +118,7 @@
|
|||||||
"name": "dusk"
|
"name": "dusk"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "792885f3-f505-42db-8c74-3d0460b575a1",
|
"id": "726acb30-928b-43da-a790-977a821c979e",
|
||||||
"idName": "sunrise",
|
"idName": "sunrise",
|
||||||
"name": "sunrise"
|
"name": "sunrise"
|
||||||
},
|
},
|
||||||
|
|||||||
@ -213,7 +213,11 @@ DeviceManager::DeviceError GuhCore::removeConfiguredDevice(const DeviceId &devic
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return m_deviceManager->removeConfiguredDevice(deviceId);
|
DeviceManager::DeviceError removeError = m_deviceManager->removeConfiguredDevice(deviceId);
|
||||||
|
if (removeError == DeviceManager::DeviceErrorNoError)
|
||||||
|
m_logger->removeDeviceLogs(deviceId);
|
||||||
|
|
||||||
|
return removeError;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! Calls the metheod DeviceManager::pairDevice(\a pairingTransactionId, \a deviceClassId, \a deviceDescriptorId).
|
/*! Calls the metheod DeviceManager::pairDevice(\a pairingTransactionId, \a deviceClassId, \a deviceDescriptorId).
|
||||||
@ -357,7 +361,11 @@ RuleEngine::RuleError GuhCore::editRule(const RuleId &id, const QString &name, c
|
|||||||
* \sa RuleEngine, */
|
* \sa RuleEngine, */
|
||||||
RuleEngine::RuleError GuhCore::removeRule(const RuleId &id)
|
RuleEngine::RuleError GuhCore::removeRule(const RuleId &id)
|
||||||
{
|
{
|
||||||
return m_ruleEngine->removeRule(id);
|
RuleEngine::RuleError removeError = m_ruleEngine->removeRule(id);
|
||||||
|
if (removeError != RuleEngine::RuleErrorNoError)
|
||||||
|
m_logger->removeRuleLogs(id);
|
||||||
|
|
||||||
|
return removeError;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! Calls the metheod RuleEngine::findRules(\a deviceId).
|
/*! Calls the metheod RuleEngine::findRules(\a deviceId).
|
||||||
|
|||||||
@ -33,17 +33,12 @@ LoggingHandler::LoggingHandler(QObject *parent) :
|
|||||||
QVariantMap params;
|
QVariantMap params;
|
||||||
QVariantMap returns;
|
QVariantMap returns;
|
||||||
|
|
||||||
// Notifications
|
|
||||||
params.clear(); returns.clear();
|
|
||||||
setDescription("LogEntryAdded", "Emitted whenever an entry is appended to the logging system. "
|
|
||||||
"The filters can be combinend.");
|
|
||||||
params.insert("logEntry", JsonTypes::logEntryRef());
|
|
||||||
setParams("LogEntryAdded", params);
|
|
||||||
|
|
||||||
params.clear(); returns.clear();
|
|
||||||
setDescription("GetLogEntries", "Get the LogEntries matching the given filter.");
|
|
||||||
|
|
||||||
QVariantMap timeFilter;
|
QVariantMap timeFilter;
|
||||||
|
params.clear(); returns.clear();
|
||||||
|
setDescription("GetLogEntries", "Get the LogEntries matching the given filter. "
|
||||||
|
"Each list element of a given filter will be connected with OR "
|
||||||
|
"to each other. Each of the given filters will be connected with AND "
|
||||||
|
"to each other.");
|
||||||
timeFilter.insert("o:startDate", JsonTypes::basicTypeToString(JsonTypes::Int));
|
timeFilter.insert("o:startDate", JsonTypes::basicTypeToString(JsonTypes::Int));
|
||||||
timeFilter.insert("o:endDate", JsonTypes::basicTypeToString(JsonTypes::Int));
|
timeFilter.insert("o:endDate", JsonTypes::basicTypeToString(JsonTypes::Int));
|
||||||
params.insert("o:timeFilters", QVariantList() << timeFilter);
|
params.insert("o:timeFilters", QVariantList() << timeFilter);
|
||||||
@ -53,13 +48,27 @@ LoggingHandler::LoggingHandler(QObject *parent) :
|
|||||||
params.insert("o:typeIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Uuid));
|
params.insert("o:typeIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Uuid));
|
||||||
params.insert("o:deviceIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Uuid));
|
params.insert("o:deviceIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Uuid));
|
||||||
params.insert("o:values", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Variant));
|
params.insert("o:values", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Variant));
|
||||||
|
|
||||||
setParams("GetLogEntries", params);
|
setParams("GetLogEntries", params);
|
||||||
returns.insert("loggingError", JsonTypes::loggingErrorRef());
|
returns.insert("loggingError", JsonTypes::loggingErrorRef());
|
||||||
returns.insert("o:logEntries", QVariantList() << JsonTypes::logEntryRef());
|
returns.insert("o:logEntries", QVariantList() << JsonTypes::logEntryRef());
|
||||||
setReturns("GetLogEntries", returns);
|
setReturns("GetLogEntries", returns);
|
||||||
|
|
||||||
|
// Notifications
|
||||||
|
params.clear();
|
||||||
|
setDescription("LogEntryAdded", "Emitted whenever an entry is appended to the logging system. ");
|
||||||
|
params.insert("logEntry", JsonTypes::logEntryRef());
|
||||||
|
setParams("LogEntryAdded", params);
|
||||||
|
|
||||||
|
params.clear();
|
||||||
|
setDescription("LogDatabaseUpdated", "Emitted whenever the database was updated. "
|
||||||
|
"The database will be updated when a log entry was deleted. A log "
|
||||||
|
"entry will be deleted when the corresponding device or a rule will "
|
||||||
|
"be removed, or when the oldest entry of the database was deleted to "
|
||||||
|
"keep to database in the size limits.");
|
||||||
|
setParams("LogDatabaseUpdated", params);
|
||||||
|
|
||||||
connect(GuhCore::instance()->logEngine(), &LogEngine::logEntryAdded, this, &LoggingHandler::logEntryAdded);
|
connect(GuhCore::instance()->logEngine(), &LogEngine::logEntryAdded, this, &LoggingHandler::logEntryAdded);
|
||||||
|
connect(GuhCore::instance()->logEngine(), &LogEngine::logDatabaseUpdated, this, &LoggingHandler::logDatabaseUpdated);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString LoggingHandler::name() const
|
QString LoggingHandler::name() const
|
||||||
@ -69,11 +78,18 @@ QString LoggingHandler::name() const
|
|||||||
|
|
||||||
void LoggingHandler::logEntryAdded(const LogEntry &logEntry)
|
void LoggingHandler::logEntryAdded(const LogEntry &logEntry)
|
||||||
{
|
{
|
||||||
|
qCDebug(dcJsonRpc) << "Notify \"Logging.LogEntryAdded\"";
|
||||||
QVariantMap params;
|
QVariantMap params;
|
||||||
params.insert("logEntry", JsonTypes::packLogEntry(logEntry));
|
params.insert("logEntry", JsonTypes::packLogEntry(logEntry));
|
||||||
emit LogEntryAdded(params);
|
emit LogEntryAdded(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LoggingHandler::logDatabaseUpdated()
|
||||||
|
{
|
||||||
|
qCDebug(dcJsonRpc) << "Notify \"Logging.LogDatabaseUpdated\"";
|
||||||
|
emit LogDatabaseUpdated(QVariantMap());
|
||||||
|
}
|
||||||
|
|
||||||
JsonReply* LoggingHandler::GetLogEntries(const QVariantMap ¶ms) const
|
JsonReply* LoggingHandler::GetLogEntries(const QVariantMap ¶ms) const
|
||||||
{
|
{
|
||||||
qCDebug(dcJsonRpc) << "Asked for log entries" << params;
|
qCDebug(dcJsonRpc) << "Asked for log entries" << params;
|
||||||
|
|||||||
@ -37,9 +37,12 @@ public:
|
|||||||
Q_INVOKABLE JsonReply *GetLogEntries(const QVariantMap ¶ms) const;
|
Q_INVOKABLE JsonReply *GetLogEntries(const QVariantMap ¶ms) const;
|
||||||
signals:
|
signals:
|
||||||
void LogEntryAdded(const QVariantMap ¶ms);
|
void LogEntryAdded(const QVariantMap ¶ms);
|
||||||
|
void LogDatabaseUpdated(const QVariantMap ¶ms);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void logEntryAdded(const LogEntry &entry);
|
void logEntryAdded(const LogEntry &entry);
|
||||||
|
void logDatabaseUpdated();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -24,20 +24,24 @@
|
|||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
||||||
#include <QSqlDatabase>
|
#include <QSqlDatabase>
|
||||||
|
#include <QSqlDriver>
|
||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
|
#include <QSqlRecord>
|
||||||
#include <QSqlError>
|
#include <QSqlError>
|
||||||
#include <QMetaEnum>
|
#include <QMetaEnum>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
|
||||||
#define DB_SCHEMA_VERSION 2
|
#define DB_SCHEMA_VERSION 2
|
||||||
|
#define DB_MAX_SIZE 3000
|
||||||
|
|
||||||
namespace guhserver {
|
namespace guhserver {
|
||||||
|
|
||||||
LogEngine::LogEngine(QObject *parent):
|
LogEngine::LogEngine(QObject *parent):
|
||||||
QObject(parent)
|
QObject(parent)
|
||||||
{
|
{
|
||||||
|
|
||||||
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
||||||
m_db.setDatabaseName("/tmp/guhd-logs.sqlite");
|
m_db.setDatabaseName("/tmp/guhd.log");
|
||||||
|
|
||||||
if (!m_db.isValid()) {
|
if (!m_db.isValid()) {
|
||||||
qCWarning(dcLogEngine) << "Database not valid:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
|
qCWarning(dcLogEngine) << "Database not valid:" << m_db.lastError().driverText() << m_db.lastError().databaseText();
|
||||||
@ -58,14 +62,19 @@ QList<LogEntry> LogEngine::logEntries(const LogFilter &filter) const
|
|||||||
QList<LogEntry> results;
|
QList<LogEntry> results;
|
||||||
QSqlQuery query;
|
QSqlQuery query;
|
||||||
|
|
||||||
QString queryCall = "SELECT * FROM entries;";
|
QString queryCall = "SELECT * FROM entries ORDER BY timestamp;";
|
||||||
if (filter.isEmpty()) {
|
if (filter.isEmpty()) {
|
||||||
query.exec(queryCall);
|
query.exec(queryCall);
|
||||||
} else {
|
} else {
|
||||||
queryCall = QString("SELECT * FROM entries WHERE %1;").arg(filter.queryString());
|
queryCall = QString("SELECT * FROM entries WHERE %1 ORDER BY timestamp;").arg(filter.queryString());
|
||||||
query.exec(queryCall);
|
query.exec(queryCall);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (query.lastError().isValid()) {
|
||||||
|
qCWarning(dcLogEngine) << "Error fetching log entries. Driver error:" << query.lastError().driverText() << "Database error:" << query.lastError().databaseText();
|
||||||
|
return QList<LogEntry>();
|
||||||
|
}
|
||||||
|
|
||||||
while (query.next()) {
|
while (query.next()) {
|
||||||
LogEntry entry(
|
LogEntry entry(
|
||||||
QDateTime::fromTime_t(query.value("timestamp").toLongLong()),
|
QDateTime::fromTime_t(query.value("timestamp").toLongLong()),
|
||||||
@ -91,7 +100,6 @@ void LogEngine::logSystemEvent(bool active, Logging::LoggingLevel level)
|
|||||||
LogEntry entry(level, Logging::LoggingSourceSystem);
|
LogEntry entry(level, Logging::LoggingSourceSystem);
|
||||||
entry.setActive(active);
|
entry.setActive(active);
|
||||||
appendLogEntry(entry);
|
appendLogEntry(entry);
|
||||||
emit logEntryAdded(entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogEngine::logEvent(const Event &event)
|
void LogEngine::logEvent(const Event &event)
|
||||||
@ -112,7 +120,6 @@ void LogEngine::logEvent(const Event &event)
|
|||||||
entry.setDeviceId(event.deviceId());
|
entry.setDeviceId(event.deviceId());
|
||||||
entry.setValue(valueList.join(", "));
|
entry.setValue(valueList.join(", "));
|
||||||
appendLogEntry(entry);
|
appendLogEntry(entry);
|
||||||
emit logEntryAdded(entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogEngine::logAction(const Action &action, Logging::LoggingLevel level, int errorCode)
|
void LogEngine::logAction(const Action &action, Logging::LoggingLevel level, int errorCode)
|
||||||
@ -126,7 +133,6 @@ void LogEngine::logAction(const Action &action, Logging::LoggingLevel level, int
|
|||||||
entry.setDeviceId(action.deviceId());
|
entry.setDeviceId(action.deviceId());
|
||||||
entry.setValue(valueList.join(", "));
|
entry.setValue(valueList.join(", "));
|
||||||
appendLogEntry(entry);
|
appendLogEntry(entry);
|
||||||
emit logEntryAdded(entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogEngine::logRuleTriggered(const Rule &rule)
|
void LogEngine::logRuleTriggered(const Rule &rule)
|
||||||
@ -134,7 +140,6 @@ void LogEngine::logRuleTriggered(const Rule &rule)
|
|||||||
LogEntry entry(Logging::LoggingSourceRules);
|
LogEntry entry(Logging::LoggingSourceRules);
|
||||||
entry.setTypeId(rule.id());
|
entry.setTypeId(rule.id());
|
||||||
appendLogEntry(entry);
|
appendLogEntry(entry);
|
||||||
emit logEntryAdded(entry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogEngine::logRuleActiveChanged(const Rule &rule)
|
void LogEngine::logRuleActiveChanged(const Rule &rule)
|
||||||
@ -143,11 +148,38 @@ void LogEngine::logRuleActiveChanged(const Rule &rule)
|
|||||||
entry.setTypeId(rule.id());
|
entry.setTypeId(rule.id());
|
||||||
entry.setActive(rule.active());
|
entry.setActive(rule.active());
|
||||||
appendLogEntry(entry);
|
appendLogEntry(entry);
|
||||||
emit logEntryAdded(entry);
|
}
|
||||||
|
|
||||||
|
void LogEngine::removeDeviceLogs(const DeviceId &deviceId)
|
||||||
|
{
|
||||||
|
qCDebug(dcLogEngine) << "Deleting log entries from device" << deviceId.toString();
|
||||||
|
|
||||||
|
QSqlQuery query;
|
||||||
|
QString queryDeleteString = QString("DELETE FROM entries WHERE deviceId = '%1';").arg(deviceId.toString());
|
||||||
|
if (!query.exec(queryDeleteString)) {
|
||||||
|
qCWarning(dcLogEngine) << "Error deleting log entries from device" << deviceId.toString() << ". Driver error:" << query.lastError().driverText() << "Database error:" << query.lastError().databaseText();
|
||||||
|
} else {
|
||||||
|
emit logDatabaseUpdated();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void LogEngine::removeRuleLogs(const RuleId &ruleId)
|
||||||
|
{
|
||||||
|
qCDebug(dcLogEngine) << "Deleting log entries from rule" << ruleId.toString();
|
||||||
|
|
||||||
|
QSqlQuery query;
|
||||||
|
QString queryDeleteString = QString("DELETE FROM entries WHERE typeId = '%1';").arg(ruleId.toString());
|
||||||
|
if (!query.exec(queryDeleteString)) {
|
||||||
|
qCWarning(dcLogEngine) << "Error deleting log entries from rule" << ruleId.toString() << ". Driver error:" << query.lastError().driverText() << "Database error:" << query.lastError().databaseText();
|
||||||
|
} else {
|
||||||
|
emit logDatabaseUpdated();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogEngine::appendLogEntry(const LogEntry &entry)
|
void LogEngine::appendLogEntry(const LogEntry &entry)
|
||||||
{
|
{
|
||||||
|
checkDBSize();
|
||||||
|
|
||||||
QString queryString = QString("INSERT INTO entries (timestamp, loggingEventType, loggingLevel, sourceType, typeId, deviceId, value, active, errorCode) values ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9');")
|
QString queryString = QString("INSERT INTO entries (timestamp, loggingEventType, loggingLevel, sourceType, typeId, deviceId, value, active, errorCode) values ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9');")
|
||||||
.arg(entry.timestamp().toTime_t())
|
.arg(entry.timestamp().toTime_t())
|
||||||
.arg(entry.eventType())
|
.arg(entry.eventType())
|
||||||
@ -160,10 +192,37 @@ void LogEngine::appendLogEntry(const LogEntry &entry)
|
|||||||
.arg(entry.errorCode());
|
.arg(entry.errorCode());
|
||||||
|
|
||||||
QSqlQuery query;
|
QSqlQuery query;
|
||||||
query.exec(queryString);
|
if (!query.exec(queryString)) {
|
||||||
|
|
||||||
if (query.lastError().isValid()) {
|
|
||||||
qCWarning(dcLogEngine) << "Error writing log entry. Driver error:" << query.lastError().driverText() << "Database error:" << query.lastError().databaseText();
|
qCWarning(dcLogEngine) << "Error writing log entry. Driver error:" << query.lastError().driverText() << "Database error:" << query.lastError().databaseText();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
emit logEntryAdded(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
void LogEngine::checkDBSize()
|
||||||
|
{
|
||||||
|
QString queryString = "SELECT ROWID FROM entries;";
|
||||||
|
QSqlQuery query;
|
||||||
|
query.exec(queryString);
|
||||||
|
int numRows = 0;
|
||||||
|
if (m_db.driver()->hasFeature(QSqlDriver::QuerySize)) {
|
||||||
|
numRows = query.size();
|
||||||
|
} else {
|
||||||
|
// this can be very slow
|
||||||
|
query.last();
|
||||||
|
numRows = query.at() + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (numRows >= DB_MAX_SIZE) {
|
||||||
|
// keep only the latest DB_MAX_SIZE entries
|
||||||
|
qCDebug(dcLogEngine) << "Deleting oldest entries and keep only the latest" << DB_MAX_SIZE << "entries.";
|
||||||
|
QString queryDeleteString = QString("DELETE FROM entries WHERE ROWID IN (SELECT ROWID FROM entries ORDER BY timestamp DESC LIMIT -1 OFFSET %1);").arg(QString::number(DB_MAX_SIZE));
|
||||||
|
if (!query.exec(queryDeleteString)) {
|
||||||
|
qCWarning(dcLogEngine) << "Error deleting oldest log entries to keep size. Driver error:" << query.lastError().driverText() << "Database error:" << query.lastError().databaseText();
|
||||||
|
} else {
|
||||||
|
emit logDatabaseUpdated();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,7 +244,7 @@ void LogEngine::initDB()
|
|||||||
if (version != DB_SCHEMA_VERSION) {
|
if (version != DB_SCHEMA_VERSION) {
|
||||||
qCWarning(dcLogEngine) << "Log schema version not matching! Schema upgrade not implemented yet. Logging might fail.";
|
qCWarning(dcLogEngine) << "Log schema version not matching! Schema upgrade not implemented yet. Logging might fail.";
|
||||||
} else {
|
} else {
|
||||||
qCDebug(dcLogEngine) << "Log database schema version" << DB_SCHEMA_VERSION << "matches";
|
qCDebug(dcLogEngine) << QString("Log database schema version \"%1\" matches").arg(DB_SCHEMA_VERSION);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
qCWarning(dcLogEngine) << "Broken log database. Version not found in metadata table.";
|
qCWarning(dcLogEngine) << "Broken log database. Version not found in metadata table.";
|
||||||
@ -230,6 +289,7 @@ void LogEngine::initDB()
|
|||||||
qCWarning(dcLogEngine) << "Error creating log table in database. Driver error:" << query.lastError().driverText() << "Database error:" << query.lastError().databaseText();
|
qCWarning(dcLogEngine) << "Error creating log table in database. Driver error:" << query.lastError().driverText() << "Database error:" << query.lastError().databaseText();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
qCDebug(dcLogEngine) << "Initialized logging DB successfully.";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,6 +43,14 @@ public:
|
|||||||
|
|
||||||
signals:
|
signals:
|
||||||
void logEntryAdded(const LogEntry &logEntry);
|
void logEntryAdded(const LogEntry &logEntry);
|
||||||
|
void logDatabaseUpdated();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QSqlDatabase m_db;
|
||||||
|
|
||||||
|
void initDB();
|
||||||
|
void appendLogEntry(const LogEntry &entry);
|
||||||
|
void checkDBSize();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Only GuhCore is allowed to log events.
|
// Only GuhCore is allowed to log events.
|
||||||
@ -52,12 +60,9 @@ private:
|
|||||||
void logAction(const Action &action, Logging::LoggingLevel level = Logging::LoggingLevelInfo, int errorCode = 0);
|
void logAction(const Action &action, Logging::LoggingLevel level = Logging::LoggingLevelInfo, int errorCode = 0);
|
||||||
void logRuleTriggered(const Rule &rule);
|
void logRuleTriggered(const Rule &rule);
|
||||||
void logRuleActiveChanged(const Rule &rule);
|
void logRuleActiveChanged(const Rule &rule);
|
||||||
|
void removeDeviceLogs(const DeviceId &deviceId);
|
||||||
|
void removeRuleLogs(const RuleId &ruleId);
|
||||||
|
|
||||||
private:
|
|
||||||
void initDB();
|
|
||||||
void appendLogEntry(const LogEntry &entry);
|
|
||||||
|
|
||||||
QSqlDatabase m_db;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -60,7 +60,7 @@ int main(int argc, char *argv[])
|
|||||||
s_loggingFilters.insert("Warnings", true);
|
s_loggingFilters.insert("Warnings", true);
|
||||||
s_loggingFilters.insert("DeviceManager", true);
|
s_loggingFilters.insert("DeviceManager", true);
|
||||||
s_loggingFilters.insert("RuleEngine", true);
|
s_loggingFilters.insert("RuleEngine", true);
|
||||||
s_loggingFilters.insert("Connection", false);
|
s_loggingFilters.insert("Connection", true);
|
||||||
s_loggingFilters.insert("JsonRpc", false);
|
s_loggingFilters.insert("JsonRpc", false);
|
||||||
s_loggingFilters.insert("Hardware", false);
|
s_loggingFilters.insert("Hardware", false);
|
||||||
s_loggingFilters.insert("LogEngine", false);
|
s_loggingFilters.insert("LogEngine", false);
|
||||||
|
|||||||
@ -276,7 +276,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Logging.GetLogEntries": {
|
"Logging.GetLogEntries": {
|
||||||
"description": "Get the LogEntries matching the given filter.",
|
"description": "Get the LogEntries matching the given filter. Each list element of a given filter will be connected with OR to each other. Each of the given filters will be connected with AND to each other.",
|
||||||
"params": {
|
"params": {
|
||||||
"o:deviceIds": [
|
"o:deviceIds": [
|
||||||
"Uuid"
|
"Uuid"
|
||||||
@ -457,8 +457,13 @@
|
|||||||
"event": "$ref:Event"
|
"event": "$ref:Event"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"Logging.LogDatabaseUpdated": {
|
||||||
|
"description": "Emitted whenever the database was updated. The database will be updated when a log entry was deleted. A log entry will be deleted when the corresponding device or a rule will be removed, or when the oldest entry of the database was deleted to keep to database in the size limits.",
|
||||||
|
"params": {
|
||||||
|
}
|
||||||
|
},
|
||||||
"Logging.LogEntryAdded": {
|
"Logging.LogEntryAdded": {
|
||||||
"description": "Emitted whenever an entry is appended to the logging system. The filters can be combinend.",
|
"description": "Emitted whenever an entry is appended to the logging system. ",
|
||||||
"params": {
|
"params": {
|
||||||
"logEntry": "$ref:LogEntry"
|
"logEntry": "$ref:LogEntry"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -412,22 +412,13 @@ void TestJSONRPC::ruleActiveChangedNotifications()
|
|||||||
QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
|
QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
|
||||||
|
|
||||||
response = injectAndWait("Rules.AddRule", params);
|
response = injectAndWait("Rules.AddRule", params);
|
||||||
|
|
||||||
clientSpy.wait(500);
|
clientSpy.wait(500);
|
||||||
qDebug() << "got" << clientSpy.count() << "notifications";
|
QVariant notificationVariant = checkNotification(clientSpy, "Rules.RuleAdded");
|
||||||
QCOMPARE(clientSpy.count(), 2);
|
verifyRuleError(response);
|
||||||
|
|
||||||
QJsonDocument jsonDocResponse = QJsonDocument::fromJson(clientSpy.at(1).at(1).toByteArray());
|
QVariantMap notificationRuleMap = notificationVariant.toMap().value("params").toMap().value("rule").toMap();
|
||||||
QJsonDocument jsonDocNotification = QJsonDocument::fromJson(clientSpy.at(0).at(1).toByteArray());
|
RuleId ruleId = RuleId(notificationRuleMap.value("id").toString());
|
||||||
|
|
||||||
verifyRuleError(jsonDocResponse.toVariant());
|
|
||||||
RuleId ruleId = RuleId(jsonDocResponse.toVariant().toMap().value("params").toMap().value("ruleId").toString());
|
|
||||||
QVERIFY(!ruleId.isNull());
|
QVERIFY(!ruleId.isNull());
|
||||||
|
|
||||||
// check the DeviceAdded notification
|
|
||||||
QCOMPARE(jsonDocNotification.toVariant().toMap().value("notification").toString(), QString("Rules.RuleAdded"));
|
|
||||||
QVariantMap notificationRuleMap = jsonDocNotification.toVariant().toMap().value("params").toMap().value("rule").toMap();
|
|
||||||
|
|
||||||
QCOMPARE(notificationRuleMap.value("enabled").toBool(), true);
|
QCOMPARE(notificationRuleMap.value("enabled").toBool(), true);
|
||||||
QCOMPARE(notificationRuleMap.value("name").toString(), params.value("name").toString());
|
QCOMPARE(notificationRuleMap.value("name").toString(), params.value("name").toString());
|
||||||
QCOMPARE(notificationRuleMap.value("id").toString(), ruleId.toString());
|
QCOMPARE(notificationRuleMap.value("id").toString(), ruleId.toString());
|
||||||
@ -438,31 +429,21 @@ void TestJSONRPC::ruleActiveChangedNotifications()
|
|||||||
// set the rule active
|
// set the rule active
|
||||||
QNetworkAccessManager nam;
|
QNetworkAccessManager nam;
|
||||||
QSignalSpy spy(&nam, SIGNAL(finished(QNetworkReply*)));
|
QSignalSpy spy(&nam, SIGNAL(finished(QNetworkReply*)));
|
||||||
QSignalSpy clientSpy2(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
|
|
||||||
|
|
||||||
// state state to 20
|
// state state to 20
|
||||||
qDebug() << "setting mock int state to 20";
|
qDebug() << "setting mock int state to 20";
|
||||||
QNetworkRequest request(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockIntStateId.toString()).arg(20)));
|
QNetworkRequest request(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockIntStateId.toString()).arg(20)));
|
||||||
QNetworkReply *reply = nam.get(request);
|
QNetworkReply *reply = nam.get(request);
|
||||||
spy.wait(500);
|
|
||||||
QCOMPARE(spy.count(), 1);
|
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
|
|
||||||
clientSpy2.wait(500);
|
clientSpy.wait(500);
|
||||||
qDebug() << "got" << clientSpy2.count() << "notifications";
|
notificationVariant = checkNotification(clientSpy, "Rules.RuleActiveChanged");
|
||||||
QCOMPARE(clientSpy2.count(), 6);
|
verifyRuleError(response);
|
||||||
|
|
||||||
for (int i = 0; i < clientSpy2.count(); i++) {
|
QCOMPARE(notificationVariant.toMap().value("params").toMap().value("ruleId").toString(), ruleId.toString());
|
||||||
QByteArray notification = clientSpy2.at(i).at(1).toByteArray();
|
QCOMPARE(notificationVariant.toMap().value("params").toMap().value("active").toBool(), true);
|
||||||
if (notification.contains("Rules.RuleActiveChanged")) {
|
|
||||||
QVariantMap notificationMap = QJsonDocument::fromJson(notification).toVariant().toMap();
|
spy.clear(); clientSpy.clear();
|
||||||
QCOMPARE(notificationMap.value("notification").toString(), QString("Rules.RuleActiveChanged"));
|
|
||||||
QCOMPARE(notificationMap.value("params").toMap().value("ruleId").toString(), ruleId.toString());
|
|
||||||
QCOMPARE(notificationMap.value("params").toMap().value("active").toBool(), true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
clientSpy2.clear();
|
|
||||||
spy.clear();
|
|
||||||
|
|
||||||
// set the rule inactive
|
// set the rule inactive
|
||||||
qDebug() << "setting mock int state to 42";
|
qDebug() << "setting mock int state to 42";
|
||||||
@ -472,39 +453,24 @@ void TestJSONRPC::ruleActiveChangedNotifications()
|
|||||||
QCOMPARE(spy.count(), 1);
|
QCOMPARE(spy.count(), 1);
|
||||||
reply2->deleteLater();
|
reply2->deleteLater();
|
||||||
|
|
||||||
clientSpy2.wait(500);
|
clientSpy.wait(500);
|
||||||
QCOMPARE(clientSpy2.count(), 5);
|
notificationVariant = checkNotification(clientSpy, "Rules.RuleActiveChanged");
|
||||||
|
verifyRuleError(response);
|
||||||
|
|
||||||
for (int i = 0; i < clientSpy2.count(); i++) {
|
QCOMPARE(notificationVariant.toMap().value("params").toMap().value("ruleId").toString(), ruleId.toString());
|
||||||
QByteArray notification = clientSpy2.at(i).at(1).toByteArray();
|
QCOMPARE(notificationVariant.toMap().value("params").toMap().value("active").toBool(), false);
|
||||||
if (notification.contains("Rules.RuleActiveChanged")) {
|
|
||||||
QVariantMap notificationMap = QJsonDocument::fromJson(notification).toVariant().toMap();
|
|
||||||
QCOMPARE(notificationMap.value("notification").toString(), QString("Rules.RuleActiveChanged"));
|
|
||||||
QCOMPARE(notificationMap.value("params").toMap().value("ruleId").toString(), ruleId.toString());
|
|
||||||
QCOMPARE(notificationMap.value("params").toMap().value("active").toBool(), false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup connection to mock client
|
|
||||||
QSignalSpy clientSpy3(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
|
|
||||||
|
|
||||||
// now remove the rule and check the RuleRemoved notification
|
// now remove the rule and check the RuleRemoved notification
|
||||||
params.clear(); response.clear();
|
params.clear(); response.clear();
|
||||||
params.insert("ruleId", ruleId);
|
params.insert("ruleId", ruleId);
|
||||||
response = injectAndWait("Rules.RemoveRule", params);
|
response = injectAndWait("Rules.RemoveRule", params);
|
||||||
|
|
||||||
clientSpy3.wait(500);
|
clientSpy.wait(500);
|
||||||
qDebug() << "got" << clientSpy3.count() << "notifications";
|
notificationVariant = checkNotification(clientSpy, "Rules.RuleRemoved");
|
||||||
QCOMPARE(clientSpy3.count(), 2); // wait for RuleRemoved notification and response
|
checkNotification(clientSpy, "Logging.LogDatabaseUpdated");
|
||||||
|
verifyRuleError(response);
|
||||||
|
|
||||||
jsonDocResponse = QJsonDocument::fromJson(clientSpy3.at(1).at(1).toByteArray());
|
QCOMPARE(notificationVariant.toMap().value("params").toMap().value("ruleId").toString(), ruleId.toString());
|
||||||
jsonDocNotification = QJsonDocument::fromJson(clientSpy3.at(0).at(1).toByteArray());
|
|
||||||
|
|
||||||
verifyRuleError(jsonDocResponse.toVariant());
|
|
||||||
|
|
||||||
// check the DeviceRemoved notification
|
|
||||||
QCOMPARE(jsonDocNotification.toVariant().toMap().value("notification").toString(), QString("Rules.RuleRemoved"));
|
|
||||||
QCOMPARE(jsonDocNotification.toVariant().toMap().value("params").toMap().value("ruleId").toString(), ruleId.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestJSONRPC::deviceParamsChangedNotifications()
|
void TestJSONRPC::deviceParamsChangedNotifications()
|
||||||
@ -519,7 +485,6 @@ void TestJSONRPC::deviceParamsChangedNotifications()
|
|||||||
QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
|
QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
|
||||||
|
|
||||||
// ADD
|
// ADD
|
||||||
|
|
||||||
// add device and wait for notification
|
// add device and wait for notification
|
||||||
QVariantList deviceParams;
|
QVariantList deviceParams;
|
||||||
QVariantMap httpportParam;
|
QVariantMap httpportParam;
|
||||||
@ -527,25 +492,15 @@ void TestJSONRPC::deviceParamsChangedNotifications()
|
|||||||
httpportParam.insert("value", 23234);
|
httpportParam.insert("value", 23234);
|
||||||
deviceParams.append(httpportParam);
|
deviceParams.append(httpportParam);
|
||||||
|
|
||||||
params.clear(); response.clear();
|
params.clear(); response.clear(); clientSpy.clear();
|
||||||
params.insert("deviceClassId", mockDeviceClassId);
|
params.insert("deviceClassId", mockDeviceClassId);
|
||||||
params.insert("deviceParams", deviceParams);
|
params.insert("deviceParams", deviceParams);
|
||||||
response = injectAndWait("Devices.AddConfiguredDevice", params);
|
response = injectAndWait("Devices.AddConfiguredDevice", params);
|
||||||
|
DeviceId deviceId = DeviceId(response.toMap().value("params").toMap().value("deviceId").toString());
|
||||||
// Lets wait for the notification
|
|
||||||
clientSpy.wait(500);
|
|
||||||
QCOMPARE(clientSpy.count(), 2); // wait for device added notification and response
|
|
||||||
|
|
||||||
QJsonDocument jsonDocResponse = QJsonDocument::fromJson(clientSpy.at(1).at(1).toByteArray());
|
|
||||||
QJsonDocument jsonDocNotification = QJsonDocument::fromJson(clientSpy.at(0).at(1).toByteArray());
|
|
||||||
|
|
||||||
verifyDeviceError(jsonDocResponse.toVariant());
|
|
||||||
DeviceId deviceId = DeviceId(jsonDocResponse.toVariant().toMap().value("params").toMap().value("deviceId").toString());
|
|
||||||
QVERIFY(!deviceId.isNull());
|
QVERIFY(!deviceId.isNull());
|
||||||
|
clientSpy.wait(500);
|
||||||
// check the DeviceAdded notification
|
verifyDeviceError(response);
|
||||||
QCOMPARE(jsonDocNotification.toVariant().toMap().value("notification").toString(), QString("Devices.DeviceAdded"));
|
QVariantMap notificationDeviceMap = checkNotification(clientSpy, "Devices.DeviceAdded").toMap().value("params").toMap().value("device").toMap();
|
||||||
QVariantMap notificationDeviceMap = jsonDocNotification.toVariant().toMap().value("params").toMap().value("device").toMap();
|
|
||||||
|
|
||||||
QCOMPARE(notificationDeviceMap.value("deviceClassId").toString(), mockDeviceClassId.toString());
|
QCOMPARE(notificationDeviceMap.value("deviceClassId").toString(), mockDeviceClassId.toString());
|
||||||
QCOMPARE(notificationDeviceMap.value("id").toString(), deviceId.toString());
|
QCOMPARE(notificationDeviceMap.value("id").toString(), deviceId.toString());
|
||||||
@ -556,10 +511,6 @@ void TestJSONRPC::deviceParamsChangedNotifications()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// EDIT
|
// EDIT
|
||||||
|
|
||||||
// Setup connection to mock client
|
|
||||||
QSignalSpy clientSpy2(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
|
|
||||||
|
|
||||||
// now edit the device and check the deviceParamsChanged notification
|
// now edit the device and check the deviceParamsChanged notification
|
||||||
QVariantList newDeviceParams;
|
QVariantList newDeviceParams;
|
||||||
QVariantMap newHttpportParam;
|
QVariantMap newHttpportParam;
|
||||||
@ -567,22 +518,13 @@ void TestJSONRPC::deviceParamsChangedNotifications()
|
|||||||
newHttpportParam.insert("value", 45473);
|
newHttpportParam.insert("value", 45473);
|
||||||
newDeviceParams.append(newHttpportParam);
|
newDeviceParams.append(newHttpportParam);
|
||||||
|
|
||||||
params.clear(); response.clear();
|
params.clear(); response.clear(); clientSpy.clear();
|
||||||
params.insert("deviceId", deviceId);
|
params.insert("deviceId", deviceId);
|
||||||
params.insert("deviceParams", newDeviceParams);
|
params.insert("deviceParams", newDeviceParams);
|
||||||
response = injectAndWait("Devices.EditDevice", params);
|
response = injectAndWait("Devices.EditDevice", params);
|
||||||
|
clientSpy.wait(500);
|
||||||
clientSpy2.wait(500);
|
verifyDeviceError(response);
|
||||||
QCOMPARE(clientSpy2.count(), 2);
|
QVariantMap editDeviceNotificationMap = checkNotification(clientSpy, "Devices.DeviceParamsChanged").toMap().value("params").toMap().value("device").toMap();
|
||||||
|
|
||||||
jsonDocResponse = QJsonDocument::fromJson(clientSpy2.at(1).at(1).toByteArray());
|
|
||||||
jsonDocNotification = QJsonDocument::fromJson(clientSpy2.at(0).at(1).toByteArray());
|
|
||||||
|
|
||||||
verifyDeviceError(jsonDocResponse.toVariant());
|
|
||||||
|
|
||||||
QCOMPARE(jsonDocNotification.toVariant().toMap().value("notification").toString(), QString("Devices.DeviceParamsChanged"));
|
|
||||||
|
|
||||||
QVariantMap editDeviceNotificationMap = jsonDocNotification.toVariant().toMap().value("params").toMap().value("device").toMap();
|
|
||||||
QCOMPARE(editDeviceNotificationMap.value("deviceClassId").toString(), mockDeviceClassId.toString());
|
QCOMPARE(editDeviceNotificationMap.value("deviceClassId").toString(), mockDeviceClassId.toString());
|
||||||
QCOMPARE(editDeviceNotificationMap.value("id").toString(), deviceId.toString());
|
QCOMPARE(editDeviceNotificationMap.value("id").toString(), deviceId.toString());
|
||||||
foreach (const QVariant ¶m, editDeviceNotificationMap.value("params").toList()) {
|
foreach (const QVariant ¶m, editDeviceNotificationMap.value("params").toList()) {
|
||||||
@ -592,26 +534,14 @@ void TestJSONRPC::deviceParamsChangedNotifications()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// REMOVE
|
// REMOVE
|
||||||
|
|
||||||
// Setup connection to mock client
|
|
||||||
QSignalSpy clientSpy3(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
|
|
||||||
|
|
||||||
// now remove the device and check the device removed notification
|
// now remove the device and check the device removed notification
|
||||||
params.clear(); response.clear();
|
params.clear(); response.clear(); clientSpy.clear();
|
||||||
params.insert("deviceId", deviceId);
|
params.insert("deviceId", deviceId);
|
||||||
response = injectAndWait("Devices.RemoveConfiguredDevice", params);
|
response = injectAndWait("Devices.RemoveConfiguredDevice", params);
|
||||||
|
clientSpy.wait(500);
|
||||||
clientSpy3.wait(500);
|
verifyDeviceError(response);
|
||||||
QCOMPARE(clientSpy3.count(), 2); // wait for device removed notification and response
|
checkNotification(clientSpy, "Devices.DeviceRemoved");
|
||||||
|
checkNotification(clientSpy, "Logging.LogDatabaseUpdated");
|
||||||
jsonDocResponse = QJsonDocument::fromJson(clientSpy3.at(1).at(1).toByteArray());
|
|
||||||
jsonDocNotification = QJsonDocument::fromJson(clientSpy3.at(0).at(1).toByteArray());
|
|
||||||
|
|
||||||
verifyDeviceError(jsonDocResponse.toVariant());
|
|
||||||
|
|
||||||
// check the DeviceRemoved notification
|
|
||||||
QCOMPARE(jsonDocNotification.toVariant().toMap().value("notification").toString(), QString("Devices.DeviceRemoved"));
|
|
||||||
QCOMPARE(jsonDocNotification.toVariant().toMap().value("params").toMap().value("deviceId").toString(), deviceId.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#include "testjsonrpc.moc"
|
#include "testjsonrpc.moc"
|
||||||
|
|||||||
Reference in New Issue
Block a user