Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | import React, { useState, useEffect, useRef } from 'react'
import { CContainer, CButton, CSpinner, CAlert } from '@coreui/react'
import CIcon from '@coreui/icons-react'
import { cilInfo } from '@coreui/icons'
import { useParams } from 'react-router-dom'
import { DynamicPopup } from '@components/Dynamic'
import { AuthCheck } from '@components'
import deviceTypes from '@config/deviceTypes'
import { getPageData, useMqtt } from '@hooks'
import { apiService } from '@services'
import AddMarker from '../floorPages/AddMarker'
import InfoMarker from '../floorPages/InfoMarker'
import { TransformWrapper, TransformComponent } from 'react-zoom-pan-pinch'
const devicesApi = apiService('devices')
const blinkKeyframes = `
@keyframes blink {
0%, 100% { border-color: red; box-shadow: 0 0 8px 2px rgba(255,0,0,0.8); }
50% { border-color: transparent; box-shadow: none; }
}
`
const blinkAnim = 'blink 1.5s infinite'
const wrapperStyle = {
position: 'relative',
width: '100%',
maxWidth: '100%',
margin: '0 auto',
}
const FloorDashboard = () => {
const { floorId, imgName } = useParams()
const floorInt = floorId
const floorImg = `/floor_maps/${imgName}.png`
const [markers, setMarkers] = useState([])
const [selectedMarker, setSelectedMarker] = useState(null)
const [pendingCoordinates, setPendingCoordinates] = useState(null)
const [highlightedTypes, setHighlightedTypes] = useState({})
const [imgDimensions, setImgDimensions] = useState({ width: 1, height: 1 })
const addMarkerRef = useRef(null)
const infoMarkerRef = useRef(null)
const { loading, error, data: dbDevices, refetch } = getPageData(devicesApi.get, {})
const mqttData = useMqtt('smartclassroom/multilateration/#', true)
const getImgSrc = (type) => deviceTypes.find(d => d.value === type)?.imgSrc || ''
useEffect(() => {
const interval = setInterval(async () => {
try {
const freshDevices = await devicesApi.get()
setMarkers(freshDevices.filter(d =>
d.location?.floor_id === floorInt &&
typeof d.location.x === 'number' &&
typeof d.location.y === 'number'
))
} catch (err) {
// Handle error if needed
}
}, 500) // refresh markers every 0.05 seconds
return () => clearInterval(interval)
}, [floorInt])
useEffect(() => {
if (!mqttData || !Object.keys(mqttData).length) return
const mqttMarkers = Object.entries(mqttData)
.map(([mac, info]) => {
if (!info?.location || info.location.floor_id !== floorInt) return null
const dev = dbDevices?.find(d => d.macAddress === mac && (d.type === 'tag' || d.type === 'robot'))
if (!dev) return null
return {
macAddress: mac,
location: { x: info.location.x, y: info.location.y },
name: dev.name,
type: dev.type,
}
})
.filter(Boolean)
setMarkers(old => [...old.filter(m => !mqttMarkers.some(n => n.macAddress === m.macAddress)), ...mqttMarkers])
}, [mqttData, floorInt, dbDevices])
useEffect(() => {
const img = new window.Image()
img.src = floorImg
img.onload = () => setImgDimensions({ width: img.naturalWidth, height: img.naturalHeight })
}, [floorImg])
const coordinateBounds = { xMax: imgDimensions.width, yMax: imgDimensions.height }
const onDoubleClick = (e) => {
const rect = e.currentTarget.getBoundingClientRect()
const x = ((e.clientX - rect.left) / rect.width) * coordinateBounds.xMax
const y = ((e.clientY - rect.top) / rect.height) * coordinateBounds.yMax
setPendingCoordinates({ x, y })
addMarkerRef.current.open()
}
const onMarkerClick = (marker) => {
setSelectedMarker(marker)
infoMarkerRef.current.open()
}
const toggleHighlight = (type) => {
const macs = markers.filter(m => m.type === type).map(m => m.macAddress)
setHighlightedTypes(prev => {
const isAllHighlighted = macs.every(mac => prev[type]?.includes(mac))
return { ...prev, [type]: isAllHighlighted ? [] : macs }
})
}
if (loading) return <CSpinner />
if (error) return <p>Fout: {error.message}</p>
return (
<>
<style>
{blinkKeyframes}
{`
.body {
align-items: center;
justify-content: center;
display: flex;
}
@media (min-width: 1400px) {
.container-xxl, .container-xl, .container-lg, .container-md, .container-sm, .container {
max-width: 100%;
}
}
.highlight-wrapper {
position: absolute;
top: 10px;
right: 10px;
z-index: 20;
display: flex;
flex-direction: column;
align-items: flex-end;
}
.highlight-header {
background-color: rgba(50, 50, 50, 0.8);
padding: 8px 12px;
border-radius: 6px;
font-weight: 600;
font-size: 0.9rem;
color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
cursor: pointer;
}
.highlight-content {
opacity: 0;
visibility: hidden;
transform: translateY(-10px);
transition: all 0.3s ease;
margin-top: 6px;
background-color: rgba(255, 255, 255, 0.9);
padding: 8px;
border-radius: 6px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
max-height: 90vh;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 8px;
}
.highlight-wrapper:hover .highlight-content {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
`}
</style>
<CContainer>
<AuthCheck rights="admin">
<CAlert color="primary" className="d-flex align-items-center" dismissible>
<CIcon icon={cilInfo} className="flex-shrink-0 me-2" width={24} height={24} />
<div>Double click on the map to place a device location</div>
</CAlert>
</AuthCheck>
<div style={wrapperStyle}>
<div className="highlight-wrapper">
<div className="highlight-header">Highlight</div>
<div className="highlight-content">
{deviceTypes.map(({ value, label, imgSrc }) => {
const isHighlighted = highlightedTypes[value]?.length > 0;
return (
<CButton
key={value}
size="sm"
color={isHighlighted ? 'success' : 'secondary'}
onClick={() => toggleHighlight(value)}
style={{ display: 'flex', alignItems: 'center', gap: '6px' }}
>
<img src={imgSrc} alt={label} style={{ width: '16px', height: '16px', objectFit: 'contain' }} />
{label}
</CButton>
)
})}
</div>
</div>
<TransformWrapper options={{ maxScale: 5, minScale: 0.5 }} wheel={{ step: 0.5 }} doubleClick={{ disabled: true }}>
<TransformComponent wrapperStyle={{ width: '100%' }} contentStyle={{ width: '100%' }}>
<div onDoubleClick={onDoubleClick} style={{ position: 'relative', width: '100%' }}>
<img src={floorImg} alt="Floorplan" style={{ width: '100%', display: 'block' }} />
{markers.map(m => (
<img
key={m.macAddress}
src={getImgSrc(m.type)}
alt={m.name}
title={m.name}
onClick={() => onMarkerClick(m)}
style={{
position: 'absolute',
top: `${(m.location.y / coordinateBounds.yMax) * 100}%`,
left: `${(m.location.x / coordinateBounds.xMax) * 100}%`,
width: 15,
height: 15,
transform: 'translate(-50%, -50%)',
cursor: 'pointer',
zIndex: 10,
pointerEvents: 'auto',
border: highlightedTypes[m.type]?.includes(m.macAddress) ? '3px solid red' : 'none',
animation: highlightedTypes[m.type]?.includes(m.macAddress) ? blinkAnim : 'none',
transition: 'top 0.5s ease, left 0.5s ease',
}}
/>
))}
</div>
</TransformComponent>
</TransformWrapper>
</div>
<DynamicPopup ref={infoMarkerRef} title="Marker Info" size="xl">
<InfoMarker popupStatus={infoMarkerRef} selectedMarker={selectedMarker} refetch={refetch} />
</DynamicPopup>
<AuthCheck rights="admin">
<DynamicPopup ref={addMarkerRef} title="Add Location">
<AddMarker
popupStatus={addMarkerRef}
floorId={floorId}
pendingCoordinates={pendingCoordinates}
refetch={refetch}
/>
</DynamicPopup>
</AuthCheck>
</CContainer>
</>
)
}
export default FloorDashboard |