-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
65 lines (58 loc) · 1.63 KB
/
main.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
package main
import (
"fmt"
"strings"
"time"
"github.com/jacobsa/go-serial/serial"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
var database *gorm.DB
func main() {
database, _ = gorm.Open("sqlite3", "smsstore.sqlite3")
database.AutoMigrate(&SMS{})
options := serial.OpenOptions{
PortName: "/dev/ttyS0",
BaudRate: 115200,
MinimumReadSize: 2,
StopBits: 1,
DataBits: 8,
}
port, err := serial.Open(options)
if err != nil {
fmt.Println("Unable to open serial port. Bailing out...")
fmt.Print(err)
} else {
startup(port)
go reader(port)
check_return = true
//as we now have a modem that talks to us, we should get to the point of telling it not to echo.
port.Write([]byte("ATE0\r\n"))
//fmt.Println(">ATE0")
<-proceed
//and we should go forward and fire up the network
port.Write([]byte("AT+COPS=1\r\n"))
//fmt.Println(">AT+COPS=1")
<-proceed
port.Write([]byte("AT+CMGF=1\r\n"))
//fmt.Println(">AT+CMGF=1")
<-proceed
check_return = false
for true {
idx := <-incoming_sms
fmt.Println("Incoming sms detected! Index: " + idx)
collect_reply = true
port.Write([]byte("AT+CMGR=" + idx + "\r\n"))
content := <-collect
header := strings.Split(content[1], ",")
number := header[1]
sms_content := content[2 : len(content)-2]
sms := strings.Join(sms_content, "\n")
fmt.Println("SMS from " + number + ":" + sms)
message := SMS{Number: number, Text: sms, Date: time.Now().Unix()}
database.Save(&message)
port.Write([]byte("AT+CMGD=" + idx + "\r\n"))
//TODO: now as we have the message, we have to call out to the world.
}
}
}