fix: YAB-2152: Spaces filter, new initialization via udev

feat/YAB-2152
Terekhin Alexandr 1 year ago
parent b5367338d2
commit 36d5996318
Signed by: didinst
GPG Key ID: D2EF94423C23BF12
  1. 28
      debian/debian/changelog
  2. 3
      debian/debian/control
  3. 2
      debian/debian/postinst
  4. 8
      debian/etc/systemd/system/keyboard-reader@.service
  5. 5
      debian/etc/udev/rules.d/81-kbd-reader.rules
  6. 15
      keyboard-reader.go
  7. 43
      rest.go

@ -1,15 +1,23 @@
yablochkov-keyboard-reader (0.1.0) release; urgency=low yablochkov-keyboard-reader (0.1.2) release; urgency=low
[ Alexander Terekhin ]
* Ignore space option
* Add udev rules
-- Alexander Terekhin <alex@bearns> Sun, 20 Feb 2024 13:03:00 +0300
[ Alexander Terekhin ] yablochkov-keyboard-reader (0.1.1) release; urgency=low
* Initial version
-- Alexander Terekhin <alex@bearns> Sun, 03 Dec 2023 17:01:00 +0600 [ Alexander Terekhin ]
* Group chars before sending
* Docker build bugfixes
* Change serial to model
yablochkov-keyboard-reader (0.1.1) release; urgency=low -- Alexander Terekhin <alex@bearns> Sun, 04 Dec 2023 12:35:00 +0300
yablochkov-keyboard-reader (0.1.0) release; urgency=low
[ Alexander Terekhin ] [ Alexander Terekhin ]
* Group chars before sending * Initial version
* Docker build bugfixes
* Change serial to model
-- Alexander Terekhin <alex@bearns> Sun, 04 Dec 2023 12:35:00 +0300 -- Alexander Terekhin <alex@bearns> Sun, 03 Dec 2023 17:01:00 +0600

@ -2,10 +2,9 @@ Source: yablochkov-keyboard-reader
Section: misc Section: misc
Priority: optional Priority: optional
Maintainer: Alexander Terekhin <alex@bearns.me> Maintainer: Alexander Terekhin <alex@bearns.me>
Standards-Version: 0.1.2
Package: yablochkov-keyboard-reader Package: yablochkov-keyboard-reader
Version: 0.1.1
Architecture: amd64 Architecture: amd64
Installed-Size: 5008848
Description: Yablochkov keyboard reader Description: Yablochkov keyboard reader

@ -1,2 +1,2 @@
chmod +x /opt/ocppc/bin/keyboard-reader chmod +x /opt/ocppc/bin/keyboard-reader
systemctl enable keyboard-reader --now udevadm control --reload

@ -1,15 +1,17 @@
[Unit] [Unit]
Description=Yablochkov keyboard reader Description=Yablochkov keyboard reader %i
[Service] [Service]
StandardOutput=syslog StandardOutput=syslog
StandardError=syslog StandardError=syslog
SyslogIdentifier=keyboard-reader SyslogIdentifier=keyboard-reader-%i
WorkingDirectory=/opt/ocppc WorkingDirectory=/opt/ocppc
EnvironmentFile=/opt/conf/configuration_vars.env EnvironmentFile=/opt/conf/configuration_vars.env
EnvironmentFile=/opt/conf/configuration_vars_model.env EnvironmentFile=/opt/conf/configuration_vars_model.env
EnvironmentFile=/opt/conf/configuration_vars_personal.env EnvironmentFile=/opt/conf/configuration_vars_personal.env
ExecStart=/opt/ocppc/bin/keyboard-reader --model=TWN4_B1.64_NKF4.64_STD2.04 ExecStart=/opt/ocppc/bin/keyboard-reader --nospace --dev=/dev/input/event%i
Restart=on-failure
RestartSec=5s
User=debian User=debian
Group=input Group=input
ExecStop=/bin/kill -TERM $MAINPID ExecStop=/bin/kill -TERM $MAINPID

@ -0,0 +1,5 @@
ACTION=="add", SUBSYSTEM=="input", ENV{ID_TYPE}=="hid", ENV{ID_MODEL}=="ACR1281_Dual_Reader", RUN+="/usr/bin/systemctl start keyboard-reader@%n"
ACTION=="add", SUBSYSTEM=="input", ENV{ID_TYPE}=="hid", ENV{ID_MODEL}=="TWN4_B1.64_NKF4.64_STD2.04", RUN+="/usr/bin/systemctl start keyboard-reader@%n"
ACTION=="remove", SUBSYSTEM=="input", ENV{ID_TYPE}=="hid", ENV{ID_MODEL}=="ACR1281_Dual_Reader", RUN+="/usr/bin/systemctl stop keyboard-reader@%n"
ACTION=="remove", SUBSYSTEM=="input", ENV{ID_TYPE}=="hid", ENV{ID_MODEL}=="TWN4_B1.64_NKF4.64_STD2.04", RUN+="/usr/bin/systemctl stop keyboard-reader@%n"

@ -6,7 +6,6 @@ import (
"keyboard-reader/evdev" "keyboard-reader/evdev"
"log" "log"
"os" "os"
"strings"
"time" "time"
) )
@ -17,9 +16,11 @@ func main() {
vendor := flag.String("vendor", "", "Device vendor") vendor := flag.String("vendor", "", "Device vendor")
model := flag.String("model", "", "Device id model") model := flag.String("model", "", "Device id model")
list := flag.Bool("list", false, "List input devices") list := flag.Bool("list", false, "List input devices")
nospace := flag.Bool("nospace", false, "Ignore spaces")
connectorId := flag.Uint("connector", 0, "Connector id") connectorId := flag.Uint("connector", 0, "Connector id")
timeout := flag.Int("timeout", 5, "Http timeout, sec") wait := flag.Int("timeout", 5, "Http timeout, sec")
url := flag.String("url", "http://localhost:8914/state/idTag", "Endpoint url") url := flag.String("url", "http://localhost:8914/state/idTag", "Endpoint url")
flag.Parse() flag.Parse()
if *list { if *list {
@ -67,7 +68,7 @@ func main() {
} }
defer dev.Release() defer dev.Release()
client := NewRest(*connectorId, *url, time.Duration(*timeout)*time.Second) client := NewRest(*connectorId, *url, time.Duration(*wait)*time.Second, *nospace)
// Run an infinite loop. // Run an infinite loop.
for { for {
@ -77,18 +78,12 @@ func main() {
os.Exit(3) os.Exit(3)
} }
var sb strings.Builder
// Iterate through the events. // Iterate through the events.
for _, event := range events { for _, event := range events {
if r, ok := event.GetKey(); ok { if r, ok := event.GetKey(); ok {
sb.WriteRune(r) client.Send(r)
} }
} }
if sb.Len() > 0 {
client.Send(sb.String())
}
} }
} }

@ -28,7 +28,7 @@ type ResponseEntity struct {
} }
type RestInterface interface { type RestInterface interface {
Send(id string) Send(id rune)
} }
type rest struct { type rest struct {
@ -38,38 +38,41 @@ type rest struct {
mutex sync.Mutex mutex sync.Mutex
timer *time.Timer timer *time.Timer
sb strings.Builder sb strings.Builder
nospace bool
} }
func NewRest(connectorId uint, url string, timeout time.Duration) RestInterface { func NewRest(connectorId uint, url string, timeout time.Duration, nospace bool) RestInterface {
r := rest{connectorId: connectorId, url: url, timeout: timeout} r := rest{connectorId: connectorId, url: url, timeout: timeout, nospace: nospace}
return &r return &r
} }
func (r *rest) Send(id string) { func (c *rest) Send(r rune) {
r.mutex.Lock() c.mutex.Lock()
defer r.mutex.Unlock() defer c.mutex.Unlock()
r.sb.WriteString(id) if !(c.nospace && r == ' ') {
c.sb.WriteRune(r)
}
if r.timer != nil { if c.timer != nil {
r.timer.Reset(timeout) c.timer.Reset(timeout)
} else { } else {
f := func() { f := func() {
s := r.sb.String() s := c.sb.String()
log.Printf("Send '%s'", s) log.Printf("Sent '%s'", s)
go r.send(s) go c.send(s)
r.sb.Reset() c.sb.Reset()
} }
r.timer = time.AfterFunc(timeout, f) c.timer = time.AfterFunc(timeout, f)
} }
} }
func (r *rest) send(id string) { func (c *rest) send(id string) {
tag := &MonitorIdTag{IdTag: id, ConnectorId: r.connectorId} tag := &MonitorIdTag{IdTag: id, ConnectorId: c.connectorId}
//if r.connectorId > 0 { //if c.connectorId > 0 {
// tag.ConnectorId = &r.connectorId // tag.ConnectorId = &c.connectorId
//} //}
var b []byte var b []byte
@ -82,10 +85,10 @@ func (r *rest) send(id string) {
return return
} }
ctx, _ := context.WithTimeout(context.Background(), r.timeout) ctx, _ := context.WithTimeout(context.Background(), c.timeout)
byteReader := bytes.NewReader(b) byteReader := bytes.NewReader(b)
if request, err = http.NewRequestWithContext(ctx, "POST", r.url, byteReader); err != nil { if request, err = http.NewRequestWithContext(ctx, "POST", c.url, byteReader); err != nil {
log.Printf("Can't build new request: %e\n", err) log.Printf("Can't build new request: %e\n", err)
return return
} }

Loading…
Cancel
Save