func (self *Service) Run() error { // connect to modem devname := expandDevName() if devname == "" { log.Fatalln("Device not found") } conf := serial.Config{Name: devname, Baud: 115200} var err error modem, err = gogsmmodem.Open(&conf, false) if err != nil { return err } log.Println("Connected") log.Println("Checking message storage for unread") msgs, err := modem.ListMessages("ALL") if err == nil { for _, msg := range *msgs { if msg.Status == "REC UNREAD" { fmt.Printf("Message from %s: %s\n", msg.Telephone, msg.Body) services.SendQuery(msg.Body, "sms", msg.Telephone, "alert") } // delete - any unread have been read modem.DeleteMessage(msg.Index) } } log.Println("Ready") events := services.Subscriber.FilteredChannel("alert") for { select { case ev := <-events: if ev.Target() == "sms" { sendMessage(ev) } case p := <-modem.OOB: log.Printf("Received: %#v\n", p) switch p := p.(type) { case gogsmmodem.MessageNotification: msg, err := modem.GetMessage(p.Index) if err == nil { fmt.Printf("Message from %s: %s\n", msg.Telephone, msg.Body) services.SendQuery(msg.Body, "sms", msg.Telephone, "alert") modem.DeleteMessage(p.Index) } } } } return nil }
func main() { conf := serial.Config{Name: "/dev/ttyUSB1", Baud: 115200} modem, err := gogsmmodem.Open(&conf, true) if err != nil { panic(err) } li, _ := modem.ListMessages("ALL") fmt.Printf("%d stored messages\n", len(*li)) for _, msg := range *li { fmt.Println(msg.Index, msg.Status, msg.Body) } }
func main() { conf := serial.Config{Name: "/dev/ttyUSB1", Baud: 115200} modem, err := gogsmmodem.Open(&conf, true) if err != nil { panic(err) } for packet := range modem.OOB { log.Printf("Received: %#v\n", packet) switch p := packet.(type) { case gogsmmodem.MessageNotification: log.Println("Message notification:", p) msg, err := modem.GetMessage(p.Index) if err == nil { fmt.Printf("Message from %s: %s\n", msg.Telephone, msg.Body) modem.DeleteMessage(p.Index) } } } }