From 7d1069cf2cead53bf7e14e8d47740a51131bae55 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 29 Sep 2023 13:21:17 +0200 Subject: [PATCH] speed complete --- speed.go | 30 +++++++++++++++++++++--------- tests/speed.py | 6 ++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/speed.go b/speed.go index 4250b2a..93f22fb 100644 --- a/speed.go +++ b/speed.go @@ -167,24 +167,26 @@ type Car struct { func (c *Car) hasTicket(one uint32, two uint32) bool { - for i := one ; i <= two; i += 86400 { - day := uint32(i / 86400) - _, ok := c.ticketDays[day] + start := uint32(float64(one) / 86400) + stop := uint32(float64(two) / 86400) + for i := start; i <= stop; i+=1 { + _, ok := c.ticketDays[i] if ok { - fmt.Printf("ticket on %d for %s\n", day, c.plate) + fmt.Printf("ticket on %d for %s\n", i, c.plate) return true } - fmt.Printf("no ticket on %d for %s\n", day, c.plate) + fmt.Printf("no ticket on %d for %s\n", i, c.plate) } return false } func (c *Car) markTicket(one uint32, two uint32) { - for i := one ; i <= two; i += 86400 { - day := uint32(i / 86400) - fmt.Printf("setting ticket on %d for %s\n", day, c.plate) - c.ticketDays[day] = true + start := uint32(float64(one) / 86400) + stop := uint32(float64(two) / 86400) + for i := start; i <= stop; i+=1 { + fmt.Printf("setting ticket on %d for %s\n", i, c.plate) + c.ticketDays[i] = true } } @@ -366,6 +368,10 @@ func (c *SpeedClient) receiver() { if err != nil { break } } if err != nil { + if err != io.EOF { + m := ErrorMessage{err.Error()} + c.q <- m + } fmt.Printf("Client %d error %s\n", c.clientId, err) } else { fmt.Printf("Client %d closing\n", c.clientId) @@ -394,6 +400,9 @@ func (c *SpeedClient) hdlWantHeartbeat() error { } func (c *SpeedClient) hdlIAmCamera() error { + if c.ctype != 0 { + return fmt.Errorf("wrong state") + } c.ctype = 1 var err error c.camRoad, err = readU16(c.con) @@ -407,6 +416,9 @@ func (c *SpeedClient) hdlIAmCamera() error { } func (c *SpeedClient) hdlIAmDispatch() error { + if c.ctype != 0 { + return fmt.Errorf("wrong state") + } c.ctype = 2 numRoads, err := readU8(c.con) if err != nil { return err } diff --git a/tests/speed.py b/tests/speed.py index 4411518..001c5e5 100644 --- a/tests/speed.py +++ b/tests/speed.py @@ -16,6 +16,11 @@ def heartbeat(s): dur = time() - start print(f"heartbeat {dur:.2f}") + +def doublecam(s): + s.sendall(pack(">BHHH", 0x80,1,0,80)) + s.sendall(pack(">BHHH", 0x80,1,0,80)) + print("Dc", s.recv(1024)) def camera(s): s.sendall(pack(">BHHH", 0x80,1,0,80)) s.sendall(b"\x20\x05snack" +pack(">I", 100000)) @@ -36,6 +41,7 @@ def readTicket(d): heartbeat(sock()) +doublecam(sock()) camera(sock()) camera2(sock()) sleep(1)