Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement babel links #456

Merged
merged 2 commits into from
Sep 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import {
} from "plugins/lime-plugin-mesh-wide/src/lib/utils";
import { useSetLinkReferenceState } from "plugins/lime-plugin-mesh-wide/src/meshWideQueries";
import {
BabelLinkErrorCodes,
BaseMacToMacLink,
BatmanLinkErrorCodes,
IBabelLinkData,
IBatManLinkData,
ILinkMtoMErrors,
IWifiLinkData,
Expand Down Expand Up @@ -63,6 +65,37 @@ const BatmanDetail = ({
);
};

const BabelDetail = ({
name,
errorsArray,
node,
}: {
name: string;
errorsArray: BabelLinkErrorCodes[] | undefined;
node: IBabelLinkData;
}) => {
return (
<>
<Row>
<div className={"flex"}>
<strong>{name}</strong>
{errorsArray?.length > 0 && <Warning />}
</div>
</Row>
<Row>
<TitleAndText title={<Trans>Iface</Trans>}>
{node?.iface}
</TitleAndText>
</Row>
<Row>
<TitleAndText title={<Trans>Source IP</Trans>}>
{node?.src_ip}
</TitleAndText>
</Row>
</>
);
};

const WifiDetail = ({
name,
errorsArray,
Expand Down Expand Up @@ -155,21 +188,43 @@ const SelectedLink = ({
{names.map((name, i) => {
const node = linkDetail.linkByName(name);
const errorsArray = errors?.linkErrors[name] ?? [];
return linkType === "wifi_links_info" ? (
<WifiDetail
key={i}
name={name}
errorsArray={errorsArray as WifiLinkErrorCodes[]}
node={node as IWifiLinkData}
/>
) : (
<BatmanDetail
key={i}
name={name}
errorsArray={errorsArray as BatmanLinkErrorCodes[]}
node={node as IBatManLinkData}
/>
);
switch (linkType) {
case "wifi_links_info":
return (
<WifiDetail
key={i}
name={name}
errorsArray={
errorsArray as WifiLinkErrorCodes[]
}
node={node as IWifiLinkData}
/>
);
case "babel_links_info":
return (
<BabelDetail
key={i}
name={name}
errorsArray={
errorsArray as BabelLinkErrorCodes[]
}
node={node as IBabelLinkData}
/>
);
case "bat_links_info":
return (
<BatmanDetail
key={i}
name={name}
errorsArray={
errorsArray as BatmanLinkErrorCodes[]
}
node={node as IBatManLinkData}
/>
);
default:
return <Trans>Unknown link type</Trans>;
}
})}
</>
);
Expand Down
8 changes: 8 additions & 0 deletions plugins/lime-plugin-mesh-wide/src/containers/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
useLocation,
} from "plugins/lime-plugin-locate/src/locateQueries";
import {
BabelLinksLayer,
BatmanLinksLayer,
WifiLinksLayer,
} from "plugins/lime-plugin-mesh-wide/src/containers/MapLayers/LinksLayers";
Expand All @@ -29,12 +30,14 @@ interface ILayersChecked {
nodes?: boolean;
wifiLinks?: boolean;
batmanLinks?: boolean;
babelLinks?: boolean;
}

export const MeshWideMap = ({
nodes = true,
wifiLinks = true,
batmanLinks = false,
babelLinks = false,
}: ILayersChecked) => {
const { data: selectedMapFeature, setData: setSelectedMapFeature } =
useSelectedMapFeature();
Expand Down Expand Up @@ -94,6 +97,11 @@ export const MeshWideMap = ({
layer: <BatmanLinksLayer />,
checked: batmanLinks,
},
babel_links_info: {
name: "Babel",
layer: <BabelLinksLayer />,
checked: babelLinks,
},
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,23 @@ export const BatmanLinksLayer = () => {
</div>
);
};

export const BabelLinksLayer = () => {
const {
locatedNewLinks: newLinks,
locatedLinks,
locatedLinksReference,
linksLoaded,
} = useLocatedLinks({ type: "babel_links_info" });

return (
<div>
<LinksLayer
links={locatedLinks}
linksReference={locatedLinksReference}
linksLoaded={linksLoaded}
newLinks={newLinks}
/>
</div>
);
};
32 changes: 29 additions & 3 deletions plugins/lime-plugin-mesh-wide/src/hooks/useLocatedLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { PontToPointLink } from "plugins/lime-plugin-mesh-wide/src/lib/links/Poi
import { mergeLinksAndCoordinates } from "plugins/lime-plugin-mesh-wide/src/lib/links/getLinksCoordinates";
import { compareLinks } from "plugins/lime-plugin-mesh-wide/src/lib/links/processLinkErrors";
import {
useMeshWideBabel,
useMeshWideBabelReference,
useMeshWideBatman,
useMeshWideBatmanReference,
useMeshWideLinks,
Expand Down Expand Up @@ -41,6 +43,11 @@ export const getQueryByLinkType = <T extends LinkType>(
state: useMeshWideBatman,
reference: useMeshWideBatmanReference,
} as getQueryByLinkTypeReturnType<T>;
case "babel_links_info":
return {
state: useMeshWideBabel,
reference: useMeshWideBabelReference,
} as getQueryByLinkTypeReturnType<T>;
case "wifi_links_info":
default:
return {
Expand Down Expand Up @@ -166,18 +173,25 @@ export const usePointToPointErrors = ({
// Define separate contexts for each type of link
const BatmanLinksContext = createContext<IUselocatedLinks | null>(null);
const MeshWideLinksContext = createContext<IUselocatedLinks | null>(null);
const BabelLinksContext = createContext<IUselocatedLinks | null>(null);

// Export a hook that return the proper context based on the type of link
export const useLocatedLinks = ({ type }: { type: LinkType }) => {
// By default use wifi links
let requestedContext = MeshWideLinksContext;
if (type === "bat_links_info") {
requestedContext = BatmanLinksContext;
switch (type) {
case "bat_links_info":
requestedContext = BatmanLinksContext;
break;
case "babel_links_info":
requestedContext = BabelLinksContext;
break;
}

const context = useContext(requestedContext);
if (context === null) {
throw new Error(
`useLocatedLinks must be used within a provider for ${requestedContext} links`
`useLocatedLinks must be used within a provider for ${type} links`
);
}
return context;
Expand Down Expand Up @@ -206,3 +220,15 @@ export const MeshWideLinksProvider = ({ children }) => {
</MeshWideLinksContext.Provider>
);
};

export const BabelLinksProvider = ({ children }) => {
const babelLinksData = useCalculateLocatedLinks({
type: "babel_links_info",
});

return (
<BabelLinksContext.Provider value={babelLinksData}>
{children}
</BabelLinksContext.Provider>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { QueryKey } from "@tanstack/react-query";
import { sharedStateQueries } from "components/shared-state/SharedStateQueriesKeys";

import {
useMeshWideBabel,
useMeshWideBabelReference,
useMeshWideBatman,
useMeshWideBatmanReference,
useMeshWideLinks,
Expand All @@ -11,6 +13,7 @@ import {
useMeshWideNodesReference,
} from "plugins/lime-plugin-mesh-wide/src/meshWideQueries";
import {
IBabelLinks,
IBatmanLinks,
INodes,
IWifiLinks,
Expand All @@ -37,7 +40,7 @@ export const useMeshWideDataErrors = () => {
const addError = (
queryKey: QueryKey,
error?: unknown,
data?: IWifiLinks | IBatmanLinks | INodes
data?: IWifiLinks | IBatmanLinks | INodes | IBabelLinks
) => {
if (data) {
// Check also node by node if reference state is empty
Expand Down Expand Up @@ -80,6 +83,23 @@ export const useMeshWideDataErrors = () => {
batmanReferenceData
);

const { error: babelError } = useMeshWideBabel({});
addError(
sharedStateQueries.getFromSharedState("babel_links_info"),
babelError
);

const {
data: babelReferenceData,
error: babelReferenceError,
isError: babelReferenceIsError,
} = useMeshWideBabelReference({});
addError(
sharedStateQueries.getFromSharedState("babel_links_info_ref"),
babelReferenceIsError ? babelReferenceError : null,
babelReferenceData
);

const { error: batmanError } = useMeshWideBatman({});
addError(
sharedStateQueries.getFromSharedState("bat_links_info"),
Expand Down
54 changes: 36 additions & 18 deletions plugins/lime-plugin-mesh-wide/src/lib/links/processLinkErrors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { PontToPointLink } from "plugins/lime-plugin-mesh-wide/src/lib/links/PointToPointLink";
import {
BabelLinkErrorCodes,
BatmanLinkErrorCodes,
IBabelLinkData,
IBatManLinkData,
ILinkPtoPErrors,
IWifiLinkData,
LinksErrorCodesTypes,
WifiLinkErrorCodes,
} from "plugins/lime-plugin-mesh-wide/src/meshWideTypes";

Expand Down Expand Up @@ -49,6 +52,17 @@ const compareBatmanData = (
return errors;
};

const compareBabelData = (
reference: IBabelLinkData,
actual: IBabelLinkData
) => {
const errors: BabelLinkErrorCodes[] = [];
if (actual === undefined) {
return [BabelLinkErrorCodes.LINK_DOWN];
}
return errors;
};

/**
* Function that receive 2 PontToPointLink and iterate over every mac to mac link and its data executing a function
* to compare the wifi data.
Expand Down Expand Up @@ -94,16 +108,27 @@ export const compareLinks = ({
Object.entries(macToMacReference.data).forEach(
([nodeNameReference, wifiDataReference]) => {
const wifiDataActual = macToMacActual?.data[nodeNameReference];
const errors =
referenceLink.type === "wifi_links_info"
? compareWifiData(
wifiDataReference as IWifiLinkData,
wifiDataActual as IWifiLinkData
)
: compareBatmanData(
wifiDataReference as IBatManLinkData,
wifiDataActual as IBatManLinkData
);
let errors: LinksErrorCodesTypes[] = [];
switch (referenceLink.type) {
case "wifi_links_info":
errors = compareWifiData(
wifiDataReference as IWifiLinkData,
wifiDataActual as IWifiLinkData
);
break;
case "bat_links_info":
errors = compareBatmanData(
wifiDataReference as IBatManLinkData,
wifiDataActual as IBatManLinkData
);
break;
case "babel_links_info":
errors = compareBabelData(
wifiDataReference as IBabelLinkData,
wifiDataActual as IBabelLinkData
);
break;
}
ptoPErrors.macToMacErrors[macToMacReference.id].linkErrors[
nodeNameReference
] = errors;
Expand All @@ -112,14 +137,7 @@ export const compareLinks = ({
true;
ptoPErrors.hasErrors = true;

if (
(errors as WifiLinkErrorCodes[]).includes(
WifiLinkErrorCodes.LINK_DOWN
) ||
(errors as BatmanLinkErrorCodes[]).includes(
BatmanLinkErrorCodes.LINK_DOWN
)
) {
if (errors.includes(WifiLinkErrorCodes.LINK_DOWN)) {
ptoPErrors.macToMacErrors[macToMacReference.id].linkUp =
false;
downCounter++;
Expand Down
4 changes: 4 additions & 0 deletions plugins/lime-plugin-mesh-wide/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export const dataTypeNameMapping = (dataType: SharedStateDataTypeKeys) => {
return t`Batman Links`;
case "bat_links_info_ref":
return t`Batman Links Reference`;
case "babel_links_info":
return t`Babel Links`;
case "babel_links_info_ref":
return t`Babel Links Reference`;
default:
return dataType;
}
Expand Down
Loading