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"); }
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); } }
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); } }
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; }
int list_devices() { tdInit(); int intNum = tdGetNumberOfDevices(); if (intNum < 0) { char *errorString = tdGetErrorString(intNum); fprintf(stderr, "Error fetching devices: %s\n", errorString); tdReleaseString(errorString); return intNum; } printf("Number of devices: %i\n", intNum); int i = 0; while (i < intNum) { print_device( i ); i++; } char protocol[DATA_LENGTH], model[DATA_LENGTH]; int sensorId = 0, dataTypes = 0; int sensorStatus = tdSensor(protocol, DATA_LENGTH, model, DATA_LENGTH, &sensorId, &dataTypes); if(sensorStatus == 0){ printf("\n\nSENSORS:\n\n%-20s\t%-20s\t%-5s\t%-5s\t%-8s\t%-20s\t%-20s\t%-20s\n", "PROTOCOL", "MODEL", "ID", "TEMP", "HUMIDITY", "RAIN", "WIND", "LAST UPDATED"); } while(sensorStatus == 0){ char tempvalue[DATA_LENGTH]; tempvalue[0] = 0; char humidityvalue[DATA_LENGTH]; humidityvalue[0] = 0; char windvalue[DATA_LENGTH]; windvalue[0] = 0; char winddirectionvalue[DATA_LENGTH]; winddirectionvalue[0] = 0; char windaveragevalue[DATA_LENGTH]; windaveragevalue[0] = 0; char windgustvalue[DATA_LENGTH]; windgustvalue[0] = 0; char rainvalue[DATA_LENGTH]; rainvalue[0] = 0; char raintotvalue[DATA_LENGTH]; raintotvalue[0] = 0; char rainratevalue[DATA_LENGTH]; rainratevalue[0] = 0; char timeBuf[80]; timeBuf[0] = 0; time_t timestamp = 0; if (dataTypes & TELLSTICK_TEMPERATURE) { tdSensorValue(protocol, model, sensorId, TELLSTICK_TEMPERATURE, tempvalue, DATA_LENGTH, (int *)×tamp); strcat(tempvalue, DEGREE); strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp)); } if (dataTypes & TELLSTICK_HUMIDITY) { tdSensorValue(protocol, model, sensorId, TELLSTICK_HUMIDITY, humidityvalue, DATA_LENGTH, (int *)×tamp); strcat(humidityvalue, "%"); strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp)); } if (dataTypes & TELLSTICK_RAINRATE) { tdSensorValue(protocol, model, sensorId, TELLSTICK_RAINRATE, rainratevalue, DATA_LENGTH, (int *)×tamp); strcat(rainratevalue, " mm/h, "); strcat(rainvalue, rainratevalue); strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp)); } if (dataTypes & TELLSTICK_RAINTOTAL) { tdSensorValue(protocol, model, sensorId, TELLSTICK_RAINTOTAL, raintotvalue, DATA_LENGTH, (int *)×tamp); //TODO detta blir väl fel, kan väl hamna i andra ordningar, eller hur? strcat(raintotvalue, " mm"); strcat(rainvalue, raintotvalue); strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp)); } if (dataTypes & TELLSTICK_WINDDIRECTION) { tdSensorValue(protocol, model, sensorId, TELLSTICK_WINDDIRECTION, winddirectionvalue, DATA_LENGTH, (int *)×tamp); //TODO or use charToInteger in common? std::stringstream inputstream; inputstream << winddirectionvalue; int direction; inputstream >> direction; direction = direction / 22.5; std::string directionabbrev = "N"; switch (direction) { case 1: directionabbrev = "NNE"; break; case 2: directionabbrev = "NE"; break; case 3: directionabbrev = "ENE"; break; case 4: directionabbrev = "E"; break; case 5: directionabbrev = "ESE"; break; case 6: directionabbrev = "SE"; break; case 7: directionabbrev = "SSE"; break; case 8: directionabbrev = "S"; break; case 9: directionabbrev = "SSW"; break; case 10: directionabbrev = "SW"; break; case 11: directionabbrev = "WSW"; break; case 12: directionabbrev = "W"; break; case 13: directionabbrev = "WNW"; break; case 14: directionabbrev = "NW"; break; case 15: directionabbrev = "NNW"; break; } strcat(windvalue, directionabbrev.c_str()); strcat(windvalue, ", "); strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp)); } if (dataTypes & TELLSTICK_WINDAVERAGE) { tdSensorValue(protocol, model, sensorId, TELLSTICK_WINDAVERAGE, windaveragevalue, DATA_LENGTH, (int *)×tamp); strcat(windaveragevalue, " m/s ("); strcat(windvalue, windaveragevalue); strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp)); } if (dataTypes & TELLSTICK_WINDGUST) { tdSensorValue(protocol, model, sensorId, TELLSTICK_WINDGUST, windgustvalue, DATA_LENGTH, (int *)×tamp); strcat(windgustvalue, " m/s) "); strcat(windvalue, windgustvalue); strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp)); } printf("%-20s\t%-20s\t%-5i\t%-5s\t%-8s\t%-20s\t%-20s\t%-20s\n", protocol, model, sensorId, tempvalue, humidityvalue, rainvalue, windvalue, timeBuf); sensorStatus = tdSensor(protocol, DATA_LENGTH, model, DATA_LENGTH, &sensorId, &dataTypes); }