Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/cmd/setup_vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func configureVehicle(
vehicle.SetLogger(loggerHandler)
vehicle.SetUpdateFactory(updateFactory)
vehicle.SetIpToBoardId(ipToBoardID)
vehicle.SetBoardAddresses(adj.Info.Addresses)
vehicle.SetIdToBoardName(idToBoard)
vehicle.SetTransport(transp)

Expand Down
13 changes: 12 additions & 1 deletion backend/pkg/broker/topics/message/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,21 @@ func (update *Update) SetAPI(api abstraction.BrokerAPI) {
type pushStruct struct {
data any
boardName string
category string
}

func Push(data any, boardName string) *pushStruct {
return &pushStruct{data: data, boardName: boardName}
}

func PushFromBoard(packet *protection.Packet, boardName string) *pushStruct {
category := "protection"
if packet.Kind == protection.WarningKind {
category = "message"
}
return &pushStruct{data: packet, boardName: boardName, category: category}
}

func (push *pushStruct) Topic() abstraction.BrokerTopic {
return UpdateName
}
Expand All @@ -117,7 +126,8 @@ func (push *pushStruct) Data(boardName string) wrapper {
switch data := push.data.(type) {
case *protection.Packet:
return wrapper{
Kind: string(data.Severity()),
Category: push.category,
Kind: string(data.Severity()),
Payload: struct {
Kind string `json:"kind"`
Data any `json:"data"`
Expand All @@ -143,6 +153,7 @@ func (push *pushStruct) Data(boardName string) wrapper {
}

type wrapper struct {
Category string `json:"category,omitempty"`
Kind string `json:"kind"`
Payload any `json:"payload"`
Board string `json:"board"`
Expand Down
7 changes: 7 additions & 0 deletions backend/pkg/vehicle/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,10 @@ func (vehicle *Vehicle) SetIpToBoardId(ipToBoardId map[string]abstraction.BoardI
vehicle.ipToBoardId = ipToBoardId
vehicle.trace.Info().Msg("set ip to board id")
}

func (vehicle *Vehicle) SetBoardAddresses(addresses map[string]string) {
vehicle.ipToBoardName = make(map[string]string, len(addresses))
for name, ip := range addresses {
vehicle.ipToBoardName[ip] = name
}
}
17 changes: 14 additions & 3 deletions backend/pkg/vehicle/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vehicle
import (
"errors"
"fmt"
"net"
"strings"

"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
Expand Down Expand Up @@ -82,8 +83,18 @@ func (vehicle *Vehicle) handlePacketNotification(notification transport.PacketNo
}

case *protection.Packet:
boardID := vehicle.ipToBoardId[strings.Split(notification.From, ":")[0]]
err := vehicle.broker.Push(message_topic.Push(p, vehicle.idToBoardName[p.Id()]))
ip, _, err := net.SplitHostPort(notification.From)
if err != nil {
ip = notification.From
}
boardName := vehicle.ipToBoardName[ip]
if boardName == "" {
boardName = vehicle.idToBoardName[p.Id()]
}
if boardName == "" {
boardName = ip
}
err = vehicle.broker.Push(message_topic.PushFromBoard(p, boardName))
if err != nil {
vehicle.trace.Error().Stack().Err(err).Msg("broker push")
return errors.Join(fmt.Errorf("update protection to frontend (%s protection with id %d and kind %d from %s to %s)", p.Severity(), p.Id(), p.Kind, notification.From, notification.To), err)
Expand All @@ -92,7 +103,7 @@ func (vehicle *Vehicle) handlePacketNotification(notification transport.PacketNo
// Log protection
err = vehicle.logger.PushRecord(&protection_logger.Record{
Packet: p,
BoardId: boardID,
BoardId: vehicle.ipToBoardId[ip],
From: notification.From,
To: notification.To,
Timestamp: notification.Timestamp,
Expand Down
1 change: 1 addition & 0 deletions backend/pkg/vehicle/vehicle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Vehicle struct {
updateFactory *update_factory.UpdateFactory
idToBoardName map[abstraction.PacketId]string
ipToBoardId map[string]abstraction.BoardId
ipToBoardName map[string]string
BlcuId abstraction.BoardId

trace zerolog.Logger
Expand Down
10 changes: 10 additions & 0 deletions frontend/testing-view/src/constants/messageKindColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@ export const MESSAGE_KIND_COLORS: Record<Message["kind"], string> = {
info: "border-blue-500 hover:bg-blue-500/5",
warning: "border-yellow-500 hover:bg-yellow-500/5",
fault: "border-red-500 hover:bg-red-500/5",
panic: "border-purple-500 hover:bg-purple-500/5",
ok: "border-green-500 hover:bg-green-500/5",
};

/** Colors for the informative badges in the messages section. */
export const MESSAGE_KIND_BADGES: Record<Message["kind"], string> = {
info: "bg-blue-500 text-white",
warning: "bg-yellow-500 text-black",
fault: "bg-red-500 text-white",
panic: "bg-purple-500 text-white",
ok: "bg-green-500 text-white",
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
} from "@hyperloopupv/frontend-kit";
import { ChevronDown } from "@hyperloopupv/frontend-kit/icons";
import { cn } from "@hyperloopupv/frontend-kit/lib";
import { Rows3, Shield, type LucideIcon } from "lucide-react";
import { useState } from "react";
import { MESSAGE_KIND_COLORS } from "../../../../../constants/messageKindColors";
import { MESSAGE_KIND_BADGES, MESSAGE_KIND_COLORS } from "../../../../../constants/messageKindColors";
import { formatTimestamp } from "../../../../../lib/utils";
import type { Message } from "../../../../../types/data/message";

Expand All @@ -18,6 +19,16 @@ interface MessageItemProps {
export const MessageItem = ({ message }: MessageItemProps) => {
const [isOpen, setIsOpen] = useState(false);

let label = "Backend";
let Icon: LucideIcon | undefined = Rows3;
if (message.category === "protection") {
label = `${message.board}`;
Icon = Shield;
} else if (message.category === "message") {
label = `${message.board}`;
Icon = undefined;
}

const renderMessageContent = (payload: any) => {
// 1. Simple string (Info messages)
if (typeof payload === "string") return payload;
Expand Down Expand Up @@ -55,17 +66,18 @@ export const MessageItem = ({ message }: MessageItemProps) => {
)}
>
<CollapsibleTrigger className="flex w-full flex-col items-start">
<div className="flex w-full items-center gap-2 px-3 py-2.5 text-left">
<div className="flex w-full items-start gap-2 px-3 py-2.5 text-left">
<span className="text-muted-foreground shrink-0 font-mono text-sm">
{formatTimestamp(message.timestamp)}
</span>

<Badge
variant="destructive"
className="h-4.5 shrink-0 px-1.5 text-[11px] uppercase"
>
{message.board}
</Badge>
<span className="text-muted-foreground shrink-0 font-mono text-sm">
<Badge
// variant="default"
className={cn("h-4.5 shrink-0 px-1.5 text-[11px]", MESSAGE_KIND_BADGES[message.kind])}
>
{label}
</Badge>
</span>

<span className="flex-1 truncate text-sm font-medium">
{message.name}
Expand All @@ -79,23 +91,39 @@ export const MessageItem = ({ message }: MessageItemProps) => {
/>
</div>

<CollapsibleContent className="w-full cursor-auto select-text px-3 pb-2 pt-0 text-xs">
<div onClick={(e) => e.stopPropagation()}>
<div className="flex w-full items-end justify-between gap-1">
{/* Block (not flex) so the floated icon sits under the time and the
expanded text flows to its right, then wraps underneath it. */}
<div
className={cn(
"flow-root w-full px-3 text-left",
(Icon || isOpen) && "pb-2",
)}
>
{Icon && (
// Same font as the timestamp, so 8ch = width of "HH:MM:SS"
<span className="float-left mb-1 mr-2 flex w-[8ch] justify-center font-mono text-sm">
<Icon
aria-hidden="true"
className="text-muted-foreground h-6 w-6"
/>
</span>
)}
<CollapsibleContent className="cursor-auto select-text text-xs">
<div onClick={(e) => e.stopPropagation()}>
{/* If it's a detailed message, showing the sub-kind can be helpful */}
{message.payload &&
typeof message.payload === "object" &&
message.payload.kind && (
<span className="text-muted-foreground text-[10px] font-bold uppercase opacity-70">
<span className="text-muted-foreground block text-[10px] font-bold uppercase opacity-70">
{message.payload.kind.replace("_", " ")}
</span>
)}
<p className="whitespace-pre-wrap font-medium leading-relaxed">
{renderMessageContent(message.payload)}
</p>
</div>
</div>
</CollapsibleContent>
</CollapsibleContent>
</div>
</CollapsibleTrigger>
</Collapsible>
);
Expand Down
4 changes: 3 additions & 1 deletion frontend/testing-view/src/types/data/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ export type DetailedPayload =
* Definition of a MessagePacket as it arrives from the backend.
*/
export type MessagePacket = {
category?: "protection" | "message" | "backend";
/** Message type */
kind: "info" | "warning" | "fault" | "ok";
kind: "info" | "warning" | "fault" | "ok" | "panic";
/** For `info` messages, the payload is a string.
* For `warning`, `fault` and `ok` messages, the payload is a DetailedPayload.
* For `panic` it's to be defined.
*/
payload: string | DetailedPayload;
board: BoardName;
Expand Down
Loading