func (this *NwayConfig) Load() bool { conf, result := SetConfig("conf.ini") if result == false { logger.Debug("load the config file failed") return false } this.Dbconf.ConnString, result = conf.GetValue("database", "connstring") if result == false { logger.Error("load database connect string failed") return false } this.Fsconf.FsHost, result = conf.GetValue("freeswitch", "host") if result == false { logger.Error("load freeswitch host string failed") return false } this.Fsconf.FsAuth, result = conf.GetValue("freeswitch", "auth") if result == false { logger.Error("load freeswitch auth string failed") return false } this.Fsconf.FsPort = conf.GetInt("freeswitch", "port", 8021) this.Fsconf.FsTimeout = conf.GetInt("freeswitch", "timeout", 500) this.Fsoutboundconf.ObPort, result = conf.GetValue("freeswitch", "outboundport") this.Fsoutboundconf.ObPPort, result = conf.GetValue("freeswitch", "pub-outboundport") return true }
func (d *DbExtension) GetExtId(ext string, call_numberid int64) (int64, bool) { var extid int64 sqlstr := "SELECT a.id FROM nway_callout_extensions a,nway_callout_numbers b " + " where a.extension_number='" + ext + "' and a.domain_id=b.domain_id and b.id=" + strconv.FormatInt(call_numberid, 10) rows, bOp := d.dbbase.Query(sqlstr) if bOp == false { logger.Error("query the data failed ") rows.Close() return 0, false } defer rows.Close() for rows.Next() { err := rows.Scan(&extid) if err != nil { logger.Error("scan the data error,", err) return 0, false } } return extid, true }
func (d *DbBaseConfig) LoadConfig() (map[string]string, bool) { logger.Info("load all config from base_config table") var configs map[string]string var param, value string configs = make(map[string]string, 4) var strsql string strsql = "SELECT config_name, config_param FROM nway_callout_base_config" rows, bOp := d.dbbase.Query(strsql) //conn.Query(strsql) if bOp == false { logger.Error("query the data failed ") rows.Close() return configs, false } defer rows.Close() for rows.Next() { err := rows.Scan(¶m, &value) if err != nil { logger.Error("scan the data error,", err) } configs[param] = value } return configs, true }
func (d *DbExtensionGroup) GetGroupByDomain(domain_name string) (map[int64]ExtensionGroup, bool) { var groups map[int64]ExtensionGroup groups = make(map[int64]ExtensionGroup, 0) sqlstr := "SELECT a.id, a.domain_id, a.group_name, a.call_order_id," + " a.call_order_crycle " + " FROM nway_callout_extension_groups a,nway_callout_domains b " + " where a.domain_id=b.domain_uuid and b.domain_name='" + domain_name + "'" rows, bOp := d.dbbase.Query(sqlstr) if bOp == false { logger.Error("query the data failed ") rows.Close() return groups, false } var group ExtensionGroup defer rows.Close() for rows.Next() { err := rows.Scan(&group.Id, &group.Domain_id, &group.Group_name, &group.Call_order_id, &group.Call_order_crycle) if err != nil { logger.Error("scan the data error,", err) return groups, false } groups[group.Id] = group } return groups, true }
func (d *DbGateway) GetGatewayById(gw_id int64) (Gateway, bool) { var gwt Gateway // sqlstr := "SELECT id, gateway_name, gateway_url, call_prefix, max_call, added_autodial," + "curr_talking, domain_id " + " FROM nway_callout_gateways " + " where id=" + strconv.FormatInt(gw_id, 10) rows, bOp := d.dbbase.Query(sqlstr) if bOp == false { logger.Error("query the data failed ") rows.Close() return gwt, false } defer rows.Close() for rows.Next() { err := rows.Scan(&gwt.Id, &gwt.Gateway_name, &gwt.Gateway_url, &gwt.Call_prefix, &gwt.Max_call, &gwt.Added_autodial, &gwt.Curr_talking, &gwt.Domain_id) if err != nil { logger.Error("scan the data error,", err) return gwt, false } } return gwt, true }
func (d *DbExtension) GetExtByExtNumber(ext_number string) (Extension, bool) { var ext Extension sqlstr := "SELECT id, domain_id, extension_name, extension_number, callout_number, " + "extension_type, group_id, extension_pswd, extension_login_state, " + "extension_reg_state, callout_gateway, is_allow_callout, limit_max, " + "limit_destination, mailbox, vm_password, accountcode, effective_caller_id_name, " + "effective_caller_id_number, outbound_caller_id_name, outbound_caller_id_number," + "call_group, hold_music, call_state, is_record, curr_talking, " + "domain_name" + "FROM nway_callout_extensions " + " where extension_number='" + ext_number + "'" rows, bOp := d.dbbase.Query(sqlstr) if bOp == false { logger.Error("query the data failed ") rows.Close() return ext, false } defer rows.Close() for rows.Next() { err := rows.Scan(&ext.Id, &ext.Domain_id, &ext.Extension_name, &ext.Extension_number, &ext.Callout_number, &ext.Extension_type, &ext.Group_id, &ext.Extension_pswd, &ext.Extension_login_state, &ext.Extension_reg_state, &ext.Callout_gateway, &ext.Is_allow_callout, &ext.Limit_max, &ext.Limit_destination, &ext.Mailbox, &ext.Vm_password, &ext.Accountcode, &ext.Effective_caller_id_name, &ext.Effective_caller_id_number, &ext.Outbound_caller_id_name, &ext.Outbound_caller_id_number, &ext.Call_group, &ext.Hold_music, &ext.Call_state, &ext.Is_record, &ext.Curr_talking, &ext.Domain_name) if err != nil { logger.Error("scan the data error,", err) return ext, false } } return ext, true }
//to return a int value of the key value //param d: a default value //param section: the section name //param name: the param name func (c *Config) GetInt(section, name string, d int) int { svalue, bvalue := c.GetValue(section, name) if bvalue { if rvalue, err := strconv.Atoi(svalue); err == nil { return rvalue } else { logger.Error("convert the value failed from section:%s,name:%s ' value", section, name) return d } } else { logger.Error("not found the section:%q,name:%q ' value", section, name) return d } }
func main() { var dbinstance *nwayconnection.NwayConnect = nil dbinstance = nwayconnection.NewDb() if dbinstance == nil { logger.Error("Database create instance failed") return } bInit := dbinstance.Init() if bInit == false { logger.Error("Database create connection failed") return } beego.Run() }
func (this *NwayConnect) Init() bool { // result := true var err error var conf *pbxconfig.NwayConfig var isload bool conf, isload = pbxconfig.LoadConf() if isload == true { dbstring := conf.Dbconf.ConnString this.NwayDb, err = sql.Open("postgres", dbstring) this.NwayDb.SetMaxIdleConns(4) this.NwayDb.SetMaxOpenConns(10) if err != nil { //log.Fatalf() logger.Error("Got error when connect database, the error is '%v'", err) fmt.Println(dbstring) return false } return true } else { return false } }
func (d *DbCallNumber) GetNumbers(taskid int64, runpostion int64) (map[int64]CallNumber, bool) { var numbers map[int64]CallNumber numbers = make(map[int64]CallNumber, 0) sqlstr := "SELECT id, domain_id, group_id, 'number', is_called, call_state, start_time," + "answer_time, hangup_time, hangup_reason_id, answer_extension_id, " + "record_file, wait_sec, cdr_id, call_out_task_id, create_time" + "FROM nway_callout_numbers" + " where call_out_task_id=" + strconv.FormatInt(taskid, 10) + " and id > " + strconv.FormatInt(runpostion, 10) rows, bOp := d.dbbase.Query(sqlstr) if bOp == false { logger.Error("query the data failed ") rows.Close() return numbers, false } var nmb CallNumber defer rows.Close() for rows.Next() { err := rows.Scan( &nmb.Id, &nmb.Domain_id, &nmb.Group_id, &nmb.Number, &nmb.Is_called, &nmb.Call_state, &nmb.Start_time, &nmb.Answer_time, &nmb.Hangup_time, &nmb.Hangup_reason_id, &nmb.Answer_extension_id, &nmb.Record_file, &nmb.Wait_sec, &nmb.Cdr_id, &nmb.Call_out_task_id, &nmb.Create_time) if err != nil { logger.Error("scan the data error,", err) return numbers, false } numbers[nmb.Id] = nmb } return numbers, true }
func (d *DbCallNumber) SetAnswerCall(id int64, ext string) bool { var dbext DbExtension ext_id, mybool := dbext.GetExtId(ext, id) if mybool == false { logger.Error("the ext id not found") return false } sqlstr := "update nway_callout_numbers set call_state=2,Answer_time=current_timestamp, " + "Answer_extension_id=" + strconv.FormatInt(ext_id, 10) + " where id=" + strconv.FormatInt(id, 10) bSuccess, err := d.dbbase.Exec(sqlstr) if err != nil { logger.Error("update call error,", err) return false } return bSuccess }
//处理outound的函数 func process_outbound(c *SocketConnection) error { msg, err := c.ReadMessage() if err != nil { logger.Error("Read esl Message error ", err) } err = c.Send("linger") if err != nil { logger.Error(err) } caller_uuid := msg.GetHeader("Caller-Unique-ID") content_type := msg.GetHeader("Content-Type") event_name := msg.GetHeader("Event-Name") call_direction := msg.GetHeader("Call-Direction") b_uuid := msg.GetHeader("Unique-ID") channel_state := msg.GetHeader("Channel-State") caller_destination_number := msg.GetHeader("Caller-Destination-Number") caller_username := msg.GetHeader("Caller-Username") caller_number := msg.GetHeader("Caller-Caller-ID-Number") channel_call_state := msg.GetHeader("Channel-Call-State") gateway_name := msg.GetHeader("Gateway-Name") if (len(gateway_name) > 0) && (call_direction == "inbound") { //由外线呼入的 Debug("caller id ", caller_uuid) Debug("content_type ", content_type) Debug("event_name ", event_name) Debug("b_uuid ", b_uuid) Debug("channel_state ", channel_state) Debug("caller_destination_number ", caller_destination_number) Debug("caller_username ", caller_username) Debug("caller_number ", caller_number) Debug("channel_call_state ", channel_call_state) //通过呼入号码查找外线呼入的号码或ivr等 outbRegex := nwayregex.NewRegex() //Debug(outbRegex) _, _, _ = outbRegex.Match("18621575908") } else { //由内呼外的 } return err }
func (d *DbBase) Query(sqlstring string) (*sql.Rows, bool) { var dbinstance *nwayconnection.NwayConnect = nil dbinstance = nwayconnection.NewDb() NwayConn := dbinstance.GetConn() if NwayConn == nil { logger.Error("Get The Connect Failed") return nil, false } rows, err := NwayConn.Query(sqlstring) if err != nil { //log.Fatal("failed to scan", err) logger.Error(err) return nil, false } return rows, true }
func (d *DbBase) Exec(sqlstring string) (bool, error) { var dbinstance *nwayconnection.NwayConnect = nil dbinstance = nwayconnection.NewDb() NwayConn := dbinstance.GetConn() if NwayConn == nil { logger.Error("Get The Connect Failed") err := errors.New("connect failed") return false, err } _, err := NwayConn.Exec(sqlstring) if err != nil { //log.Fatal("failed to scan", err) logger.Error(err) return false, err } return true, nil }
func (d *DbBase) QureyPaging(sqlstring string, page, perpage int) (*sql.Rows, bool) { sqlstring += " limit " + strconv.Itoa(perpage) + " offset " + strconv.Itoa((page-1)*perpage) var dbinstance *nwayconnection.NwayConnect = nil dbinstance = nwayconnection.NewDb() NwayConn := dbinstance.GetConn() if NwayConn == nil { logger.Error("Get The Connect Failed") return nil, false } rows, err := NwayConn.Query(sqlstring) if err != nil { //log.Fatal("failed to scan", err) logger.Error(err) return nil, false } return rows, true }
func (d *DbDomainSetting) GetDomainSetting(domain_id int64) (map[int64]DomainSetting, bool) { var dss map[int64]DomainSetting dss = make(map[int64]DomainSetting, 4) var sqlstr string sqlstr = "SELECT domain_setting_uuid, domain_setting_category, domain_setting_subcategory," + "domain_setting_name, domain_setting_value, domain_setting_enabled," + "FROM nway_callout_domain_settings where domain_uuid=" + strconv.FormatInt(domain_id, 10) rows, bOp := d.dbbase.Query(sqlstr) //conn.Query(strsql) if bOp == false { logger.Error("query the data failed ") rows.Close() return dss, false } var ( Domain_setting_uuid int64 Domain_setting_category string Domain_setting_subcategory string Domain_setting_name string Domain_setting_value string Domain_setting_enabled string ) defer rows.Close() for rows.Next() { err := rows.Scan(&Domain_setting_uuid, &Domain_setting_category, &Domain_setting_subcategory, &Domain_setting_name, &Domain_setting_value, &Domain_setting_enabled) if err != nil { logger.Error("scan the data error,", err) } var ds DomainSetting ds.Domain_setting_category = Domain_setting_category ds.Domain_setting_enabled = Domain_setting_enabled ds.Domain_setting_name = Domain_setting_name ds.Domain_setting_subcategory = Domain_setting_subcategory ds.Domain_setting_uuid = Domain_setting_uuid ds.Domain_uuid = domain_id dss[Domain_setting_uuid] = ds } return dss, true }
func (d *DbCdr) BlegHangup(cdrid int64) bool { var sqlstr string sqlstr = "update nway_callout_cdr set b_end_stamp=current_timestamp where id=" + strconv.FormatInt(cdrid, 10) bOp, err := d.dbbase.Exec(sqlstr) if err != nil && bOp == false { logger.Error("update Aleg answer failed!", err) return false } return true }
//一般outbound用于走呼入流程会更好 func (this *OutboundService) Start() error { nwayconfig, result := pbxconfig.LoadConf() if result == false { logger.Error("load config file failed") } if len(nwayconfig.Fsoutboundconf.ObPort) > 0 && len(nwayconfig.Fsoutboundconf.ObPPort) > 0 { s, err := NewOutboundServer(":" + nwayconfig.Fsoutboundconf.ObPort) if err != nil { logger.Error("Start OutboundService Failed ", err) } else { go handle(s) s.Start() } } else { return errors.New("Not Found the outboud port or public outbound port") } return nil }
//Create an empty configuration file func SetConfig(filepath string) (*Config, bool) { c := new(Config) c.filepath = filepath file, err := os.Open(c.filepath) defer file.Close() if err != nil { CheckErr(err) logger.Error("load config file err:", err) return nil, false } return c, true }
func (this *NwayConnect) GetConn() *sql.DB { //保证所有的连接都是有效的 strsql := "select now();" rows, err := this.NwayDb.Query(strsql) if err != nil { logger.Error("failed to get a connect '%v'", err) return nil } rows.Close() return this.NwayDb }
func (d *DbDomain) GetDomainId(doamin_name string) (int64, bool) { var strsql string strsql = "select domain_uuid from nway_callout_domains where domain_name='" + doamin_name + "'" rows, bOp := d.dbbase.Query(strsql) if bOp == false { logger.Error("query the data failed ") rows.Close() return 0, false } var id int64 = 0 defer rows.Close() for rows.Next() { err := rows.Scan(&id) if err != nil { logger.Error("scan the data error,", err) return 0, false } } return id, true }
func (d *DbCallNumber) SetStartCall(id int64) bool { sqlstr := "update nway_callout_numbers set call_state=1," + "Start_time=current_timestamp,Is_called=True " + " where id=" + strconv.FormatInt(id, 10) bSuccess, err := d.dbbase.Exec(sqlstr) if err != nil { logger.Error("update call error,", err) return false } return bSuccess }
func (d *DbCallNumber) SetHangupCall(id int64, reson_id int) bool { sqlstr := "update nway_callout_numbers set call_state=1," + "Hangup_time=current_timestamp,Hangup_reason_id=" + strconv.Itoa(reson_id) + " where id=" + strconv.FormatInt(id, 10) bSuccess, err := d.dbbase.Exec(sqlstr) if err != nil { logger.Error("update call error,", err) return false } return bSuccess }
func (this *InboundService) Start() error { nwayconfig, result := pbxconfig.LoadConf() if result == false { logger.Error("load config file failed") } //c, err := eventsocket.Dial(nwayconfig.Fsconf.FsHost+":"+strconv.Itoa(nwayconfig.Fsconf.FsPort), // nwayconfig.Fsconf.FsAuth) client, err := NewClient(nwayconfig.Fsconf.FsHost, uint(nwayconfig.Fsconf.FsPort), nwayconfig.Fsconf.FsAuth, nwayconfig.Fsconf.FsTimeout) if err != nil { logger.Error("Error while creating new client: %s", err) return err } Debug("Nway! New client: %q", client) // Apparently all is good... Let us now handle connection :) // We don't want this to be inside of new connection as who knows where it my lead us. // Remember that this is crutial part in handling incoming messages :) go client.Handle() for { msg, err := client.ReadMessage() if err != nil { // If it contains EOF, we really dont care... if !strings.Contains(err.Error(), "EOF") && err.Error() != "unexpected end of JSON input" { logger.Error("Error while reading Freeswitch message: %s", err) } //break } Debug("%s", msg) } return err }
func (d *DbRing) GetPathById(ring_id int64) (string, bool) { sqlstr := "SELECT ring_name " + " FROM nway_callout_rings " + " where id=" + strconv.FormatInt(ring_id, 10) rows, bOp := d.dbbase.Query(sqlstr) if bOp == false { logger.Error("query the data failed ") rows.Close() return "", false } var ringpath string defer rows.Close() for rows.Next() { err := rows.Scan(&ringpath) if err != nil { logger.Error("scan the data error,", err) return "", false } } return ringpath, true }
func (d *DbCdr) InsertCdr(c *Cdr) (int64, bool) { var sqlstr string sqlstr = "SELECT insertnewcdr('" + c.Domain_name + "'," sqlstr += "'" + c.Caller_id_name + "'," sqlstr += "'" + c.Caller_id_number + "'," sqlstr += "'" + c.Called_number + "'," if c.Auto_callout { sqlstr += "True," } else { sqlstr += "False," } if c.Task_id > -1 { sqlstr += strconv.FormatInt(c.Task_id, 10) + ")" } rows, bOp := d.dbbase.Query(sqlstr) //conn.Query(sqlstr) if bOp == false { logger.Error("get the query connection failed! ") rows.Close() return 0, false } var mynum int64 = 0 defer rows.Close() for rows.Next() { err := rows.Scan(&mynum) if err != nil { logger.Error("Insert a new cdr failed! ", err) return 0, false } } return mynum, true }
func handle(s *OutboundServer) { for { select { case conn := <-s.Conns: Notice("New incomming connection: %v", conn) if err := conn.Connect(); err != nil { logger.Error("Got error while accepting connection: %s", err) //break } default: // YabbaDabbaDooooo! //Flintstones. Meet the Flintstones. They're the modern stone age family. From the town of Bedrock, // They're a page right out of history. La la,lalalalala la :D } } }
func (d *DbExtension) GetExtensionByGroup(group_id int64) (map[int64]Extension, bool) { var exts map[int64]Extension exts = make(map[int64]Extension, 0) sqlstr := "SELECT id, domain_id, extension_name, extension_number, callout_number, " + "extension_type, group_id, extension_pswd, extension_login_state, " + "extension_reg_state, callout_gateway, is_allow_callout, limit_max, " + "limit_destination, mailbox, vm_password, accountcode, effective_caller_id_name, " + "effective_caller_id_number, outbound_caller_id_name, outbound_caller_id_number," + "call_group, hold_music, call_state, is_record, curr_talking, " + "domain_name" + "FROM nway_callout_extensions " + " where group_id=" + strconv.FormatInt(group_id, 10) rows, bOp := d.dbbase.Query(sqlstr) if bOp == false { logger.Error("query the data failed ") rows.Close() return exts, false } var ext Extension defer rows.Close() for rows.Next() { err := rows.Scan(&ext.Id, &ext.Domain_id, &ext.Extension_name, &ext.Extension_number, &ext.Callout_number, &ext.Extension_type, &ext.Group_id, &ext.Extension_pswd, &ext.Extension_login_state, &ext.Extension_reg_state, &ext.Callout_gateway, &ext.Is_allow_callout, &ext.Limit_max, &ext.Limit_destination, &ext.Mailbox, &ext.Vm_password, &ext.Accountcode, &ext.Effective_caller_id_name, &ext.Effective_caller_id_number, &ext.Outbound_caller_id_name, &ext.Outbound_caller_id_number, &ext.Call_group, &ext.Hold_music, &ext.Call_state, &ext.Is_record, &ext.Curr_talking, &ext.Domain_name) if err != nil { logger.Error("scan the data error,", err) return exts, false } exts[ext.Id] = ext } return exts, true }
func (d *DbTask) GetTasks() (map[int64]Task, bool) { var tasks map[int64]Task tasks = make(map[int64]Task, 0) // sqlstr := "SELECT id, domain_id, callout_name, number_group_id, number_group_uploadfile, " + "run_position, time_rule_id, start_time, stop_time, ring_id, after_ring_play, " + "ring_timeout, group_id, call_project_id, concurr_type_id, concurr_number, " + "callout_state_id, total_number, wait_number, success_number, " + "failed_number, cancel_number, has_parse_from_file, callout_gateway_id, " + "max_concurr_number, second_ring_id, second_after_ring_opt, after_ring_key, " + "after_key_opt_id, outside_line_number " + "FROM nway_callout_tasks" + " where where t.start_time < now() and t.stop_time >now() " + " and (callout_state_id=1 or callout_state_id=2 or callout_state_id=4) and has_parse_from_file=True " rows, bOp := d.dbbase.Query(sqlstr) if bOp == false { logger.Error("query the data failed ") rows.Close() return tasks, false } var task Task defer rows.Close() for rows.Next() { err := rows.Scan( &task.Id, &task.Domain_id, &task.Callout_name, &task.Number_group_id, &task.Number_group_uploadfile, &task.Run_position, &task.Time_rule_id, &task.Start_time, &task.Stop_time, &task.Ring_id, &task.After_ring_play, &task.Ring_timeout, &task.Group_id, &task.Call_project_id, &task.Concurr_type_id, &task.Concurr_number, &task.Callout_state_id, &task.Total_number, &task.Wait_number, &task.Success_number, &task.Failed_number, &task.Cancel_number, &task.Has_parse_from_file, &task.Callout_gateway_id, &task.Max_concurr_number, &task.Second_ring_id, &task.Second_after_ring_opt, &task.After_ring_key, &task.After_key_opt_id, &task.Outside_line_number) if err != nil { logger.Error("scan the data error,", err) return tasks, false } tasks[task.Id] = task } return tasks, true }