Skip to content

Commit

Permalink
240707.0008
Browse files Browse the repository at this point in the history
  • Loading branch information
ea7kir committed Jul 6, 2024
1 parent b293290 commit 9c42bc9
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 81 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# TODO

- review qLog log levels
- review log levels
- test install.sh
- add /doc folder with more info
- check for memory leaks
Expand Down
9 changes: 4 additions & 5 deletions ds18b20monitor/ds18b20monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
package ds18b20monitor

import (
"log"
"os"
"strconv"
"time"

"strings"
"sync"

"github.com/ea7kir/qLog"
)

const (
Expand Down Expand Up @@ -100,20 +99,20 @@ func readSensors(sensorList []*ds18b20Type, done chan struct{}) {
tempC = 0.0
data, err = os.ReadFile(file) // 75 bytes
if err != nil {
qLog.Error("1-Wire %s failed to read\n%s", sensor.slaveId, err)
log.Printf("ERROR 1-Wire %s failed to read\n%s", sensor.slaveId, err)
continue
}
str := string(data)
if !strings.Contains(str, "YES") {
// This flood the log file, so ignore it for now
// qLog.Warn("1-Wire %s did not contain YES", sensor.slaveId)
// log.Printf("WARN 1-Wire %s did not contain YES", sensor.slaveId)
continue
} //else {
subStr := strings.Split(str, "t=")
subStr1 := strings.TrimSpace(subStr[1])
tempC, err = strconv.ParseFloat(subStr1, 64)
if err != nil {
qLog.Warn("1-Wire %s failed to create float: %s", sensor.slaveId, err)
log.Printf("WARN 1-Wire %s failed to create float: %s", sensor.slaveId, err)
continue
}
//}
Expand Down
4 changes: 2 additions & 2 deletions etc/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# Install Q100 PA Server on Raspberry Pi 4
# Orignal design by Michael, EA7KIR

# updated to GO 1.22 on March 2 2024
# updated to GO 1.25 on July 6 2024

GOVERSION=1.22.2
GOVERSION=1.22.5

whoami | grep -q pi
if [ $? != 0 ]; then
Expand Down
5 changes: 4 additions & 1 deletion etc/q100paserver.service
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ After=mult-user.target
User=pi
Group=pi

StandardOutput=append:/home/pi/Q100/paserver.log
StandardError=append:/home/pi/Q100/paserver.log

#WorkingDirectory=/home/pi/Q100/q100paserver

Type=simple

@Restart=always
#Restart=always
#Restart=on-failure
#RestartSec=5

Expand Down
10 changes: 4 additions & 6 deletions fanMonitor/fanMonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
package fanMonitor

import (
"os"
"log"
"sync"
"time"

"github.com/ea7kir/qLog"
"github.com/warthog618/go-gpiocdev"
"github.com/warthog618/go-gpiocdev/device/rpi"
)
Expand Down Expand Up @@ -56,8 +55,7 @@ func newFan(j8Pin int) *fanType {
// WithDebounce(deboucePeriod)
l, err := gpiocdev.RequestLine("gpiochip0", j8Pin, gpiocdev.AsInput)
if err != nil {
qLog.Fatal("line %v failed: %s", l, err)
os.Exit(1)
log.Fatalf("FATAL line %v failed: %s", l, err)
}
return &fanType{
mu: sync.Mutex{},
Expand Down Expand Up @@ -144,12 +142,12 @@ func readFanSpeeds(fanList []*fanType, done chan struct{}) {
}
v1, err := fan.line.Value()
if err != nil {
qLog.Warn(" %s", err)
log.Printf("WARN %s", err)
}
time.Sleep(3 * time.Millisecond)
v2, err := fan.line.Value()
if err != nil {
qLog.Warn(" %s", err)
log.Printf("WARN %s", err)
}
if v1 != v2 {
newRpm += 30
Expand Down
10 changes: 2 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
module q100paserver

// updated to GO 1.22.2 on March 2 2024
// updated to g-gpiocdev April 25 2024
go 1.22.5

go 1.22.2

require (
github.com/ea7kir/qLog v1.0.1
github.com/warthog618/go-gpiocdev v0.9.0
)
require github.com/warthog618/go-gpiocdev v0.9.0

require golang.org/x/sys v0.18.0 // indirect
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ea7kir/qLog v1.0.1 h1:Yn2cbeK1up+8ynMC3uDaotpGZGDb2kfzGeGBU4iAD3o=
github.com/ea7kir/qLog v1.0.1/go.mod h1:Io6KIIMhZFYJchcRPvpvCy997jf1tnWYhPc5swn2Cgg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
14 changes: 5 additions & 9 deletions ina226monitor/ina226monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
package ina226monitor

import (
"os"
"log"
"q100paserver/ina226Driver"
"sync"
"time"

"github.com/ea7kir/qLog"
)

const (
Expand Down Expand Up @@ -66,8 +64,7 @@ func Configure() {
finalPA = newIna226sensor(kFinalPaI2cAddress, kFinalPaShunt, kFinalPaMaxAmps)
sensor, err := ina226Driver.NewDriver(kI2cBus, kFinalPaI2cAddress)
if err != nil {
qLog.Fatal("failed NewDriver %s: ", err)
os.Exit(1)
log.Fatalf("FATAL failed NewDriver %s: ", err)
}
err = sensor.Configure(
ina226Driver.INA226_SHUNT_CONV_TIME_1100US,
Expand All @@ -76,8 +73,7 @@ func Configure() {
ina226Driver.INA226_MODE_SHUNT_BUS_CONT,
)
if err != nil {
qLog.Fatal("failed Configure %s: ", err)
os.Exit(1)
log.Fatalf("FATAL failed Configure %s: ", err)
}
sensor.Calibrate(kFinalPaShunt, kFinalPaMaxAmps)
finalPA.sensor = sensor
Expand Down Expand Up @@ -113,11 +109,11 @@ func readVoltsAmpsFor(sensor *ina226sensorType, done chan struct{}) {

vBus, err := sensor.sensor.ReadBusVoltage()
if err != nil {
qLog.Error("%s", err)
log.Printf("ERROR %s", err)
}
iShunt, err := sensor.sensor.ReadShuntCurrent()
if err != nil {
qLog.Error("%s", err)
log.Printf("ERROR %s", err)
}
// fmt.Printf("%v volts, %v amps\n", vBus, iShunt)

Expand Down
47 changes: 16 additions & 31 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bufio"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
Expand All @@ -21,8 +22,6 @@ import (
"strings"
"sync"
"syscall"

"github.com/ea7kir/qLog"
)

const kServerAddress = "0.0.0.0:9999" // "0.0.0.0:8765"
Expand Down Expand Up @@ -53,15 +52,15 @@ func readDevices() string {

func shutdownDevices() {
psuSwitcher.Shutdown()
qLog.Info("Shutdown psudriver - done")
log.Printf("INFO Shutdown psudriver - done")
fanMonitor.Shutdown()
qLog.Info("Shutdown fandriver - done")
log.Printf("INFO Shutdown fandriver - done")
ina226monitor.Shutdown()
qLog.Info("Shutdown ina226driver - done")
log.Printf("INFO Shutdown ina226driver - done")
ds18b20monitor.Shutdown()
qLog.Info("Shutdown ds18b20driver - done")
log.Printf("INFO Shutdown ds18b20driver - done")
rpiMonitor.Shutdown()
qLog.Info("Shutdown rpidriver - done")
log.Printf("INFO Shutdown rpidriver - done")
}

// https://eli.thegreenplace.net/2020/graceful-shutdown-of-a-tcp-server-in-go/
Expand All @@ -86,8 +85,7 @@ func NewServer(addr string) *Server {
}
l, err := net.Listen("tcp", addr)
if err != nil {
qLog.Fatal("Failed to create listener: %s", err)
os.Exit(1)
log.Fatalf("FATAL Failed to create listener: %s", err)
}
s.listener = l
s.wg.Add(1)
Expand All @@ -111,7 +109,7 @@ func (s *Server) serve() {
case <-s.quit:
return
default:
qLog.Warn("accept error: %s", err)
log.Printf("WARN accept error: %s", err)
}
} else {
s.wg.Add(1)
Expand All @@ -126,7 +124,7 @@ func (s *Server) serve() {
func (s *Server) handleConection(conn net.Conn) {
defer conn.Close()

qLog.Info("got connection from: %v\n", conn.RemoteAddr())
log.Printf("INFO got connection from: %v\n", conn.RemoteAddr())
psuSwitcher.Up()
clientReader := bufio.NewReader(conn)

Expand All @@ -137,16 +135,16 @@ func (s *Server) handleConection(conn net.Conn) {
case nil:
clientRequest := strings.TrimSpace(clientRequest)
if clientRequest == "CLOSE" {
qLog.Info("Connection closed with CLOSE")
log.Printf("INFO Connection closed with CLOSE")
psuSwitcher.Down()
return
}
case io.EOF:
qLog.Info("Connection closed with io.EOF")
log.Printf("INFO Connection closed with io.EOF")
psuSwitcher.Down()
return
default:
qLog.Warn("Connection closed abnormally: %s", err)
log.Printf("WARN Connection closed abnormally: %s", err)
psuSwitcher.Down()
return
}
Expand All @@ -155,27 +153,14 @@ func (s *Server) handleConection(conn net.Conn) {
str := readDevices() + "\n"

if _, err = conn.Write([]byte(str)); err != nil {
qLog.Warn("failed to respond to client: %s\n", err)
log.Printf("WARN failed to respond to client: %s\n", err)
}
}
}

func main() {

qLog.SetDebugLevel()

// qLog.Open("mylog.txt")
logFile, err := os.OpenFile("/home/pi/Q100/paserver.log", os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
fmt.Println("failed to open log file:", err)
os.Exit(1)
}

// log.SetOutput(os.Stderr)
qLog.SetOutput(logFile)
defer qLog.Close()

qLog.Info("Q-100 PA Server has started")
log.Printf("INFO Q-100 PA Server has started")

// capture exit signals to ensure pins are reverted to input on exit.
quit := make(chan os.Signal, 1)
Expand All @@ -188,12 +173,12 @@ func main() {

<-quit // wait on interupt

qLog.Info("---------- got interupt ----------")
log.Printf("INFO ---------- got interupt ----------")

server.Stop()

shutdownDevices()
qLog.Info("Q-100 PA Server has stopped")
log.Printf("INFO Q-100 PA Server has stopped")

// TODO: shutdown or reboot Rasberry Pi
}
20 changes: 8 additions & 12 deletions psuSwitcher/psuSwitcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
package psuSwitcher

import (
"os"
"log"
"time"

"github.com/ea7kir/qLog"
"github.com/warthog618/go-gpiocdev"
"github.com/warthog618/go-gpiocdev/device/rpi"
)
Expand Down Expand Up @@ -39,22 +38,19 @@ var (
func Configure() {
relay28vLine, err := gpiocdev.RequestLine("gpiochip0", k28vRelayPin, gpiocdev.AsOutput(0))
if err != nil {
qLog.Fatal("Failed to configure 28v rpi.J8p37 : %s", err)
os.Exit(1)
log.Fatalf("FATAL Failed to configure 28v rpi.J8p37 : %s", err)
}
relay28v = relay28vLine
relay28v.SetValue(kRelayOff)
relay12vLine, err := gpiocdev.RequestLine("gpiochip0", k12vRelayPin, gpiocdev.AsOutput(0))
if err != nil {
qLog.Fatal("Failed to configure 12v rpi.J8p38 : %s", err)
os.Exit(1)
log.Fatalf("FATAL Failed to configure 12v rpi.J8p38 : %s", err)
}
relay12v = relay12vLine
relay12v.SetValue(kRelayOff)
relay5vLine, err := gpiocdev.RequestLine("gpiochip0", k5vRelayPin, gpiocdev.AsOutput(0))
if err != nil {
qLog.Fatal("Failed to configure 5v rpi.J8p40 : %s", err)
os.Exit(1)
log.Fatalf("FATAL Failed to configure 5v rpi.J8p40 : %s", err)
}
relay5v = relay5vLine
relay5v.SetValue(kRelayOff)
Expand All @@ -74,23 +70,23 @@ func Shutdown() {
}

func Up() {
qLog.Info("Power UP is starting...")
log.Printf("INFO Power UP is starting...")
relay5v.SetValue(kRelayOn)
time.Sleep(200 * time.Millisecond)
relay28v.SetValue(kRelayOn)
time.Sleep(200 * time.Millisecond)
relay12v.SetValue(kRelayOn)
isUp = true
qLog.Info("Power UP has completed\n")
log.Printf("INFO Power UP has completed\n")
}

func Down() {
qLog.Info("Power DOWN is starting...\n")
log.Printf("INFO Power DOWN is starting...\n")
relay28v.SetValue(kRelayOff)
time.Sleep(200 * time.Millisecond)
relay5v.SetValue(kRelayOff)
time.Sleep(200 * time.Millisecond)
relay12v.SetValue(kRelayOff)
isUp = false
qLog.Info("Power DOWN has completed\n")
log.Printf("INFO Power DOWN has completed\n")
}
Loading

0 comments on commit 9c42bc9

Please sign in to comment.