handle_alarm.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. ============================================================================
  3. Name : generate_paging_conf.sh
  4. Author : ssc
  5. Version : v1.0
  6. Copyright : ZYCOO copyright
  7. Description : Generate paging info from mysql to paging conf file
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <assert.h>
  15. #include <time.h>
  16. #include <sys/time.h>
  17. #include <ctype.h>
  18. #include <unistd.h>
  19. #include <hiredis/hiredis.h>
  20. #include <cjson/cJSON.h>
  21. int main(int argc, char **argv)
  22. {
  23. char callfile[64], cmd[128], key[64];
  24. char *redis_host = getenv("REDIS");
  25. char *redis_password = getenv("REDIS_PASSWORD");
  26. unsigned int redis_port = atoi(getenv("REDIS_PORT"));
  27. char *exten;
  28. cJSON *pJson;
  29. redisReply *reply;
  30. redisReply *reply2;
  31. struct timeval timeout = { 1, 500000 };
  32. redisContext * pContext = redisConnectWithTimeout(redis_host, redis_port, timeout);
  33. if (pContext == NULL || pContext->err) {
  34. if (pContext) {
  35. printf("Connection error: %s\n", pContext->errstr);
  36. redisFree(pContext);
  37. } else {
  38. printf("Connection error: can't allocate redis context\n");
  39. }
  40. return 0;
  41. }
  42. //数据库登录认证
  43. reply = redisCommand(pContext, "AUTH %s", redis_password);
  44. if (reply->type == REDIS_REPLY_ERROR) {
  45. printf("Redis认证失败!\n");
  46. freeReplyObject(reply);
  47. redisFree(pContext);
  48. return 0;
  49. }
  50. freeReplyObject(reply);
  51. //选择数据库
  52. reply = redisCommand(pContext, "SELECT 0");
  53. freeReplyObject(reply);
  54. while(1)
  55. {
  56. reply = redisCommand(pContext, "KEYS alarm-*");
  57. if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
  58. printf("Error: %s\n", reply ? reply->str : "NULL reply");
  59. continue;
  60. }
  61. if (reply->type == REDIS_REPLY_ARRAY) {
  62. for (size_t i = 0; i < reply->elements; i++) {
  63. memset(key, '\0', sizeof(key));
  64. strcpy(key, reply->element[i]->str);
  65. exten = reply->element[i]->str + 6;
  66. char *n = strchr(exten,'-');
  67. if(n)
  68. {
  69. exten[n - exten] = '\0';
  70. }
  71. reply2 = redisCommand(pContext, "GET sip-status-id-%s", exten);
  72. if (reply2 == NULL || reply2->type == REDIS_REPLY_ERROR) {
  73. printf("Error: %s\n", reply2 ? reply2->str : "NULL reply");
  74. continue;
  75. }
  76. if(reply2->type == REDIS_REPLY_STRING)
  77. {
  78. if(strcmp(reply2->str,"idle") == 0)
  79. {
  80. freeReplyObject(reply2);
  81. reply2 = redisCommand(pContext, "GET %s", key);
  82. if (reply2 == NULL || reply2->type == REDIS_REPLY_ERROR) {
  83. printf("Error: %s\n", reply2 ? reply2->str : "NULL reply");
  84. continue;
  85. }
  86. if(reply2->type == REDIS_REPLY_STRING)
  87. {
  88. pJson = cJSON_Parse(reply2->str);
  89. if ( pJson ) {
  90. memset(callfile, 0, sizeof(callfile));
  91. sprintf(callfile, "/tmp/%s.call", key);
  92. FILE *callfile_fp = fopen(callfile, "w+");
  93. fprintf(callfile_fp, "\
  94. Channel: Local/%s@clickdial\n\
  95. Context: playback-ttspaging\n\
  96. Extension: s\n\
  97. Priority: 1\n\
  98. Callerid: Alarm <999999>\n\
  99. MaxRetries: 30\n\
  100. RetryTime: 10\n\
  101. Setvar: __AUDIOFILE=%s\n\
  102. Setvar: WDAutoAnswer=1\n\
  103. WaitTime: 600\n\
  104. \n", \
  105. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  106. cJSON_GetObjectItem(pJson, "audiofile")->valuestring\
  107. );
  108. fclose(callfile_fp);
  109. sprintf(cmd, "mv %s /var/spool/asterisk/outgoing/", callfile);
  110. system(cmd);
  111. redisCommand(pContext, "DEL %s", key);
  112. }
  113. }
  114. }
  115. }
  116. freeReplyObject(reply2);
  117. sleep(1);
  118. }
  119. }
  120. freeReplyObject(reply);
  121. sleep(1);
  122. }
  123. redisFree( pContext );
  124. }