Refactoring.

This commit is contained in:
Andrey Egorov 2024-07-26 22:58:34 +03:00
parent 12555f2f46
commit b9056d2299
4 changed files with 71 additions and 57 deletions

View File

@ -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()
}

64
api/modem/gpio/gpio.go Normal file
View File

@ -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()
}

View File

@ -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(),
}
}

10
main.go
View File

@ -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
}