user_esp_platform_timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: esp_platform_user_timer.c
  5. *
  6. * Description:
  7. *
  8. * Modification history:
  9. * 2014/5/09, v1.0 create this file.
  10. *******************************************************************************/
  11. #include "ets_sys.h"
  12. #include "os_type.h"
  13. #include "mem.h"
  14. #include "osapi.h"
  15. #include "user_interface.h"
  16. #include "espconn.h"
  17. #include "user_esp_platform.h"
  18. #define ESP_DEBUG
  19. #ifdef ESP_DEBUG
  20. #define ESP_DBG os_printf
  21. #else
  22. #define ESP_DBG
  23. #endif
  24. LOCAL os_timer_t device_timer;
  25. uint32 min_wait_second;
  26. char timestamp_str[11];
  27. int timestamp = 0;
  28. char *timer_splits[20] = {NULL};
  29. struct esp_platform_wait_timer_param {
  30. uint8 wait_time_param[11];
  31. uint8 wait_action[15];
  32. int wait_time_second;
  33. };
  34. struct wait_param {
  35. uint8 action[20][15];
  36. uint16 action_number;
  37. uint16 count;
  38. uint32 min_time_backup;
  39. };
  40. void esp_platform_timer_action(struct esp_platform_wait_timer_param *timer_wait_param, uint16 count);
  41. /******************************************************************************
  42. * FunctionName : split
  43. * Description : split string p1 according to sting p2 and save the splits
  44. * Parameters : p1 , p2 ,splits[]
  45. * Returns : the number of splits
  46. *******************************************************************************/
  47. uint16 ICACHE_FLASH_ATTR
  48. split(char *p1, char *p2, char *splits[])
  49. {
  50. int i = 0;
  51. int j = 0;
  52. while (i != -1) {
  53. int start = i;
  54. int end = indexof(p1, p2, start);
  55. if (end == -1) {
  56. end = os_strlen(p1);
  57. }
  58. char *p = (char *) os_zalloc(100);
  59. os_memcpy(p, p1 + start, end - start);
  60. p[end - start] = '\0';
  61. splits[j] = p;
  62. j++;
  63. i = end + 1;
  64. if (i > os_strlen(p1)) {
  65. break;
  66. }
  67. }
  68. return j;
  69. }
  70. /******************************************************************************
  71. * FunctionName : indexof
  72. * Description : calculate the offset of p2 relate to start of p1
  73. * Parameters : p1,p1,start
  74. * Returns : the offset of p2 relate to the start
  75. *******************************************************************************/
  76. int ICACHE_FLASH_ATTR
  77. indexof(char *p1, char *p2, int start)
  78. {
  79. char *find = (char *)os_strstr(p1 + start, p2);
  80. if (find != NULL) {
  81. return (find - p1);
  82. }
  83. return -1;
  84. }
  85. /******************************************************************************
  86. * FunctionName : esp_platform_find_min_time
  87. * Description : find the minimum wait second in timer list
  88. * Parameters : timer_wait_param -- param of timer action and wait time param
  89. * count -- The number of timers given by server
  90. * Returns : none
  91. *******************************************************************************/
  92. void ICACHE_FLASH_ATTR
  93. esp_platform_find_min_time(struct esp_platform_wait_timer_param *timer_wait_param , uint16 count)
  94. {
  95. uint16 i = 0;
  96. min_wait_second = 0xFFFFFFF;
  97. for (i = 0; i < count ; i++) {
  98. if (timer_wait_param[i].wait_time_second < min_wait_second && timer_wait_param[i].wait_time_second >= 0) {
  99. min_wait_second = timer_wait_param[i].wait_time_second;
  100. }
  101. }
  102. }
  103. /******************************************************************************
  104. * FunctionName : user_platform_timer_first_start
  105. * Description : calculate the wait time of each timer
  106. * Parameters : count -- The number of timers given by server
  107. * Returns : none
  108. *******************************************************************************/
  109. void ICACHE_FLASH_ATTR
  110. user_platform_timer_first_start(uint16 count)
  111. {
  112. int i = 0;
  113. struct esp_platform_wait_timer_param timer_wait_param[100] = {0};
  114. ESP_DBG("current timestamp= %ds\n", timestamp);
  115. timestamp = timestamp + min_wait_second;
  116. for (i = 0 ; i < count ; i++) {
  117. char *str = timer_splits[i];
  118. if (indexof(str, "f", 0) == 0) {
  119. char *fixed_wait[2];
  120. ESP_DBG("timer is fixed mode\n");
  121. split(str, "=", fixed_wait);
  122. os_memcpy(timer_wait_param[i].wait_time_param, fixed_wait[0] + 1, os_strlen(fixed_wait[0]) - 1);
  123. os_memcpy(timer_wait_param[i].wait_action, fixed_wait[1], os_strlen(fixed_wait[1]));
  124. timer_wait_param[i].wait_time_second = atoi(timer_wait_param[i].wait_time_param) - timestamp;
  125. os_free(fixed_wait[0]);
  126. os_free(fixed_wait[1]);
  127. }
  128. else if (indexof(str, "l", 0) == 0) {
  129. char *loop_wait[2];
  130. ESP_DBG("timer is loop mode\n");
  131. split(str, "=", loop_wait);
  132. os_memcpy(timer_wait_param[i].wait_time_param, loop_wait[0] + 1, os_strlen(loop_wait[0]) - 1);
  133. os_memcpy(timer_wait_param[i].wait_action, loop_wait[1], os_strlen(loop_wait[1]));
  134. timer_wait_param[i].wait_time_second = atoi(timer_wait_param[i].wait_time_param) - (timestamp % atoi(timer_wait_param[i].wait_time_param));
  135. os_free(loop_wait[0]);
  136. os_free(loop_wait[1]);
  137. } else if (indexof(str, "w", 0) == 0) {
  138. char *week_wait[2];
  139. int monday_wait_time = 0;
  140. ESP_DBG("timer is weekend mode\n");
  141. split(str, "=", week_wait);
  142. os_memcpy(timer_wait_param[i].wait_time_param, week_wait[0] + 1, os_strlen(week_wait[0]) - 1);
  143. os_memcpy(timer_wait_param[i].wait_action, week_wait[1], os_strlen(week_wait[1]));
  144. monday_wait_time = (timestamp - 1388937600) % (7 * 24 * 3600);
  145. ESP_DBG("monday_wait_time == %d", monday_wait_time);
  146. if (atoi(timer_wait_param[i].wait_time_param) > monday_wait_time) {
  147. timer_wait_param[i].wait_time_second = atoi(timer_wait_param[i].wait_time_param) - monday_wait_time;
  148. } else {
  149. timer_wait_param[i].wait_time_second = 7 * 24 * 3600 - monday_wait_time + atoi(timer_wait_param[i].wait_time_param);
  150. }
  151. os_free(week_wait[0]);
  152. os_free(week_wait[1]);
  153. }
  154. }
  155. esp_platform_find_min_time(timer_wait_param, count);
  156. if(min_wait_second == 0) {
  157. return;
  158. }
  159. esp_platform_timer_action(timer_wait_param, count);
  160. }
  161. /******************************************************************************
  162. * FunctionName : user_esp_platform_device_action
  163. * Description : Execute the actions of minimum wait time
  164. * Parameters : pwait_action -- point the list of actions which need execute
  165. *
  166. * Returns : none
  167. *******************************************************************************/
  168. void ICACHE_FLASH_ATTR
  169. user_esp_platform_device_action(struct wait_param *pwait_action)
  170. {
  171. uint8 i = 0;
  172. uint16 count = pwait_action->count;
  173. uint16 action_number = pwait_action->action_number;
  174. ESP_DBG("there is %d action at the same time\n", pwait_action->action_number);
  175. #if PLUG_DEVICE
  176. for (i = 0; i < action_number && pwait_action->action[i][0] != '0'; i++) {
  177. ESP_DBG("%s\n",pwait_action->action[i]);
  178. if (os_strcmp(pwait_action->action[i], "on_switch", 9) == 0) {
  179. user_plug_set_status(0x01);
  180. } else if (os_strcmp(pwait_action->action[i], "off_switch", 10) == 0) {
  181. user_plug_set_status(0x00);
  182. } else if (os_strcmp(pwait_action->action[i], "on_off_switch", 13) == 0) {
  183. if (user_plug_get_status() == 0) {
  184. user_plug_set_status(0x01);
  185. } else {
  186. user_plug_set_status(0x00);
  187. }
  188. } else {
  189. return;
  190. }
  191. }
  192. user_platform_timer_first_start(count);
  193. #endif
  194. }
  195. /******************************************************************************
  196. * FunctionName : user_platform_timer_start
  197. * Description : Processing the message about timer from the server
  198. * Parameters : timer_wait_param -- The received data from the server
  199. * count -- the espconn used to connetion with the host
  200. * Returns : none
  201. *******************************************************************************/
  202. void ICACHE_FLASH_ATTR
  203. user_esp_platform_wait_time_overflow_check(struct wait_param *pwait_action)
  204. {
  205. ESP_DBG("min_wait_second = %d", min_wait_second);
  206. if (pwait_action->min_time_backup >= 3600) {
  207. os_timer_disarm(&device_timer);
  208. os_timer_setfn(&device_timer, (os_timer_func_t *)user_esp_platform_wait_time_overflow_check, pwait_action);
  209. os_timer_arm(&device_timer, 3600000, 0);
  210. ESP_DBG("min_wait_second is extended\n");
  211. } else {
  212. os_timer_disarm(&device_timer);
  213. os_timer_setfn(&device_timer, (os_timer_func_t *)user_esp_platform_device_action, pwait_action);
  214. os_timer_arm(&device_timer, pwait_action->min_time_backup * 1000, 0);
  215. ESP_DBG("min_wait_second is = %dms\n", pwait_action->min_time_backup * 1000);
  216. }
  217. pwait_action->min_time_backup -= 3600;
  218. }
  219. /******************************************************************************
  220. * FunctionName : user_platform_timer_start
  221. * Description : Processing the message about timer from the server
  222. * Parameters : timer_wait_param -- The received data from the server
  223. * count -- the espconn used to connetion with the host
  224. * Returns : none
  225. *******************************************************************************/
  226. void ICACHE_FLASH_ATTR
  227. esp_platform_timer_action(struct esp_platform_wait_timer_param *timer_wait_param, uint16 count)
  228. {
  229. uint16 i = 0;
  230. uint16 action_number;
  231. struct wait_param pwait_action = {0};
  232. pwait_action.count = count;
  233. action_number = 0;
  234. for (i = 0; i < count ; i++) {
  235. if (timer_wait_param[i].wait_time_second == min_wait_second) {
  236. os_memcpy(pwait_action.action[action_number], timer_wait_param[i].wait_action, os_strlen(timer_wait_param[i].wait_action));
  237. ESP_DBG("*****%s*****\n", timer_wait_param[i].wait_action);
  238. action_number++;
  239. }
  240. }
  241. pwait_action.action_number = action_number;
  242. pwait_action.min_time_backup = min_wait_second;
  243. user_esp_platform_wait_time_overflow_check(&pwait_action);
  244. }
  245. /******************************************************************************
  246. * FunctionName : user_platform_timer_start
  247. * Description : Processing the message about timer from the server
  248. * Parameters : pbuffer -- The received data from the server
  249. * Returns : none
  250. *******************************************************************************/
  251. void ICACHE_FLASH_ATTR
  252. user_platform_timer_start(char *pbuffer)
  253. {
  254. int str_begin = 0;
  255. int str_end = 0;
  256. uint8 i = 0;
  257. char *pstr_start = NULL;
  258. char *pstr_end = NULL;
  259. struct esp_platform_wait_timer_param timer_wait_param[20];
  260. char *pstr = NULL;
  261. min_wait_second = 0;
  262. if ((pstr = (char *)os_strstr(pbuffer, "\"timestamp\":")) != NULL) {
  263. pstr_start = pstr + 13;
  264. pstr_end = (char *)os_strstr(pstr_start, ",");
  265. if (pstr != NULL) {
  266. os_memcpy(timestamp_str, pstr_start, pstr_end - pstr_start);
  267. timestamp = atoi(timestamp_str);
  268. }
  269. }
  270. for (i = 0 ; i < 20 ; i++) {
  271. if (timer_splits[i] != NULL) {
  272. os_free(timer_splits[i]);
  273. timer_splits[i] = NULL;
  274. }
  275. }
  276. if ((pstr_start = (char *)os_strstr(pbuffer, "\"timers\": \"")) != NULL) {
  277. str_begin = 11;
  278. str_end = indexof(pstr_start, "\"", str_begin);
  279. if (str_begin == str_end) {
  280. os_timer_disarm(&device_timer);
  281. return;
  282. }
  283. char *split_buffer = (char *)os_zalloc(str_end - str_begin + 1);
  284. os_memcpy(split_buffer, pstr_start + str_begin, str_end - str_begin);
  285. uint16 count = split(split_buffer , ";" , timer_splits);
  286. os_free(split_buffer);
  287. user_platform_timer_first_start(count);
  288. }
  289. }