Initial commit: Next.js UWB positioning webapp
- Complete Next.js 14 app with TypeScript and Tailwind CSS - ESP32 serial communication via API routes - Real-time UWB positioning visualization - Interactive 2D warehouse mapping with Canvas - Device connection interface with auto-detection - AT command parsing for UWBHelper library integration - Clean project structure with comprehensive documentation
This commit is contained in:
commit
fa75faa69d
20 changed files with 7600 additions and 0 deletions
91
pages/api/serial/connect.ts
Normal file
91
pages/api/serial/connect.ts
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { SerialPort } from 'serialport'
|
||||
import { ReadlineParser } from '@serialport/parser-readline'
|
||||
|
||||
// Global connection state
|
||||
let serialConnection: SerialPort | null = null
|
||||
let parser: ReadlineParser | null = null
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse
|
||||
) {
|
||||
if (req.method === 'POST') {
|
||||
const { port, baudRate = 115200 } = req.body
|
||||
|
||||
if (!port) {
|
||||
return res.status(400).json({ message: 'Port is required' })
|
||||
}
|
||||
|
||||
try {
|
||||
// Close existing connection
|
||||
if (serialConnection?.isOpen) {
|
||||
serialConnection.close()
|
||||
}
|
||||
|
||||
// Create new connection
|
||||
serialConnection = new SerialPort({
|
||||
path: port,
|
||||
baudRate: baudRate,
|
||||
autoOpen: false
|
||||
})
|
||||
|
||||
// Set up parser for line-based data
|
||||
parser = serialConnection.pipe(new ReadlineParser({ delimiter: '\r\n' }))
|
||||
|
||||
// Open connection
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
serialConnection!.open((err) => {
|
||||
if (err) reject(err)
|
||||
else resolve()
|
||||
})
|
||||
})
|
||||
|
||||
res.status(200).json({
|
||||
message: 'Connected successfully',
|
||||
port,
|
||||
baudRate,
|
||||
isOpen: serialConnection.isOpen
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('Serial connection error:', error)
|
||||
res.status(500).json({
|
||||
message: 'Failed to connect',
|
||||
error: error instanceof Error ? error.message : 'Unknown error'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
else if (req.method === 'DELETE') {
|
||||
// Disconnect
|
||||
try {
|
||||
if (serialConnection?.isOpen) {
|
||||
serialConnection.close()
|
||||
serialConnection = null
|
||||
parser = null
|
||||
}
|
||||
|
||||
res.status(200).json({ message: 'Disconnected successfully' })
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
message: 'Failed to disconnect',
|
||||
error: error instanceof Error ? error.message : 'Unknown error'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
else if (req.method === 'GET') {
|
||||
// Get connection status
|
||||
res.status(200).json({
|
||||
connected: serialConnection?.isOpen || false,
|
||||
port: serialConnection?.path || null
|
||||
})
|
||||
}
|
||||
|
||||
else {
|
||||
res.status(405).json({ message: 'Method not allowed' })
|
||||
}
|
||||
}
|
||||
|
||||
export { serialConnection, parser }
|
||||
Loading…
Add table
Add a link
Reference in a new issue