From 1b741c7dab5e3f2d5d03608382d7981a818bc7fb Mon Sep 17 00:00:00 2001
From: Andrey Egorov <and7643@gmail.com>
Date: Mon, 29 Jul 2024 16:51:54 +0300
Subject: [PATCH] Added sms test.

---
 api/modem/modem.go   | 32 +++++++++++++++++++++++++++-----
 api/modem/sms/sms.go | 11 +++++++++++
 main.go              |  5 +++++
 3 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/api/modem/modem.go b/api/modem/modem.go
index 632c13d..ad7803e 100644
--- a/api/modem/modem.go
+++ b/api/modem/modem.go
@@ -12,6 +12,7 @@ import (
 	"github.com/CGSG-2021-AE4/modem-test/api/modem/at"
 	"github.com/CGSG-2021-AE4/modem-test/api/modem/gpio"
 	"github.com/CGSG-2021-AE4/modem-test/api/modem/internet"
+	"github.com/CGSG-2021-AE4/modem-test/api/modem/sms"
 )
 
 type ModemData struct {
@@ -40,6 +41,9 @@ type modem struct {
 
 	// Internet connection
 	ic internet.Conn
+
+	// Sms and calls
+	sms sms.Dialer
 }
 
 type Modem interface {
@@ -47,6 +51,10 @@ type Modem interface {
 	Validate() bool
 	Update() error
 	GetInfo() ModemData
+
+	// Temp access to sms interface
+	Sms() sms.Dialer
+
 	io.Closer
 }
 
@@ -95,11 +103,17 @@ func (m *modem) Init() error {
 		return fmt.Errorf("testGPS: %w", err)
 	}
 
-	// Establish internet connection
-	m.logger.Println("=============================== Internet")
-	m.ic = internet.New(log.New(m.logger.Writer(), "internet", log.LstdFlags), m.port)
-	if err := m.ic.Init(); err != nil {
-		return fmt.Errorf("internet connection init: %w", err)
+	// // Establish internet connection
+	// m.logger.Println("=============================== Internet")
+	// m.ic = internet.New(log.New(m.logger.Writer(), "internet", log.LstdFlags), m.port)
+	// if err := m.ic.Init(); err != nil {
+	// 	return fmt.Errorf("internet connection init: %w", err)
+	// }
+
+	// Init sms dialer
+	m.sms = sms.New(log.New(m.logger.Writer(), "sms", log.LstdFlags), m.port)
+	if err := m.sms.Init(); err != nil {
+		return fmt.Errorf("sms dialer init %w", err)
 	}
 	return nil
 }
@@ -155,7 +169,15 @@ func (m *modem) GetInfo() ModemData {
 	}
 }
 
+func (m *modem) Sms() sms.Dialer {
+	return m.sms
+}
+
 func (m *modem) Close() error {
+	if err := m.sms.Close(); err != nil {
+		return fmt.Errorf("sms: %w", err)
+	}
+
 	// Not right way I think
 	if err := m.port.Close(); err != nil {
 		return fmt.Errorf("serial port: %w", err)
diff --git a/api/modem/sms/sms.go b/api/modem/sms/sms.go
index 58cc9b7..72646ca 100644
--- a/api/modem/sms/sms.go
+++ b/api/modem/sms/sms.go
@@ -21,6 +21,13 @@ type Dialer interface {
 	io.Closer
 }
 
+func New(logger *log.Logger, port at.Port) Dialer {
+	return &dialer{
+		logger: logger,
+		port:   port,
+	}
+}
+
 func (d *dialer) Init() error {
 	// Ensure serial port
 	if !d.port.IsConnected() {
@@ -59,3 +66,7 @@ func (d *dialer) ReadNew() ([]string, error) {
 	msgs := strings.Split(strings.Replace(string(resp), "\r", "", -1), "\n")
 	return msgs, nil // TODO
 }
+
+func (d *dialer) Close() error {
+	return nil
+}
diff --git a/main.go b/main.go
index c8a148c..1eaf633 100644
--- a/main.go
+++ b/main.go
@@ -27,5 +27,10 @@ func mainE() error {
 	}
 	log.Println("||||||||||||||||| GET INFO |||||||||||||||||")
 	log.Println(m.GetInfo())
+
+	log.Println("||||||||||||||||| SEND SMS |||||||||||||||||")
+	if err := m.Sms().Send("+", "CGSG forever"); err != nil {
+		return err
+	}
 	return nil
 }