Skip to content

Commit

Permalink
♻️ refactor: updated codebase #14
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Sep 23, 2023
1 parent 477d263 commit dd30624
Show file tree
Hide file tree
Showing 19 changed files with 356 additions and 396 deletions.
15 changes: 7 additions & 8 deletions pkg/ami/ami_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

"github.com/pnguyen215/gobase-voip-core/pkg/ami/config"
"github.com/pnguyen215/gobase-voip-core/pkg/ami/utils"
)

type AMICallbackService interface {
Expand Down Expand Up @@ -72,7 +71,7 @@ func (a *AMICallbackHandler) AppendIgnoredEvents(values ...string) *AMICallbackH
}

func (a *AMICallbackHandler) Json() string {
return utils.ToJson(a)
return JsonString(a)
}

func (h *AMICallbackHandler) Send() (AMIResultRaw, error) {
Expand Down Expand Up @@ -100,14 +99,14 @@ func (h *AMICallbackHandler) Send() (AMIResultRaw, error) {
}

if len(response) > 0 && err == nil {
if h.Socket.AllowTrace {
if h.Socket.DebugMode {
log.Printf("Send(). callback return for the %v time(s) and waste time = %v", i, _end)
}
break
}
}

if h.Socket.AllowTrace {
if h.Socket.DebugMode {
log.Printf("Send(). callback total waste time = %v", total)
}

Expand Down Expand Up @@ -140,14 +139,14 @@ func (h *AMICallbackHandler) SendLevel() (AMIResultRawLevel, error) {
}

if len(response) > 0 && err == nil {
if h.Socket.AllowTrace {
if h.Socket.DebugMode {
log.Printf("SendLevel(). callback return for the %v time(s) and waste time = %v", i, _end)
}
break
}
}

if h.Socket.AllowTrace {
if h.Socket.DebugMode {
log.Printf("SendLevel(). callback total waste time = %v", total)
}

Expand Down Expand Up @@ -179,14 +178,14 @@ func (h *AMICallbackHandler) SendSuperLevel() ([]AMIResultRaw, error) {
}

if len(response) > 0 && err == nil {
if h.Socket.AllowTrace {
if h.Socket.DebugMode {
log.Printf("SendSuperLevel(). callback return for the %v time(s) and waste time = %v", i, _end)
}
break
}
}

if h.Socket.AllowTrace {
if h.Socket.DebugMode {
log.Printf("SendSuperLevel(). callback total waste time = %v", total)
}

Expand Down
41 changes: 20 additions & 21 deletions pkg/ami/ami_cdr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/pnguyen215/gobase-voip-core/pkg/ami/config"
"github.com/pnguyen215/gobase-voip-core/pkg/ami/utils"
)

func NewAMICdr() *AMICdr {
Expand All @@ -19,27 +18,27 @@ func NewAMICdr() *AMICdr {
}

func (r *AMICdr) SetEvent(value string) *AMICdr {
r.Event = utils.TrimAllSpace(value)
r.Event = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetAccountCode(value string) *AMICdr {
r.AccountCode = utils.TrimAllSpace(value)
r.AccountCode = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetSource(value string) *AMICdr {
r.Source = utils.TrimAllSpace(value)
r.Source = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetDestination(value string) *AMICdr {
r.Destination = utils.TrimAllSpace(value)
r.Destination = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetDestinationContext(value string) *AMICdr {
r.DestinationContext = utils.TrimAllSpace(value)
r.DestinationContext = TrimStringSpaces(value)
return r
}

Expand All @@ -49,22 +48,22 @@ func (r *AMICdr) SetCallerId(value string) *AMICdr {
}

func (r *AMICdr) SetChannel(value string) *AMICdr {
r.Channel = utils.TrimAllSpace(value)
r.Channel = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetDestinationChannel(value string) *AMICdr {
r.DestinationChannel = utils.TrimAllSpace(value)
r.DestinationChannel = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetLastApplication(value string) *AMICdr {
r.LastApplication = utils.TrimAllSpace(value)
r.LastApplication = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetLastData(value string) *AMICdr {
r.LastData = utils.TrimAllSpace(value)
r.LastData = TrimStringSpaces(value)
return r
}

Expand Down Expand Up @@ -162,12 +161,12 @@ func (r *AMICdr) SetBillableSecondWith(value string) *AMICdr {
}

func (r *AMICdr) SetDisposition(value string) *AMICdr {
r.Disposition = utils.TrimAllSpace(value)
r.Disposition = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetAmaFlag(value string) *AMICdr {
r.AmaFlags = utils.TrimAllSpace(value)
r.AmaFlags = TrimStringSpaces(value)
return r
}

Expand Down Expand Up @@ -200,7 +199,7 @@ func (r *AMICdr) SetPrivilege(value string) *AMICdr {
}

func (r *AMICdr) SetDirection(value string) *AMICdr {
r.Direction = utils.TrimAllSpace(value)
r.Direction = TrimStringSpaces(value)
return r
}

Expand All @@ -210,22 +209,22 @@ func (r *AMICdr) SetFlowCall(value string) *AMICdr {
}

func (r *AMICdr) SetTypeDirection(value string) *AMICdr {
r.TypeDirection = utils.TrimAllSpace(value)
r.TypeDirection = TrimStringSpaces(value)
return r
}

func (r *AMICdr) SetUserExten(value string) *AMICdr {
r.UserExtension = utils.TrimAllSpace(value)
r.UserExtension = TrimStringSpaces(value)
return r
}

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

func (r *AMICdr) SetExtenSplitterSymbol(value string) *AMICdr {
r.ExtenSplitterSymbol = utils.TrimAllSpace(value)
r.ExtenSplitterSymbol = TrimStringSpaces(value)
return r
}

Expand All @@ -235,7 +234,7 @@ func (r *AMICdr) SetPlaybackUrl(value string) *AMICdr {
}

func (r *AMICdr) Json() string {
return utils.ToJson(r)
return JsonString(r)
}

func (r *AMICdr) IsCdrNoAnswer() bool {
Expand Down Expand Up @@ -346,8 +345,8 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
// detect outbound, inbound
// if the field destination is phone number, so mark this cdr belong to outbound, otherwise mark as inbound
form := "flow_call_from_'%v'_to_'%v'"
phone := utils.RemovePrefix(r.Destination, e.PhonePrefix...)
if IsPhoneNumberAbsolute(phone, e.Region) {
phone := RemoveStringPrefix(r.Destination, e.PhonePrefix...)
if IsPhoneNumberWith(phone, e.Region) {
flow := fmt.Sprintf(form, r.Channel, phone)
r.SetFlowCall(flow)
r.SetDirection(config.AmiOutboundDirection)
Expand Down Expand Up @@ -386,7 +385,7 @@ func ParseCdr(e *AMIMessage, d *AMIDictionary) *AMICdr {
r.SetPhoneNumber(r.Source)
}
if !inCase {
log.Printf("ParseCdr, CDR exception case = %v", utils.ToJson(r))
log.Printf("ParseCdr, CDR exception case = %v", JsonString(r))
}
}
return r
Expand Down
9 changes: 4 additions & 5 deletions pkg/ami/ami_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/pnguyen215/gobase-voip-core/pkg/ami/config"
"github.com/pnguyen215/gobase-voip-core/pkg/ami/utils"
)

func NewChannel() *AMIChannel {
Expand All @@ -18,7 +17,7 @@ func NewChannel() *AMIChannel {

func (c *AMIChannel) SetChannelProtocol(protocol string) *AMIChannel {
if ok := config.AmiChannelProtocols[protocol]; !ok {
msg := fmt.Sprintf(config.AmiErrorProtocolMessage, strings.Join(utils.Keys(config.AmiChannelProtocols), ","))
msg := fmt.Sprintf(config.AmiErrorProtocolMessage, strings.Join(GetKeys(config.AmiChannelProtocols), ","))
log.Panic(config.AmiErrorInvalidProtocol, "\n", msg)
}
c.ChannelProtocol = protocol
Expand Down Expand Up @@ -66,10 +65,10 @@ func (c *AMIChannel) ValidWith(channelProtocol string, regex string, digitsExten
if len(digitsExten) == 0 {
return false
}
if utils.IsEmptyAbsolute(extension) {
if IsStringEmpty(extension) {
return false
}
if utils.IsEmptyAbsolute(regex) {
if IsStringEmpty(regex) {
return false
}
c.SetChannelProtocol(channelProtocol)
Expand Down Expand Up @@ -103,7 +102,7 @@ func (c *AMIChannel) ValidSIPDefaultWith(digitsExten []interface{}, extension st
// Return as form [email protected]
func (c *AMIChannel) JoinHostChannel(protocol, ip string) string {
c.SetChannelProtocol(protocol)
host, _, _ := utils.IPDecode(ip)
host, _, _ := DecodeIp(ip)
form := "%v@%v"

if len(host) > 0 {
Expand Down
19 changes: 9 additions & 10 deletions pkg/ami/ami_chanspy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"

"github.com/pnguyen215/gobase-voip-core/pkg/ami/config"
"github.com/pnguyen215/gobase-voip-core/pkg/ami/utils"
)

func NewAMIPayloadChanspy() *AMIPayloadChanspy {
Expand All @@ -18,20 +17,20 @@ func NewAMIPayloadChanspy() *AMIPayloadChanspy {
func (s *AMIPayloadChanspy) SetJoin(value string) *AMIPayloadChanspy {
ok := config.AmiChanspy[value]
if !ok {
msg := fmt.Sprintf(config.AmiErrorChanspyMessage, strings.Join(utils.Keys(config.AmiChanspy), ","))
msg := fmt.Sprintf(config.AmiErrorChanspyMessage, strings.Join(GetKeys(config.AmiChanspy), ","))
log.Panic(config.AmiErrorInvalidChanspy, "\n", msg)
}
s.Join = utils.TrimAllSpace(value)
s.Join = TrimStringSpaces(value)
return s
}

func (s *AMIPayloadChanspy) SetSourceExten(value string) *AMIPayloadChanspy {
s.SourceExten = utils.TrimAllSpace(value)
s.SourceExten = TrimStringSpaces(value)
return s
}

func (s *AMIPayloadChanspy) SetCurrentExten(value string) *AMIPayloadChanspy {
s.CurrentExten = utils.TrimAllSpace(value)
s.CurrentExten = TrimStringSpaces(value)
return s
}

Expand All @@ -47,10 +46,10 @@ func (s *AMIPayloadChanspy) SetAllowDebug(value bool) *AMIPayloadChanspy {
}

func (s *AMIPayloadChanspy) CommandChanspy(channelExten string) string {
if utils.IsEmptyAbsolute(s.Join) {
if IsStringEmpty(s.Join) {
return ""
}
if utils.IsEmptyAbsolute(channelExten) {
if IsStringEmpty(channelExten) {
return ""
}
if strings.EqualFold(s.Join, config.AmiChanspySpy) {
Expand All @@ -69,13 +68,13 @@ func (s *AMIPayloadChanspy) CommandChanspy(channelExten string) string {
func Chanspy(ctx context.Context, s AMISocket, ch AMIPayloadChanspy) (AMIResultRawLevel, error) {
ok := config.AmiChanspy[ch.Join]
if !ok {
msg := fmt.Sprintf(config.AmiErrorChanspyMessage, strings.Join(utils.Keys(config.AmiChanspy), ","))
msg := fmt.Sprintf(config.AmiErrorChanspyMessage, strings.Join(GetKeys(config.AmiChanspy), ","))
log.Panic(config.AmiErrorInvalidChanspy, "\n", msg)
}
if utils.IsEmptyAbsolute(ch.SourceExten) {
if IsStringEmpty(ch.SourceExten) {
return AMIResultRawLevel{}, fmt.Errorf("Source exten is required")
}
if utils.IsEmptyAbsolute(ch.CurrentExten) {
if IsStringEmpty(ch.CurrentExten) {
return AMIResultRawLevel{}, fmt.Errorf("Current exten is required")
}
sourceValid, err := HasSIPPeerStatus(ctx, s, ch.SourceExten)
Expand Down
31 changes: 15 additions & 16 deletions pkg/ami/ami_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ami

import (
"github.com/pnguyen215/gobase-voip-core/pkg/ami/config"
"github.com/pnguyen215/gobase-voip-core/pkg/ami/utils"
)

var chargingEvents *map[string]string = &map[string]string{}
Expand All @@ -16,28 +15,28 @@ func (e *AMIEvent) SnapChargingEvent() *map[string]string {

_merged := make(map[string]string)

_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassCommands))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassSecurities))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassCalls))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassSystems))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassUsers))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassDialPlans))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassAgents))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassAgis))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassAocs))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassCallDetailRecords))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassChannelEventLoggings))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClasses))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassReports))
_merged = utils.MergeMaps(_merged, e.SwapCharging(config.AmiClassDualToneMultiFrequencies))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassCommands))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassSecurities))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassCalls))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassSystems))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassUsers))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassDialPlans))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassAgents))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassAgis))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassAocs))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassCallDetailRecords))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassChannelEventLoggings))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClasses))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassReports))
_merged = MergeMaps(_merged, e.SwapCharging(config.AmiClassDualToneMultiFrequencies))

chargingEvents = &_merged
return chargingEvents
}

// SnapChargingEventWith
func (e *AMIEvent) SnapChargingEventWith(ls map[string][]string) *map[string]string {
chargingEvents := utils.MergeMaps(*chargingEvents, e.SwapCharging(ls))
chargingEvents := MergeMaps(*chargingEvents, e.SwapCharging(ls))
return &chargingEvents
}

Expand Down
Loading

0 comments on commit dd30624

Please sign in to comment.