make it a little more typesafe

This commit is contained in:
Michael Zanetti 2014-04-18 01:48:04 +02:00
parent 53a8498172
commit 6485ebbff5
11 changed files with 41 additions and 19 deletions

View File

@ -311,7 +311,7 @@ QList<Device *> DeviceManager::findConfiguredDevices(const DeviceClassId &device
/*! For conveninece, this returns the \{DeviceClass} with the id given by \a deviceClassId. /*! For conveninece, this returns the \{DeviceClass} with the id given by \a deviceClassId.
Note: The returned DeviceClass may be invalid.*/ Note: The returned DeviceClass may be invalid.*/
DeviceClass DeviceManager::findDeviceClass(const QUuid &deviceClassId) const DeviceClass DeviceManager::findDeviceClass(const DeviceClassId &deviceClassId) const
{ {
foreach (const DeviceClass &deviceClass, m_supportedDevices) { foreach (const DeviceClass &deviceClass, m_supportedDevices) {
if (deviceClass.id() == deviceClassId) { if (deviceClass.id() == deviceClassId) {

View File

@ -78,7 +78,7 @@ public:
Device* findConfiguredDevice(const DeviceId &id) const; Device* findConfiguredDevice(const DeviceId &id) const;
QList<Device*> findConfiguredDevices(const DeviceClassId &deviceClassId) const; QList<Device*> findConfiguredDevices(const DeviceClassId &deviceClassId) const;
DeviceClass findDeviceClass(const QUuid &deviceClassId) const; DeviceClass findDeviceClass(const DeviceClassId &deviceClassId) const;
signals: signals:
void loaded(); void loaded();

View File

@ -105,7 +105,7 @@ void Device::setStates(const QList<State> &states)
m_states = states; m_states = states;
} }
bool Device::hasState(const QUuid &stateTypeId) const bool Device::hasState(const StateTypeId &stateTypeId) const
{ {
foreach (const State &state, m_states) { foreach (const State &state, m_states) {
if (state.stateTypeId() == stateTypeId) { if (state.stateTypeId() == stateTypeId) {
@ -116,7 +116,7 @@ bool Device::hasState(const QUuid &stateTypeId) const
} }
/*! For convenience, this finds the \l{State} matching the given \a stateTypeId and returns the current valie in this Device. */ /*! For convenience, this finds the \l{State} matching the given \a stateTypeId and returns the current valie in this Device. */
QVariant Device::stateValue(const QUuid &stateTypeId) const QVariant Device::stateValue(const StateTypeId &stateTypeId) const
{ {
qDebug() << "device has states:" << m_states.count(); qDebug() << "device has states:" << m_states.count();
foreach (const State &state, m_states) { foreach (const State &state, m_states) {

View File

@ -49,8 +49,8 @@ public:
QList<State> states() const; QList<State> states() const;
void setStates(const QList<State> &states); void setStates(const QList<State> &states);
bool hasState(const QUuid &stateTypeId) const; bool hasState(const StateTypeId &stateTypeId) const;
QVariant stateValue(const QUuid &stateTypeId) const; QVariant stateValue(const StateTypeId &stateTypeId) const;
void setStateValue(const StateTypeId &stateTypeId, const QVariant &value); void setStateValue(const StateTypeId &stateTypeId, const QVariant &value);
signals: signals:

View File

@ -71,6 +71,13 @@ void Event::setParams(const QVariantMap &params)
Events are equal (returns true) if eventTypeId, deviceId and params match. */ Events are equal (returns true) if eventTypeId, deviceId and params match. */
bool Event::operator ==(const Event &other) const bool Event::operator ==(const Event &other) const
{ {
bool result =m_eventTypeId == other.eventTypeId()
&& m_deviceId == other.deviceId()
&& m_params == other.params();
qDebug() << "comparing event" << *this << "with" << other << "result is" << result << "params" << m_params << "other" << other.params();
return m_eventTypeId == other.eventTypeId() return m_eventTypeId == other.eventTypeId()
&& m_deviceId == other.deviceId() && m_deviceId == other.deviceId()
&& m_params == other.params(); && m_params == other.params();

View File

@ -11,6 +11,9 @@ public: \
type##Id(): QUuid() {} \ type##Id(): QUuid() {} \
static type##Id create##type##Id() { return type##Id(QUuid::createUuid().toString()); } \ static type##Id create##type##Id() { return type##Id(QUuid::createUuid().toString()); } \
static type##Id fromUuid(const QUuid &uuid) { return type##Id(uuid.toString()); } \ static type##Id fromUuid(const QUuid &uuid) { return type##Id(uuid.toString()); } \
bool operator==(const type##Id &other) const { \
return toString() == other.toString(); \
} \
}; \ }; \
Q_DECLARE_METATYPE(type##Id); Q_DECLARE_METATYPE(type##Id);
@ -25,4 +28,14 @@ DECLARE_TYPE_ID(StateType)
DECLARE_TYPE_ID(ActionType) DECLARE_TYPE_ID(ActionType)
DECLARE_TYPE_ID(Plugin) DECLARE_TYPE_ID(Plugin)
enum ParamOperand {
ParamOperandEquals,
ParamOperandNotEquals,
ParamOperandLess,
ParamOperandGreater,
ParamOperandLessThan,
ParamOperandGreaterThan
};
#endif // TYPEUTILS_H #endif // TYPEUTILS_H

View File

@ -114,12 +114,10 @@ void DevicePluginLircd::buttonPressed(const QString &remoteName, const QString &
qDebug() << "found remote" << remoteName << supportedDevices().first().events().count(); qDebug() << "found remote" << remoteName << supportedDevices().first().events().count();
foreach (const EventType &eventType, supportedDevices().first().events()) { foreach (const EventType &eventType, supportedDevices().first().events()) {
qDebug() << "checking eventType" << eventType.name() << "with" << buttonName;
if (eventType.name() == buttonName) { if (eventType.name() == buttonName) {
QVariantMap param; QVariantMap param;
param.insert("repeat", repeat); param.insert("repeat", repeat);
Event event(eventType.id(), remote->id(), param); Event event(eventType.id(), remote->id(), param);
qDebug() << "emitting event";
emitEvent(event); emitEvent(event);
} }
} }

View File

@ -35,9 +35,9 @@ void HttpDaemon::incomingConnection(qintptr socket)
} }
void HttpDaemon::actionExecuted(const QUuid &actionTypeId) void HttpDaemon::actionExecuted(const ActionTypeId &actionTypeId)
{ {
m_actionList.append(qMakePair<QUuid, QDateTime>(actionTypeId, QDateTime::currentDateTime())); m_actionList.append(qMakePair<ActionTypeId, QDateTime>(actionTypeId, QDateTime::currentDateTime()));
} }
void HttpDaemon::readClient() void HttpDaemon::readClient()
@ -160,7 +160,7 @@ QString HttpDaemon::generateWebPage()
body.append("<table border=2px>"); body.append("<table border=2px>");
body.append("<tr><td>Name</td><td>Type ID</td><td>Timestamp</td></tr>"); body.append("<tr><td>Name</td><td>Type ID</td><td>Timestamp</td></tr>");
for (int i = 0; i < m_actionList.count(); ++i) { for (int i = 0; i < m_actionList.count(); ++i) {
QUuid actionTypeId = m_actionList.at(i).first; ActionTypeId actionTypeId = ActionTypeId(m_actionList.at(i).first);
QDateTime timestamp = m_actionList.at(i).second; QDateTime timestamp = m_actionList.at(i).second;
QString actionName; QString actionName;
foreach (const ActionType &at, deviceClass.actions()) { foreach (const ActionType &at, deviceClass.actions()) {

View File

@ -18,7 +18,7 @@ public:
void incomingConnection(qintptr socket) override; void incomingConnection(qintptr socket) override;
void actionExecuted(const QUuid&actionTypeId); void actionExecuted(const ActionTypeId &actionTypeId);
signals: signals:
void setState(const StateTypeId &stateTypeId, const QVariant &value); void setState(const StateTypeId &stateTypeId, const QVariant &value);
@ -38,7 +38,7 @@ private:
DevicePlugin *m_plugin; DevicePlugin *m_plugin;
Device *m_device; Device *m_device;
QList<QPair<QUuid, QDateTime> > m_actionList; QList<QPair<ActionTypeId, QDateTime> > m_actionList;
}; };
#endif // HTTPDAEMON_H #endif // HTTPDAEMON_H

View File

@ -332,7 +332,7 @@ JsonReply* DeviceHandler::GetEventTypes(const QVariantMap &params) const
QVariantMap returns; QVariantMap returns;
QVariantList eventList; QVariantList eventList;
DeviceClass deviceClass = GuhCore::instance()->deviceManager()->findDeviceClass(params.value("deviceClassId").toUuid()); DeviceClass deviceClass = GuhCore::instance()->deviceManager()->findDeviceClass(DeviceClassId(params.value("deviceClassId").toString()));
foreach (const EventType &eventType, deviceClass.events()) { foreach (const EventType &eventType, deviceClass.events()) {
eventList.append(JsonTypes::packEventType(eventType)); eventList.append(JsonTypes::packEventType(eventType));
} }
@ -345,7 +345,7 @@ JsonReply* DeviceHandler::GetActionTypes(const QVariantMap &params) const
QVariantMap returns; QVariantMap returns;
QVariantList actionList; QVariantList actionList;
DeviceClass deviceClass = GuhCore::instance()->deviceManager()->findDeviceClass(params.value("deviceClassId").toUuid()); DeviceClass deviceClass = GuhCore::instance()->deviceManager()->findDeviceClass(DeviceClassId(params.value("deviceClassId").toString()));
foreach (const ActionType &actionType, deviceClass.actions()) { foreach (const ActionType &actionType, deviceClass.actions()) {
actionList.append(JsonTypes::packActionType(actionType)); actionList.append(JsonTypes::packActionType(actionType));
} }
@ -358,7 +358,7 @@ JsonReply* DeviceHandler::GetStateTypes(const QVariantMap &params) const
QVariantMap returns; QVariantMap returns;
QVariantList stateList; QVariantList stateList;
DeviceClass deviceClass = GuhCore::instance()->deviceManager()->findDeviceClass(params.value("deviceClassId").toUuid()); DeviceClass deviceClass = GuhCore::instance()->deviceManager()->findDeviceClass(DeviceClassId(params.value("deviceClassId").toString()));
foreach (const StateType &stateType, deviceClass.states()) { foreach (const StateType &stateType, deviceClass.states()) {
stateList.append(JsonTypes::packStateType(stateType)); stateList.append(JsonTypes::packStateType(stateType));
} }
@ -376,12 +376,12 @@ JsonReply* DeviceHandler::GetStateValue(const QVariantMap &params) const
returns.insert("errorMessage", "No such device"); returns.insert("errorMessage", "No such device");
return createReply(returns); return createReply(returns);
} }
if (!device->hasState(params.value("stateTypeId").toUuid())) { if (!device->hasState(StateTypeId(params.value("stateTypeId").toString()))) {
returns.insert("success", false); returns.insert("success", false);
returns.insert("errorMessage", QString("Device %1 %2 doesn't have such a state.").arg(device->name()).arg(device->id().toString())); returns.insert("errorMessage", QString("Device %1 %2 doesn't have such a state.").arg(device->name()).arg(device->id().toString()));
return createReply(returns); return createReply(returns);
} }
QVariant stateValue = device->stateValue(params.value("stateTypeId").toUuid()); QVariant stateValue = device->stateValue(StateTypeId(params.value("stateTypeId").toString()));
returns.insert("success", true); returns.insert("success", true);
returns.insert("errorMessage", ""); returns.insert("errorMessage", "");

View File

@ -26,6 +26,10 @@ LIBS += -L../plugins/deviceplugins/openweathermap -lguh_devicepluginopenweatherm
LIBS += -L../plugins/deviceplugins/lircd -lguh_devicepluginlircd LIBS += -L../plugins/deviceplugins/lircd -lguh_devicepluginlircd
boblight { boblight {
LIBS += -L../plugins/deviceplugins/boblight -lguh_devicepluginboblight -L/usr/local/lib/ -lboblight xcompile {
LIBS += -L../plugins/deviceplugins/boblight -lguh_devicepluginboblight -lboblight
} else {
LIBS += -L../plugins/deviceplugins/boblight -lguh_devicepluginboblight -L/usr/local/lib/ -lboblight
}
DEFINES += USE_BOBLIGHT DEFINES += USE_BOBLIGHT
} }