Skip to content

Commit d27be95

Browse files
committed
Implemented: MySensors, Saving/Loading VAR values
1 parent 8b7ac17 commit d27be95

File tree

4 files changed

+67
-12
lines changed

4 files changed

+67
-12
lines changed

History.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Version 2.0.xxxx (xxxx 2015)
2222
- Fixed: TeleInfo (TDF) Hardware ID
2323
- Changed: Air Quality (CO-2) sensor class
2424
- Implemented: MySensors Water meter
25+
- Implemented: MySensors, Saving/Loading VAR values
2526

2627
Version 2.0.2284 (February 22th 2015)
2728
- Fixed: Firefox, RGBW/Setpoint popup

hardware/MySensorsBase.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,45 @@ bool MySensorsBase::WriteToHardware(const char *pdata, const unsigned char lengt
528528
return true;
529529
}
530530

531+
void MySensorsBase::UpdateVar(const int NodeID, const int ChildID, const int VarID, const std::string &svalue)
532+
{
533+
std::stringstream sstr;
534+
535+
std::vector<std::vector<std::string> > result;
536+
sstr << "SELECT ROWID FROM MySensorsVars WHERE (HardwareID=" << m_HwdID << ") AND (NodeID=" << NodeID << ") AND (NodeID=" << NodeID << ") AND (ChildID=" << ChildID << ") AND (VarID=" << VarID << ")";
537+
result = m_sql.query(sstr.str());
538+
if (result.size() == 0)
539+
{
540+
//Insert
541+
sstr.clear();
542+
sstr.str("");
543+
sstr << "INSERT INTO MySensorsVars (HardwareID, NodeID, ChildID, VarID, [Value]) VALUES (" << m_HwdID << ", " << NodeID << "," << ChildID << ", " << VarID << ",'" << svalue << "')";
544+
result = m_sql.query(sstr.str());
545+
}
546+
else
547+
{
548+
//Update
549+
sstr.clear();
550+
sstr.str("");
551+
sstr << "UPDATE MySensorsVars SET [Value]='" << svalue << "' WHERE (ROWID = " << result[0][0] << ")";
552+
result = m_sql.query(sstr.str());
553+
}
554+
555+
}
556+
557+
bool MySensorsBase::GetVar(const int NodeID, const int ChildID, const int VarID, std::string &sValue)
558+
{
559+
std::stringstream sstr;
560+
561+
std::vector<std::vector<std::string> > result;
562+
sstr << "SELECT [Value] FROM MySensorsVars WHERE (HardwareID=" << m_HwdID << ") AND (NodeID=" << NodeID << ") AND (NodeID=" << NodeID << ") AND (ChildID=" << ChildID << ") AND (VarID=" << VarID << ")";
563+
result = m_sql.query(sstr.str());
564+
if (result.size() < 1)
565+
return false;
566+
std::vector<std::string> sd = result[0];
567+
sValue = sd[0];
568+
return true;
569+
}
531570

532571
void MySensorsBase::ParseLine()
533572
{
@@ -668,7 +707,7 @@ void MySensorsBase::ParseLine()
668707
case V_VAR3:
669708
case V_VAR4:
670709
case V_VAR5:
671-
//_log.Log(LOG_STATUS, "MySensors: Custom vars ignored! Node: %d, Child: %d, Payload: %s", node_id, child_sensor_id, payload.c_str());
710+
UpdateVar(node_id, child_sensor_id, sub_type, payload);
672711
break;
673712
case V_TRIPPED:
674713
// Tripped status of a security sensor. 1 = Tripped, 0 = Untripped
@@ -833,6 +872,9 @@ void MySensorsBase::ParseLine()
833872
case V_VAR4:
834873
case V_VAR5:
835874
//cant send back a custom variable
875+
tmpstr = "";
876+
if (GetVar(node_id, child_sensor_id, sub_type, tmpstr)==true)
877+
SendCommand(node_id, child_sensor_id, message_type, sub_type, tmpstr);
836878
break;
837879
default:
838880
while (1==0);

hardware/MySensorsBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ class MySensorsBase : public CDomoticzHardwareBase
183183
void InsertSensor(_tMySensorSensor device);
184184
void UpdateNodeBatteryLevel(const int nodeID, const int Level);
185185

186+
void UpdateVar(const int NodeID, const int ChildID, const int VarID, const std::string &svalue);
187+
bool GetVar(const int NodeID, const int ChildID, const int VarID, std::string &sValue);
188+
186189
std::map<int, _tMySensorNode> m_nodes;
187190

188191
static const int readBufferSize=1028;

main/SQLHelper.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "../msbuild/WindowsHelper.h"
2727
#endif
2828

29-
#define DB_VERSION 62
29+
#define DB_VERSION 63
3030

3131
extern http::server::CWebServer m_webserver;
3232
extern std::string szWWWFolder;
@@ -138,12 +138,6 @@ const char *sqlCreateTemperature_Calendar =
138138
"[SetPoint_Avg] FLOAT DEFAULT 0, "
139139
"[Date] DATE NOT NULL);";
140140

141-
const char *sqlCreateTempVars =
142-
"CREATE TABLE IF NOT EXISTS [TempVars] ("
143-
"[Key] VARCHAR(200) NOT NULL, "
144-
"[nValue] INTEGER DEFAULT 0, "
145-
"[sValue] VARCHAR(200));";
146-
147141
const char *sqlCreateTimers =
148142
"CREATE TABLE IF NOT EXISTS [Timers] ("
149143
"[ID] INTEGER PRIMARY KEY, "
@@ -523,6 +517,14 @@ const char *sqlCreateMySensors =
523517
" [SketchName] VARCHAR(100) DEFAULT Unknown,"
524518
" [SketchVersion] VARCHAR(40) DEFAULT(1.0));";
525519

520+
const char *sqlCreateMySensorsVariables =
521+
"CREATE TABLE IF NOT EXISTS [MySensorsVars]("
522+
" [HardwareID] INTEGER NOT NULL,"
523+
" [NodeID] INTEGER NOT NULL,"
524+
" [ChildID] INTEGER NOT NULL,"
525+
" [VarID] INTEGER NOT NULL,"
526+
" [Value] VARCHAR(100) NOT NULL);";
527+
526528
extern std::string szStartupFolder;
527529

528530
CSQLHelper::CSQLHelper(void)
@@ -597,7 +599,6 @@ bool CSQLHelper::OpenDatabase()
597599
query(sqlCreateRain_Calendar);
598600
query(sqlCreateTemperature);
599601
query(sqlCreateTemperature_Calendar);
600-
//query(sqlCreateTempVars);
601602
query(sqlCreateTimers);
602603
query(sqlCreateSetpointTimers);
603604
query(sqlCreateUV);
@@ -640,6 +641,7 @@ bool CSQLHelper::OpenDatabase()
640641
query(sqlCreateFloorplanOrderTrigger);
641642
query(sqlCreateCustomImages);
642643
query(sqlCreateMySensors);
644+
query(sqlCreateMySensorsVariables);
643645

644646
if ((!bNewInstall) && (dbversion < DB_VERSION))
645647
{
@@ -1225,6 +1227,10 @@ bool CSQLHelper::OpenDatabase()
12251227
query(szQuery2.str());
12261228
}
12271229
}
1230+
if (dbversion < 63)
1231+
{
1232+
query("DROP TABLE IF EXISTS [TempVars]");
1233+
}
12281234
}
12291235
else if (bNewInstall)
12301236
{
@@ -6983,7 +6989,7 @@ std::string CSQLHelper::CheckUserVariable(const int vartype, const std::string &
69836989
return "Not a valid integer";
69846990
}
69856991
}
6986-
if (vartype == 1) {
6992+
else if (vartype == 1) {
69876993
//float
69886994
std::istringstream iss(varvalue);
69896995
float f;
@@ -6993,19 +6999,22 @@ std::string CSQLHelper::CheckUserVariable(const int vartype, const std::string &
69936999
return "Not a valid float";
69947000
}
69957001
}
6996-
if (vartype == 3) {
7002+
else if (vartype == 3) {
69977003
//date
69987004
int d, m, y;
69997005
if (!CheckDate(varvalue, d, m, y))
70007006
{
70017007
return "Not a valid date notation (DD/MM/YYYY)";
70027008
}
70037009
}
7004-
if (vartype == 4) {
7010+
else if (vartype == 4) {
70057011
//time
70067012
if (!CheckTime(varvalue))
70077013
return "Not a valid time notation (HH:MM)";
70087014
}
7015+
else if (vartype == 5) {
7016+
return "OK";
7017+
}
70097018
return "OK";
70107019
}
70117020

0 commit comments

Comments
 (0)