From b9056d22990efe2dde670b0be729a98e6c9abfe0 Mon Sep 17 00:00:00 2001 From: Andrey Egorov Date: Fri, 26 Jul 2024 22:58:34 +0300 Subject: [PATCH] Refactoring. --- api/modem/gpio.go | 49 -------------------------------- api/modem/gpio/gpio.go | 64 ++++++++++++++++++++++++++++++++++++++++++ api/modem/modem.go | 5 ++-- main.go | 10 +++---- 4 files changed, 71 insertions(+), 57 deletions(-) delete mode 100644 api/modem/gpio.go create mode 100644 api/modem/gpio/gpio.go diff --git a/api/modem/gpio.go b/api/modem/gpio.go deleted file mode 100644 index dba973f..0000000 --- a/api/modem/gpio.go +++ /dev/null @@ -1,49 +0,0 @@ -package modem - -import ( - "log" - "time" - - gpio "github.com/stianeikeland/go-rpio/v4" -) - -type gpioPin struct { - Logger *log.Logger - Pin gpio.Pin -} - -func (p gpioPin) Init() error { - return gpio.Open() -} - -func (p gpioPin) sendOnOffSignal() { - p.Pin.Output() - p.Logger.Println("Power on 0/3 + 100ms") - p.Pin.Low() - p.Pin.Toggle() - time.Sleep(100 * time.Millisecond) - - p.Logger.Println("Power on 1/3 + 3s") - p.Pin.High() - p.Pin.Toggle() - time.Sleep(3 * time.Second) - - p.Logger.Println("Power on 2/3 + 30s") - p.Pin.Low() - p.Pin.Toggle() - time.Sleep(30 * time.Second) - - p.Logger.Println("Power on 3/3") -} - -func (p gpioPin) PowerOn() { - p.sendOnOffSignal() -} - -func (p *gpioPin) PowerOff() { - p.sendOnOffSignal() -} - -func (p *gpioPin) Close() error { - return gpio.Close() -} diff --git a/api/modem/gpio/gpio.go b/api/modem/gpio/gpio.go new file mode 100644 index 0000000..e3f1761 --- /dev/null +++ b/api/modem/gpio/gpio.go @@ -0,0 +1,64 @@ +package gpio + +import ( + "io" + "log" + "time" + + gpio "github.com/stianeikeland/go-rpio/v4" +) + +type gpioPin struct { + logger *log.Logger + pin gpio.Pin +} + +type Pin interface { + Init() error + PowerOn() + PowerOff() + io.Closer +} + +func New(logger *log.Logger, pin uint8) Pin { + return gpioPin{ + logger: logger, + pin: gpio.Pin(pin), + } +} + +func (p gpioPin) Init() error { + return gpio.Open() +} + +func (p gpioPin) sendOnOffSignal() { + p.pin.Output() + p.logger.Println("Power on 0/3 + 100ms") + p.pin.Low() + p.pin.Toggle() + time.Sleep(100 * time.Millisecond) + + p.logger.Println("Power on 1/3 + 3s") + p.pin.High() + p.pin.Toggle() + time.Sleep(3 * time.Second) + + p.logger.Println("Power on 2/3 + 30s") + p.pin.Low() + p.pin.Toggle() + time.Sleep(30 * time.Second) + + p.logger.Println("Power on 3/3") +} + +func (p gpioPin) PowerOn() { + p.sendOnOffSignal() +} + +func (p gpioPin) PowerOff() { + p.sendOnOffSignal() +} + +func (p gpioPin) Close() error { + return gpio.Close() +} diff --git a/api/modem/modem.go b/api/modem/modem.go index 49550e9..b89f0bc 100644 --- a/api/modem/modem.go +++ b/api/modem/modem.go @@ -10,6 +10,7 @@ import ( "time" "github.com/CGSG-2021-AE4/modem-test/api/modem/at" + "github.com/CGSG-2021-AE4/modem-test/api/modem/gpio" ) type ModemData struct { @@ -28,7 +29,7 @@ type modem struct { port at.Port // Gpio values - onOffPin gpioPin + onOffPin gpio.Pin // Other values gpsInfo GpsData @@ -47,7 +48,7 @@ func New(logger *log.Logger) Modem { return &modem{ logger: logger, baudrate: 115200, - onOffPin: gpioPin{Logger: logger, Pin: 6}, + onOffPin: gpio.New(logger, 6), lastUpdateTime: time.Now(), } } diff --git a/main.go b/main.go index adea31c..c8a148c 100644 --- a/main.go +++ b/main.go @@ -21,13 +21,11 @@ func mainE() error { if err := m.Init(); err != nil { return err } + if !m.Validate() { + log.Println("AAAAAAAAAAAAAAA Validation failed") + return nil + } log.Println("||||||||||||||||| GET INFO |||||||||||||||||") log.Println(m.GetInfo()) return nil - // ports, err := serial.GetPortsList() - // if err != nil { - // return err - // } - // log.Println(ports) - // return nil }