-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75859c3
commit 07bf720
Showing
5 changed files
with
199 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"log/slog" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/themulle/chronokeyaccess/pkg/dooropener" | ||
) | ||
|
||
func main() { | ||
relayPin := flag.Int("relaypin", 18, "relay pin number") | ||
closedState := flag.Bool("closedstate", true, "default closed state") | ||
logLevelFlag := flag.String("loglevel", "info", "Log level (debug, info, error)") | ||
open := flag.Bool("open", true, "initialize opener to default state") | ||
// Parse command-line flags | ||
flag.Parse() | ||
|
||
setupLogger(*logLevelFlag) | ||
|
||
slog.Info("starting door opener") | ||
opener := dooropener.NewDoorOpener(*relayPin, *closedState) | ||
opener.InitAsOutput() | ||
if *open { | ||
opener.OpenDoor() | ||
} | ||
|
||
slog.Info("done") | ||
} | ||
|
||
func startShutdownListener(cancel context.CancelFunc) { | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) | ||
go func() { | ||
<-c | ||
log.Println("shutdown") | ||
cancel() | ||
}() | ||
} | ||
|
||
func setupLogger(logLevelFlag string) error { | ||
logLevel := slog.LevelInfo | ||
switch logLevelFlag { | ||
case "debug": | ||
logLevel = slog.LevelDebug | ||
case "info": | ||
logLevel = slog.LevelInfo | ||
case "error": | ||
logLevel = slog.LevelError | ||
default: | ||
return fmt.Errorf("Invalid log level: %s. Use 'debug', 'info', or 'error'", logLevelFlag) | ||
} | ||
|
||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ | ||
Level: logLevel, | ||
}))) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"log/slog" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/themulle/chronokeyaccess/pkg/wiegand" | ||
"github.com/themulle/chronokeyaccess/pkg/wiegand/wieganddecoder" | ||
) | ||
|
||
func main() { | ||
reader := wiegand.NewWiegandReader(14, 15) | ||
decoder := wieganddecoder.NewCommonDecoder() | ||
func setupLogger(logLevelFlag string) error { | ||
logLevel := slog.LevelInfo | ||
switch logLevelFlag { | ||
case "debug": | ||
logLevel = slog.LevelDebug | ||
case "info": | ||
logLevel = slog.LevelInfo | ||
case "error": | ||
logLevel = slog.LevelError | ||
default: | ||
return fmt.Errorf("Invalid log level: %s. Use 'debug', 'info', or 'error'", logLevelFlag) | ||
} | ||
|
||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ | ||
Level: slog.LevelInfo, | ||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{ | ||
Level: logLevel, | ||
}))) | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
data0pin := flag.Int("data0pin", 14, "data0 pin number") | ||
data1pin := flag.Int("data1pin", 15, "data1 pin number") | ||
logLevelFlag := flag.String("loglevel", "info", "Log level (debug, info, error)") | ||
// Parse command-line flags | ||
flag.Parse() | ||
|
||
setupLogger(*logLevelFlag) | ||
|
||
reader := wiegand.NewWiegandReader(*data0pin, *data1pin) | ||
decoder := wieganddecoder.NewCommonDecoder() | ||
|
||
slog.Info("starting wiegand reader") | ||
if err := reader.Start(); err != nil { | ||
slog.Error("Error initializing Wiegand reader", "error", err) | ||
return | ||
} | ||
|
||
for { | ||
time.Sleep(time.Millisecond * 100) | ||
bits, start, stop := reader.GetCache() | ||
if decoder.CheckInputComplete(bits, start, stop) { | ||
reader.ResetCache() | ||
number, err := decoder.Decode(bits) | ||
slog.Info("got input", "number", number, | ||
"cache", wieganddecoder.BitsToString(bits), | ||
"error", err, | ||
"start", start, | ||
"stop", stop) | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
startShutdownListener(cancel) | ||
|
||
} | ||
go func() { | ||
for { | ||
time.Sleep(time.Millisecond * 100) | ||
bits, start, stop := reader.GetCache() | ||
if decoder.CheckInputComplete(bits, start, stop) { | ||
reader.ResetCache() | ||
number, err := decoder.Decode(bits) | ||
slog.Info("got input", "number", number, | ||
"cache", wieganddecoder.BitsToString(bits), | ||
"error", err, | ||
"start", start, | ||
"stop", stop) | ||
|
||
fmt.Printf("%d %s %s %s %t\n", number, start.Format(time.RFC3339), stop.Format(time.RFC3339), wieganddecoder.BitsToString(bits), err == nil) | ||
} | ||
|
||
} | ||
}() | ||
|
||
<-ctx.Done() | ||
reader.Close() | ||
|
||
fmt.Println("quitting") | ||
slog.Info("done") | ||
|
||
} | ||
|
||
func startShutdownListener(cancel context.CancelFunc) { | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) | ||
go func() { | ||
<-c | ||
log.Println("shutdown") | ||
cancel() | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package dooropener | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/warthog618/go-gpiocdev" | ||
) | ||
|
||
type DoorOpener struct { | ||
RelayPin int | ||
Chip string | ||
ClosedState int | ||
OpenState int | ||
Runtime time.Duration | ||
} | ||
|
||
// NewWiegandReader erstellt einen neuen WiegandReader | ||
func NewDoorOpener(relayPin int, closedState bool) *DoorOpener { | ||
opener := &DoorOpener{ | ||
RelayPin: relayPin, | ||
Chip: "gpiochip0", | ||
Runtime: time.Second * 3, | ||
} | ||
if closedState { | ||
opener.ClosedState = 1 | ||
opener.OpenState = 0 | ||
} else { | ||
opener.ClosedState = 0 | ||
opener.OpenState = 1 | ||
} | ||
|
||
return opener | ||
} | ||
|
||
func (d *DoorOpener) InitAsOutput() error { | ||
line, err := gpiocdev.RequestLine(d.Chip, d.RelayPin, gpiocdev.AsOutput(d.ClosedState)) | ||
if err != nil { | ||
return err | ||
} | ||
return line.Close() | ||
} | ||
|
||
func (d *DoorOpener) OpenDoor() error { | ||
line, err := gpiocdev.RequestLine(d.Chip, d.RelayPin, gpiocdev.AsOutput(d.ClosedState)) | ||
if err != nil { | ||
return err | ||
} | ||
defer line.Close() | ||
defer line.SetValue(d.ClosedState) | ||
|
||
start := time.Now() | ||
for time.Since(start) < d.Runtime { | ||
if err = line.SetValue(d.OpenState); err != nil { | ||
return err | ||
} | ||
time.Sleep(time.Millisecond * 400) | ||
if err = line.SetValue(d.ClosedState); err != nil { | ||
return err | ||
} | ||
time.Sleep(time.Millisecond * 100) | ||
} | ||
|
||
return nil | ||
} |