rework esp code en update py code

This commit is contained in:
MReenen 2023-11-16 21:11:55 +01:00
parent 5e151e5bc2
commit b03ec14c0f
2 changed files with 55 additions and 107 deletions

View File

@ -3,15 +3,24 @@
import asyncio
from websockets.server import serve
pin = "1234"
boats = ["999", "999", "999", "999"]
adminId = "1234"
BOAT_STATE_INCTRL = 1
BOAT_STATE_AVAILABLE = 0
BOAT_STATE_LOCKED = -1
boats = [
{ "id": 0, "name": "test boat", "state": BOAT_STATE_AVAILABLE }
]
clients = []
def onCMD(data):
global boats, pin
global boats, adminId
print("command:" + str(data))
if (len(data) != 4):
return
if (data[1] != pin):
if (data[1] != adminId):
return
boot = int(data[2])
if (boot >= 0 and boot <= 3):
@ -33,10 +42,19 @@ def onMessage(message):
sendData(data)
break
def newClient(message, ws):
print("new client connected: " + message)
return None
async def run(ws, path):
client = {}
async for message in ws:
onMessage(message)
client = newClient(message, ws)
if (client is None):
return
async for message in ws:
onMessage(message, client)
async def main():
async with serve(run, "0.0.0.0", 8080):

View File

@ -8,100 +8,13 @@
WiFiUDP UDP;
#define RX_BUFFER_SIZE 255
char rxBuffer[RX_BUFFER_SIZE];
uint8_t rxBuffer_writePointer = 0;
char lineBuffer[RX_BUFFER_SIZE];
uint8_t rxBuffer_readPointer = 0;
uint8_t rxBuffer_lineStart = 0;
uint8_t rxBuffer_lineEnd = 0;
bool unProcessedLineAvailable = false;
class FlipFlop_Buffer
{
private:
void *_buffer_flip;
void *_buffer_flop;
size_t _size;
void *_active_buffer;
void *_active_buffer_end;
void *_write_head;
public:
Buffer(size_t size)
{
this->_size = size;
this->_buffer_flip = maloc(size);
this->_buffer_flop = maloc(size);
this->_active_buffer = this->_buffer_flip;
this->_active_buffer_end = this->_buffer_flip + this->_size;
}
~Buffer()
{
free(this->_buffer_flip);
free(this->_buffer_flop);
}
bool put(void *data, size_t len)
{
bool sucsess;
if (this->_write_head + len > this->_active_buffer_end)
{
memcpy(this->_write_head, data, this->_active_buffer_end - this->_write_head);
this->_write_head = this._active_buffer_end;
sucsess = false;
}
else
{
memcpy(this->write_p, data, len);
this->_write_head += len;
sucsess = true;
}
return sucsess;
}
void swich_buffer()
{
if (this->_active_buffer == this->_buffer_flip)
{
this->_active_buffer = this->_buffer_flop;
}
else
{
this->_active_buffer == this->_buffer_flip;
}
this->_active_buffer_end = this._active_buffer + this._size;
return;
}
}
class LineFlip_Buffer : FlipFlop_Buffer
{
public:
bool overrite put(void *data, size_t len)
{
bool justSwiched = false;
bool sucsess = true;
for (int i = 0; i < len; i++)
{
if (&(data + i) == '\n' || &(data + i) == '\r')
{
if (!justSwiched)
{
this->swich_buffer();
justSwiched = true;
}
}
else
{
if(!FlipFlop_Buffer::put(data + i, 1))
{
sucsess = false;
}
justSwiched = false;
}
}
return sucsess
}
}
bool rxBuffer_overflow = false;
void setup() {
WiFi.begin(WIFI_SSID, WIFI_PASS);
@ -114,18 +27,35 @@ void setup() {
UDP.begin(UDP_PORT);
}
void processLine()
{
rxBuffer[rxBuffer_lineEnd] = 0;
// what are the commands
}
int readNextLine()
{
while (rxBuffer_readPointer < RX_BUFFER_SIZE)
{
if (rxBuffer[rxBuffer_readPointer] == '\n')
{
rxBuffer_lineEnd = rxBuffer_readPointer;
processLine();
}
rxBuffer_readPointer++;
}
}
void loop() {
int packetSize = UDP.parsePacket();
if (packetSize) {
int len = UDP.read(&rxBuffer[rxBuffer_writePointer], RX_BUFFER_SIZE-rxBuffer_writePointer);
if (rxBuffer_writePointer >= RX_BUFFER_SIZE)
{
rxBuffer_writePointer -= RX_BUFFER_SIZE;
}
}
int len = UDP.read(&rxBuffer[0], RX_BUFFER_SIZE);
if (rxBuffer_writePointer != rxBuffer_readPointer)
{
rxBuffer_readPointer = 0;
rxBuffer_lineStart = 0;
rxBuffer_lineEnd = 0;
readNextLine();
}
}