Possiblity to add sensors from TelldusCenter to be visible in Telldus Live! This closes #186.
This commit is contained in:
parent
103eaf05e0
commit
b73eae288f
8 changed files with 168 additions and 9 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define LIVEMESSAGETOKEN_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QDateTime>
|
||||
#include <QString>
|
||||
#include <QHash>
|
||||
#include <QMetaType>
|
||||
|
@ -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;
|
||||
|
|
|
@ -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<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) {
|
||||
configUI.findChild('errorLabel').text = msg;
|
||||
}
|
||||
|
@ -124,7 +180,10 @@ com.telldus.live = function() {
|
|||
}
|
||||
|
||||
return { //Public functions
|
||||
init:init
|
||||
init:init,
|
||||
sendSensorsReport:sendSensorsReport,
|
||||
isRegisteredToLive:isRegisteredToLive,
|
||||
sendSensorValues:sendSensorValues
|
||||
}
|
||||
}();
|
||||
|
||||
|
|
|
@ -41,11 +41,19 @@ Column {
|
|||
HeaderTitle {
|
||||
id: visibleinlistTitle
|
||||
text: qsTr("Visible in list")
|
||||
anchors.right: sensorinformationTitle.left
|
||||
anchors.right: sendtoliveTitle.left
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
visible: main.state == "EDIT"
|
||||
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 {
|
||||
id: sensorinformationTitle
|
||||
text: qsTr("Sensor information")
|
||||
|
@ -72,9 +80,11 @@ Column {
|
|||
onClicked: {
|
||||
if(main.state == "VIEW"){
|
||||
main.state = "EDIT"
|
||||
sendSensorReport.callWith(1);
|
||||
}
|
||||
else{
|
||||
main.state = "VIEW"
|
||||
sendSensorReport.callWith(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ Item{
|
|||
id: visibleinlistcheckbox
|
||||
height: 40
|
||||
width: 100
|
||||
anchors.right: sensorInfo.left
|
||||
anchors.right: sendtolivecheckbox.left
|
||||
CheckBox {
|
||||
id: checkBox
|
||||
anchors.centerIn: parent
|
||||
|
@ -93,6 +93,20 @@ Item{
|
|||
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 {
|
||||
id: sensorInfo
|
||||
|
|
|
@ -11,7 +11,7 @@ com.telldus.sensors = function() {
|
|||
var sensorData = 0;
|
||||
sensorList = loadSensorModel();
|
||||
sensorList.rowsRemoved.connect(function(){saveSensorModel();});
|
||||
sensorList.rowsInserted.connect(function(){saveSensorModel();});
|
||||
sensorList.rowsInserted.connect(function(){saveSensorModel(); sendSensorReport();});
|
||||
|
||||
while(sensorData = com.telldus.core.sensor()) {
|
||||
var p = sensorData["protocol"];
|
||||
|
@ -38,7 +38,8 @@ com.telldus.sensors = function() {
|
|||
com.telldus.core.sensorEvent.connect(sensorEvent);
|
||||
view = new com.telldus.qml.view({
|
||||
deleteSensor: deleteSensor,
|
||||
getWindDirection: getWindDirection
|
||||
getWindDirection: getWindDirection,
|
||||
sendSensorReport: sendSensorReport
|
||||
});
|
||||
|
||||
view.setProperty('sensorModel', sensorList);
|
||||
|
@ -55,7 +56,29 @@ com.telldus.sensors = function() {
|
|||
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();
|
||||
sensor.protocol = protocol;
|
||||
sensor.model = model;
|
||||
|
@ -64,6 +87,8 @@ com.telldus.sensors = function() {
|
|||
sensor.nameChanged.connect(function() { saveSensorModel(); });
|
||||
sensor.showInList = showInList;
|
||||
sensor.showInListChanged.connect(function() { saveSensorModel(); });
|
||||
sensor.sendToLive = sendToLive;
|
||||
sensor.sendToLiveChanged.connect(function() { saveSensorModel(); sendSensorReport(); });
|
||||
return sensor;
|
||||
}
|
||||
|
||||
|
@ -133,7 +158,7 @@ com.telldus.sensors = function() {
|
|||
var sensorProperties = settings.value("sensors", "");
|
||||
if(sensorProperties){
|
||||
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++) {
|
||||
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 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);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
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();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue