Possiblity to add sensors from TelldusCenter to be visible in Telldus Live! This closes #186.

This commit is contained in:
Stefan Persson 2013-09-11 14:32:33 +02:00
parent 103eaf05e0
commit b73eae288f
8 changed files with 168 additions and 9 deletions

View file

@ -166,3 +166,11 @@ void LiveMessageTokenScriptWrapper::set(const QString &key, const QString &value
token.stringVal = value; token.stringVal = value;
p_token.dictVal[key] = token; 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;
}

View file

@ -2,6 +2,7 @@
#define LIVEMESSAGETOKEN_H #define LIVEMESSAGETOKEN_H
#include <QByteArray> #include <QByteArray>
#include <QDateTime>
#include <QString> #include <QString>
#include <QHash> #include <QHash>
#include <QMetaType> #include <QMetaType>
@ -42,6 +43,7 @@ public slots:
void set(const QString &key, int value); void set(const QString &key, int value);
void set(const QString &key, const QString &value); void set(const QString &key, const QString &value);
void set(const QString &key, const QDateTime value);
private: private:
LiveMessageToken p_token; LiveMessageToken p_token;

View file

@ -69,6 +69,10 @@ com.telldus.live = function() {
} }
} }
function isRegisteredToLive(){
return isRegistered;
}
function registered(msg) { function registered(msg) {
if (menuId > 0) { if (menuId > 0) {
com.telldus.systray.removeMenuItem(menuId); com.telldus.systray.removeMenuItem(menuId);
@ -115,6 +119,58 @@ com.telldus.live = function() {
socket.sendMessage(msg); 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<sensorvalues.length; j++ ) {
valueElement = new LiveMessageToken()
valueElement.set('type', sensorvalues[j].type);
valueElement.set('lastUp', sensorvalues[j].lastUpdated);
valueElement.set('value', sensorvalues[j].value);
valueList.add(valueElement);
}
msg.appendToken(sensortoken);
msg.appendToken(valueList);
socket.sendMessage(msg);
}
function errorChanged(msg) { function errorChanged(msg) {
configUI.findChild('errorLabel').text = msg; configUI.findChild('errorLabel').text = msg;
} }
@ -124,7 +180,10 @@ com.telldus.live = function() {
} }
return { //Public functions return { //Public functions
init:init init:init,
sendSensorsReport:sendSensorsReport,
isRegisteredToLive:isRegisteredToLive,
sendSensorValues:sendSensorValues
} }
}(); }();

View file

@ -41,11 +41,19 @@ Column {
HeaderTitle { HeaderTitle {
id: visibleinlistTitle id: visibleinlistTitle
text: qsTr("Visible in list") text: qsTr("Visible in list")
anchors.right: sensorinformationTitle.left anchors.right: sendtoliveTitle.left
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
visible: main.state == "EDIT" visible: main.state == "EDIT"
width: 100 width: 100
} }
HeaderTitle {
id: sendtoliveTitle
text: qsTr("Send to Telldus Live!")
anchors.right: sensorinformationTitle.left
horizontalAlignment: Text.AlignHCenter
visible: main.state == "EDIT"
width: 150
}
HeaderTitle { HeaderTitle {
id: sensorinformationTitle id: sensorinformationTitle
text: qsTr("Sensor information") text: qsTr("Sensor information")
@ -72,9 +80,11 @@ Column {
onClicked: { onClicked: {
if(main.state == "VIEW"){ if(main.state == "VIEW"){
main.state = "EDIT" main.state = "EDIT"
sendSensorReport.callWith(1);
} }
else{ else{
main.state = "VIEW" main.state = "VIEW"
sendSensorReport.callWith(2);
} }
} }
} }

View file

@ -83,7 +83,7 @@ Item{
id: visibleinlistcheckbox id: visibleinlistcheckbox
height: 40 height: 40
width: 100 width: 100
anchors.right: sensorInfo.left anchors.right: sendtolivecheckbox.left
CheckBox { CheckBox {
id: checkBox id: checkBox
anchors.centerIn: parent anchors.centerIn: parent
@ -93,6 +93,20 @@ Item{
onClicked: modelData.setShowInList(!modelData.showInList) onClicked: modelData.setShowInList(!modelData.showInList)
} }
} }
Item {
id: sendtolivecheckbox
height: 40
width: 150
anchors.right: sensorInfo.left
CheckBox {
id: checkBoxSTL
anchors.centerIn: parent
width: checkBoxSTL.height
visible: sensorViewItem.state == "EDIT"
checked: modelData.sendToLive
onClicked: modelData.setSendToLive(!modelData.sendToLive)
}
}
Column { Column {
id: sensorInfo id: sensorInfo

View file

@ -11,7 +11,7 @@ com.telldus.sensors = function() {
var sensorData = 0; var sensorData = 0;
sensorList = loadSensorModel(); sensorList = loadSensorModel();
sensorList.rowsRemoved.connect(function(){saveSensorModel();}); sensorList.rowsRemoved.connect(function(){saveSensorModel();});
sensorList.rowsInserted.connect(function(){saveSensorModel();}); sensorList.rowsInserted.connect(function(){saveSensorModel(); sendSensorReport();});
while(sensorData = com.telldus.core.sensor()) { while(sensorData = com.telldus.core.sensor()) {
var p = sensorData["protocol"]; var p = sensorData["protocol"];
@ -38,7 +38,8 @@ com.telldus.sensors = function() {
com.telldus.core.sensorEvent.connect(sensorEvent); com.telldus.core.sensorEvent.connect(sensorEvent);
view = new com.telldus.qml.view({ view = new com.telldus.qml.view({
deleteSensor: deleteSensor, deleteSensor: deleteSensor,
getWindDirection: getWindDirection getWindDirection: getWindDirection,
sendSensorReport: sendSensorReport
}); });
view.setProperty('sensorModel', sensorList); view.setProperty('sensorModel', sensorList);
@ -55,7 +56,29 @@ com.telldus.sensors = function() {
application.addWidget("sensors.gui", "icon.png", view); application.addWidget("sensors.gui", "icon.png", view);
} }
function createSensor(protocol, model, id, name, showInList){ function allValuesUpdated(sensorValues) {
var numSensors = sensorValues.length;
if (numSensors == 0) {
//error
return false;
}
if (numSensors == 1) {
//this sensor has only one sensor, so all values are updated
return true;
}
var updatedValue = sensorValues[0]['lastUpdated'];
for (var i=1; i<numSensors; i++) {
if (updatedValue.valueOf() != sensorValues[i]['lastUpdated'].valueOf()) {
//not all values updated yet
return false;
}
}
return true;
}
function createSensor(protocol, model, id, name, showInList, sendToLive){
var sensor = new com.telldus.sensors.sensor(); var sensor = new com.telldus.sensors.sensor();
sensor.protocol = protocol; sensor.protocol = protocol;
sensor.model = model; sensor.model = model;
@ -64,6 +87,8 @@ com.telldus.sensors = function() {
sensor.nameChanged.connect(function() { saveSensorModel(); }); sensor.nameChanged.connect(function() { saveSensorModel(); });
sensor.showInList = showInList; sensor.showInList = showInList;
sensor.showInListChanged.connect(function() { saveSensorModel(); }); sensor.showInListChanged.connect(function() { saveSensorModel(); });
sensor.sendToLive = sendToLive;
sensor.sendToLiveChanged.connect(function() { saveSensorModel(); sendSensorReport(); });
return sensor; return sensor;
} }
@ -133,7 +158,7 @@ com.telldus.sensors = function() {
var sensorProperties = settings.value("sensors", ""); var sensorProperties = settings.value("sensors", "");
if(sensorProperties){ if(sensorProperties){
for (var i = 0; i < sensorProperties.length; i++) { for (var i = 0; i < sensorProperties.length; i++) {
var sensor = createSensor(sensorProperties[i].protocol, sensorProperties[i].model, sensorProperties[i].id, sensorProperties[i].name, sensorProperties[i].showInList=="true"); var sensor = createSensor(sensorProperties[i].protocol, sensorProperties[i].model, sensorProperties[i].id, sensorProperties[i].name, sensorProperties[i].showInList=="true", sensorProperties[i].sendToLive=="true");
for (var j = 0; j < sensorProperties[i].values.length; j++) { for (var j = 0; j < sensorProperties[i].values.length; j++) {
sensor.setValue(sensorProperties[i].values[j].type, sensorProperties[i].values[j].value, sensorProperties[i].values[j].lastUpdated) sensor.setValue(sensorProperties[i].values[j].type, sensorProperties[i].values[j].value, sensorProperties[i].values[j].lastUpdated)
} }
@ -192,13 +217,34 @@ com.telldus.sensors = function() {
var allValues = pickSensorValues(sensor); var allValues = pickSensorValues(sensor);
var sensorProp = {protocol:sensor.protocol, model:sensor.model, id:sensor.id, values:allValues, name:sensor.name, showInList:sensor.showInList}; var sensorProp = {protocol:sensor.protocol, model:sensor.model, id:sensor.id, values:allValues, name:sensor.name, showInList:sensor.showInList, sendToLive:sensor.sendToLive};
sensorProperties.push(sensorProp); sensorProperties.push(sensorProp);
} }
settings.setValue("sensors", sensorProperties); settings.setValue("sensors", sensorProperties);
} }
function sendSensorReport(){
//TODO Only show "sendToLive" if Telldus Live! is activated
if(!com.telldus.live.isRegisteredToLive()){
return;
}
var liveSensors = new Array();
for (var i = 0; i < sensorList.length; ++i) {
var sensor = sensorList.get(i);
if(sensor.sendToLive){
var allValues = pickSensorValues(sensor);
liveSensors.push({protocol:sensor.protocol, model:sensor.model, id:sensor.id, values:allValues, name:sensor.name});
}
}
if(liveSensors.length > 0){
com.telldus.live.sendSensorsReport(liveSensors);
}
}
function sensorEvent(protocol, model, id, dataType, value, timestamp, avoidSave) { function sensorEvent(protocol, model, id, dataType, value, timestamp, avoidSave) {
var sensor = 0; var sensor = 0;
@ -222,6 +268,12 @@ com.telldus.sensors = function() {
} }
sensor.setValue(dataType, value, timestamp); sensor.setValue(dataType, value, timestamp);
if (sensor.sendToLive){
sensorValues = pickSensorValues(sensor)
if (allValuesUpdated(sensorValues)) {
com.telldus.live.sendSensorValues(sensor, sensorValues);
}
}
if(!avoidSave){ if(!avoidSave){
saveSensorModel(); saveSensorModel();

View file

@ -5,7 +5,7 @@
class Sensor::PrivateData { class Sensor::PrivateData {
public: public:
bool hasTemperature, hasHumidity, hasRainRate, hasRainTotal, hasWindDirection, hasWindAverage, hasWindGust, showInList; bool hasTemperature, hasHumidity, hasRainRate, hasRainTotal, hasWindDirection, hasWindAverage, hasWindGust, showInList, sendToLive;
int id; int id;
QString model, name, protocol; QString model, name, protocol;
QDateTime lastUpdated; QDateTime lastUpdated;
@ -18,6 +18,7 @@ Sensor::Sensor(QObject *parent) :
d = new PrivateData; d = new PrivateData;
d->id = 0; d->id = 0;
d->showInList = false; d->showInList = false;
d->sendToLive = false;
} }
Sensor::~Sensor() { Sensor::~Sensor() {
@ -131,3 +132,12 @@ void Sensor::setShowInList(bool show){
d->showInList = show; d->showInList = show;
emit showInListChanged(); emit showInListChanged();
} }
bool Sensor::sendToLive() const{
return d->sendToLive;
}
void Sensor::setSendToLive(bool send){
d->sendToLive = send;
emit sendToLiveChanged();
}

View file

@ -23,6 +23,7 @@ class Sensor : public QObject
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString protocol READ protocol WRITE setProtocol NOTIFY protocolChanged) Q_PROPERTY(QString protocol READ protocol WRITE setProtocol NOTIFY protocolChanged)
Q_PROPERTY(bool showInList READ showInList WRITE setShowInList NOTIFY showInListChanged) Q_PROPERTY(bool showInList READ showInList WRITE setShowInList NOTIFY showInListChanged)
Q_PROPERTY(bool sendToLive READ sendToLive WRITE setSendToLive NOTIFY sendToLiveChanged)
public: public:
explicit Sensor(QObject *parent = 0); explicit Sensor(QObject *parent = 0);
@ -48,11 +49,13 @@ public:
bool hasWindAverage() const; bool hasWindAverage() const;
bool hasWindGust() const; bool hasWindGust() const;
bool showInList() const; bool showInList() const;
bool sendToLive() const;
Q_INVOKABLE QObject *sensorValue(int type); Q_INVOKABLE QObject *sensorValue(int type);
Q_INVOKABLE void setValue(int type, const QString &value, const QDateTime &timestamp); Q_INVOKABLE void setValue(int type, const QString &value, const QDateTime &timestamp);
Q_INVOKABLE void setName(const QString &name); Q_INVOKABLE void setName(const QString &name);
Q_INVOKABLE void setShowInList(bool show); Q_INVOKABLE void setShowInList(bool show);
Q_INVOKABLE void setSendToLive(bool send);
signals: signals:
void idChanged(); void idChanged();
@ -67,6 +70,7 @@ signals:
void nameChanged(); void nameChanged();
void protocolChanged(); void protocolChanged();
void showInListChanged(); void showInListChanged();
void sendToLiveChanged();
private: private:
class PrivateData; class PrivateData;