-
Notifications
You must be signed in to change notification settings - Fork 0
/
delivery_receipt_info.go
130 lines (112 loc) · 2.34 KB
/
delivery_receipt_info.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
package zkm
import (
"fmt"
"strconv"
"strings"
"time"
)
type DeliveryReceiptState uint8
const (
EnRoute DeliveryReceiptState = iota + 1
Delivered
Expired
Deleted
Undeliverable
Accepted
Unknown
Rejected
)
type DeliveryReceiptInfo struct {
Id string
State DeliveryReceiptState
Err uint16
Text string
}
//"id:cb9c40f1-0aa1-4b3e-afb8-7dd24d03a716 sub:001 dlvrd:001 submit date:2010241205 done date:2010241206 stat:DELIVRD err:000"
func NewDeliveryReceiptInfoByPdu(pdu *Pdu) *DeliveryReceiptInfo {
dri := &DeliveryReceiptInfo{}
if pdu.id != DeliverSm {
return dri
}
if esmClass, err := pdu.GetMainAsUint32(ESMClass); (esmClass&0x24) == 0 || err != nil {
return dri
}
rawText, err := pdu.GetMainAsRaw(ShortMessage)
if err != nil {
return dri
}
dri.Text = string(rawText)
rawFields := strings.Split(dri.Text, " ")
for _, rawF := range rawFields {
f := strings.Split(rawF, ":")
if len(f) != 2 {
continue
}
switch f[0] {
case "id":
dri.Id = f[1]
case "stat":
switch f[1] {
case "ENROUTE":
dri.State = EnRoute
case "DELIVRD":
dri.State = Delivered
case "EXPIRED":
dri.State = Expired
case "DELETED":
dri.State = Deleted
case "UNDELIV":
dri.State = Undeliverable
case "ACCEPTD":
dri.State = Accepted
case "UNKNOWN":
dri.State = Unknown
case "REJECTD":
dri.State = Rejected
}
case "err":
e, err := strconv.ParseUint(f[1], 10, 64)
if err == nil {
dri.Err = uint16(e)
}
}
}
return dri
}
func NewDeliveryReceiptInfo(msgId string, submitTime, doneTime int64, state DeliveryReceiptState, err uint16) *DeliveryReceiptInfo {
success := 1
if state != Delivered {
success = 0
}
return &DeliveryReceiptInfo{
Id: msgId,
State: state,
Err: err,
Text: fmt.Sprintf("id:%v sub:001 dlvrd:00%v submit date:%v done date:%v stat:%v err:%v",
msgId, success, time.Unix(submitTime, 0).Format("0601021504"),
time.Unix(doneTime, 0).Format("0601021504"),
state, err%1000),
}
}
func (s DeliveryReceiptState) String() string {
switch s {
case EnRoute:
return "ENROUTE"
case Delivered:
return "DELIVRD"
case Expired:
return "EXPIRED"
case Deleted:
return "DELETED"
case Undeliverable:
return "UNDELIV"
case Accepted:
return "ACCEPTD"
case Unknown:
return "UNKNOWN"
case Rejected:
return "REJECTD"
default:
return "UNKNOWN"
}
}