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

Refactor middleware hostname in Feed component #6538

Merged
merged 1 commit into from
Nov 3, 2023
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
2 changes: 2 additions & 0 deletions src/Common/hooks/useMSEplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface UseMSEMediaPlayerOption {
export interface ICameraAssetState {
id: string;
accessKey: string;
middleware_address: string;
location_middleware: string;
}

export enum StreamStatus {
Expand Down
12 changes: 7 additions & 5 deletions src/Components/Assets/AssetType/ONVIFCamera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ const ONVIFCamera = ({ assetId, facilityId, asset, onUpdated }: Props) => {
}
}, [facility, facilityId]);

const fallbackMiddleware =
asset?.location_object?.middleware_address || facilityMiddlewareHostname;

const currentMiddleware = middlewareHostname || fallbackMiddleware;

useEffect(() => {
if (asset) {
setAssetType(asset?.asset_class);
Expand Down Expand Up @@ -105,7 +110,7 @@ const ONVIFCamera = ({ assetId, facilityId, asset, onUpdated }: Props) => {
try {
setLoadingAddPreset(true);
const presetData = await axios.get(
`https://${facilityMiddlewareHostname}/status?hostname=${config.hostname}&port=${config.port}&username=${config.username}&password=${config.password}`
`https://${currentMiddleware}/status?hostname=${config.hostname}&port=${config.port}&username=${config.username}&password=${config.password}`
);

const { res } = await request(routes.createAssetBed, {
Expand Down Expand Up @@ -136,9 +141,6 @@ const ONVIFCamera = ({ assetId, facilityId, asset, onUpdated }: Props) => {
};
if (isLoading || loading || !facility) return <Loading />;

const fallbackMiddleware =
asset?.location_object?.middleware_address || facilityMiddlewareHostname;

return (
<div className="space-y-6">
{["DistrictAdmin", "StateAdmin"].includes(authUser.user_type) && (
Expand Down Expand Up @@ -223,7 +225,7 @@ const ONVIFCamera = ({ assetId, facilityId, asset, onUpdated }: Props) => {
addPreset={addPreset}
isLoading={loadingAddPreset}
refreshPresetsHash={refreshPresetsHash}
facilityMiddlewareHostname={facilityMiddlewareHostname}
facilityMiddlewareHostname={currentMiddleware}
/>
) : null}
</div>
Expand Down
27 changes: 21 additions & 6 deletions src/Components/Facility/Consultations/Feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ export const Feed: React.FC<IFeedProps> = ({ consultationId, facilityId }) => {
const [cameraAsset, setCameraAsset] = useState<ICameraAssetState>({
id: "",
accessKey: "",
middleware_address: "",
location_middleware: "",
});
const [cameraMiddlewareHostname, setCameraMiddlewareHostname] = useState("");
const [facilityMiddlewareHostname, setFacilityMiddlewareHostname] =
useState("");
const [cameraConfig, setCameraConfig] = useState<any>({});
const [isLoading, setIsLoading] = useState(true);
const [bedPresets, setBedPresets] = useState<any>([]);
Expand All @@ -66,13 +69,19 @@ export const Feed: React.FC<IFeedProps> = ({ consultationId, facilityId }) => {
const res = await dispatch(getPermittedFacility(facilityId));

if (res.status === 200 && res.data) {
setCameraMiddlewareHostname(res.data.middleware_address);
setFacilityMiddlewareHostname(res.data.middleware_address);
}
};

if (facilityId) fetchFacility();
}, [dispatch, facilityId]);

const fallbackMiddleware =
cameraAsset.location_middleware || facilityMiddlewareHostname;

const currentMiddleware =
cameraAsset.middleware_address || fallbackMiddleware;

useEffect(() => {
if (cameraState) {
setCameraState({
Expand Down Expand Up @@ -130,6 +139,12 @@ export const Feed: React.FC<IFeedProps> = ({ consultationId, facilityId }) => {
setCameraAsset({
id: bedAssets.data.results[0].asset_object.id,
accessKey: config[2] || "",
middleware_address:
bedAssets.data.results[0].asset_object?.meta
?.middleware_hostname,
location_middleware:
bedAssets.data.results[0].asset_object.location_object
?.middleware_address,
});
setCameraConfig(bedAssets.data.results[0].meta);
setCameraState({
Expand Down Expand Up @@ -170,8 +185,8 @@ export const Feed: React.FC<IFeedProps> = ({ consultationId, facilityId }) => {
);

const url = !isIOS
? `wss://${cameraMiddlewareHostname}/stream/${cameraAsset?.accessKey}/channel/0/mse?uuid=${cameraAsset?.accessKey}&channel=0`
: `https://${cameraMiddlewareHostname}/stream/${cameraAsset?.accessKey}/channel/0/hls/live/index.m3u8?uuid=${cameraAsset?.accessKey}&channel=0`;
? `wss://${currentMiddleware}/stream/${cameraAsset?.accessKey}/channel/0/mse?uuid=${cameraAsset?.accessKey}&channel=0`
: `https://${currentMiddleware}/stream/${cameraAsset?.accessKey}/channel/0/hls/live/index.m3u8?uuid=${cameraAsset?.accessKey}&channel=0`;

const {
startStream,
Expand All @@ -182,7 +197,7 @@ export const Feed: React.FC<IFeedProps> = ({ consultationId, facilityId }) => {
: // eslint-disable-next-line react-hooks/rules-of-hooks
useMSEMediaPlayer({
config: {
middlewareHostname: cameraMiddlewareHostname,
middlewareHostname: currentMiddleware,
...cameraAsset,
},
url,
Expand Down Expand Up @@ -229,7 +244,7 @@ export const Feed: React.FC<IFeedProps> = ({ consultationId, facilityId }) => {
});
getBedPresets(cameraAsset);
}
}, [cameraAsset, cameraMiddlewareHostname]);
}, [cameraAsset, currentMiddleware]);

useEffect(() => {
let tId: any;
Expand Down
Loading