alarm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #define LOG_MODULE LOG_MAIN_MODULE
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "log.h"
  6. #include "rtc.h"
  7. #include "alarm.h"
  8. #define n 3
  9. struct _snooze
  10. {
  11. struct _tm snoozeStart;
  12. struct _tm snoozeEnd;
  13. };
  14. struct _alarm alarm[n];
  15. struct _snooze snooze[n];
  16. int checkAlarms(){
  17. int i = 0;
  18. int check = 0;
  19. for (i = 0; i < n; i++){
  20. setState(i);
  21. if (alarm[i].state == 1){
  22. check = 1;
  23. }
  24. }
  25. if (check == 1){
  26. return 1;
  27. }
  28. return 0;
  29. }
  30. struct _alarm getAlarm(int idx){
  31. return alarm[idx];
  32. }
  33. int getState(int idx){
  34. return alarm[idx].state;
  35. }
  36. void setState(int idx){
  37. struct _tm ct;
  38. X12RtcGetClock(&ct);
  39. if (compareTime(ct, alarm[idx].time) == 1 && alarm[idx].time.tm_year != 0){
  40. alarm[idx].state = 1;
  41. } else {
  42. alarm[idx].state = 0;
  43. }
  44. /*if (compareTime(alarm[idx].time,snooze[idx].snoozeStart)){
  45. alarm[idx].state = 2;
  46. }
  47. if (alarm[idx].state == 2){
  48. if (compareTime(alarm[idx].time, snooze[idx].snoozeEnd)){
  49. snooze[idx].snoozeStart.tm_min += alarm[idx].snooze + 1;
  50. snooze[idx].snoozeEnd.tm_min += alarm[idx].snooze + 1;
  51. }
  52. }*/
  53. }
  54. /*void getAlarm(struct _alarm *am){
  55. int i = 0;
  56. for (i = 0; i < n; i++){
  57. am[i] = alarm[i];
  58. }
  59. }*/
  60. void setAlarm(struct _tm time, char* name, char* ip, u_short port, int snooze, int type, int idx){
  61. alarm[idx].time = time;
  62. strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
  63. strncpy(alarm[idx].ip, name, sizeof(alarm[idx].ip));
  64. alarm[idx].port = port;
  65. alarm[idx].snooze = snooze;
  66. alarm[idx].type = type;
  67. alarm[idx].state = 0;
  68. //snooze[idx].snoozeStart = time;
  69. //snooze[idx].snoozeEnd = time;
  70. //snooze[idx].snoozeStart += 1;
  71. //snooze[idx].snoozeEnd += (snooze +1);
  72. }
  73. void deleteAlarm(int idx){
  74. struct _tm tm;
  75. alarm[idx].time = tm;
  76. alarm[idx].port = 0;
  77. alarm[idx].snooze = 5;
  78. alarm[idx].type = -1;
  79. alarm[idx].state = -1;
  80. }
  81. void handleAlarm(int idx){
  82. alarm[idx].time.tm_min = alarm[idx].time.tm_min + 1;
  83. alarm[idx].state = 0;
  84. }
  85. int compareTime(tm t1,tm t2){
  86. if (t1.tm_year > t2.tm_year){
  87. return 1;
  88. }
  89. if (t1.tm_year == t2.tm_year && t1.tm_mon > t2.tm_mon){
  90. return 1;
  91. }
  92. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday > t2.tm_mday){
  93. return 1;
  94. }
  95. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour > t2.tm_hour){
  96. return 1;
  97. }
  98. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min > t2.tm_min){
  99. return 1;
  100. }
  101. if (t1.tm_year == t2.tm_year && t1.tm_mon == t2.tm_mon && t1.tm_mday == t2.tm_mday && t1.tm_hour == t2.tm_hour && t1.tm_min == t2.tm_min &&t1.tm_sec > t2.tm_sec){
  102. return 1;
  103. }
  104. return 0;
  105. }