iot_schedule.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (c) 2021 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "schedule_list.h"
  19. #include "schedule_store.h"
  20. #include "iot_schedule.h"
  21. #define MAX_SCHEDULE_LIST 7
  22. static SCHEDULE_LIST gScheduleList[MAX_SCHEDULE_LIST];
  23. static int g_isUpdate = 0;
  24. static int g_startSeconds = -1;
  25. static int g_durationSeconds = -1;
  26. static int g_curWeekDay = -1;
  27. static ScheduleCommand g_cmd = {0};
  28. static int g_oneTime = 0;
  29. // 获取类似 1001101 结构的weeks 的天数 返回 数组 Days下标和 值都是周几
  30. static int GetWeekDay(unsigned char weekday, int Days[], int size)
  31. {
  32. int n = 0;
  33. if (size != MAX_SCHEDULE_LIST) {
  34. SCH_ERR("weekday array is OOR! \n");
  35. return -1;
  36. }
  37. for (int i = 0; i < MAX_SCHEDULE_LIST; i++) {
  38. if ((weekday >> i) & 0x01) {
  39. Days[n++] = i;
  40. }
  41. }
  42. return n;
  43. }
  44. // 将每一天都放进一个链表的尾部
  45. static void ScheduleGetStore(void)
  46. {
  47. int total = ScheduleStoreGetTotal();
  48. if (total <= 0) {
  49. SCH_ERR("no schedule stored! \n");
  50. return;
  51. }
  52. for (int i = 0; i < total; i++) {
  53. ScheduleInfo info = {0};
  54. int weeknum = 0;
  55. int week[MAX_SCHEDULE_LIST] = {0};
  56. if (ScheduleStoreGetInfo(&info, i) < 0) {
  57. SCH_ERR("read schedulestore %d failed! \n", i);
  58. continue;
  59. }
  60. weeknum = GetWeekDay(info.week, week, MAX_SCHEDULE_LIST);
  61. if (weeknum <= 0) {
  62. SCH_ERR("the schedule has no weekday info! \n");
  63. continue;
  64. }
  65. for (int j = 0; j < weeknum; j++) {
  66. int weekday = week[j];
  67. if (weekday >= MAX_SCHEDULE_LIST) {
  68. SCH_ERR("weekday is OOR!\n");
  69. break;
  70. }
  71. if (ScheduleListAppend(gScheduleList[weekday], &info) < 0) {
  72. SCH_ERR("ScheduleListAppend failed!\n");
  73. break;
  74. }
  75. SCH_DBG("add info : id--%s, starttime--%d, duration--%d, week(%d)\n",
  76. info.id, info.starttime, info.duration, weekday);
  77. }
  78. }
  79. }
  80. // 创建链表
  81. int IOT_ScheduleInit(void)
  82. {
  83. int retval = 0;
  84. for (int i = 0; i < MAX_SCHEDULE_LIST; i++) {
  85. gScheduleList[i] = ScheduleListCreate(NULL);
  86. if (gScheduleList[i] == NULL) {
  87. SCH_ERR("ScheduleListCreate (%d) failed! \n", i);
  88. retval = -1;
  89. break;
  90. }
  91. }
  92. if (retval < 0) {
  93. for (int i = 0; i < MAX_SCHEDULE_LIST; i++) {
  94. if (gScheduleList[i] != NULL) {
  95. ScheduleListDestroy(gScheduleList[i]);
  96. gScheduleList[i] = NULL;
  97. }
  98. }
  99. } else {
  100. ScheduleGetStore();
  101. g_isUpdate = 1;
  102. }
  103. return retval;
  104. }
  105. // 根据新的数据更新链表
  106. static int ScheduleUpdate(const char *id, int *day, int size, int startTime, int durTime,
  107. SCHEDULE_OPTION option, int cmd, int value)
  108. {
  109. typedef int (*OptionFunc)(SCHEDULE_LIST mHandle, ScheduleInfo *info);
  110. OptionFunc opFunc[] = {ScheduleListAppend, ScheduleListUpdate, ScheduleListDelete};
  111. ScheduleInfo info = {0};
  112. info.starttime = startTime;
  113. info.duration = durTime;
  114. info.scheduleCmd.cmd = cmd;
  115. info.scheduleCmd.value = value;
  116. SCH_DBG("cmd = %d, value = %d \n", cmd, value);
  117. if (option > SCHEDULE_OPTION_DELETE) {
  118. SCH_ERR("OPTION OOR!\n");
  119. return -1;
  120. }
  121. if (size == 1 && day[0] == 0) { // one time schedule
  122. g_oneTime = 1;
  123. g_isUpdate = 1;
  124. g_startSeconds = startTime;
  125. g_durationSeconds = durTime;
  126. g_cmd.value = value;
  127. g_cmd.cmd = cmd;
  128. SCH_DBG("this schedule is onetime! \n");
  129. return 0;
  130. }
  131. strncpy(info.id, id, strlen(id));
  132. for (int i = 0; i < size; i++) {
  133. int idx = day[i] - 1;
  134. if (idx >= MAX_SCHEDULE_LIST) {
  135. SCH_ERR("weekday is OOR! \n");
  136. continue;
  137. }
  138. info.week += (1 << idx);
  139. if (opFunc[option](gScheduleList[idx], &info) < 0) {
  140. SCH_ERR("ScheduleListAppend failed! \n");
  141. return -1;
  142. }
  143. SCH_DBG("add info : id--%s, starttime--%d, duration--%d, week(%d)--0x%x, cmd--%d, value -- %d\n",
  144. info.id, info.starttime, info.duration, idx, info.week, info.scheduleCmd.cmd, info.scheduleCmd.value);
  145. }
  146. ScheduleStoreUpdate(&info, option);
  147. if ((option == SCHEDULE_OPTION_DELETE) && (g_startSeconds == startTime)) {
  148. g_startSeconds = 0;
  149. g_durationSeconds = 0;
  150. }
  151. g_isUpdate = 1;
  152. return 0;
  153. }
  154. int IOT_ScheduleAdd(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value)
  155. {
  156. return ScheduleUpdate(id, day, size, startTime, durTime, SCHEDULE_OPTION_ADD, cmd, value);
  157. }
  158. int IOT_ScheduleUpdate(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value)
  159. {
  160. return ScheduleUpdate(id, day, size, startTime, durTime, SCHEDULE_OPTION_UPDATE, cmd, value);
  161. }
  162. int IOT_ScheduleDelete(const char *id, int *day, int size, int startTime, int durTime, int cmd, int value)
  163. {
  164. return ScheduleUpdate(id, day, size, startTime, durTime, SCHEDULE_OPTION_DELETE, cmd, value);
  165. }
  166. int IOT_ScheduleIsUpdate(unsigned int weekday, int curtime)
  167. {
  168. int total = 0;
  169. int retval = 0;
  170. if (g_isUpdate == 0) {
  171. return 0;
  172. }
  173. g_isUpdate = 0;
  174. if (g_oneTime) {
  175. g_oneTime = 0;
  176. if (g_startSeconds > curtime) {
  177. SCH_DBG("get one time schedule! \n");
  178. return 1;
  179. }
  180. }
  181. if (weekday >= MAX_SCHEDULE_LIST || curtime < 0) {
  182. return 0;
  183. }
  184. total = ScheduleListGetSize(gScheduleList[weekday]);
  185. if (total <= 0) {
  186. return 0;
  187. }
  188. for (int i = 0; i < total; i++) {
  189. ScheduleInfo info;
  190. if (ScheduleListGet(gScheduleList[weekday], i, (ScheduleInfo *)&info) < 0) {
  191. SCH_ERR("ScheduleListGet failed! \n");
  192. return 0;
  193. }
  194. if (curtime < (info.starttime + info.duration)) {
  195. if (g_curWeekDay == weekday) {
  196. if (g_startSeconds != info.starttime ||
  197. g_durationSeconds != info.duration) {
  198. g_startSeconds = info.starttime;
  199. g_durationSeconds = info.duration;
  200. g_cmd.cmd = info.scheduleCmd.cmd;
  201. g_cmd.value = info.scheduleCmd.value;
  202. retval = 1;
  203. }
  204. } else {
  205. g_curWeekDay = weekday;
  206. g_startSeconds = info.starttime;
  207. g_durationSeconds = info.duration;
  208. g_cmd.cmd = info.scheduleCmd.cmd;
  209. g_cmd.value = info.scheduleCmd.value;
  210. retval = 1;
  211. }
  212. break;
  213. }
  214. }
  215. if (retval == 1) {
  216. SCH_DBG("cmd = %d, value = %d \n", g_cmd.cmd, g_cmd.value);
  217. }
  218. return retval;
  219. }
  220. void IOT_ScheduleSetUpdate(int update)
  221. {
  222. g_isUpdate = update;
  223. }
  224. int IOT_ScheduleGetStartTime(void)
  225. {
  226. return g_startSeconds;
  227. }
  228. int IOT_ScheduleGetDurationTime(void)
  229. {
  230. return g_durationSeconds;
  231. }
  232. int IOT_ScheduleGetCommand(int *cmd, int *value)
  233. {
  234. if (cmd != NULL) {
  235. *cmd = g_cmd.cmd;
  236. }
  237. if (value != NULL) {
  238. *value = g_cmd.value;
  239. }
  240. return 0;
  241. }
  242. void IOT_ScheduleDeinit(void)
  243. {
  244. for (int i = 0; i < MAX_SCHEDULE_LIST; i++) {
  245. if (gScheduleList[i] != NULL) {
  246. ScheduleListDestroy(gScheduleList[i]);
  247. gScheduleList[i] = NULL;
  248. }
  249. }
  250. }