Yu.Ding 1 semana atrás
pai
commit
2f5dc15c9b
4 arquivos alterados com 148 adições e 10 exclusões
  1. 6 6
      generate_extension_conf.c
  2. 3 1
      generate_group_conf.c
  3. 3 3
      generate_pjsip_conf.c
  4. 136 0
      handle_alarm.c

+ 6 - 6
generate_extension_conf.c

@@ -97,6 +97,9 @@ int main(int argc, char **argv) {
 cJSON *pJson,*pSub;
 int iCount=0;
 char sip_udp_nat[2048], sip_tcp_nat[2048], sip_tls_nat[2048], localnet[1792];
+memset(sip_udp_nat, 0, sizeof(sip_udp_nat));
+memset(sip_tcp_nat, 0, sizeof(sip_tcp_nat));
+memset(sip_tls_nat, 0, sizeof(sip_tls_nat));
 
 typedef struct keys_action {
     char key[MINI_SIZE];
@@ -484,9 +487,6 @@ RINGTIME = 30\n\
         else if(strcmp(g_row[1],"pbx.nat.config") == 0){
             pJson = cJSON_Parse(g_row[2]);
             if(pJson && cJSON_GetObjectItem(pJson, "enable")->valueint == 1){
-                memset(sip_udp_nat, 0, sizeof(sip_udp_nat));
-                memset(sip_tcp_nat, 0, sizeof(sip_tcp_nat));
-                memset(sip_tls_nat, 0, sizeof(sip_tls_nat));
                 memset(localnet, 0, sizeof(localnet));
                 cJSON *localnetArray = cJSON_GetObjectItem( pJson, "localnet");
                 if(localnetArray != NULL){
@@ -631,7 +631,7 @@ cos=3\n\
 allow_reload=no\n\
 ",cJSON_GetObjectItem(pJson, "udp")->valueint\
 );
-                if(sip_udp_nat)
+                if(strlen(sip_udp_nat))
                 {
                     fprintf(conf_sipsetting_fp, "%s", sip_udp_nat);
                 }
@@ -648,7 +648,7 @@ cos=3\n\
 allow_reload=no\n\
 ",cJSON_GetObjectItem(pSub, "port")->valueint\
 );
-                if(sip_tcp_nat)
+                if(strlen(sip_tcp_nat))
                 {
                     fprintf(conf_sipsetting_fp, "%s", sip_tcp_nat);
                 }
@@ -669,7 +669,7 @@ cos=3\n\
 allow_reload=no\n\
 ",cJSON_GetObjectItem(pSub, "port")->valueint\
 );
-                if(sip_tls_nat)
+                if(strlen(sip_tls_nat))
                 {
                     fprintf(conf_sipsetting_fp, "%s", sip_tls_nat);
                 }

+ 3 - 1
generate_group_conf.c

@@ -191,8 +191,10 @@ while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
     fprintf(conf_extens_fp, "[extens-group-%s]\n",g_row[2]);
     fprintf(conf_ipphones_fp, "[phones-group-%s]\n",g_row[2]);
     memset(sql_tmp,0,sizeof(sql_tmp));
+    // sprintf(sql_tmp,"select exten,type_id,allowed_pa from t_paging_deviceGroups JOIN t_paging_devices on t_paging_devices.id = t_paging_deviceGroups.DeviceId\
+    // where GroupId = %s and t_paging_devices.type_id in('1','2','3','5') and t_paging_devices.user_id is NULL",g_row[0]);
     sprintf(sql_tmp,"select exten,type_id,allowed_pa from t_paging_deviceGroups JOIN t_paging_devices on t_paging_devices.id = t_paging_deviceGroups.DeviceId\
-    where GroupId = %s and t_paging_devices.type_id in('1','2','3','5') and t_paging_devices.user_id is NULL",g_row[0]);
+    where GroupId = %s and t_paging_devices.type_id in('1','2','3','6')",g_row[0]);
     if (executesql(sql_tmp)){
         print_mysql_error(NULL);
         exit(1);

+ 3 - 3
generate_pjsip_conf.c

@@ -318,12 +318,12 @@ direct_media_glare_mitigation = incoming\n\
 disable_direct_media_on_nat = yes\n\
 dtmf_mode = rfc4733\n\
 force_rport = yes\n\
-ice_support = no\n\
+ice_support = yes\n\
 identify_by = username\n\
 moh_suggest = default\n\
 outbound_proxy = \n\
-rewrite_contact = no\n\
-rtp_symmetric = no\n\
+rewrite_contact = yes\n\
+rtp_symmetric = yes\n\
 send_diversion = no\n\
 send_pai = no\n\
 send_rpid = no\n\

+ 136 - 0
handle_alarm.c

@@ -0,0 +1,136 @@
+/*
+============================================================================
+Name        : generate_paging_conf.sh
+Author      : ssc
+Version     : v1.0
+Copyright   : ZYCOO copyright
+Description : Generate paging info from mysql to paging conf file
+============================================================================
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <assert.h>
+#include <time.h>
+#include <sys/time.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <hiredis/hiredis.h>
+#include <cjson/cJSON.h>
+
+int main(int argc, char **argv) 
+{
+    char callfile[64], cmd[128], key[64];
+    char *redis_host = getenv("REDIS");
+    char *redis_password = getenv("REDIS_PASSWORD");
+    unsigned int redis_port = atoi(getenv("REDIS_PORT"));
+    char *exten;
+    cJSON *pJson;
+    redisReply *reply;
+    redisReply *reply2;
+
+    struct timeval timeout = { 1, 500000 };
+    redisContext * pContext = redisConnectWithTimeout(redis_host, redis_port, timeout);
+    if (pContext == NULL || pContext->err) {
+        if (pContext) {
+            printf("Connection error: %s\n", pContext->errstr);
+            redisFree(pContext);
+        } else {
+            printf("Connection error: can't allocate redis context\n");
+        }
+        return 0;
+    }
+
+    //数据库登录认证
+    reply = redisCommand(pContext, "AUTH %s", redis_password);
+    if (reply->type == REDIS_REPLY_ERROR) {
+        printf("Redis认证失败!\n");
+        freeReplyObject(reply);
+        redisFree(pContext);
+        return 0;
+    }
+    freeReplyObject(reply);
+
+    //选择数据库
+    reply = redisCommand(pContext, "SELECT 0");
+    freeReplyObject(reply);
+
+	while(1)
+	{
+        reply = redisCommand(pContext, "KEYS alarm-*");
+    
+        if (reply == NULL || reply->type == REDIS_REPLY_ERROR) {
+            printf("Error: %s\n", reply ? reply->str : "NULL reply");
+            continue;
+        }
+        
+        if (reply->type == REDIS_REPLY_ARRAY) {
+            for (size_t i = 0; i < reply->elements; i++) {
+                memset(key, '\0', sizeof(key));
+                strcpy(key, reply->element[i]->str);
+                exten = reply->element[i]->str + 6;
+                char *n = strchr(exten,'-');
+                if(n)
+                {
+                    exten[n - exten] = '\0';
+                }
+
+                reply2 = redisCommand(pContext, "GET sip-status-id-%s", exten);
+                if (reply2 == NULL || reply2->type == REDIS_REPLY_ERROR) {
+                    printf("Error: %s\n", reply2 ? reply2->str : "NULL reply");
+                    continue;
+                }
+                if(reply2->type == REDIS_REPLY_STRING)
+                {
+                    if(strcmp(reply2->str,"idle") == 0)
+                    {
+                        freeReplyObject(reply2);
+                        reply2 = redisCommand(pContext, "GET %s", key);
+                        if (reply2 == NULL || reply2->type == REDIS_REPLY_ERROR) {
+                            printf("Error: %s\n", reply2 ? reply2->str : "NULL reply");
+                            continue;
+                        }
+                        if(reply2->type == REDIS_REPLY_STRING)
+                        {
+                            pJson =  cJSON_Parse(reply2->str);
+                            if ( pJson ) {
+                                memset(callfile, 0, sizeof(callfile));
+                                sprintf(callfile, "/tmp/%s.call", key);
+                                FILE *callfile_fp = fopen(callfile, "w+");
+                                fprintf(callfile_fp, "\
+Channel: Local/%s@clickdial\n\
+Context: playback-ttspaging\n\
+Extension: s\n\
+Priority: 1\n\
+Callerid: Alarm <999999>\n\
+MaxRetries: 30\n\
+RetryTime: 10\n\
+Setvar: __AUDIOFILE=%s\n\
+Setvar: WDAutoAnswer=1\n\
+WaitTime: 600\n\
+\n", \
+cJSON_GetObjectItem(pJson, "exten")->valuestring,\
+cJSON_GetObjectItem(pJson, "audiofile")->valuestring\
+);
+
+                                fclose(callfile_fp);
+                                sprintf(cmd, "mv %s /var/spool/asterisk/outgoing/", callfile);
+                                system(cmd);
+                                redisCommand(pContext, "DEL %s", key);
+                            }
+                        }
+                    }
+                }
+                freeReplyObject(reply2);
+		        sleep(1);
+            }
+        }
+        
+        freeReplyObject(reply);
+        sleep(1);
+	}
+    redisFree( pContext );
+
+}