From a3e2526c421e41db93ba3186fb25adb730fd6ef8 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 3 Jul 2015 20:21:07 +0300 Subject: [PATCH] published first event --- engine/balances.go | 19 +++++++++++++++++++ engine/calldesc.go | 7 +++++++ engine/pubsub.go | 2 +- utils/events.go | 5 +++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 utils/events.go diff --git a/engine/balances.go b/engine/balances.go index 56fac1c74..e995452f0 100644 --- a/engine/balances.go +++ b/engine/balances.go @@ -22,6 +22,7 @@ import ( "errors" "fmt" "sort" + "strconv" "strings" "time" @@ -280,6 +281,24 @@ func (b *Balance) GetCost(cd *CallDescriptor, getStandardIfEmpty bool) (*CallCos } func (b *Balance) SubstractAmount(amount float64) { + accountId := "" + if b.account != nil { + accountId = b.account.Id + } + Publish(CgrEvent{ + "EventName": utils.EVT_ACCOUNT_BALANCE_MODIFIED, + "Uuid": b.Uuid, + "Id": b.Id, + "Value": strconv.FormatFloat(b.Value, 'f', -1, 64), + "ExpirationDate": b.ExpirationDate.String(), + "Weight": strconv.FormatFloat(b.Weight, 'f', -1, 64), + "DestinationIds": b.DestinationIds, + "RatingSubject": b.RatingSubject, + "Category": b.Category, + "SharedGroup": b.SharedGroup, + "TimingIDs": b.TimingIDs, + "Account": accountId, + }) b.Value -= amount b.Value = utils.Round(b.Value, globalRoundingDecimals, utils.ROUNDING_MIDDLE) b.dirty = true diff --git a/engine/calldesc.go b/engine/calldesc.go index 2e45d3c54..0f3562638 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -108,6 +108,13 @@ func SetPubSub(ps PublisherSubscriber) { pubSubServer = ps } +func Publish(event CgrEvent) { + if pubSubServer != nil { + var s string + pubSubServer.Publish(PublishInfo{Event: event}, &s) + } +} + /* The input stucture that contains call information. */ diff --git a/engine/pubsub.go b/engine/pubsub.go index 72dfde16a..b6b3fcea8 100644 --- a/engine/pubsub.go +++ b/engine/pubsub.go @@ -132,7 +132,7 @@ func (ps *PubSub) Publish(pi PublishInfo, reply *string) error { ps.removeSubscriber(key) continue // subscription expired, do not send event } - if !pi.Event.PassFilters(subData.Filters) { + if subData.Filters == nil || !pi.Event.PassFilters(subData.Filters) { continue // the event does not match the filters } split := utils.InfieldSplit(key) diff --git a/utils/events.go b/utils/events.go new file mode 100644 index 000000000..98007c821 --- /dev/null +++ b/utils/events.go @@ -0,0 +1,5 @@ +package utils + +const ( + EVT_ACCOUNT_BALANCE_MODIFIED = "ACCOUNT_BALANCE_MODIFIED" +)