Skip to content

Commit

Permalink
feat : base structure (#1)
Browse files Browse the repository at this point in the history
* Create base structure of library

* fix: Change the serial library with the one has timeout feature

* refactor: get tidy
  • Loading branch information
selengalp committed Oct 31, 2023
1 parent 2171b1f commit 35cd703
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 21 deletions.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

142 changes: 142 additions & 0 deletions atcom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Created by: Yasin Kaya (selengalp), [email protected], 2023
Copyright (c) 2023 Sixfab Inc.
*/
package atcom

import (
"bufio"
"context"
"errors"
"strings"
"time"

"github.com/tarm/serial"
)

func open(args map[string]interface{}) (port *serial.Port, err error) {

portname := "/dev/ttyUSB2"
baudrate := 115200

for key, value := range args {
switch key {
case "port":
portname = value.(string)
case "baud":
baudrate = value.(int)
}
}

config := &serial.Config{
Name: portname,
Baud: baudrate,
ReadTimeout: time.Millisecond * 100,
}

return serial.OpenPort(config)
}

func SendAT(command string, args map[string]interface{}) ([]string, error) {

var lineEnd bool = true
var desired []string
var fault []string
var timeout int

for key, value := range args {
switch key {
case "desired":
desired = value.([]string)
case "fault":
fault = value.([]string)
case "timeout":
timeout = value.(int)
case "lineEnd":
lineEnd = value.(bool)
}
}

serialPort, err := open(args)

if err != nil {
return nil, err
}

defer serialPort.Close()

if lineEnd {
command += "\r\n"
}

_, err = serialPort.Write([]byte(command))
if err != nil {
return nil, err
}

scanner := bufio.NewScanner(serialPort)
response := make([]string, 0)
timeoutDuration := time.Duration(timeout) * time.Second

found := make(chan error)
defer close(found)

ctxScan, cancelScan := context.WithCancel(context.Background())
defer cancelScan()

go func(ctx context.Context) {
for {
if !scanner.Scan() {
return
}
line := scanner.Text()
line = strings.TrimSpace(line)
line = strings.Trim(line, "\r")
line = strings.Trim(line, "\n")

if line != "" {
response = append(response, line)
}

if line == "OK" {

data := ""
for _, word := range response {
if word != "OK" {
data += word
}
}

for _, desiredStr := range desired {
if strings.Contains(data, desiredStr) {
found <- nil
return
}
}

for _, faultStr := range fault {
if strings.Contains(data, faultStr) {
found <- nil
return
}
}
} else if line == "ERROR" || strings.Contains(line, "+CME ERROR") {
found <- errors.New("modem error")
return
}
}
}(ctxScan)

timeoutCh := time.After(timeoutDuration)

for {
select {
case err := <-found:
return response, err
case <-timeoutCh:
cancelScan()
return response, errors.New("timeout")
}
}
}
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/sixfab/atcom

go 1.21.1

require github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07

require golang.org/x/sys v0.13.0 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUYb9Fbt8uiOSooupjTq10vpvnU=
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
3 changes: 3 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package atcom

const Version = "0.1.0"

0 comments on commit 35cd703

Please sign in to comment.