HL7-1: rename userId to stationId
This commit is contained in:
parent
3e00cf88e7
commit
bb3a6a5d8e
3 changed files with 32 additions and 34 deletions
|
@ -19,7 +19,7 @@
|
||||||
|
|
||||||
// connection state
|
// connection state
|
||||||
let ws = $state<WebSocket | undefined>(undefined);
|
let ws = $state<WebSocket | undefined>(undefined);
|
||||||
let userId = $state<string | undefined>(undefined);
|
let stationId = $state<string | undefined>(undefined); // stationId assigned by server
|
||||||
let connectionState = $state<ConnectionState>(ConnectionState.disconnected);
|
let connectionState = $state<ConnectionState>(ConnectionState.disconnected);
|
||||||
|
|
||||||
// client state
|
// client state
|
||||||
|
@ -80,7 +80,7 @@
|
||||||
|
|
||||||
// initial message from server assigning ID
|
// initial message from server assigning ID
|
||||||
case MessageType.assign_id:
|
case MessageType.assign_id:
|
||||||
userId = message.payload.userId;
|
stationId = message.payload.stationId;
|
||||||
composedMessage = segmentTemplates.MSH.template();
|
composedMessage = segmentTemplates.MSH.template();
|
||||||
connectionState = ConnectionState.connected;
|
connectionState = ConnectionState.connected;
|
||||||
break;
|
break;
|
||||||
|
@ -128,7 +128,7 @@
|
||||||
|
|
||||||
function handleSendMessage(message: string) {
|
function handleSendMessage(message: string) {
|
||||||
|
|
||||||
if (!ws || ws.readyState !== WebSocket.OPEN || isSending || !userId) {
|
if (!ws || ws.readyState !== WebSocket.OPEN || isSending || !stationId) {
|
||||||
console.log('Socket not ready');
|
console.log('Socket not ready');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -150,9 +150,9 @@
|
||||||
isSending = false;
|
isSending = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function copyUserId() {
|
function copyStationId() {
|
||||||
if (userId) {
|
if (stationId) {
|
||||||
navigator.clipboard.writeText(userId).then(() => {
|
navigator.clipboard.writeText(stationId).then(() => {
|
||||||
copySuccess = true;
|
copySuccess = true;
|
||||||
setTimeout(() => copySuccess = false, 2000);
|
setTimeout(() => copySuccess = false, 2000);
|
||||||
});
|
});
|
||||||
|
@ -174,7 +174,7 @@
|
||||||
<div class="max-w-7xl mx-auto">
|
<div class="max-w-7xl mx-auto">
|
||||||
<main class="grid grid-cols-1 lg:grid-cols-3 gap-6">
|
<main class="grid grid-cols-1 lg:grid-cols-3 gap-6">
|
||||||
|
|
||||||
<!-- Left Column: User Info & Logs -->
|
<!-- Left Column: Station Info & Logs -->
|
||||||
<div class="space-y-6">
|
<div class="space-y-6">
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader class="flex flex-row items-center">
|
<CardHeader class="flex flex-row items-center">
|
||||||
|
@ -182,11 +182,11 @@
|
||||||
Your Info
|
Your Info
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
{#if connectionState === ConnectionState.connected && userId}
|
{#if connectionState === ConnectionState.connected && stationId}
|
||||||
<Label class="">Station ID</Label>
|
<Label class="">Station ID</Label>
|
||||||
<div class="flex items-center space-x-2">
|
<div class="flex items-center space-x-2">
|
||||||
<Input disabled bind:value={userId}/>
|
<Input disabled bind:value={stationId}/>
|
||||||
<Button variant="outline" size="icon" onclick={copyUserId}>
|
<Button variant="outline" size="icon" onclick={copyStationId}>
|
||||||
{#if copySuccess}
|
{#if copySuccess}
|
||||||
<CheckIcon class={'text-green-400'}/>
|
<CheckIcon class={'text-green-400'}/>
|
||||||
{:else}
|
{:else}
|
||||||
|
@ -232,7 +232,7 @@
|
||||||
<span>HL7v2 Message Editor</span>
|
<span>HL7v2 Message Editor</span>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
{#if connectionState === ConnectionState.connected && userId}
|
{#if connectionState === ConnectionState.connected && stationId}
|
||||||
<div>
|
<div>
|
||||||
<div class="flex flex-wrap gap-2 mb-4">
|
<div class="flex flex-wrap gap-2 mb-4">
|
||||||
{#each segmentTypes as type (type)}
|
{#each segmentTypes as type (type)}
|
||||||
|
|
|
@ -49,35 +49,33 @@ wss.on('connection', (ws) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get ID from pool and assign to user
|
// get ID from pool and assign to station
|
||||||
const userId = availableIds.pop();
|
const stationId = availableIds.pop();
|
||||||
if (!userId) {
|
if (!stationId) {
|
||||||
console.log('Connection rejected: Failed to retrieve ID.');
|
console.log('Connection rejected: Failed to retrieve ID.');
|
||||||
ws.close(1013, 'Server is full. Please try again later.'); // 1013: Try again later
|
ws.close(1013, 'Server is full. Please try again later.'); // 1013: Try again later
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// store client in map
|
// store client in map
|
||||||
clients.set(userId, ws);
|
clients.set(stationId, ws);
|
||||||
|
|
||||||
setTimeout(() => {
|
// send station ID to client
|
||||||
// send user ID to client
|
const welcomeMessage = {
|
||||||
const welcomeMessage = {
|
type: 'assign_id',
|
||||||
type: 'assign_id',
|
payload: {
|
||||||
payload: {
|
stationId: stationId,
|
||||||
userId: userId,
|
},
|
||||||
},
|
} as Message;
|
||||||
} as Message;
|
ws.send(JSON.stringify(welcomeMessage));
|
||||||
ws.send(JSON.stringify(welcomeMessage));
|
|
||||||
|
|
||||||
console.log(`Client connected. Assigning ID: ${userId}`);
|
console.log(`Client connected. Assigning ID: ${stationId}`);
|
||||||
}, 0);
|
|
||||||
|
|
||||||
ws.on('message', (message) => {
|
ws.on('message', (message) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const parsedMessage: Message = JSON.parse(message.toString());
|
const parsedMessage: Message = JSON.parse(message.toString());
|
||||||
console.log(`Received message from ${userId}:`, parsedMessage);
|
console.log(`Received message from ${stationId}:`, parsedMessage);
|
||||||
|
|
||||||
// We only expect one type of message from clients: 'send_hl7v2'
|
// We only expect one type of message from clients: 'send_hl7v2'
|
||||||
if (parsedMessage.type === MessageType.send_hl7v2) {
|
if (parsedMessage.type === MessageType.send_hl7v2) {
|
||||||
|
@ -127,22 +125,22 @@ wss.on('connection', (ws) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Failed to process message from ${userId}:`, error);
|
console.error(`Failed to process message from ${stationId}:`, error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// listen to disconnects to free IDs
|
// listen to disconnects to free IDs
|
||||||
ws.on('close', () => {
|
ws.on('close', () => {
|
||||||
console.log(`Client ${userId} disconnected.`);
|
console.log(`Client ${stationId} disconnected.`);
|
||||||
clients.delete(userId);
|
clients.delete(stationId);
|
||||||
if (userId) {
|
if (stationId) {
|
||||||
availableIds.push(userId);
|
availableIds.push(stationId);
|
||||||
}
|
}
|
||||||
availableIds = shake(availableIds);
|
availableIds = shake(availableIds);
|
||||||
});
|
});
|
||||||
|
|
||||||
ws.on('error', (error) => {
|
ws.on('error', (error) => {
|
||||||
console.error(`WebSocket error for client ${userId}:`, error);
|
console.error(`WebSocket error for client ${stationId}:`, error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ export enum MessageType {
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Message =
|
export type Message =
|
||||||
| { type: MessageType.assign_id, payload: { userId: string }}
|
| { type: MessageType.assign_id, payload: { stationId: string }}
|
||||||
| { type: MessageType.send_hl7v2, payload: { message: string }}
|
| { type: MessageType.send_hl7v2, payload: { message: string }}
|
||||||
| { type: MessageType.receive_hl7v2, payload: { message: string, timestamp: string }}
|
| { type: MessageType.receive_hl7v2, payload: { message: string, timestamp: string }}
|
||||||
| { type: MessageType.delivery_error, payload: { error: string }}
|
| { type: MessageType.delivery_error, payload: { error: string }}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue