-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15e1e22
commit 8bb911b
Showing
25 changed files
with
634 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export enum WebSocketEvent { | ||
CHECK_DEVICE_CONNECTION = 'checkDeviceConnection', | ||
TOGGLE_GATE = 'toggleGate', | ||
SET_NGROK_DATA = 'setNgrokData', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { | ||
BadGatewayException, | ||
CACHE_MANAGER, | ||
Controller, | ||
Get, | ||
Inject, | ||
Logger, | ||
Res, | ||
} from '@nestjs/common'; | ||
import { Cache } from 'cache-manager'; | ||
import { Response } from 'express'; | ||
import https from 'https'; | ||
|
||
import { Auth } from '../auth/decorators/auth.decorator'; | ||
|
||
export interface NgrokData { | ||
url: string; | ||
auth: string; | ||
} | ||
|
||
@Auth() | ||
@Controller('camera') | ||
export class CameraController { | ||
private readonly logger: Logger = new Logger(CameraController.name); | ||
|
||
constructor(@Inject(CACHE_MANAGER) private readonly cacheManager: Cache) {} | ||
|
||
@Get() | ||
async proxyCameraRequest(@Res() res: Response) { | ||
const ngrokData = (await this.cacheManager.get('ngrokData')) as NgrokData; | ||
if (!ngrokData?.url || !ngrokData?.auth) { | ||
throw new BadGatewayException(); | ||
} | ||
|
||
res.set({ | ||
'Content-Type': 'multipart/x-mixed-replace; boundary=BoundaryString', | ||
'Cache-Control': 'no-cache, private', | ||
'Max-Age': 0, | ||
Expires: 0, | ||
Connection: 'close', | ||
Pragma: 'no-cache', | ||
}); | ||
|
||
const ngrokRequest = https | ||
.request(ngrokData.url, { auth: ngrokData.auth }, (httpRes) => { | ||
res.on('close', () => { | ||
ngrokRequest.destroy(new Error('Client disconnected')); | ||
}); | ||
|
||
httpRes.on('data', (dataBuffer) => { | ||
if (httpRes.statusCode !== 200) { | ||
return; | ||
} | ||
res.write(dataBuffer); | ||
}); | ||
|
||
httpRes.on('close', () => { | ||
if (httpRes.statusCode !== 200) { | ||
this.logger.error( | ||
`Ngrok connection closed with status code: ${ | ||
httpRes.statusCode?.toString() as string | ||
}`, | ||
); | ||
res.status(httpRes.statusCode ?? 500).end(); | ||
return; | ||
} | ||
|
||
this.logger.log( | ||
`Ngrok connection closed with status code: ${httpRes.statusCode?.toString()}`, | ||
); | ||
res.status(httpRes.statusCode ?? 200).end(); | ||
}); | ||
}) | ||
.on('error', (e) => { | ||
if (e.message === 'Client disconnected') { | ||
// Info about disconnect handled above in ngrok request | ||
return; | ||
} | ||
this.logger.error(e.message); | ||
}); | ||
ngrokRequest.end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { CacheModule, Module } from '@nestjs/common'; | ||
|
||
import { AuthModule } from '../auth/auth.module'; | ||
import { TokenModule } from '../auth/token/token.module'; | ||
import { RepositoryModule } from '../repository/repository.module'; | ||
import { UsersModule } from '../users/users.module'; | ||
import { CameraController } from './camera.controller'; | ||
|
||
@Module({ | ||
imports: [UsersModule, AuthModule, RepositoryModule, TokenModule, CacheModule.register()], | ||
controllers: [CameraController], | ||
}) | ||
export class CameraModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/client/src/pages/authorized/Dashboard/Dashboard.styled.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import styled, { css } from 'styled-components'; | ||
|
||
export const RowSection = styled.div( | ||
({ theme: { breakpoints, down } }) => css` | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: 100px; | ||
margin: 80px 0 20px; | ||
${down(breakpoints.md)} { | ||
gap: 0; | ||
align-items: center; | ||
flex-direction: column-reverse; | ||
& > :not(:first-child) { | ||
margin-bottom: 50px; | ||
} | ||
} | ||
`, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...c/pages/authorized/Dashboard/sections/CameraPreviewSection/CameraPreviewSection.styled.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import styled, { css } from 'styled-components'; | ||
|
||
export const LoadingContainer = styled.div( | ||
({ theme: { palette, sizes, down, breakpoints } }) => css` | ||
position: relative; | ||
width: 520px; | ||
height: 345px; | ||
border-radius: ${sizes.borderRadius}; | ||
background: ${palette.background.paper}; | ||
${down(breakpoints.sm)} { | ||
width: 100%; | ||
padding-bottom: 66.66%; | ||
} | ||
`, | ||
); | ||
|
||
export const LoadingContent = styled.div` | ||
position: absolute; | ||
top: 0; | ||
bottom: 0; | ||
left: 0; | ||
right: 0; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
text-align: center; | ||
`; | ||
|
||
export const CameraPreview = styled.img<CameraPreviewProps>( | ||
({ isLoaded, theme: { sizes, down, breakpoints } }) => css` | ||
border-radius: ${sizes.borderRadius}; | ||
width: 520px; | ||
height: 345px; | ||
${!isLoaded && | ||
css` | ||
visibility: hidden; | ||
`}; | ||
${down(breakpoints.sm)} { | ||
width: 100%; | ||
height: unset; | ||
} | ||
`, | ||
); |
3 changes: 3 additions & 0 deletions
3
.../pages/authorized/Dashboard/sections/CameraPreviewSection/CameraPreviewSection.types.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
interface CameraPreviewProps { | ||
isLoaded: boolean; | ||
} |
48 changes: 48 additions & 0 deletions
48
packages/client/src/pages/authorized/Dashboard/sections/CameraPreviewSection/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React, { useCallback, useLayoutEffect, useRef, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { CameraPreview, LoadingContainer, LoadingContent } from './CameraPreviewSection.styled'; | ||
|
||
const CameraPreviewSection = () => { | ||
const cameraURL = process.env.REACT_APP_API_URL && `${process.env.REACT_APP_API_URL}/camera`; | ||
const previewRef = useRef<HTMLImageElement>(null); | ||
const [isPreviewLoaded, setIsPreviewLoaded] = useState(false); | ||
const { t } = useTranslation(); | ||
|
||
const reloadCameraPreview = useCallback(() => { | ||
if (previewRef.current && cameraURL) { | ||
const requestPreviewTime = new Date().getTime(); | ||
previewRef.current.src = `${cameraURL}/?t=${requestPreviewTime}`; | ||
} | ||
}, [cameraURL]); | ||
|
||
useLayoutEffect(() => { | ||
reloadCameraPreview(); | ||
window.addEventListener('focus', reloadCameraPreview); | ||
return () => { | ||
window.removeEventListener('focus', reloadCameraPreview); | ||
}; | ||
}, [cameraURL, reloadCameraPreview]); | ||
|
||
return ( | ||
<div> | ||
{!isPreviewLoaded && ( | ||
<LoadingContainer> | ||
<LoadingContent> | ||
<h3>{t('routes.dashboard.sections.camera.loadingPreview')}</h3> | ||
</LoadingContent> | ||
</LoadingContainer> | ||
)} | ||
<CameraPreview | ||
ref={previewRef} | ||
isLoaded={isPreviewLoaded} | ||
alt={t('routes.dashboard.sections.camera.title')} | ||
onLoad={() => { | ||
setIsPreviewLoaded(true); | ||
}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CameraPreviewSection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
#API | ||
# API | ||
API_URL=http://localhost:3030 | ||
AUTH_TICKET=ticket | ||
|
||
# Camera | ||
CAMERA_ENABLED=false | ||
CAMERA_USE_WIRED=true | ||
NGROK_LOCAL_CAMERA_ADDRESS=http://localhost:8081 | ||
NGROK_REGION=eu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.