From b73eae288f8c0800b0e073a1047c9362dfbc0da4 Mon Sep 17 00:00:00 2001 From: Stefan Persson Date: Wed, 11 Sep 2013 14:32:33 +0200 Subject: [PATCH] Possiblity to add sensors from TelldusCenter to be visible in Telldus Live! This closes #186. --- telldus-gui/Plugins/Live/LiveMessageToken.cpp | 8 +++ telldus-gui/Plugins/Live/LiveMessageToken.h | 2 + telldus-gui/Plugins/Live/__init__.js | 61 +++++++++++++++++- telldus-gui/Plugins/Sensors/SensorList.qml | 12 +++- telldus-gui/Plugins/Sensors/SensorView.qml | 16 ++++- telldus-gui/Plugins/Sensors/__init__.js | 62 +++++++++++++++++-- telldus-gui/Plugins/Sensors/sensor.cpp | 12 +++- telldus-gui/Plugins/Sensors/sensor.h | 4 ++ 8 files changed, 168 insertions(+), 9 deletions(-) diff --git a/telldus-gui/Plugins/Live/LiveMessageToken.cpp b/telldus-gui/Plugins/Live/LiveMessageToken.cpp index 9c64f6d0..8ef4f84f 100644 --- a/telldus-gui/Plugins/Live/LiveMessageToken.cpp +++ b/telldus-gui/Plugins/Live/LiveMessageToken.cpp @@ -166,3 +166,11 @@ void LiveMessageTokenScriptWrapper::set(const QString &key, const QString &value token.stringVal = value; p_token.dictVal[key] = token; } + +void LiveMessageTokenScriptWrapper::set(const QString &key, const QDateTime value) { + p_token.valueType = LiveMessageToken::Dictionary; + LiveMessageToken token; + token.valueType = LiveMessageToken::Int; + token.intVal = value.toMSecsSinceEpoch(); + p_token.dictVal[key] = token; +} diff --git a/telldus-gui/Plugins/Live/LiveMessageToken.h b/telldus-gui/Plugins/Live/LiveMessageToken.h index a94f476d..5806ecfe 100644 --- a/telldus-gui/Plugins/Live/LiveMessageToken.h +++ b/telldus-gui/Plugins/Live/LiveMessageToken.h @@ -2,6 +2,7 @@ #define LIVEMESSAGETOKEN_H #include +#include #include #include #include @@ -42,6 +43,7 @@ public slots: void set(const QString &key, int value); void set(const QString &key, const QString &value); + void set(const QString &key, const QDateTime value); private: LiveMessageToken p_token; diff --git a/telldus-gui/Plugins/Live/__init__.js b/telldus-gui/Plugins/Live/__init__.js index 1fbb20f0..0ebaec0c 100644 --- a/telldus-gui/Plugins/Live/__init__.js +++ b/telldus-gui/Plugins/Live/__init__.js @@ -69,6 +69,10 @@ com.telldus.live = function() { } } + function isRegisteredToLive(){ + return isRegistered; + } + function registered(msg) { if (menuId > 0) { com.telldus.systray.removeMenuItem(menuId); @@ -115,6 +119,58 @@ com.telldus.live = function() { socket.sendMessage(msg); } + function sendSensorsReport(sensorList) { + if (!isRegistered) { + return; + } + msg = new LiveMessage("SensorsReport"); + + list = new LiveMessageToken(); + for( i in sensorList ) { + sensorFrame = new LiveMessageToken(); + sensor = new LiveMessageToken(); + sensor.set('name', sensorList[i].name); + sensor.set('protocol', sensorList[i].protocol); + sensor.set('model', sensorList[i].model); + sensor.set('sensor_id', sensorList[i].id); + var valueList = new LiveMessageToken(); + for( j in sensorList[i].values ){ + valueElement = new LiveMessageToken() + valueElement.set('type', sensorList[i].values[j].type); + valueElement.set('lastUp', sensorList[i].values[j].lastUpdated); + valueElement.set('value', sensorList[i].values[j].value); + valueList.add(valueElement); + } + sensorFrame.add(sensor); + sensorFrame.add(valueList); + list.add(sensorFrame); + } + msg.appendToken(list); + socket.sendMessage(msg); + } + + function sendSensorValues(sensor, sensorvalues){ + if (!isRegistered) { + return; + } + msg = new LiveMessage("SensorEvent"); + sensortoken = new LiveMessageToken(); + sensortoken.set('protocol', sensor.protocol); + sensortoken.set('model', sensor.model); + sensortoken.set('sensor_id', sensor.id); + var valueList = new LiveMessageToken(); + for( j=0; j 0){ + com.telldus.live.sendSensorsReport(liveSensors); + } + } + function sensorEvent(protocol, model, id, dataType, value, timestamp, avoidSave) { var sensor = 0; @@ -222,6 +268,12 @@ com.telldus.sensors = function() { } sensor.setValue(dataType, value, timestamp); + if (sensor.sendToLive){ + sensorValues = pickSensorValues(sensor) + if (allValuesUpdated(sensorValues)) { + com.telldus.live.sendSensorValues(sensor, sensorValues); + } + } if(!avoidSave){ saveSensorModel(); diff --git a/telldus-gui/Plugins/Sensors/sensor.cpp b/telldus-gui/Plugins/Sensors/sensor.cpp index 64b730fd..9723b2d8 100644 --- a/telldus-gui/Plugins/Sensors/sensor.cpp +++ b/telldus-gui/Plugins/Sensors/sensor.cpp @@ -5,7 +5,7 @@ class Sensor::PrivateData { public: - bool hasTemperature, hasHumidity, hasRainRate, hasRainTotal, hasWindDirection, hasWindAverage, hasWindGust, showInList; + bool hasTemperature, hasHumidity, hasRainRate, hasRainTotal, hasWindDirection, hasWindAverage, hasWindGust, showInList, sendToLive; int id; QString model, name, protocol; QDateTime lastUpdated; @@ -18,6 +18,7 @@ Sensor::Sensor(QObject *parent) : d = new PrivateData; d->id = 0; d->showInList = false; + d->sendToLive = false; } Sensor::~Sensor() { @@ -131,3 +132,12 @@ void Sensor::setShowInList(bool show){ d->showInList = show; emit showInListChanged(); } + +bool Sensor::sendToLive() const{ + return d->sendToLive; +} + +void Sensor::setSendToLive(bool send){ + d->sendToLive = send; + emit sendToLiveChanged(); +} diff --git a/telldus-gui/Plugins/Sensors/sensor.h b/telldus-gui/Plugins/Sensors/sensor.h index 82a32f81..66615f24 100644 --- a/telldus-gui/Plugins/Sensors/sensor.h +++ b/telldus-gui/Plugins/Sensors/sensor.h @@ -23,6 +23,7 @@ class Sensor : public QObject Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(QString protocol READ protocol WRITE setProtocol NOTIFY protocolChanged) Q_PROPERTY(bool showInList READ showInList WRITE setShowInList NOTIFY showInListChanged) + Q_PROPERTY(bool sendToLive READ sendToLive WRITE setSendToLive NOTIFY sendToLiveChanged) public: explicit Sensor(QObject *parent = 0); @@ -48,11 +49,13 @@ public: bool hasWindAverage() const; bool hasWindGust() const; bool showInList() const; + bool sendToLive() const; Q_INVOKABLE QObject *sensorValue(int type); Q_INVOKABLE void setValue(int type, const QString &value, const QDateTime ×tamp); Q_INVOKABLE void setName(const QString &name); Q_INVOKABLE void setShowInList(bool show); + Q_INVOKABLE void setSendToLive(bool send); signals: void idChanged(); @@ -67,6 +70,7 @@ signals: void nameChanged(); void protocolChanged(); void showInListChanged(); + void sendToLiveChanged(); private: class PrivateData;