From b0d3d2af30e37f0832487bb4e4f6a84acc0a9f34 Mon Sep 17 00:00:00 2001 From: Stefan Persson Date: Wed, 8 May 2013 13:44:25 +0200 Subject: [PATCH] Added hasta version 2 --- telldus-core/service/Protocol.cpp | 5 + telldus-core/service/ProtocolHasta.cpp | 142 +++++++++++++++++- telldus-core/service/ProtocolHasta.h | 5 + .../TelldusGui/data/telldus/devices.xml | 1 + 4 files changed, 151 insertions(+), 2 deletions(-) diff --git a/telldus-core/service/Protocol.cpp b/telldus-core/service/Protocol.cpp index defc7775..2a8f00e6 100644 --- a/telldus-core/service/Protocol.cpp +++ b/telldus-core/service/Protocol.cpp @@ -256,6 +256,11 @@ std::list Protocol::decodeData(const std::string &fullData) { if (decoded != "") { retval.push_back(decoded); } + } else if(TelldusCore::comparei(dataMsg.protocol(), L"hasta") ) { + decoded = ProtocolHasta::decodeData(dataMsg); + if (decoded != "") { + retval.push_back(decoded); + } } return retval; diff --git a/telldus-core/service/ProtocolHasta.cpp b/telldus-core/service/ProtocolHasta.cpp index 0c5899de..0cb46376 100644 --- a/telldus-core/service/ProtocolHasta.cpp +++ b/telldus-core/service/ProtocolHasta.cpp @@ -8,12 +8,20 @@ #include #include #include +#include "common/Strings.h" int ProtocolHasta::methods() const { return TELLSTICK_UP | TELLSTICK_DOWN | TELLSTICK_STOP | TELLSTICK_LEARN; } std::string ProtocolHasta::getStringForMethod(int method, unsigned char, Controller *) { + if (TelldusCore::comparei(model(), L"selflearningv2")) { + return getStringForMethodv2(method); + } + return getStringForMethodv1(method); +} + +std::string ProtocolHasta::getStringForMethodv1(int method) { int house = this->getIntParameter(L"house", 1, 65536); int unit = this->getIntParameter(L"unit", 1, 15); std::string strReturn; @@ -21,10 +29,10 @@ std::string ProtocolHasta::getStringForMethod(int method, unsigned char, Control std::string preamble; strReturn.append(1, 190); strReturn.append(1, 1); - strReturn.append(1, 190); + strReturn.append(1, 107); strReturn.append(1, 1); strReturn.append(1, 190); - strReturn.append(1, 190); + strReturn.append(1, 165); strReturn.append(convertByte( (house & 0xFF) )); strReturn.append(convertByte( (house>>8) & 0xFF )); @@ -71,3 +79,133 @@ std::string ProtocolHasta::convertByte(unsigned char byte) { } return retval; } + +std::string ProtocolHasta::getStringForMethodv2(int method) { + int house = this->getIntParameter(L"house", 1, 65536); + int unit = this->getIntParameter(L"unit", 1, 15); + int sum = 0; + std::string strReturn; + + std::string preamble; + strReturn.append(1, 190); + strReturn.append(1, 1); + strReturn.append(1, 107); + strReturn.append(1, 1); + strReturn.append(1, 190); + strReturn.append(1, 190); + strReturn.append(1, 1); + strReturn.append(1, 60); + strReturn.append(1, 160); + strReturn.append(1, 40); + + strReturn.append(convertBytev2( (house>>8) & 0xFF )); + sum = ((house>>8)&0xFF); + strReturn.append(convertBytev2( (house & 0xFF) )); + sum += (house & 0xFF); + + int byte = unit&0x0F; + + if (method == TELLSTICK_UP) { + byte |= 0xC0; + + } else if (method == TELLSTICK_DOWN) { + byte |= 0x10; + + } else if (method == TELLSTICK_STOP) { + byte |= 0x50; + + } else if (method == TELLSTICK_LEARN) { + byte |= 0x40; + + } else { + return ""; + } + strReturn.append(convertBytev2(byte)); + sum += byte; + + strReturn.append(convertBytev2(0x01)); + sum += 0x01; + + int checksum = (((int)(sum/256)+1)*256+1) - sum; + strReturn.append(convertBytev2(checksum)); + strReturn.append(1, 68); + strReturn.append(1, 36); + + return strReturn; +} + +std::string ProtocolHasta::convertBytev2(unsigned char byte) { + std::string retval; + for(int i = 0; i < 8; ++i) { + if (byte & 1) { + retval.append(1, 68); + retval.append(1, 36); + } else { + retval.append(1, 36); + retval.append(1, 68); + } + byte >>= 1; + } + return retval; +} + +std::string ProtocolHasta::decodeData(const ControllerMessage& dataMsg) { + uint64_t allData = dataMsg.getInt64Parameter("data"); + + unsigned int house = 0; + unsigned int unit = 0; + unsigned int method = 0; + std::string model; + std::string methodstring; + + allData >>= 8; + unit = allData & 0xF; + allData >>= 4; + method = allData & 0xF; + allData >>= 4; + if(TelldusCore::comparei(dataMsg.model(), L"selflearning")) { + //version1 + house = allData & 0xFFFF; + house = ((house << 8) | (house >> 8)) & 0xFFFF; + model = "selflearning"; + if(method == 0){ + methodstring = "up"; + } + else if(method == 1){ + methodstring = "down"; + } + else if(method == 5){ + methodstring = "stop"; + } + else{ + return ""; + } + } + else{ + //version2 + house = allData & 0xFFFF; + + model = "selflearningv2"; + if(method == 12){ + methodstring = "up"; + } + else if(method == 1 or method == 8){ //is method 8 correct? + methodstring = "down"; + } + else if(method == 5){ + methodstring = "stop"; + } + else{ + return ""; + } + } + + if(house < 1 || house > 65535 || unit < 1 || unit > 16) { + // not hasta + return ""; + } + + std::stringstream retString; + retString << "class:command;protocol:hasta;model:" << model << ";house:" << house << ";unit:" << unit << ";method:" << methodstring << ";"; + return retString.str(); +} diff --git a/telldus-core/service/ProtocolHasta.h b/telldus-core/service/ProtocolHasta.h index 0ab8a488..7a122bb2 100644 --- a/telldus-core/service/ProtocolHasta.h +++ b/telldus-core/service/ProtocolHasta.h @@ -8,15 +8,20 @@ #define TELLDUS_CORE_SERVICE_PROTOCOLHASTA_H_ #include +#include "service/ControllerMessage.h" #include "service/Protocol.h" class ProtocolHasta : public Protocol { public: int methods() const; virtual std::string getStringForMethod(int method, unsigned char data, Controller *controller); + static std::string decodeData(const ControllerMessage &dataMsg); protected: static std::string convertByte(unsigned char byte); + static std::string convertBytev2(unsigned char byte); + std::string getStringForMethodv1(int method); + std::string getStringForMethodv2(int method); }; #endif // TELLDUS_CORE_SERVICE_PROTOCOLHASTA_H_ diff --git a/telldus-gui/TelldusGui/data/telldus/devices.xml b/telldus-gui/TelldusGui/data/telldus/devices.xml index bd8c6b4c..1ed81603 100644 --- a/telldus-gui/TelldusGui/data/telldus/devices.xml +++ b/telldus-gui/TelldusGui/data/telldus/devices.xml @@ -124,6 +124,7 @@ Blinds + Blinds (version 2) Projector screen