-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.go
143 lines (125 loc) · 3.07 KB
/
sms.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
134
135
136
137
138
139
140
141
142
143
/**
* File : sms.go
* Author : Jiang Yitao <jiangyt.cn#gmail.com>
* Date : 11.08.2019
* Last Modified Date: 08.09.2019
* Last Modified By : Jiang Yitao <jiangyt.cn#gmail.com>
*/
package sms
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"github.com/go-chi/chi"
"github.com/jiangytcn/gosms-ng/modem"
)
type SMS struct {
ID string `json:"id"`
CMGLID string `json:"cmgl_id"`
Mobile string `json:"mobile"`
Body string `json:"body"`
Status int `json:"status"`
Type string `json:"type"`
Retries int `json:"retries"`
Device string `json:"device"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
func Routes() chi.Router {
router := chi.NewRouter()
router.Get("/", GetAllMessages)
router.Delete("/{id}", deleteMessage)
return router
}
var raspModem *modem.GSMModem
func init() {
_port := "/dev/ttyS0"
_baud := 115200
raspModem = modem.New(_port, _baud, "mymodem")
if err := raspModem.Connect(); err != nil {
panic(err)
}
}
func deleteMessage(w http.ResponseWriter, r *http.Request) {
var (
smsid, err = strconv.Atoi(chi.URLParam(r, "id"))
)
if err != nil {
w.Write([]byte(err.Error()))
} else {
res := raspModem.DeleteSMS(smsid)
w.Write([]byte(res))
}
}
func GetAllMessages(w http.ResponseWriter, r *http.Request) {
var smss []SMS
var err error
allMsgs := raspModem.ReadSMSs()
msgsStr := strings.Join(allMsgs, "")
data := strings.Split(msgsStr, "\n")
var sanitized_data []string
// +CMGL: 13,"REC READ","15675180530","","19/08/07,21:54:54+32"
// Hhhhhh
for _, val := range data {
if len(val) >= 5 {
sanitized_data = append(sanitized_data, val)
}
}
for i := 0; i < len(sanitized_data); i = i + 2 {
sms := SMS{}
headers := strings.Split(sanitized_data[i], ",")
if len(headers) < 5 {
continue
}
sms.CMGLID = strings.Trim((strings.Split(headers[0], ":"))[1], " ")
sms.ID = sms.CMGLID
sms.Type = cleanStr(headers[1])
sms.Mobile = cleanStr(headers[2])
sms.CreatedAt = cleanStr(fmt.Sprintf("%s %s", headers[4], headers[5]))
if sms.Body, err = decodeHexUTF16(sanitized_data[i+1]); err != nil {
sms.Body = sanitized_data[i+1]
}
smss = append(smss, sms)
}
smssData := struct {
SMSS []SMS `json:"smss"`
}{
SMSS: smss,
}
bdata, err := json.Marshal(smssData)
if err != nil {
w.Write([]byte(err.Error()))
} else {
w.Write(bdata)
}
}
func cleanStr(text string) string {
return strings.Trim(strings.TrimSpace(text), "\"")
}
func decodeHexUTF16(text string) (string, error) {
l := len(text) / 4
var encodedStr []string
var pre, last int
for i := 0; i < l; i++ {
if i == 0 {
pre = 0
} else {
pre += 4
}
last = pre + 4
if safeSubstring, err := unquoteCodePoint((text[pre:last])); err != nil {
return "", err
} else {
encodedStr = append(encodedStr, safeSubstring)
}
}
return strings.Join(encodedStr, ""), nil
}
func unquoteCodePoint(s string) (string, error) {
// 16 specifies hex encoding
// 32 is size in bits of the rune type
r, err := strconv.ParseInt(s, 16, 32)
return string(r), err
}