Skip to content

Commit

Permalink
Merge pull request #1242 from domoticz/Python-Plugins
Browse files Browse the repository at this point in the history
Python plugins
  • Loading branch information
gizmocuz committed Feb 18, 2017
2 parents 20453c2 + fe1e51e commit 2ce43bb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
25 changes: 21 additions & 4 deletions hardware/plugins/PluginMessages.h
Expand Up @@ -37,10 +37,6 @@ namespace Plugins {
m_Type(Type), m_HwdID(HwdID), m_Unit(-1), m_iLevel(-1), m_iHue(-1), m_iValue(-1), m_Message(Message), m_Object(NULL) {
m_When = time(0);
};
CPluginMessage(ePluginMessageType Type, int HwdID, int Unit, const std::string & Message, const int level, const int hue) :
m_Type(Type), m_HwdID(HwdID), m_Unit(Unit), m_iLevel(level), m_iHue(hue), m_iValue(-1), m_Message(Message), m_Object(NULL) {
m_When = time(0);
};
CPluginMessage(ePluginMessageType Type, ePluginDirectiveType dType, int HwdID, const std::string & Message) :
m_Type(Type), m_Directive(dType), m_HwdID(HwdID), m_Unit(-1), m_iLevel(-1), m_iHue(-1), m_iValue(-1), m_Message(Message), m_Object(NULL) {
m_When = time(0);
Expand All @@ -64,6 +60,7 @@ namespace Plugins {
m_Message = m.m_Message;
m_iValue = m.m_iValue;
m_iLevel = m.m_iLevel;
m_fLevel = m.m_fLevel;
m_iHue = m.m_iHue;
m_Address = m.m_Address;
m_Port = m.m_Port;
Expand All @@ -89,6 +86,7 @@ namespace Plugins {
std::string m_Message;
int m_iValue;
int m_iLevel;
float m_fLevel;
int m_iHue;
std::string m_Address;
std::string m_Port;
Expand All @@ -108,6 +106,10 @@ namespace Plugins {
m_Type(Type), m_HwdID(HwdID), m_Unit(-1), m_iLevel(-1), m_iHue(-1), m_iValue(-1), m_Object(NULL) {
m_When = time(0);
};
CPluginMessage(ePluginMessageType Type, int HwdID, int Unit, const std::string & Message, const int level, const int hue) :
m_Type(Type), m_HwdID(HwdID), m_Unit(Unit), m_iLevel(level), m_iHue(hue), m_iValue(-1), m_Message(Message), m_Object(NULL) {
m_When = time(0);
};
};

class InitializeMessage : public CPluginMessage
Expand Down Expand Up @@ -140,6 +142,21 @@ namespace Plugins {
DisconnectMessage(int HwdID) : CPluginMessage(PMT_Disconnect, HwdID) {};
};

class CommandMessage : public CPluginMessage
{
public:
CommandMessage(int HwdID, int Unit, const std::string & Message, const int level, const int hue) : CPluginMessage(PMT_Command, HwdID, Unit, Message, level, hue)
{
m_fLevel = -273.15;
};
CommandMessage(int HwdID, int Unit, const std::string & Message, const float level) : CPluginMessage(PMT_Command, HwdID)
{
m_Unit = Unit;
m_fLevel = level;
m_Message = Message;
};
};

class NotificationMessage : public CPluginMessage
{
public:
Expand Down
20 changes: 18 additions & 2 deletions hardware/plugins/Plugins.cpp
Expand Up @@ -1592,7 +1592,14 @@ namespace Plugins {
break;
case PMT_Command:
sHandler = "onCommand";
pParams = Py_BuildValue("isii", Message.m_Unit, Message.m_Message.c_str(), Message.m_iLevel, Message.m_iHue);
if (Message.m_fLevel != -273.15)
{
pParams = Py_BuildValue("isfi", Message.m_Unit, Message.m_Message.c_str(), Message.m_fLevel, 0);
}
else
{
pParams = Py_BuildValue("isii", Message.m_Unit, Message.m_Message.c_str(), Message.m_iLevel, Message.m_iHue);
}
break;
case PMT_Stop:
sHandler = "onStop";
Expand Down Expand Up @@ -1864,7 +1871,16 @@ namespace Plugins {
void CPlugin::SendCommand(const int Unit, const std::string &command, const int level, const int hue)
{
// Add command to message queue
CPluginMessage Message(PMT_Command, m_HwdID, Unit, command, level, hue);
CommandMessage Message(m_HwdID, Unit, command, level, hue);
{
boost::lock_guard<boost::mutex> l(PluginMutex);
PluginMessageQueue.push(Message);
}
}
void CPlugin::SendCommand(const int Unit, const std::string & command, const float level)
{
// Add command to message queue
CommandMessage Message(m_HwdID, Unit, command, level);
{
boost::lock_guard<boost::mutex> l(PluginMutex);
PluginMessageQueue.push(Message);
Expand Down
1 change: 1 addition & 0 deletions hardware/plugins/Plugins.h
Expand Up @@ -41,6 +41,7 @@ namespace Plugins {
bool WriteToHardware(const char *pdata, const unsigned char length);
void Restart();
void SendCommand(const int Unit, const std::string &command, const int level, const int hue);
void SendCommand(const int Unit, const std::string &command, const float level);

std::string m_PluginKey;
CPluginProtocol* m_pProtocol;
Expand Down
12 changes: 11 additions & 1 deletion main/mainworker.cpp
Expand Up @@ -11544,7 +11544,17 @@ bool MainWorker::SetSetPointInt(const std::vector<std::string> &sd, const float
CDomoticzHardwareBase *pHardware=GetHardware(HardwareID);
if (pHardware==NULL)
return false;
if (
//
// For plugins all the specific logic below is irrelevent
// so just send the full details to the plugin so that it can take appropriate action
//
if (pHardware->HwdType == HTYPE_PythonPlugin)
{
#ifdef USE_PYTHON_PLUGINS
((Plugins::CPlugin*)pHardware)->SendCommand(Unit, "Set Level", TempValue);
#endif
}
else if (
(pHardware->HwdType == HTYPE_OpenThermGateway) ||
(pHardware->HwdType == HTYPE_OpenThermGatewayTCP) ||
(pHardware->HwdType == HTYPE_ICYTHERMOSTAT) ||
Expand Down
6 changes: 3 additions & 3 deletions plugins/examples/BaseTemplate.py
Expand Up @@ -31,8 +31,8 @@ def onMessage(self, Data, Status, Extra):
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))

def onNotification(self, Data):
Domoticz.Log("onNotification: " + str(Data))
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)

def onDisconnect(self):
Domoticz.Log("onDisconnect called")
Expand Down Expand Up @@ -63,7 +63,7 @@ def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)

def onNotification(Data):
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
global _plugin
_plugin.onNotification(Data)

Expand Down

0 comments on commit 2ce43bb

Please sign in to comment.