forked from cgrates/cgrates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsevent.go
133 lines (120 loc) · 4.23 KB
/
fsevent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Rating system designed to be used in VoIP Carriers World
Copyright (C) 2013 ITsysCOM
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package sessionmanager
import (
"fmt"
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/fsock"
"strconv"
"strings"
"time"
)
// Event type holding a mapping of all event's proprieties
type FSEvent map[string]string
const (
// Freswitch event proprities names
DIRECTION = "Call-Direction"
SUBJECT = "variable_cgr_subject"
ACCOUNT = "variable_cgr_account"
DESTINATION = "variable_cgr_destination"
REQTYPE = "variable_cgr_reqtype" //prepaid or postpaid
TOR = "variable_cgr_tor"
UUID = "Unique-ID" // -Unique ID for this call leg
CSTMID = "variable_cgr_tenant"
CALL_DEST_NR = "Caller-Destination-Number"
PARK_TIME = "Caller-Profile-Created-Time"
START_TIME = "Caller-Channel-Answered-Time"
END_TIME = "Caller-Channel-Hangup-Time"
NAME = "Event-Name"
HEARTBEAT = "HEARTBEAT"
ANSWER = "CHANNEL_ANSWER"
HANGUP = "CHANNEL_HANGUP_COMPLETE"
PARK = "CHANNEL_PARK"
AUTH_OK = "+AUTH_OK"
DISCONNECT = "+SWITCH DISCONNECT"
INSUFFICIENT_FUNDS = "-INSUFFICIENT_FUNDS"
MISSING_PARAMETER = "-MISSING_PARAMETER"
SYSTEM_ERROR = "-SYSTEM_ERROR"
MANAGER_REQUEST = "+MANAGER_REQUEST"
USERNAME = "Caller-Username"
)
// Nice printing for the event object.
func (fsev FSEvent) String() (result string) {
for k, v := range fsev {
result += fmt.Sprintf("%s = %s\n", k, v)
}
result += "=============================================================="
return
}
// Loads the new event data from a body of text containing the key value proprieties.
// It stores the parsed proprieties in the internal map.
func (fsev FSEvent) New(body string) Event {
fsev = fsock.FSEventStrToMap(body, nil)
return fsev
}
func (fsev FSEvent) GetName() string {
return fsev[NAME]
}
func (fsev FSEvent) GetDirection() string {
//TODO: implement direction
return "*out"
//return fsev[DIRECTION]
}
func (fsev FSEvent) GetSubject() string {
return utils.FirstNonEmpty(fsev[SUBJECT], fsev[USERNAME])
}
func (fsev FSEvent) GetAccount() string {
return utils.FirstNonEmpty(fsev[ACCOUNT], fsev[USERNAME])
}
// Charging destination number
func (fsev FSEvent) GetDestination() string {
return utils.FirstNonEmpty(fsev[DESTINATION], fsev[CALL_DEST_NR])
}
// Original dialed destination number, useful in case of unpark
func (fsev FSEvent) GetCallDestNr() string {
return fsev[CALL_DEST_NR]
}
func (fsev FSEvent) GetTOR() string {
return utils.FirstNonEmpty(fsev[TOR], cfg.DefaultTOR)
}
func (fsev FSEvent) GetUUID() string {
return fsev[UUID]
}
func (fsev FSEvent) GetTenant() string {
return utils.FirstNonEmpty(fsev[CSTMID], cfg.DefaultTenant)
}
func (fsev FSEvent) GetReqType() string {
return utils.FirstNonEmpty(fsev[REQTYPE], cfg.DefaultReqType)
}
func (fsev FSEvent) MissingParameter() bool {
return strings.TrimSpace(fsev.GetDirection()) == "" ||
strings.TrimSpace(fsev.GetSubject()) == "" ||
strings.TrimSpace(fsev.GetAccount()) == "" ||
strings.TrimSpace(fsev.GetDestination()) == "" ||
strings.TrimSpace(fsev.GetTOR()) == "" ||
strings.TrimSpace(fsev.GetUUID()) == "" ||
strings.TrimSpace(fsev.GetTenant()) == "" ||
strings.TrimSpace(fsev.GetCallDestNr()) == ""
}
func (fsev FSEvent) GetStartTime(field string) (t time.Time, err error) {
st, err := strconv.ParseInt(fsev[field], 0, 64)
t = time.Unix(0, st*1000)
return
}
func (fsev FSEvent) GetEndTime() (t time.Time, err error) {
st, err := strconv.ParseInt(fsev[END_TIME], 0, 64)
t = time.Unix(0, st*1000)
return
}