Skip to content

Commit

Permalink
♻️ refactor: updated function relating to CDR #14
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jan 14, 2024
1 parent 941edb4 commit 7a9286b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 67 deletions.
29 changes: 14 additions & 15 deletions pkg/ami/ami_cdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ami

import (
"fmt"
"log"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -213,13 +212,13 @@ func (r *AMICdr) SetType(value string) *AMICdr {
return r
}

func (r *AMICdr) SetUserExtension(value string) *AMICdr {
r.UserExtension = TrimStringSpaces(value)
func (r *AMICdr) SetExtension(value string) *AMICdr {
r.Extension = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetPhoneNumber(value string) *AMICdr {
r.PhoneNumber = TrimStringSpaces(value)
func (r *AMICdr) SetNumber(value string) *AMICdr {
r.Number = TrimStringSpaces(value)
return r
}

Expand All @@ -228,8 +227,8 @@ func (r *AMICdr) SetSymbol(value string) *AMICdr {
return r
}

func (r *AMICdr) SetPlaybackUrl(value string) *AMICdr {
r.PlaybackUrl = value
func (r *AMICdr) SetMediaLink(value string) *AMICdr {
r.MediaLink = value
return r
}

Expand Down Expand Up @@ -351,8 +350,8 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
r.SetDesc(flow)
r.SetDirection(config.AmiOutboundDirection)
r.SetType(config.AmiTypeOutboundNormalDirection)
r.SetUserExtension(strings.Split(r.Channel, r.symbol)[0])
r.SetPhoneNumber(phone)
r.SetExtension(strings.Split(r.Channel, r.symbol)[0])
r.SetNumber(phone)
} else {
var inCase bool = false
// from outbound chan-spy
Expand All @@ -362,7 +361,7 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
r.SetDesc(flow)
r.SetType(config.AmiTypeChanSpyDirection)
r.SetDirection(config.AmiOutboundDirection)
r.SetUserExtension(strings.Split(r.Channel, r.symbol)[0])
r.SetExtension(strings.Split(r.Channel, r.symbol)[0])
}
// from inbound dial
if strings.EqualFold(r.LastApplication, config.AmiLastApplicationDial) {
Expand All @@ -371,8 +370,8 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
r.SetDesc(flow)
r.SetDirection(config.AmiInboundDirection)
r.SetType(config.AmiTypeInboundDialDirection)
r.SetUserExtension(strings.Split(r.DestinationChannel, r.symbol)[0])
r.SetPhoneNumber(r.Source)
r.SetExtension(strings.Split(r.DestinationChannel, r.symbol)[0])
r.SetNumber(r.Source)
}
// from inbound queue
if strings.EqualFold(r.LastApplication, config.AmiLastApplicationQueue) {
Expand All @@ -381,11 +380,11 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
r.SetDesc(flow)
r.SetDirection(config.AmiInboundDirection)
r.SetType(config.AmiTypeInboundQueueDirection)
r.SetUserExtension(strings.Split(r.Channel, r.symbol)[0])
r.SetPhoneNumber(r.Source)
r.SetExtension(strings.Split(r.Channel, r.symbol)[0])
r.SetNumber(r.Source)
}
if !inCase {
log.Printf("ParseCdr, CDR got an error exception case:: %v", JsonString(r))
D().Error("ParseCdr, CDR got an error exception case:: %v", JsonString(r))
}
}
return r
Expand Down
49 changes: 25 additions & 24 deletions pkg/ami/ami_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,21 @@ type AMIPayloadQueue struct {
// ChannelId - Channel UniqueId to be set on the channel.
// OtherChannelId - Channel UniqueId to be set on the second local channel.
type AMIOriginate struct {
Channel string `ami:"Channel,omitempty"`
Exten string `ami:"Exten,omitempty"`
Context string `ami:"Context,omitempty"`
Priority int `ami:"Priority,omitempty"`
Application string `ami:"Application,omitempty"`
Data string `ami:"Data,omitempty"`
Timeout int `ami:"Timeout,omitempty"`
CallerID string `ami:"CallerID,omitempty"`
Variable []string `ami:"Variable,omitempty"`
Account string `ami:"Account,omitempty"`
EarlyMedia string `ami:"EarlyMedia,omitempty"`
Async string `ami:"Async,omitempty"`
Codecs string `ami:"Codecs,omitempty"`
ChannelID string `ami:"ChannelId,omitempty"`
OtherChannelID string `ami:"OtherChannelId,omitempty"`
Channel string `ami:"Channel,omitempty"`
Exten string `ami:"Exten,omitempty"`
Context string `ami:"Context,omitempty"`
Priority int `ami:"Priority,omitempty"`
Application string `ami:"Application,omitempty"`
Data string `ami:"Data,omitempty"`
Timeout int `ami:"Timeout,omitempty"`
CallerID string `ami:"CallerID,omitempty"`
Variable string `ami:"Variable,omitempty"`
Account string `ami:"Account,omitempty"`
EarlyMedia string `ami:"EarlyMedia,omitempty"`
Async string `ami:"Async,omitempty"`
Codecs string `ami:"Codecs,omitempty"`
ChannelID string `ami:"ChannelId,omitempty"`
OtherChannelID string `ami:"OtherChannelId,omitempty"`
}

// AMIPayloadCall holds the call data to transfer.
Expand Down Expand Up @@ -339,12 +339,13 @@ type AMIPayloadDb struct {
}

type AMIDialCall struct {
Telephone string `json:"telephone" binding:"required"` // like customer phone number or no. extension internal from all routes
ChannelProtocol string `json:"channel_protocol" binding:"required"` // protocols include: SIP, H323, IAX...
Extension int `json:"extension" binding:"required"` // like current user using no. extension
DebugMode bool `json:"debug_mode"` // allow to trace log
Timeout int `json:"timeout"` // set timeout while calling
ExtensionExists bool `json:"extension_exists"` // allow validate the extension and channel
ChannelProtocol string `json:"channel_protocol" binding:"required"` // protocols include: SIP, H323, IAX...
Telephone string `json:"telephone" binding:"required"` // just customer phone number or no. extension internal from all routes
Extension int `json:"extension" binding:"required"` // just current user using no. extension
Timeout int `json:"timeout"` // set timeout while calling
Variables map[string]string `json:"variables,omitempty"` // external variables configuration which returned by event
DebugMode bool `json:"debug_mode"` // allow to trace log
ExtensionExists bool `json:"extension_exists"` // allow validate the extension and channel
}

type AMIExtensionStatesConf struct {
Expand Down Expand Up @@ -444,9 +445,9 @@ type AMICdr struct {
Direction string `json:"direction"` // inbound, outbound
Desc string `json:"desc"`
Type string `json:"type"`
UserExtension string `json:"user_extension,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"`
PlaybackUrl string `json:"playback_url,omitempty"` // the only cdr has status answered
Extension string `json:"extension,omitempty"`
Number string `json:"number,omitempty"`
MediaLink string `json:"media_link,omitempty"` // the only cdr has status answered
symbol string // default extension splitter symbol: -, example: SIP/1000-00098fec then split by -
}

Expand Down
67 changes: 39 additions & 28 deletions pkg/ami/ami_originate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,14 @@ func (o *AMIOriginate) SetCallerId(value string) *AMIOriginate {
return o
}

func (o *AMIOriginate) SetVar(value ...string) *AMIOriginate {
o.Variable = append(o.Variable, value...)
return o
// "custom_parameter1=value1"
func (o *AMIOriginate) SetVariable(value string) {
o.Variable = value
}

func (c *AMIOriginate) SetVars(delimiter string, variables ...string) *AMIOriginate {
if strings.EqualFold(delimiter, "") {
delimiter = ";"
}
vars := strings.Join(variables, delimiter)
c.SetVar(vars)
return c
}

func (c *AMIOriginate) SetVarsMap(delimiter string, variables map[string]interface{}) *AMIOriginate {
if len(variables) <= 0 {
return c
}
_vars := make([]string, len(variables))

for k, v := range variables {
str := fmt.Sprintf("%s=%v", k, v)
_vars = append(_vars, str)
}

c.SetVars(delimiter, _vars...)
return c
// "custom_parameter1=value1" & "custom_parameter2=value2"
func (o *AMIOriginate) SetMultipleVariables(variables ...string) {
o.Variable = strings.Join(variables, "&")
}

func (o *AMIOriginate) SetAccount(value string) *AMIOriginate {
Expand Down Expand Up @@ -168,11 +149,39 @@ func (o *AMIDialCall) SetExtensionExists(value bool) *AMIDialCall {
return o
}

func (o *AMIDialCall) SetVariables(values map[string]string) *AMIDialCall {
o.Variables = values
return o
}

func (o *AMIDialCall) Json() string {
return JsonString(o)
}

// DialCall
func (o *AMIDialCall) OfVars() []string {
var vars []string
if len(o.Variables) == 0 {
return vars
}
for k, v := range o.Variables {
vars = append(vars, fmt.Sprintf("%v=%v", k, v))
}
return vars
}

func GetAmiDialCallSample() *AMIDialCall {
a := NewAmiDialCall()
a.SetDebugMode(true).SetExtensionExists(true)
a.SetChannelProtocol("SIP").
SetExtension(1001).
SetTelephone("012345678").
SetTimeout(30000).
SetVariables(map[string]string{
"key": "123",
})
return a
}

func DialCall(ctx context.Context, s AMISocket, originate AMIOriginate) (AmiReply, error) {
return Originate(ctx, s, originate)
}
Expand All @@ -196,6 +205,7 @@ func DialOut(ctx context.Context, s AMISocket, d AMIDialCall) (AmiReply, bool, e
SetContext(config.AmiContextOutbound).
SetExtension(strings.TrimSpace(d.Telephone)).
SetChannel(channel.JoinChannelWith(channel.ChannelProtocol, fmt.Sprintf("%v", d.Extension)))
o.SetMultipleVariables(d.OfVars()...)

if d.ExtensionExists {
peer, err := SIPPeerStatusShort(ctx, s, fmt.Sprintf("%v", d.Extension))
Expand All @@ -209,7 +219,7 @@ func DialOut(ctx context.Context, s AMISocket, d AMIDialCall) (AmiReply, bool, e
}
if d.DebugMode {
D().Info("DialOut, an outgoing call with originate request body: %v", o.Json())
D().Info("DialOut, an outgoing call with original request body (setter): %v", d.Json())
D().Info("DialOut, an outgoing call with original request body: %v", d.Json())
}
response, err := DialCall(ctx, s, *o)
return response, IsSuccess(response), err
Expand All @@ -234,6 +244,7 @@ func DialIn(ctx context.Context, s AMISocket, d AMIDialCall) (AmiReply, bool, er
SetContext(config.AmiContextFromInternal).
SetExtension(strings.TrimSpace(d.Telephone)).
SetChannel(channel.JoinChannelWith(channel.ChannelProtocol, fmt.Sprintf("%v", d.Extension)))
o.SetMultipleVariables(d.OfVars()...)

if d.ExtensionExists {
peer, err := SIPPeerStatusShort(ctx, s, fmt.Sprintf("%v", d.Extension))
Expand All @@ -247,7 +258,7 @@ func DialIn(ctx context.Context, s AMISocket, d AMIDialCall) (AmiReply, bool, er
}
if d.DebugMode {
D().Info("DialIn, an internal call with originate request body: %v", o.Json())
D().Info("DialIn, an internal call with original request body (setter): %v", d.Json())
D().Info("DialIn, an internal call with original request body: %v", d.Json())
}
response, err := DialCall(ctx, s, *o)
return response, IsSuccess(response), err
Expand Down

0 comments on commit 7a9286b

Please sign in to comment.