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 ]
* Initial version
yablochkov-keyboard-reader (0.1.1) release; urgency=low
-- 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 ]
* Group chars before sending
* Docker build bugfixes
* Change serial to model
[ Alexander Terekhin ]
* Initial version
-- 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
Priority: optional
Maintainer: Alexander Terekhin <alex@bearns.me>
Standards-Version: 0.1.2
Package: yablochkov-keyboard-reader
Version: 0.1.1
Architecture: amd64
Installed-Size: 5008848
Description: Yablochkov keyboard reader

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

@ -1,15 +1,17 @@
[Unit]
Description=Yablochkov keyboard reader
Description=Yablochkov keyboard reader %i
[Service]
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=keyboard-reader
SyslogIdentifier=keyboard-reader-%i
WorkingDirectory=/opt/ocppc
EnvironmentFile=/opt/conf/configuration_vars.env
EnvironmentFile=/opt/conf/configuration_vars_model.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
Group=input
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"
"log"
"os"
"strings"
"time"
)
@ -17,9 +16,11 @@ func main() {
vendor := flag.String("vendor", "", "Device vendor")
model := flag.String("model", "", "Device id model")
list := flag.Bool("list", false, "List input devices")
nospace := flag.Bool("nospace", false, "Ignore spaces")
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")
flag.Parse()
if *list {
@ -67,7 +68,7 @@ func main() {
}
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.
for {
@ -77,18 +78,12 @@ func main() {
os.Exit(3)
}
var sb strings.Builder
// Iterate through the events.
for _, event := range events {
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 {
Send(id string)
Send(id rune)
}
type rest struct {
@ -38,38 +38,41 @@ type rest struct {
mutex sync.Mutex
timer *time.Timer
sb strings.Builder
nospace bool
}
func NewRest(connectorId uint, url string, timeout time.Duration) RestInterface {
r := rest{connectorId: connectorId, url: url, timeout: timeout}
func NewRest(connectorId uint, url string, timeout time.Duration, nospace bool) RestInterface {
r := rest{connectorId: connectorId, url: url, timeout: timeout, nospace: nospace}
return &r
}
func (r *rest) Send(id string) {
r.mutex.Lock()
defer r.mutex.Unlock()
func (c *rest) Send(r rune) {
c.mutex.Lock()
defer c.mutex.Unlock()
r.sb.WriteString(id)
if !(c.nospace && r == ' ') {
c.sb.WriteRune(r)
}
if r.timer != nil {
r.timer.Reset(timeout)
if c.timer != nil {
c.timer.Reset(timeout)
} else {
f := func() {
s := r.sb.String()
log.Printf("Send '%s'", s)
go r.send(s)
r.sb.Reset()
s := c.sb.String()
log.Printf("Sent '%s'", s)
go c.send(s)
c.sb.Reset()
}
r.timer = time.AfterFunc(timeout, f)
c.timer = time.AfterFunc(timeout, f)
}
}
func (r *rest) send(id string) {
tag := &MonitorIdTag{IdTag: id, ConnectorId: r.connectorId}
func (c *rest) send(id string) {
tag := &MonitorIdTag{IdTag: id, ConnectorId: c.connectorId}
//if r.connectorId > 0 {
// tag.ConnectorId = &r.connectorId
//if c.connectorId > 0 {
// tag.ConnectorId = &c.connectorId
//}
var b []byte
@ -82,10 +85,10 @@ func (r *rest) send(id string) {
return
}
ctx, _ := context.WithTimeout(context.Background(), r.timeout)
ctx, _ := context.WithTimeout(context.Background(), c.timeout)
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)
return
}

Loading…
Cancel
Save