# shthermostat.cpp

Go to the documentation of this file.

    00001 /*
    00002  * SmartHome server: Plug-in thermostat
    00003  *
    00004  * Copyright (C) 2010 Daniel Pisi <dan.pisi@gmail.com>
    00005  *
    00006  * This program is free software; you can redistribute it and/or modify it
    00007  * under the terms of version 2 of the GNU General Public License
    00008  * as published by the Free Software Foundation.
    00009  *
    00010  */
    00011
    00018 #include "shthermostat.h"
    00019
    00020 // -----------------------------------------------------------------------------
    00021
    00025 ShThermostat::ShThermostat()
    00026 {
    00027     mDesiredTemp  = 20;
    00028     mMeasuredTemp = 20;
    00029     mHysteresis   = 1;
    00030     mOutput       = -1;
    00031 }
    00032 // -----------------------------------------------------------------------------
    00033
    00039 bool ShThermostat::init(QString param)
    00040 {
    00041     qDebug("Thermostat initialized");
    00042     return true;
    00043 }
    00044 // -----------------------------------------------------------------------------
    00045
    00049 void ShThermostat::updateOutput()
    00050 {
    00051     if ((mOutput != 0) && (mMeasuredTemp >= mDesiredTemp + mHysteresis/2.0))
    00052     {
    00053         mOutput = 0;
    00054         outputUpdated("Y", "0");
    00055     }
    00056     else if ((mOutput != 1) && (mMeasuredTemp <= mDesiredTemp - mHysteresis/2.0))
    00057     {
    00058         mOutput = 1;
    00059         outputUpdated("Y", "1");
    00060     }
    00061 }
    00062 // -----------------------------------------------------------------------------
    00063
    00068 void ShThermostat::processMessage(ShMessagePtr message)
    00069 {
    00070     qDebug("Thermostat: Processing message from object %d, signal = %s, value = %s",
    00071            message->senderId(), qPrintable(message->signal()), qPrintable(message->value().toString()));
    00072
    00073     if (message->msgType() == ShMessage::mtValue)
    00074     {
    00075         double *tempPtr = NULL;
    00076
    00077         if (message->signal() == "W")
    00078             tempPtr = &mDesiredTemp;
    00079         else if (message->signal() == "X")
    00080             tempPtr = &mMeasuredTemp;
    00081         else
    00082         {
    00083             errorMessage(message, "Invalid signal");
    00084             return;
    00085         }
    00086
    00087         bool ok = false;
    00088         double temp = message->value().toDouble(&ok);
    00089
    00090         if (ok)
    00091         {
    00092             *tempPtr = temp;
    00093             updateOutput();
    00094
    00095             if (message->requireAck())
    00096                 ackMessage(message);
    00097         }
    00098         else
    00099             errorMessage(message, "Invalid value");
    00100     }
    00101 }
    00102 // -----------------------------------------------------------------------------
    00103
    00104 Q_EXPORT_PLUGIN2(thermostat, ShThermostat);

All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends