示例#1
0
void sendDevList(xPL_MessagePtr msg) {
	int deviceCount = 0, i;
	char deviceList[128];
	xPL_MessagePtr message = NULL;

	message = xPL_createBroadcastMessage(telldusService, xPL_MESSAGE_STATUS);
	xPL_setSchema(message, "lighting", "devlist");

	xPL_setMessageNamedValue(message, "network", xPL_getMessageNamedValue(msg, "network"));

	if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(msg, "network"), "1") == 0) {
		xPL_setMessageNamedValue(message, "status", "ok");
		deviceCount = tdGetNumberOfDevices();
		for( i = 0; i < deviceCount; ++i ) {
			if (i == 0) { /* First */
				strcpy(deviceList, xPL_intToStr(tdGetDeviceId(i)));
			} else {
				strcat(deviceList, ",");
				strcat(deviceList, xPL_intToStr(tdGetDeviceId(i)));
			}
		}

		xPL_setMessageNamedValue(message, "device-count", xPL_intToStr(deviceCount) );
		xPL_setMessageNamedValue(message, "device", deviceList );
	} else {
		xPL_setMessageNamedValue(message, "status", "not-found");
	}

	xPL_sendMessage(message);

	xPL_releaseMessage(message);
}
示例#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
文件: main.cpp 项目: 4ib3r/domoticz
void print_device( int index ) {
	tdInit();
	int intId = tdGetDeviceId(index);
	char *name = tdGetName(intId);
	printf("%i\t%s\t", intId, name);
	tdReleaseString(name);
	int lastSentCommand = tdLastSentCommand(intId, SUPPORTED_METHODS);
	char *level = 0;
	switch(lastSentCommand) {
		case TELLSTICK_TURNON:
			printf("ON");
			break;
		case TELLSTICK_TURNOFF:
			printf("OFF");
			break;
		case TELLSTICK_DIM:
			level = tdLastSentValue(intId);
			printf("DIMMED:%s", level);
			tdReleaseString(level);
			break;
		default:
			printf("Unknown state");
	}
	printf("\n");
}
示例#4
0
void sendDevInfo(xPL_MessagePtr msg) {
	xPL_MessagePtr message = NULL;
	Bool found = FALSE, deviceCount = tdGetNumberOfDevices();
	int deviceId = 0, methods = 0, i = 0, lastSentCommand, level = 0;
	char *name, buffer[12], *value;

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

	message = xPL_createBroadcastMessage(telldusService, xPL_MESSAGE_STATUS);
	xPL_setSchema(message, "lighting", "devinfo");

	xPL_setMessageNamedValue(message, "network", xPL_getMessageNamedValue(msg, "network"));
	xPL_setMessageNamedValue(message, "device", xPL_getMessageNamedValue(msg, "device"));

	if (xPL_strcmpIgnoreCase(xPL_getMessageNamedValue(msg, "network"), "1") == 0 && found == TRUE) {
		lastSentCommand = tdLastSentCommand(deviceId, TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM);
		if (lastSentCommand == TELLSTICK_TURNON) {
			level = 100;
		} else if (lastSentCommand == TELLSTICK_DIM) {
			value = tdLastSentValue(deviceId);
			level = atoi(value);
			free(value);
 			level = round((float)level / 255.0 * 100.0);
			if (level > 100) {
				level = 100;
			} else if (level < 0) {
				level = 0;
			}
		} else {
			level = 0;
		}
		methods = tdMethods(deviceId, TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM);
		name = tdGetName(deviceId);
		sprintf(buffer, "1,%s,0,%i", (methods & TELLSTICK_DIM ? "true" : "false"), level);
		xPL_setMessageNamedValue(message, "status", "ok");
		xPL_setMessageNamedValue(message, "name", name );
		xPL_setMessageNamedValue(message, "report-on-manual", "false" );
		xPL_setMessageNamedValue(message, "channel-count", "1" );
		xPL_setMessageNamedValue(message, "primary-channel", "1" );
		xPL_setMessageNamedValue(message, "channel", buffer );
		xPL_setMessageNamedValue(message, "scene-count", "0" );

		free(name);
	} else {
		xPL_setMessageNamedValue(message, "status", "not-found");
	}

	xPL_sendMessage(message);

	xPL_releaseMessage(message);
}
示例#5
0
void CTellstick::Init()
{
  tdInit();
  tdRegisterDeviceEvent( reinterpret_cast<TDDeviceEvent>(&CTellstick::deviceEventCallback), this );
  int intNumberOfDevices = tdGetNumberOfDevices();
  for (int i = 0; i < intNumberOfDevices; i++) {
    int id = tdGetDeviceId(i);
    char *name = tdGetName(id);
    _log.Log(LOG_NORM, "Tellstick: %s method %d", name, tdMethods(id, TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM) & TELLSTICK_DIM);
    bool isDimmer = tdMethods(id, TELLSTICK_TURNON | TELLSTICK_TURNOFF | TELLSTICK_DIM) & TELLSTICK_DIM;
    AddSwitchIfNotExits(id, name, isDimmer);
    tdReleaseString(name);
  }

}
示例#6
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;
}