-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.go
More file actions
85 lines (70 loc) · 1.79 KB
/
event.go
File metadata and controls
85 lines (70 loc) · 1.79 KB
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
package main
import (
"errors"
"strconv"
)
// An Event represents what happened to the telephony system
type Event interface {
}
// Ring means someone is calling and the phone is ringing
type Ring struct {
From string `json:"from"`
To string `json:"to"`
}
// Disconnect means a connection has terminated
type Disconnect struct {
Duration int `json:"duration"`
}
// Connect means a connections was sucessfull (talking)
type Connect struct {
Extension string `json:"extension"`
From string `json:"from"`
}
// Call means an outgoing call is being initiated (not yet connected)
type Call struct {
Extension string `json:"extension"`
From string `json:"from"`
To string `json:"to"`
}
func parseEvent(eventType string, fields []string) (Event, error) {
switch eventType {
case "RING":
return parseRing(fields)
case "DISCONNECT":
return parseDisconnect(fields)
case "CONNECT":
return parseConnect(fields)
case "CALL":
return parseCall(fields)
default:
return nil, errors.New("Invalid event type")
}
}
func parseRing(fields []string) (Ring, error) {
if len(fields) < 2 {
return Ring{}, errors.New("Not enough fields")
}
return Ring{fields[0], fields[1]}, nil
}
func parseDisconnect(fields []string) (Disconnect, error) {
if len(fields) < 1 {
return Disconnect{}, errors.New("Not enough fields")
}
duration, err := strconv.Atoi(fields[0])
if err != nil {
return Disconnect{}, err
}
return Disconnect{duration}, nil
}
func parseConnect(fields []string) (Connect, error) {
if len(fields) < 2 {
return Connect{}, errors.New("Not enough fields")
}
return Connect{fields[0], fields[1]}, nil
}
func parseCall(fields []string) (Call, error) {
if len(fields) < 3 {
return Call{}, errors.New("Not enough fields")
}
return Call{fields[0], fields[1], fields[2]}, nil
}