Skip to content
Snippets Groups Projects
Commit b04758da authored by Kevin Lyda's avatar Kevin Lyda :speech_balloon:
Browse files

Add mutexes for serial access.

parent c4a8662d
No related branches found
No related tags found
No related merge requests found
Pipeline #
...@@ -13,7 +13,6 @@ import ( ...@@ -13,7 +13,6 @@ import (
"log" "log"
"net/http" "net/http"
"path" "path"
//"sync"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"gitlab.com/lyda/gqgmc/devices/geiger" "gitlab.com/lyda/gqgmc/devices/geiger"
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"strconv" "strconv"
"sync"
"time" "time"
"github.com/tarm/serial" "github.com/tarm/serial"
...@@ -55,6 +56,7 @@ type GQGMCCounter struct { ...@@ -55,6 +56,7 @@ type GQGMCCounter struct {
version, version,
model, model,
serial string serial string
mutex *sync.Mutex
} }
// NewGQGMC creates a new GQGMC Counter instance // NewGQGMC creates a new GQGMC Counter instance
...@@ -72,6 +74,7 @@ func NewGQGMC(c Config) (*GQGMCCounter, error) { ...@@ -72,6 +74,7 @@ func NewGQGMC(c Config) (*GQGMCCounter, error) {
return nil, err return nil, err
} }
gc.port = p gc.port = p
gc.mutex = &sync.Mutex{}
buf, err = gc.communicate(cmdGetVersion, 14) buf, err = gc.communicate(cmdGetVersion, 14)
if err == nil { if err == nil {
gc.model = string(buf[:7]) gc.model = string(buf[:7])
...@@ -165,7 +168,9 @@ func (gc *GQGMCCounter) TurnOnCPS() error { ...@@ -165,7 +168,9 @@ func (gc *GQGMCCounter) TurnOnCPS() error {
return errors.New("Unsupported version") return errors.New("Unsupported version")
} }
gc.mutex.Lock()
gc.sendCmd(cmdTurnOnCPS) gc.sendCmd(cmdTurnOnCPS)
gc.mutex.Unlock()
return nil return nil
} }
...@@ -178,14 +183,19 @@ func (gc *GQGMCCounter) TurnOffCPS() error { ...@@ -178,14 +183,19 @@ func (gc *GQGMCCounter) TurnOffCPS() error {
return errors.New("Unsupported version") return errors.New("Unsupported version")
} }
gc.mutex.Lock()
gc.sendCmd(cmdTurnOffCPS) gc.sendCmd(cmdTurnOffCPS)
gc.Clear() gc.Clear()
gc.mutex.Unlock()
return nil return nil
} }
// GetAutoCPS gets a reading once auto CPS is turned on // GetAutoCPS gets a reading once auto CPS is turned on
func (gc *GQGMCCounter) GetAutoCPS() (uint16, error) { func (gc *GQGMCCounter) GetAutoCPS() (uint16, error) {
gc.mutex.Lock()
buf, err := gc.readCmd(2) buf, err := gc.readCmd(2)
gc.mutex.Unlock()
if err != nil { if err != nil {
return 0, err return 0, err
} }
...@@ -203,7 +213,9 @@ func (gc *GQGMCCounter) TurnOnPower() { ...@@ -203,7 +213,9 @@ func (gc *GQGMCCounter) TurnOnPower() {
return return
} }
gc.mutex.Lock()
gc.sendCmd(cmdTurnOnPwr) gc.sendCmd(cmdTurnOnPwr)
gc.mutex.Unlock()
return return
} }
...@@ -216,7 +228,9 @@ func (gc *GQGMCCounter) TurnOffPower() { ...@@ -216,7 +228,9 @@ func (gc *GQGMCCounter) TurnOffPower() {
return return
} }
gc.mutex.Lock()
gc.sendCmd(cmdTurnOffPwr) gc.sendCmd(cmdTurnOffPwr)
gc.mutex.Unlock()
return return
} }
...@@ -256,6 +270,8 @@ func (gc *GQGMCCounter) SetTime(t time.Time) { ...@@ -256,6 +270,8 @@ func (gc *GQGMCCounter) SetTime(t time.Time) {
if gc.versionLT("Re 2.23") { if gc.versionLT("Re 2.23") {
return return
} }
gc.mutex.Lock()
defer gc.mutex.Unlock()
if gc.versionLT("Re 3.30") { if gc.versionLT("Re 3.30") {
gc.setTimeParts(t) gc.setTimeParts(t)
} }
...@@ -280,6 +296,7 @@ func (gc *GQGMCCounter) setTimeParts(t time.Time) { ...@@ -280,6 +296,7 @@ func (gc *GQGMCCounter) setTimeParts(t time.Time) {
copy(cmd[:], c.cmd) copy(cmd[:], c.cmd)
cmd[10] = uint8(c.unit) cmd[10] = uint8(c.unit)
copy(cmd[11:], ">>") copy(cmd[11:], ">>")
// Mutex acquired in setTime()
gc.port.Write(cmd) gc.port.Write(cmd)
gc.readCmd(1) gc.readCmd(1)
} }
...@@ -295,6 +312,7 @@ func (gc *GQGMCCounter) setTimeAll(t time.Time) { ...@@ -295,6 +312,7 @@ func (gc *GQGMCCounter) setTimeAll(t time.Time) {
cmd[16] = uint8(t.Minute()) cmd[16] = uint8(t.Minute())
cmd[17] = uint8(t.Second()) cmd[17] = uint8(t.Second())
copy(cmd[18:], ">>") copy(cmd[18:], ">>")
// Mutex acquired in setTime()
gc.port.Write(cmd) gc.port.Write(cmd)
gc.readCmd(1) gc.readCmd(1)
} }
...@@ -373,7 +391,9 @@ func (gc *GQGMCCounter) FactoryReset() { ...@@ -373,7 +391,9 @@ func (gc *GQGMCCounter) FactoryReset() {
return return
} }
gc.mutex.Lock()
gc.sendCmd(cmdFactoryReset) gc.sendCmd(cmdFactoryReset)
gc.mutex.Unlock()
return return
} }
...@@ -386,7 +406,9 @@ func (gc *GQGMCCounter) Reboot() { ...@@ -386,7 +406,9 @@ func (gc *GQGMCCounter) Reboot() {
return return
} }
gc.mutex.Lock()
gc.sendCmd(cmdReboot) gc.sendCmd(cmdReboot)
gc.mutex.Unlock()
return return
} }
...@@ -404,6 +426,8 @@ func (gc *GQGMCCounter) versionLT(version string) bool { ...@@ -404,6 +426,8 @@ func (gc *GQGMCCounter) versionLT(version string) bool {
} }
func (gc *GQGMCCounter) communicate(cmd string, length uint32) ([]byte, error) { func (gc *GQGMCCounter) communicate(cmd string, length uint32) ([]byte, error) {
gc.mutex.Lock()
defer gc.mutex.Unlock()
gc.Clear() gc.Clear()
if len(cmd) > 0 { if len(cmd) > 0 {
gc.sendCmd(cmd) gc.sendCmd(cmd)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment