# shx10.cpp

Go to the documentation of this file.

    00001 /*
    00002  * SmartHome server: Plug-in x10
    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 "shx10.h"
    00019 #include <QStringList>
    00020
    00021 // -----------------------------------------------------------------------------
    00022
    00027 ShX10::ShX10(QObject *parent) : ShObjectHelper(parent)
    00028 {
    00029 }
    00030 // -----------------------------------------------------------------------------
    00031
    00037 bool ShX10::init(QString param)
    00038 {
    00039     mInterface = new X10Interface(param);
    00040
    00041     connect(mInterface, SIGNAL(transmissionDone()),
    00042             this, SLOT(emitAck()));
    00043
    00044     connect(mInterface, SIGNAL(transmissionFailed(QString)),
    00045             this, SLOT(emitErr(QString)));
    00046
    00047     mInterface->sendTime();
    00048
    00049     qDebug("X10 initialized");
    00050     return true;
    00051 }
    00052 // -----------------------------------------------------------------------------
    00053
    00058 void ShX10::processMessage(ShMessagePtr message)
    00059 {
    00060     qDebug("X10: Processing message from object %d, signal = %s, value = %s",
    00061            message->senderId(), qPrintable(message->signal()), qPrintable(message->value().toString()));
    00062
    00063     if (message->msgType() == ShMessage::mtValue)
    00064     {
    00065         mMessageQueue.enqueue(message);
    00066         processNextMessage();
    00067     }
    00068 }
    00069 // -----------------------------------------------------------------------------
    00070
    00074 void ShX10::processNextMessage()
    00075 {
    00076     if ((mMessageQueue.isEmpty()) ||
    00077         (mInterface->state() != X10Interface::stReady))
    00078         return;
    00079
    00080     QRegExp rxn("([A-P])(1[0-5]|[0-9])((?:\\s*,\\s*)(1[0-5]|[0-9]))*");
    00081     QRegExp rxa("([A-P])\\*");
    00082
    00083     QString  devices = mMessageQueue.head()->signal().toUpper();
    00084     QVariant value   = mMessageQueue.head()->value();
    00085     QString  valStr  = value.toString().toUpper();
    00086
    00087     if (rxn.exactMatch(devices))
    00088     {
    00089         if ( (valStr == "ON") ||
    00090             ((valStr == "1") && (mMessageQueue.head()->pin().type() == ShPin::ptBool)) )
    00091         {
    00092             mInterface->onOffDevices(devices, true);
    00093         }
    00094         else if ((valStr == "OFF") || (valStr == "0"))
    00095         {
    00096             mInterface->onOffDevices(devices, false);
    00097         }
    00098         else if (value.canConvert<int>())
    00099         {
    00100             mInterface->dimLights(devices, value.toInt());
    00101         }
    00102         else
    00103             emitErr("unsupported value");
    00104     }
    00105     else if (rxa.exactMatch(devices))
    00106     {
    00107         mInterface->onOffAllLights(devices[0].toAscii(), value.toBool() || valStr == "ON");
    00108     }
    00109 }
    00110 // -----------------------------------------------------------------------------
    00111
    00115 void ShX10::emitAck()
    00116 {
    00117     ShMessagePtr message = mMessageQueue.dequeue();
    00118
    00119     if (message->requireAck())
    00120         ackMessage(message);
    00121
    00122     processNextMessage();
    00123 }
    00124 // -----------------------------------------------------------------------------
    00125
    00129 void ShX10::emitErr(QString reason)
    00130 {
    00131     ShMessagePtr message = mMessageQueue.dequeue();
    00132
    00133     errorMessage(message, reason);
    00134     processNextMessage();
    00135 }
    00136 // -----------------------------------------------------------------------------
    00137
    00138 Q_EXPORT_PLUGIN2(x10, ShX10);

All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends