示例#1
0
bool CTellstick::WriteToHardware(const char *pdata, const unsigned char length)
{
	_tGeneralSwitch *pSwitch = (_tGeneralSwitch*)pdata;
	//tRBUF *pCmd=(tRBUF*) pdata;
	_log.Log(LOG_NORM, "Tellstick: WriteToHardware %d id: %d cmd: %d", pSwitch->type, pSwitch->id, pSwitch->cmnd);

	if (pSwitch->type != pTypeGeneralSwitch)
		return false; //only allowed to control switches

	if (pSwitch->cmnd == gswitch_sOn)
	{
	  _log.Log(LOG_NORM, "Tellstick: Switch ON");
	  tdTurnOn(pSwitch->id);
	}
	else if(pSwitch->cmnd == gswitch_sOff)
	{
	  _log.Log(LOG_NORM, "Tellstick: Switch OFF");
	  tdTurnOff(pSwitch->id);
	}
	else if(pSwitch->cmnd == gswitch_sSetLevel)
	{
	  _log.Log(LOG_NORM, "Tellstick: Dim level %d %d", pSwitch->level, pSwitch->level * 255 / 99);
	  tdDim(pSwitch->id, pSwitch->level * 255 / 99);
	}

	return true;
}
示例#2
0
static void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
{
        struct context *ctx = obj;
        if(ctx->debug > 1) {
                fprintf(stderr, "topic %s: payload %s\n", msg->topic, (const char*)msg->payload);
        }
        int intNumberOfDevices = tdGetNumberOfDevices();
        for (int i = 0; i < intNumberOfDevices; i++) {
                int id = tdGetDeviceId( i );
                char *name = tdGetName( id );
                char topic[100];
                snprintf(topic,sizeof(topic)-1,"%s/%s/method",ctx->sub_prefix,name);
                if(ctx->debug > 3) {
                        fprintf(stderr, "considering topic %s\n", topic);
                }
                if(!strcmp(topic,msg->topic)) {
                        if(ctx->debug > 2) {
                                fprintf(stderr, "topic %s match\n", topic);
                        }
                        if(!strncmp("turnon",(char*)msg->payload, msg->payloadlen)) {
                                tdTurnOn(id);
                        }
                        if(!strncmp("turnoff",(char*)msg->payload, msg->payloadlen)) {
                                tdTurnOff(id);
                        }
                        if(!strncmp("bell",(char*)msg->payload, msg->payloadlen)) {
                                tdBell(id);
                        }
                }
                tdReleaseString(name);
        }
}
示例#3
0
void lightingCommandHandler(xPL_ServicePtr theService, xPL_MessagePtr theMessage, xPL_ObjectPtr userValue) {
	Bool found = FALSE;
	int deviceCount = 0, deviceId = 0, i, level = 0;
	
	if (!xPL_doesMessageNamedValueExist(theMessage, "command")) {
		return;
	} else if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(theMessage, "command"), "goto") != 0) {
		return;
	}

	if (!xPL_doesMessageNamedValueExist(theMessage, "network")) {
		return;
	} else if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(theMessage, "network"), "1") != 0) {
		return;
	}

	if (!xPL_doesMessageNamedValueExist(theMessage, "device")) {
		return;
	} else {
		/* Loop the devices to see it realy exists */
		deviceCount = tdGetNumberOfDevices();

		xPL_strToInt(xPL_getMessageNamedValue(theMessage, "device"), &deviceId);
		for( i = 0; i < deviceCount; ++i ) {
			if (tdGetDeviceId(i) == deviceId) {
				found = TRUE;
				break;
			}
		}
		if (found == FALSE) {
			return;
		}
	}

	if (!xPL_doesMessageNamedValueExist(theMessage, "level")) {
		return;
	} else {
		xPL_strToInt(xPL_getMessageNamedValue(theMessage, "level"), &level);
		if (level < 0 || level > 100) {
			return;
		}
		level = (float)level * 255.0 / 100.0;
	}

	if (level > 0 && level < 255) {
		/* See if the device supports dim */
		if (!(tdMethods(deviceId, TELLSTICK_DIM) & TELLSTICK_DIM)) {
			/* Non dimmable device was dimmed */
			return;
		}
		tdDim(deviceId, (unsigned char)level);
	} else if (level == 255) {
		/* See if the device supports dim */
		if (!(tdMethods(deviceId, TELLSTICK_TURNON) & TELLSTICK_TURNON)) {
			/* Non dimmable device was dimmed */
			return;
		}
		tdTurnOn(deviceId);
	} else if (level == 0) {
		/* See if the device supports dim */
		if (!(tdMethods(deviceId, TELLSTICK_TURNOFF) & TELLSTICK_TURNOFF)) {
			/* Non dimmable device was dimmed */
			return;
		}
		tdTurnOff(deviceId);
	}

}
int main()
{
    qDebug() << "WeekTimer";
    tdInit();
    SQLiteWrapper lite;

    // This list comes from /etc/tellstick.conf
    // that was created with TelldusCenter.
    //
    // The name given in TelldusCenter also becomes the
    // uniq part of the mqtt topic.

    QList<WeekTimer> *weekTimerList;
    weekTimerList = new QList<WeekTimer>;

    int intNumberOfDevices = tdGetNumberOfDevices();
    for (int i = 0; i < intNumberOfDevices; i++)
    {
        int id = tdGetDeviceId( i );
        char *nameTmp = tdGetName( id );
        QString name = QString(nameTmp);

        int methods = tdMethods( id, (TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_BELL ) );
        if ( (methods & TELLSTICK_TURNON ) && (methods & TELLSTICK_TURNOFF) )
        {
            WeekTimer wt(name);
            wt.setID(id);

            QString timerdata = lite.getWeekTimer(name);
            if(!timerdata.isEmpty())
            {
                wt.addNewTimers(timerdata);
            }
            // lite.getForce(name)
            weekTimerList->append(wt);
        }

        if ( (methods & TELLSTICK_BELL) )
        {
            /// @todo What to do with a door bell
            qDebug() << id << name << "is bell";
        }

        qDebug() << id << name;
        tdReleaseString(nameTmp);
    }


    for (int z = 0; z < weekTimerList->size(); ++z)
    {
        WeekTimer wt = weekTimerList->at(z);
        qDebug() << "WeekTimer name:" << wt.getName() << "id:" << wt.getID();
    }

    class MosqConnect *mqtt;
    int rc;

    mosqpp::lib_init();

    mqtt = new MosqConnect(
            "FunTechHouse_WeekTimer_Nexa",
            "mosqhub",
            1883,
            weekTimerList,
            &lite
            );

    while(1)
    {
        //Check if there is new mess for me
        rc = mqtt->loop();
        if(rc)
        {
            mqtt->reconnect();
        }

        //Then every minute or so,
        //update the timer output
        if(UnixTime::get()%30==0)
        {
            //qDebug() << UnixTime::get();

            QDate nowDate = QDate::currentDate();
            int dow = nowDate.dayOfWeek();

            QTime nowTime = QTime::currentTime();
            int hour = nowTime.hour();
            int min = nowTime.minute();

            qDebug() << "Time:" << dow << hour << min;


            for (int z = 0; z < weekTimerList->size(); ++z)
            {
                WeekTimer wt = weekTimerList->at(z);
                //qDebug() << "Rum" << wt.getName() << wt.getID();

                int id = wt.getID();
                if(id != -1)
                {
                    int methods = tdMethods( id, (TELLSTICK_TURNON | TELLSTICK_TURNOFF) );
                    if ( (methods & TELLSTICK_TURNON ) && (methods & TELLSTICK_TURNOFF) )
                    {
                        /*
                        //Check for remote ctrl action, has the output changes since last time?
                        int last = tdLastSentCommand( id, methods);
                        switch ( last )
                        {
                            case TELLSTICK_TURNON :
                                qDebug() << wt.getName() << "Last cmd was Turn ON";
                                break;
                            case TELLSTICK_TURNOFF :
                                qDebug() << wt.getName() << "Last cmd was Turn OFF";
                                break;
                            default :
                                break;
                        }
                        */

                        WeekTimerOut status = wt.isON(dow, hour, min);
                        switch ( status )
                        {
                            case WT_ON:
                                {
                                    qDebug() << wt.getName() << "Turn ON  at:" << dow << hour << min;
                                    tdTurnOn(id);
                                    sleep(1);
                                }
                                break;
                            case WT_DISABLED:
                                //Do nothing!
                                qDebug() << wt.getName() << "Not active at:" << dow << hour << min;
                                break;
                            case WT_OFF: //Fall thru ok
                            default :
                                {
                                    qDebug() << wt.getName() << "Turn OFF at:" << dow << hour << min;
                                    tdTurnOff(id);
                                    sleep(1);
                                }
                                break;
                        }
                    }
                }
            }
        }
    }

    mosqpp::lib_cleanup();

    return 0;
}