-
Notifications
You must be signed in to change notification settings - Fork 0
/
modem.go
94 lines (79 loc) · 1.89 KB
/
modem.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
package main
import (
"bytes"
"encoding/json"
"log"
"net/http"
"time"
"github.com/barnybug/gogsmmodem"
"github.com/google/uuid"
"github.com/jinzhu/gorm"
)
func saveAndDelete(db *gorm.DB, modem *gogsmmodem.Modem, msg *gogsmmodem.Message, notificationUrl string) error {
message := Message{
ID: uuid.New().String(),
Number: msg.Telephone,
Body: msg.Body,
Incoming: true,
Time: readTimeAsUTC(msg.Timestamp, time.Now().Location()),
}
// store message
db.Create(&message)
deleteErr := modem.DeleteMessage(msg.Index)
if deleteErr != nil {
return deleteErr
}
str, marshalErr := json.Marshal(&message)
if marshalErr != nil {
return marshalErr
}
// post result
res, err := http.Post(notificationUrl, "application/json", bytes.NewBuffer(str))
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode <= 299 {
// update handled
message.Handled = true
db.Save(&message)
}
return nil
}
func listenOnModem(db *gorm.DB, modem *gogsmmodem.Modem, notificationUrl string) chan error {
errorChannel := make(chan error, 1)
go func() {
// retrieve old messages
msgs, err := modem.ListMessages("ALL")
if err != nil {
errorChannel <- err
return
}
for _, msg := range []gogsmmodem.Message(*msgs) {
err := saveAndDelete(db, modem, &msg, notificationUrl)
if err != nil {
errorChannel <- err
}
}
for {
for packet := range modem.OOB {
switch p := packet.(type) {
case gogsmmodem.MessageNotification:
msg, err := modem.GetMessage(p.Index)
if err != nil {
errorChannel <- err
continue
}
log.Printf("Received message %v: %v\n", msg.Telephone, msg.Body)
saveErr := saveAndDelete(db, modem, msg, notificationUrl)
if saveErr != nil {
errorChannel <- saveErr
continue
}
}
}
time.Sleep(500 * time.Millisecond)
}
}()
return errorChannel
}