mqtt.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2013 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial contribution
  15. * Ian Craggs - change delimiter option from char to string
  16. * Guilherme Maciel Ferreira - add keep alive option
  17. *******************************************************************************/
  18. /*
  19. stdout subscriber
  20. compulsory parameters:
  21. --topic topic to subscribe to
  22. defaulted parameters:
  23. --host localhost
  24. --port 1883
  25. --qos 2
  26. --delimiter \n
  27. --clientid stdout-subscriber
  28. --showtopics off
  29. --keepalive 10
  30. --userid none
  31. --password none
  32. */
  33. #include "MQTTClient.h"
  34. #include "MQTTClientPersistence.h"
  35. #include <stdio.h>
  36. #include <signal.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include "home_easy.h"
  40. #if defined(WIN32)
  41. #define sleep Sleep
  42. #else
  43. #include <sys/time.h>
  44. #endif
  45. unsigned int id = 0x01379F0E;
  46. volatile int toStop = 0;
  47. typedef struct Item {
  48. char * topic;
  49. unsigned long int id;
  50. char section;
  51. char channel;
  52. char type;
  53. } Item;
  54. struct opts_struct
  55. {
  56. char* clientid;
  57. int nodelimiter;
  58. char* delimiter;
  59. int qos;
  60. char* username;
  61. char* password;
  62. char* host;
  63. char* port;
  64. int showtopics;
  65. int keepalive;
  66. } opts =
  67. {
  68. "stdout-subscriber", 0, "\n", 2, NULL, NULL, "localhost", "1883", 0, 10
  69. };
  70. void usage(void)
  71. {
  72. printf("MQTT stdout subscriber\n");
  73. printf("Usage: stdoutsub topicname <options>, where options are:\n");
  74. printf(" --host <hostname> (default is %s)\n", opts.host);
  75. printf(" --port <port> (default is %s)\n", opts.port);
  76. printf(" --qos <qos> (default is %d)\n", opts.qos);
  77. printf(" --delimiter <delim> (default is \\n)\n");
  78. printf(" --clientid <clientid> (default is %s)\n", opts.clientid);
  79. printf(" --username none\n");
  80. printf(" --password none\n");
  81. printf(" --showtopics <on or off> (default is on if the topic has a wildcard, else off)\n");
  82. printf(" --keepalive <seconds> (default is %d seconds)\n", opts.keepalive);
  83. exit(EXIT_FAILURE);
  84. }
  85. void myconnect(MQTTClient* client, MQTTClient_connectOptions* opts)
  86. {
  87. int rc = 0;
  88. if ((rc = MQTTClient_connect(*client, opts)) != 0)
  89. {
  90. printf("Failed to connect, return code %d\n", rc);
  91. exit(EXIT_FAILURE);
  92. }
  93. }
  94. void cfinish(int sig)
  95. {
  96. signal(SIGINT, NULL);
  97. toStop = 1;
  98. }
  99. void getopts(int argc, char** argv);
  100. int main(int argc, char** argv)
  101. {
  102. MQTTClient client;
  103. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  104. Item lampH = { "chacon/maison/salon/lampehaute", 0x01379F0E, 'A', 1, 0};
  105. Item videoP = { "chacon/maison/salon/videoproj", 0x00DE29E6, 'A', 2, 0};
  106. Item tardis = { "chacon/maison/salon/tardis", 0x01379F0E, 'A', 3, 0};
  107. Item lampC = { "chacon/maison/chambre/lampe", 0x00DE29E6, 'A', 1, 0};
  108. Item lampP = { "chacon/maison/salon/lampe", 0x1379F0E, 'A', 2, 0};
  109. Item portail = { "chacon/maison/portail", 0, 'B', 0, 1};
  110. Item garage = { "chacon/maison/garage", 0, 'A', 0, 1};
  111. //Item lampH = { "chacon/maison/salon/lampehaute", 0x01379F0E, 'A', 1};
  112. Item * listItem[10];
  113. listItem[0] = &lampH;
  114. listItem[1] = &videoP;
  115. listItem[2] = &tardis;
  116. listItem[3] = &lampC;
  117. listItem[4] = &lampP;
  118. listItem[5] = &portail;
  119. listItem[6] = &garage;
  120. char* topic = "chacon/#";
  121. int rc = 0;
  122. char url[100];
  123. opts.showtopics = 1;
  124. if (opts.showtopics)
  125. printf("topic is %s\n", topic);
  126. getopts(argc, argv);
  127. sprintf(url, "%s:%s", opts.host, opts.port);
  128. rc = MQTTClient_create(&client, url, opts.clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL);
  129. signal(SIGINT, cfinish);
  130. signal(SIGTERM, cfinish);
  131. conn_opts.keepAliveInterval = opts.keepalive;
  132. conn_opts.reliable = 0;
  133. conn_opts.cleansession = 1;
  134. // conn_opts.username = opts.username;
  135. // conn_opts.password = opts.password;
  136. myconnect(&client, &conn_opts);
  137. rc = MQTTClient_subscribe(client, topic, opts.qos);
  138. while (!toStop)
  139. {
  140. char* topicName = NULL;
  141. int topicLen;
  142. MQTTClient_message* message = NULL;
  143. rc = MQTTClient_receive(client, &topicName, &topicLen, &message, 1000);
  144. if (message)
  145. {
  146. if (opts.showtopics)
  147. printf("%s\t", topicName);
  148. for(int i=0;i < 7; i++){
  149. if(strcmp(listItem[i]->topic, topicName) == 0){
  150. initIO();
  151. if(listItem[i]->type == 0){
  152. ((char*)message->payload)[message->payloadlen] = 0;
  153. char onoff = strcmp(message->payload,"1") ? 0 : 1;
  154. printf("payload %s %d\n", message->payload, message->payloadlen);
  155. printf("%s %x %c %d %d \n",listItem[i]->topic,listItem[i]->id,listItem[i]->section,listItem[i]->channel, onoff);
  156. sendHomeEasyCommand(listItem[i]->id,listItem[i]->section,listItem[i]->channel, onoff,10);
  157. }else{
  158. printf("sendPortail\n");
  159. sendPortailCommand(0,((char*)message->payload)[0],100);
  160. printf("Command Sent\n");
  161. }
  162. }
  163. }
  164. if (opts.nodelimiter)
  165. printf("%.*s", message->payloadlen, (char*)message->payload);
  166. else
  167. printf("%.*s%s", message->payloadlen, (char*)message->payload, opts.delimiter);
  168. fflush(stdout);
  169. MQTTClient_freeMessage(&message);
  170. MQTTClient_free(topicName);
  171. }
  172. if (rc != 0)
  173. myconnect(&client, &conn_opts);
  174. }
  175. printf("Stopping\n");
  176. MQTTClient_disconnect(client, 0);
  177. MQTTClient_destroy(&client);
  178. return EXIT_SUCCESS;
  179. }
  180. void getopts(int argc, char** argv)
  181. {
  182. int count = 2;
  183. while (count < argc)
  184. {
  185. if (strcmp(argv[count], "--qos") == 0)
  186. {
  187. if (++count < argc)
  188. {
  189. if (strcmp(argv[count], "0") == 0)
  190. opts.qos = 0;
  191. else if (strcmp(argv[count], "1") == 0)
  192. opts.qos = 1;
  193. else if (strcmp(argv[count], "2") == 0)
  194. opts.qos = 2;
  195. else
  196. usage();
  197. }
  198. else
  199. usage();
  200. }
  201. else if (strcmp(argv[count], "--host") == 0)
  202. {
  203. if (++count < argc)
  204. opts.host = argv[count];
  205. else
  206. usage();
  207. }
  208. else if (strcmp(argv[count], "--port") == 0)
  209. {
  210. if (++count < argc)
  211. opts.port = argv[count];
  212. else
  213. usage();
  214. }
  215. else if (strcmp(argv[count], "--clientid") == 0)
  216. {
  217. if (++count < argc)
  218. opts.clientid = argv[count];
  219. else
  220. usage();
  221. }
  222. else if (strcmp(argv[count], "--username") == 0)
  223. {
  224. if (++count < argc)
  225. opts.username = argv[count];
  226. else
  227. usage();
  228. }
  229. else if (strcmp(argv[count], "--password") == 0)
  230. {
  231. if (++count < argc)
  232. opts.password = argv[count];
  233. else
  234. usage();
  235. }
  236. else if (strcmp(argv[count], "--delimiter") == 0)
  237. {
  238. if (++count < argc)
  239. opts.delimiter = argv[count];
  240. else
  241. opts.nodelimiter = 1;
  242. }
  243. else if (strcmp(argv[count], "--showtopics") == 0)
  244. {
  245. if (++count < argc)
  246. {
  247. if (strcmp(argv[count], "on") == 0)
  248. opts.showtopics = 1;
  249. else if (strcmp(argv[count], "off") == 0)
  250. opts.showtopics = 0;
  251. else
  252. usage();
  253. }
  254. else
  255. usage();
  256. }
  257. else if (strcmp(argv[count], "--keepalive") == 0)
  258. {
  259. if (++count < argc)
  260. opts.keepalive = atoi(argv[count]);
  261. else
  262. usage();
  263. }
  264. count++;
  265. }
  266. }