JsonRPCServer::JsonRPCServer(QObject *parent): JsonHandler(parent), #ifdef TESTING_ENABLED m_tcpServer(new MockTcpServer(this)), #else m_tcpServer(new TcpServer(this)), #endif m_notificationId(0) { // First, define our own JSONRPC methods QVariantMap returns; QVariantMap params; params.clear(); returns.clear(); setDescription("Introspect", "Introspect this API."); setParams("Introspect", params); returns.insert("methods", JsonTypes::basicTypeToString(JsonTypes::Object)); returns.insert("types", JsonTypes::basicTypeToString(JsonTypes::Object)); setReturns("Introspect", returns); params.clear(); returns.clear(); setDescription("Version", "Version of this Guh/JSONRPC interface."); setParams("Version", params); returns.insert("version", JsonTypes::basicTypeToString(JsonTypes::String)); returns.insert("protocol version", JsonTypes::basicTypeToString(JsonTypes::String)); setReturns("Version", returns); params.clear(); returns.clear(); setDescription("SetNotificationStatus", "Enable/Disable notifications for this connections."); params.insert("enabled", JsonTypes::basicTypeToString(JsonTypes::Bool)); setParams("SetNotificationStatus", params); returns.insert("enabled", JsonTypes::basicTypeToString(JsonTypes::Bool)); setReturns("SetNotificationStatus", returns); // Now set up the logic connect(m_tcpServer, SIGNAL(clientConnected(const QUuid &)), this, SLOT(clientConnected(const QUuid &))); connect(m_tcpServer, SIGNAL(clientDisconnected(const QUuid &)), this, SLOT(clientDisconnected(const QUuid &))); connect(m_tcpServer, SIGNAL(dataAvailable(const QUuid &, QByteArray)), this, SLOT(processData(const QUuid &, QByteArray))); m_tcpServer->startServer(); QMetaObject::invokeMethod(this, "setup", Qt::QueuedConnection); }
StateHandler::StateHandler(QObject *parent) : JsonHandler(parent) { QVariantMap params; QVariantMap returns; params.clear(); returns.clear(); setDescription("GetStateType", "Get the StateType for the given stateTypeId."); params.insert("stateTypeId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("GetStateType", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); returns.insert("o:stateType", JsonTypes::stateTypeRef()); setReturns("GetStateType", returns); }
LoggingHandler::LoggingHandler(QObject *parent) : JsonHandler(parent) { QVariantMap params; QVariantMap returns; 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:endDate", JsonTypes::basicTypeToString(JsonTypes::Int)); params.insert("o:timeFilters", QVariantList() << timeFilter); params.insert("o:loggingSources", QVariantList() << JsonTypes::loggingSourceRef()); params.insert("o:loggingLevels", QVariantList() << JsonTypes::loggingLevelRef()); params.insert("o:eventTypes", QVariantList() << JsonTypes::loggingEventTypeRef()); params.insert("o:typeIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("o:deviceIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("o:values", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Variant)); setParams("GetLogEntries", params); returns.insert("loggingError", JsonTypes::loggingErrorRef()); returns.insert("o:logEntries", QVariantList() << JsonTypes::logEntryRef()); 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::logDatabaseUpdated, this, &LoggingHandler::logDatabaseUpdated); }
/*! Constructs a new \l EventHandler with the given \a parent. */ EventHandler::EventHandler(QObject *parent) : JsonHandler(parent) { QVariantMap params; QVariantMap returns; // Notifications params.clear(); returns.clear(); setDescription("EventTriggered", "Emitted whenever an Event is triggered."); params.insert("event", JsonTypes::eventRef()); setParams("EventTriggered", params); params.clear(); returns.clear(); setDescription("GetEventType", "Get the EventType for the given eventTypeId."); params.insert("eventTypeId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("GetEventType", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); returns.insert("o:eventType", JsonTypes::eventTypeRef()); setReturns("GetEventType", returns); connect(GuhCore::instance(), &GuhCore::eventTriggered, this, &EventHandler::eventTriggered); }
RulesHandler::RulesHandler(QObject *parent) : JsonHandler(parent) { QVariantMap params; QVariantMap returns; params.clear(); returns.clear(); setDescription("GetRules", "Get the descriptions of all configured rules. If you need more information about a specific rule use the " "method Rules.GetRuleDetails."); setParams("GetRules", params); returns.insert("ruleDescriptions", QVariantList() << JsonTypes::ruleDescriptionRef()); setReturns("GetRules", returns); params.clear(); returns.clear(); setDescription("GetRuleDetails", "Get details for the rule identified by ruleId"); params.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("GetRuleDetails", params); returns.insert("o:rule", JsonTypes::ruleRef()); returns.insert("ruleError", JsonTypes::ruleErrorRef()); setReturns("GetRuleDetails", returns); params.clear(); returns.clear(); setDescription("AddRule", "Add a rule. You can describe rules by one or many EventDesciptors and a StateEvaluator. Note that only " "one of either eventDescriptor or eventDescriptorList may be passed at a time. A rule can be created but left disabled, " "meaning it won't actually be executed until set to enabled. If not given, enabled defaults to true."); params.insert("o:eventDescriptor", JsonTypes::eventDescriptorRef()); params.insert("o:eventDescriptorList", QVariantList() << JsonTypes::eventDescriptorRef()); params.insert("o:stateEvaluator", JsonTypes::stateEvaluatorRef()); params.insert("o:exitActions", QVariantList() << JsonTypes::ruleActionRef()); params.insert("o:enabled", JsonTypes::basicTypeToString(JsonTypes::Bool)); params.insert("o:executable", JsonTypes::basicTypeToString(JsonTypes::Bool)); params.insert("name", JsonTypes::basicTypeToString(JsonTypes::String)); QVariantList actions; actions.append(JsonTypes::ruleActionRef()); params.insert("actions", actions); setParams("AddRule", params); returns.insert("ruleError", JsonTypes::ruleErrorRef()); returns.insert("o:ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setReturns("AddRule", returns); params.clear(); returns.clear(); actions.clear(); setDescription("EditRule", "Edit the parameters of a rule. The configuration of the rule with the given ruleId " "will be replaced with the new given configuration. In ordert to enable or disable a Rule, please use the " "methods \"Rules.EnableRule\" and \"Rules.DisableRule\". If successfull, the notification \"Rule.RuleConfigurationChanged\" " "will be emitted."); params.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("name", JsonTypes::basicTypeToString(JsonTypes::String)); params.insert("o:eventDescriptor", JsonTypes::eventDescriptorRef()); params.insert("o:eventDescriptorList", QVariantList() << JsonTypes::eventDescriptorRef()); params.insert("o:stateEvaluator", JsonTypes::stateEvaluatorRef()); params.insert("o:exitActions", QVariantList() << JsonTypes::ruleActionRef()); params.insert("o:enabled", JsonTypes::basicTypeToString(JsonTypes::Bool)); params.insert("o:executable", JsonTypes::basicTypeToString(JsonTypes::Bool)); actions.append(JsonTypes::ruleActionRef()); params.insert("actions", actions); setParams("EditRule", params); returns.insert("ruleError", JsonTypes::ruleErrorRef()); returns.insert("o:rule", JsonTypes::ruleRef()); setReturns("EditRule", returns); params.clear(); returns.clear(); setDescription("RemoveRule", "Remove a rule"); params.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("RemoveRule", params); returns.insert("ruleError", JsonTypes::ruleErrorRef()); setReturns("RemoveRule", returns); params.clear(); returns.clear(); setDescription("FindRules", "Find a list of rules containing any of the given parameters."); params.insert("deviceId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("FindRules", params); returns.insert("ruleIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Uuid)); setReturns("FindRules", returns); params.clear(); returns.clear(); setDescription("EnableRule", "Enabled a rule that has previously been disabled." "If successfull, the notification \"Rule.RuleConfigurationChanged\" will be emitted."); params.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("EnableRule", params); returns.insert("ruleError", JsonTypes::ruleErrorRef()); setReturns("EnableRule", returns); params.clear(); returns.clear(); setDescription("DisableRule", "Disable a rule. The rule won't be triggered by it's events or state changes while it is disabled. " "If successfull, the notification \"Rule.RuleConfigurationChanged\" will be emitted."); params.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("DisableRule", params); returns.insert("ruleError", JsonTypes::ruleErrorRef()); setReturns("DisableRule", returns); params.clear(); returns.clear(); setDescription("ExecuteActions", "Execute the action list of the rule with the given ruleId."); params.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("ExecuteActions", params); returns.insert("ruleError", JsonTypes::ruleErrorRef()); setReturns("ExecuteActions", returns); params.clear(); returns.clear(); setDescription("ExecuteExitActions", "Execute the exit action list of the rule with the given ruleId."); params.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("ExecuteExitActions", params); returns.insert("ruleError", JsonTypes::ruleErrorRef()); setReturns("ExecuteExitActions", returns); // Notifications params.clear(); returns.clear(); setDescription("RuleRemoved", "Emitted whenever a Rule was removed."); params.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("RuleRemoved", params); params.clear(); returns.clear(); setDescription("RuleAdded", "Emitted whenever a Rule was added."); params.insert("rule", JsonTypes::ruleRef()); setParams("RuleAdded", params); params.clear(); returns.clear(); setDescription("RuleActiveChanged", "Emitted whenever the active state of a Rule changed."); params.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("active", JsonTypes::basicTypeToString(JsonTypes::Bool)); setParams("RuleActiveChanged", params); params.clear(); returns.clear(); setDescription("RuleConfigurationChanged", "Emitted whenever the configuration of a Rule changed."); params.insert("rule", JsonTypes::ruleRef()); setParams("RuleConfigurationChanged", params); connect(GuhCore::instance(), &GuhCore::ruleAdded, this, &RulesHandler::ruleAddedNotification); connect(GuhCore::instance(), &GuhCore::ruleRemoved, this, &RulesHandler::ruleRemovedNotification); connect(GuhCore::instance(), &GuhCore::ruleActiveChanged, this, &RulesHandler::ruleActiveChangedNotification); connect(GuhCore::instance(), &GuhCore::ruleConfigurationChanged, this, &RulesHandler::ruleConfigurationChangedNotification); }
DeviceHandler::DeviceHandler(QObject *parent) : JsonHandler(parent) { QVariantMap returns; QVariantMap params; params.clear(); returns.clear(); setDescription("GetSupportedVendors", "Returns a list of supported Vendors."); setParams("GetSupportedVendors", params); QVariantList vendors; vendors.append(JsonTypes::vendorRef()); returns.insert("vendors", vendors); setReturns("GetSupportedVendors", returns); params.clear(); returns.clear(); setDescription("GetSupportedDevices", "Returns a list of supported Device classes, optionally filtered by vendorId."); params.insert("o:vendorId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("GetSupportedDevices", params); QVariantList deviceClasses; deviceClasses.append(JsonTypes::deviceClassRef()); returns.insert("deviceClasses", deviceClasses); setReturns("GetSupportedDevices", returns); params.clear(); returns.clear(); setDescription("GetPlugins", "Returns a list of loaded plugins."); setParams("GetPlugins", params); QVariantList plugins; plugins.append(JsonTypes::pluginRef()); returns.insert("plugins", plugins); setReturns("GetPlugins", returns); params.clear(); returns.clear(); setDescription("GetPluginConfiguration", "Get a plugin's params."); params.insert("pluginId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("GetPluginConfiguration", params); QVariantList pluginParams; pluginParams.append(JsonTypes::paramRef()); returns.insert("deviceError", JsonTypes::deviceErrorRef()); returns.insert("o:configuration", pluginParams); setReturns("GetPluginConfiguration", returns); params.clear(); returns.clear(); setDescription("SetPluginConfiguration", "Set a plugin's params."); params.insert("pluginId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("configuration", pluginParams); setParams("SetPluginConfiguration", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); setReturns("SetPluginConfiguration", returns); params.clear(); returns.clear(); setDescription("AddConfiguredDevice", "Add a configured device with a setupMethod of SetupMethodJustAdd. " "For devices with a setupMethod different than SetupMethodJustAdd, use PairDevice. " "Use deviceDescriptorId or deviceParams, depending on the createMethod of the device class. " "CreateMethodJustAdd takes the parameters you want to have with that device. " "CreateMethodDiscovery requires the use of a deviceDescriptorId." ); params.insert("deviceClassId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("o:deviceDescriptorId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); QVariantList deviceParams; deviceParams.append(JsonTypes::paramRef()); params.insert("o:deviceParams", deviceParams); setParams("AddConfiguredDevice", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); returns.insert("o:deviceId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setReturns("AddConfiguredDevice", returns); returns.clear(); // Reused params from above! setDescription("PairDevice", "Pair a device. " "Use this for DeviceClasses with a setupMethod different than SetupMethodJustAdd. " "Use deviceDescriptorId or deviceParams, depending on the createMethod of the device class. " "CreateMethodJustAdd takes the parameters you want to have with that device. " "CreateMethodDiscovery requires the use of a deviceDescriptorId. " "If success is true, the return values will contain a pairingTransactionId, a displayMessage and " "the setupMethod. Depending on the setupMethod you should either proceed with AddConfiguredDevice " "or PairDevice." ); setParams("PairDevice", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); returns.insert("o:pairingTransactionId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); returns.insert("o:displayMessage", JsonTypes::basicTypeToString(JsonTypes::String)); returns.insert("o:setupMethod", JsonTypes::setupMethodRef()); setReturns("PairDevice", returns); params.clear(); returns.clear(); setDescription("ConfirmPairing", "Confirm an ongoing pairing. In case of SetupMethodEnterPin also provide the pin in the params."); params.insert("pairingTransactionId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("o:secret", JsonTypes::basicTypeToString(JsonTypes::String)); setParams("ConfirmPairing", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); returns.insert("o:deviceId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setReturns("ConfirmPairing", returns); params.clear(); returns.clear(); setDescription("GetConfiguredDevices", "Returns a list of configured devices."); setParams("GetConfiguredDevices", params); QVariantList devices; devices.append(JsonTypes::deviceRef()); returns.insert("devices", devices); setReturns("GetConfiguredDevices", returns); params.clear(); returns.clear(); setDescription("GetDiscoveredDevices", "Performs a device discovery and returns the results. This function may take a while to return."); params.insert("deviceClassId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); QVariantList discoveryParams; discoveryParams.append(JsonTypes::paramRef()); params.insert("o:discoveryParams", discoveryParams); setParams("GetDiscoveredDevices", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); QVariantList deviceDescriptors; deviceDescriptors.append(JsonTypes::deviceDescriptorRef()); returns.insert("o:deviceDescriptors", deviceDescriptors); setReturns("GetDiscoveredDevices", returns); params.clear(); returns.clear(); setDescription("EditDevice", "Edit the parameters of a device. The device params will be set to the " "passed parameters and the setup device will be called. If the device is discoverable, " "you can perform a GetDiscoveredDevices before calling this method and pass " "the new DeviceDescriptor (rediscover). If a parameter is not writable, you will find a " "'readOnly': true in the ParamType. By default, every Param is writable."); params.insert("deviceId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("o:deviceDescriptorId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); QVariantList newDeviceParams; newDeviceParams.append(JsonTypes::paramRef()); params.insert("o:deviceParams", newDeviceParams); setParams("EditDevice", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); setReturns("EditDevice", returns); params.clear(); returns.clear(); setDescription("RemoveConfiguredDevice", "Remove a device from the system."); params.insert("deviceId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); QVariantList removePolicyList; QVariantMap policy; policy.insert("ruleId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); policy.insert("policy", JsonTypes::removePolicyRef()); removePolicyList.append(policy); params.insert("o:removePolicy", JsonTypes::removePolicyRef()); params.insert("o:removePolicyList", removePolicyList); setParams("RemoveConfiguredDevice", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); returns.insert("o:ruleIds", QVariantList() << JsonTypes::basicTypeToString(JsonTypes::Uuid)); setReturns("RemoveConfiguredDevice", returns); params.clear(); returns.clear(); setDescription("GetEventTypes", "Get event types for a specified deviceClassId."); params.insert("deviceClassId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("GetEventTypes", params); QVariantList events; events.append(JsonTypes::eventTypeRef()); returns.insert("eventTypes", events); setReturns("GetEventTypes", returns); params.clear(); returns.clear(); setDescription("GetActionTypes", "Get action types for a specified deviceClassId."); params.insert("deviceClassId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("GetActionTypes", params); QVariantList actions; actions.append(JsonTypes::actionTypeRef()); returns.insert("actionTypes", actions); setReturns("GetActionTypes", returns); params.clear(); returns.clear(); setDescription("GetStateTypes", "Get state types for a specified deviceClassId."); params.insert("deviceClassId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("GetStateTypes", params); QVariantList states; states.append(JsonTypes::stateTypeRef()); returns.insert("stateTypes", states); setReturns("GetStateTypes", returns); params.clear(); returns.clear(); setDescription("GetStateValue", "Get the value of the given device and the given stateType"); params.insert("deviceId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("stateTypeId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("GetStateValue", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); returns.insert("o:value", JsonTypes::basicTypeToString(JsonTypes::Variant)); setReturns("GetStateValue", returns); params.clear(); returns.clear(); setDescription("GetStateValues", "Get all the state values of the given device."); params.insert("deviceId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("GetStateValues", params); returns.insert("deviceError", JsonTypes::deviceErrorRef()); states.clear(); QVariantMap state; state.insert("stateTypeId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); state.insert("value", JsonTypes::basicTypeToString(JsonTypes::Variant)); states.append(state); returns.insert("o:values", states); setReturns("GetStateValues", returns); // Notifications params.clear(); returns.clear(); setDescription("StateChanged", "Emitted whenever a State of a device changes."); params.insert("deviceId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("stateTypeId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); params.insert("value", JsonTypes::basicTypeToString(JsonTypes::Variant)); setParams("StateChanged", params); params.clear(); returns.clear(); setDescription("DeviceRemoved", "Emitted whenever a Device was removed."); params.insert("deviceId", JsonTypes::basicTypeToString(JsonTypes::Uuid)); setParams("DeviceRemoved", params); params.clear(); returns.clear(); setDescription("DeviceAdded", "Emitted whenever a Device was added."); params.insert("device", JsonTypes::deviceRef()); setParams("DeviceAdded", params); params.clear(); returns.clear(); setDescription("DeviceParamsChanged", "Emitted whenever the params of a Device changed (by editing or rediscovering)."); params.insert("device", JsonTypes::deviceRef()); setParams("DeviceParamsChanged", params); connect(GuhCore::instance(), &GuhCore::deviceStateChanged, this, &DeviceHandler::deviceStateChanged); connect(GuhCore::instance(), &GuhCore::deviceRemoved, this, &DeviceHandler::deviceRemovedNotification); connect(GuhCore::instance(), &GuhCore::deviceAdded, this, &DeviceHandler::deviceAddedNotification); connect(GuhCore::instance(), &GuhCore::deviceParamsChanged, this, &DeviceHandler::deviceParamsChangedNotification); connect(GuhCore::instance(), &GuhCore::devicesDiscovered, this, &DeviceHandler::devicesDiscovered, Qt::QueuedConnection); connect(GuhCore::instance(), &GuhCore::deviceSetupFinished, this, &DeviceHandler::deviceSetupFinished); connect(GuhCore::instance(), &GuhCore::deviceEditFinished, this, &DeviceHandler::deviceEditFinished); connect(GuhCore::instance(), &GuhCore::pairingFinished, this, &DeviceHandler::pairingFinished); }