int main(void) { char protocol[DATA_LENGTH], model[DATA_LENGTH]; int sensorId = 0, dataTypes = 0; char value[DATA_LENGTH]; char timeBuf[80]; time_t timestamp = 0; tdInit(); while(tdSensor(protocol, DATA_LENGTH, model, DATA_LENGTH, &sensorId, &dataTypes) == TELLSTICK_SUCCESS) { //Print the sensor printf("%s,\t%s,\t%i\n", protocol, model, sensorId); //Retrieve the values the sensor supports if (dataTypes & TELLSTICK_TEMPERATURE) { tdSensorValue(protocol, model, sensorId, TELLSTICK_TEMPERATURE, value, DATA_LENGTH, (int *)×tamp); strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp)); printf("Temperature:\t%sÂș\t(%s)\n", value, timeBuf); } if (dataTypes & TELLSTICK_HUMIDITY) { tdSensorValue(protocol, model, sensorId, TELLSTICK_HUMIDITY, value, DATA_LENGTH, (int *)×tamp); strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp)); printf("Humidity:\t%s%%\t(%s)\n", value, timeBuf); } printf("\n"); } tdClose(); return 0; }
int main(void) { tdInit(); Events ev; //Our own simple eventloop while(1) { sleep(100); } tdClose(); return 0; }
int main(void) { int callbackId = 0; tdInit(); //Register for callback callbackId = tdRegisterSensorEvent( (TDSensorEvent)&sensorEvent, 0 ); //Our own simple eventloop while(1) { sleep(100); } //Cleanup tdUnregisterCallback( callbackId ); tdClose(); return 0; }
int main(int argc, char *argv[]) { int port = 1883; int reconnect_delay = 1; char *host = "localhost"; char subscription[128]; int rawcallback; struct context *ctx = malloc(sizeof(struct context)+sizeof(default_relay_rules)); ctx->debug = 0; ctx->failures = 0; ctx->relay_rules=default_relay_rules; // TODO read relay rules from a configuration file ctx->num_relay_rules=sizeof(default_relay_rules)/sizeof(*default_relay_rules); int opt; ctx->sub_prefix = "telldus"; ctx->pub_prefix = ""; struct relay_rule *relay_rules = ctx->relay_rules; snprintf(subscription,sizeof(subscription)-1,"%s/#",ctx->sub_prefix); while ((opt = getopt(argc, argv, "vS:d:h:p:P:")) != -1) { switch (opt) { case 'v': ctx->debug++; break; case 'h': host=strdup(optarg); break; case 'S': ctx->sub_prefix=strdup(optarg); snprintf(subscription,sizeof(subscription)-1,"%s/#",ctx->sub_prefix); break; case 'P': ctx->pub_prefix=strdup(optarg); break; case 'p': port=atoi(optarg); break; case 'd': reconnect_delay=atoi(optarg); break; default: /* '?' */ fprintf(stderr, "Usage: %s [-v] " "[-h <host>] " "[-p <port>]\n\t" "[-S <subscription topic prefix>] " "[-P <publishing topic prefix>]\n\t" "[-d <reconnect after n seconds> ]\n\n" "\t%s connects to MQTT broker at %s:%d.\n\n" "\tIt subscribes messages for topic '%s'.\n" "\tWhen a 'turnon', 'turnoff' or 'bell' message is received at %s/<device>/method it will trigger\n" "\tthe corresponding operation on a Telldus device with the same name.\n", argv[0], argv[0], host, port, subscription, ctx->sub_prefix); fprintf(stderr, "\n\tIt listens for raw events from Telldus.\n"); int f; for(f=0; f<ctx->num_relay_rules; f++) { fprintf(stderr, "\tWhen it receives a raw event where "); int i = 0; const char *separator=""; while(ctx->relay_rules[f].filters[i].key && relay_rules[f].filters[i].value) { fprintf(stderr, "%sfield '%s' value is '%s'", separator, relay_rules[f].filters[i].key, relay_rules[f].filters[i].value); i++; separator = relay_rules[f].filters[i+1].key ? ", " : " and "; } separator=""; fprintf(stderr, "\n\t\tit publishes "); int j = 0; while(relay_rules[f].mqtt_template[j].topicformat && relay_rules[f].mqtt_template[j].messageformat) { fprintf(stderr, "%sa message '%s' on topic '%s%s'", separator, relay_rules[f].mqtt_template[j].messageformat, ctx->pub_prefix, relay_rules[f].mqtt_template[j].topicformat); j++; separator = relay_rules[f].mqtt_template[j+1].topicformat ? ", \n\t\t" : " and \n\t\t"; } fprintf(stderr, "\n"); } exit(EXIT_FAILURE); } } tdInit(); // TODO: move after mqtt connection has been established when unregistering and re-registering works ok rawcallback = tdRegisterRawDeviceEvent(raw_event,ctx); do { ctx->failures = 0; // TODO: synchronization for telldus threading char hostname[21]; char id[30]; memset(hostname, 0, sizeof(hostname)); gethostname(hostname, sizeof(hostname)-1); snprintf(id, sizeof(id)-1, "mosq_pub_%d_%s", getpid(), hostname); mosquitto_lib_init(); ctx->mosq = mosquitto_new(id, true, ctx); if(!ctx->mosq) { fprintf(stderr, "Error: Out of memory.\n"); return 1; } if(ctx->debug > 0) { mosquitto_log_callback_set(ctx->mosq, my_log_callback); } int rc; rc = mosquitto_connect(ctx->mosq, host, port, 30); if(rc) { if(ctx->debug > 0) { fprintf(stderr, "failed to connect %s:%d\n", host, port); } goto clean; } mosquitto_message_callback_set(ctx->mosq, my_message_callback); rc = mosquitto_subscribe(ctx->mosq, NULL, subscription, 0); if(rc) { if(ctx->debug > 0) { fprintf(stderr, "failed to subscribe %s\n", subscription); } goto clean; } do { rc = mosquitto_loop(ctx->mosq, 60, 1); } while(rc == MOSQ_ERR_SUCCESS && !ctx->failures); clean: mosquitto_destroy(ctx->mosq); mosquitto_lib_cleanup(); } while(reconnect_delay >= 0 && ( sleep(reconnect_delay), true)); tdUnregisterCallback(rawcallback); tdClose(); return 0; }
bool CTellstick::StopHardware() { tdClose(); m_bIsStarted=false; return true; }