threedire.jsx 11.9 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/* eslint-disable array-callback-return */
/* eslint-disable no-unused-vars */
import * as THREE from "three";
import {
  Canvas,
  useFrame,
  extend,
  useLoader,
  useThree,
  axesHelper,
} from "react-three-fiber";
import { useRef, Suspense, useState, useEffect } from "react";
import { OrbitControls } from "@react-three/drei";
wuhao's avatar
wuhao committed
14
import { Scrollbars } from "react-custom-scrollbars";
wuhao's avatar
wuhao committed
15 16 17 18 19 20 21 22 23 24 25 26
import {
  useWebSocket,
  useFullscreen,
  useThrottle,
  useRequest,
  useAsyncEffect,
} from "ahooks";
import {
  ShrinkOutlined,
  CloseCircleFilled,
  ExclamationCircleFilled,
} from "@ant-design/icons";
wuhao's avatar
wuhao committed
27
import { Tooltip, notification, Empty } from "antd";
wuhao's avatar
wuhao committed
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
import Model from "./models/gltf";
import Robot from "./models";
import Jixiebi from "./models/jiqiebi";
import { doFetch } from "../utils/doFetch";
import {
  Selection,
  EffectComposer,
  Outline,
} from "@react-three/postprocessing";

let infos = {
  device_5: "能耗1(1-12设备)",
  device_6: "能耗2(13-24设备)",
  device_1: "数控车床T360-2",
  device_2: "数控车床T360-1",
  device_3: "立式智能加工中心M850b",
  device_4: "立式智能加工中心M850",
};

const modelconfig = [
  {
    name: "0",
    position: [-24, 0, 24],
    status: 1,
    modelname: "cqg",
    scale: 0.035,
  },
  {
    name: "1",
    position: [-21.5, 0, 24],
    status: 1,
    modelname: "kyj",
  },
  {
    name: "2",
    position: [-21.5, 0, 25.5],
    status: 1,
    modelname: "lgj",
    scale: 0.06,
  },
  {
    name: "3",
    position: [-19, 0, 15],
    status: 1,
    modelname: "ptwymc",
    rotation: [0, 1.6, 0],
  },
  {
    name: "4",
    position: [-21, 0, 6.5],
    status: 1,
    modelname: "pmmc",
    scale: 0.06,
  },
  {
wuhao's avatar
wuhao committed
83
    name: "6",
wuhao's avatar
wuhao committed
84 85 86 87 88
    position: [-21, 0, -12],
    status: 1,
    modelname: "M1.4",
  },
  {
wuhao's avatar
wuhao committed
89
    name: "5",
wuhao's avatar
wuhao committed
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    position: [-21, 0, -4],
    status: 1,
    modelname: "M1.4",
  },
  {
    name: "7",
    position: [-21, 0, -22],
    status: 1,
    modelname: "M8",
    rotation: [0, 1.6, 0],
  },
  {
    name: "8",
    position: [-6, 0, -22],
    status: 1,
    modelname: "M8",
    rotation: [0, 1.6, 0],
  },
  {
    name: "9",
    position: [3, 0, -22],
    status: 1,
    modelname: "M8",
    rotation: [0, 1.6, 0],
  },
  {
    name: "10",
    position: [12, 0, -22],
    status: 1,
    modelname: "M8",
    rotation: [0, 1.6, 0],
  },
  {
    name: "11",
    position: [21, 0, -22],
    status: 1,
    modelname: "M8",
    rotation: [0, 1.6, 0],
  },
  {
    name: "12",
    position: [18, 0, -20.5],
    status: 1,
    modelname: "T360",
    scale: 0.75,
  },
  {
    name: "13",
    position: [5.7, 0, -7],
    status: 1,
    modelname: "T360",
    scale: 0.75,
    rotation: [0, 1.6, 0],
  },
  {
    name: "14",
    position: [19.8, 0, 8],
    status: 1,
    modelname: "M850",
    scale: 0.052,
    rotation: [0, 1.58, 0],
  },
  {
    name: "15",
    position: [18, 0, 17],
    status: 1,
    modelname: "M8",
  },
  {
    name: "16",
    position: [18, 0, 25],
    status: 1,
    modelname: "M8",
  },
  {
    name: "17",
    position: [11, 0, 26],
    status: 1,
    modelname: "M1.4",
  },
  {
    name: "18",
    position: [16.5, 0, 18.5],
    status: 1,
    modelname: "T360",
    scale: 0.75,
    rotation: [0, -1.58, 0],
  },
  {
    name: "19",
    position: [11, 0, 8],
    status: 1,
    modelname: "M850",
    scale: 0.052,
    rotation: [0, -1.58, 0],
  },
  {
    name: "20",
    position: [-2, 0, 9.2],
    status: 1,
    modelname: "T3.4",
    scale: 0.23,
    rotation: [0, 1.58, 0],
  },
  {
    name: "21",
    position: [-0.5, 0, 26],
    status: 1,
    modelname: "M1.4",
  },
  {
    name: "22",
    position: [-9, 0, 24.5],
    status: 1,
    modelname: "CAK5085",
    rotation: [0, -1.58, 0],
    scale: 0.04,
  },
  {
    name: "23",
    position: [-9, 0, 16.6],
    status: 1,
    modelname: "wyjmmc",
    rotation: [0, -1.58, 0],
    scale: 0.055,
  },
  {
    name: "24",
    position: [-8.79, 0, 8],
    status: 1,
    modelname: "nymc",
    rotation: [0, -1.58, 0],
    scale: 0.055,
  },
];
const statusbar = {
  1: {
    color: "#6AE49B",
    name: "运行",
  },
  2: {
    color: "#4887E0",
    name: "待机",
  },
  3: {
    color: "#8D8D8D",
    name: "关机",
  },
  4: {
    color: "#F31A1A",
    name: "故障",
  },
};
const Floor = (props) => {
  return (
    <>
      <mesh position={props.position} receiveShadow>
        <boxBufferGeometry args={[60, 0.1, 60]} />
        <meshPhysicalMaterial color={"#12284A"} />
      </mesh>
    </>
  );
};

const Bulb = (props) => {
  return (
    <mesh {...props}>
      <pointLight castShadow></pointLight>
      <ambientLight color={"#999999"}></ambientLight>
      {/* <sphereBufferGeometry args={[0.2, 60, 60]} />
      <meshPhongMaterial emissive={"yellow"} /> */}
    </mesh>
  );
};

function Threedire() {
  const texture = useLoader(THREE.TextureLoader, require("@/assets/floor.png"));
  const [equpmentNo, setequpmentNo] = useState();
  const [config, setconfig] = useState(modelconfig);
  const [info, setinfo] = useState({});
  const [modalprops, setmodalprops] = useState({
    visible: false,
    data: {},
  });
  const [alarm, setalarm] = useState({
    visible: false,
    data: {},
  });
  const { data } = useRequest(
    async () => {
      //format result
      let res = await doFetch({
          url: "/ngic-statistics/deviceStatus/queryEquipmentStatus",
          params: {},
        }),
        result = res?.data?.data ?? {};
      return result;
    },
    {
wuhao's avatar
wuhao committed
289
      pollingInterval: 5000,
wuhao's avatar
wuhao committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    }
  );

  useEffect(() => {
    if (data) {
      setconfig((s) => {
        let news = JSON.parse(JSON.stringify(s ?? {}));
        news = news.map((it) => {
          it.status = it.name == "0" ? 3 : data[it.name];
          return it;
        });
        return news;
      });
    }
  }, [data]);

  useAsyncEffect(async () => {
    if (equpmentNo) {
      let res = await doFetch({
        url: "/ngic-statistics/deviceStatus/queryEquipmentParam",
        params: { equipmentNo: equpmentNo },
      });

      setmodalprops({
        visible: true,
        data: res?.data?.data,
      });
    } else {
      setmodalprops({
        visible: false,
      });
    }
  }, [equpmentNo]);

  const { sendMessage, lastMessage, readyState } = useWebSocket(
wuhao's avatar
wuhao committed
325
    `ws://192.168.40.14:17901/ws/device/status`, //192.168.40.50:7910
wuhao's avatar
wuhao committed
326 327 328 329 330 331 332 333 334 335 336 337
    {
      onOpen: () => {
        console.log("readyState is ", readyState);
      },
      onClose: () => {
        console.log("连接关闭");
      },
      onError: (e) => {
        console.log(e);
      },
      onMessage: (message) => {
        let uir = JSON.parse(message.data);
wuhao's avatar
wuhao committed
338 339
        if (Object.values(uir).includes(0)) {
          setalarm((s) => ({
wuhao's avatar
wuhao committed
340
            ...s,
wuhao's avatar
wuhao committed
341 342
            visible: true,
          }));
wuhao's avatar
wuhao committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
        }
        setinfo((s) => {
          return {
            ...s,
            ...uir,
          };
        });
      },
    }
  );

  return (
    <>
      <div
        className="status"
        style={{
          transform: alarm?.visible ? "translateY(-4px)" : `translateY(-800px)`,
          transition: "all 0.4s",
        }}
      >
wuhao's avatar
wuhao committed
363 364 365 366 367 368 369
        <p
          style={{
            display: "flex",
            justifyContent: "space-between",
            alignItems: "center",
          }}
        >
wuhao's avatar
wuhao committed
370
          <span>离线设备</span>
wuhao's avatar
wuhao committed
371 372 373 374 375 376 377 378
          <CloseCircleFilled
            onClick={() => {
              setalarm((s) => ({
                ...s,
                visible: false,
              }));
            }}
          />
wuhao's avatar
wuhao committed
379 380 381
        </p>
        {Object.keys(infos).map((it) => {
          return (
wuhao's avatar
wuhao committed
382
            <div key={it} style={{ display: info[it] == 1 ? "none" : "block" }}>
wuhao's avatar
wuhao committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
              <ExclamationCircleFilled style={{ color: "red" }} />
              <span> {infos[it]}</span>
            </div>
          );
        })}
      </div>

      <div
        className="fixder"
        style={{
          transform: modalprops?.visible
            ? "translateY(-4px)"
            : `translateY(-800px)`,
          transition: "all 0.4s",
        }}
      >
        <div className="spread">
wuhao's avatar
wuhao committed
400
          <span>
wuhao's avatar
wuhao committed
401
            {modalprops?.data?.equipmentName}
wuhao's avatar
wuhao committed
402
          </span>
wuhao's avatar
wuhao committed
403 404 405 406 407 408 409
          <span style={{ color: statusbar[config[equpmentNo]?.status]?.color }}>
            {statusbar[config[equpmentNo]?.status]?.name}
          </span>
        </div>

        <div
          style={{
wuhao's avatar
wuhao committed
410 411 412 413 414
            overflow: "hidden",
            borderBottom:
              modalprops?.data?.paramList?.length > 0
                ? "2px solid rgba(255,255,255,0.5)"
                : "none",
wuhao's avatar
wuhao committed
415 416 417 418
            paddingBottom: 12,
            marginBottom: 12,
          }}
        >
wuhao's avatar
wuhao committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
          <Scrollbars
            thumbMinSize={10}
            autoHide
            style={{
              width: "100%",
              height: "200px",
            }}
            hideTracksWhenNotNeeded
          >
            {modalprops?.data?.paramList?.length>0?modalprops?.data?.paramList?.map?.((it) => {
              return (
                <div className="spreaditem">
                  <div>
                    {it.name}
                  </div>
wuhao's avatar
wuhao committed
434
                  <div>{it.value ?? "-"}</div>
wuhao's avatar
wuhao committed
435 436 437 438
                </div>
              );
            }):<Empty style={{opacity:0.4,marginTop:36}} description={<span style={{color:"#ffffff"}}>暂无数据</span>}></Empty>}
          </Scrollbars>
wuhao's avatar
wuhao committed
439 440 441
        </div>
        <div style={{ color: "rgba(255,255,255,0.6)" }}>
          <span>告警信息: </span>
wuhao's avatar
wuhao committed
442
          <span> {modalprops?.data?.alarm ?? '-'}</span>
wuhao's avatar
wuhao committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
        </div>
      </div>
      <Canvas
        shadows
        camera={{
          position: [0, 30, 20],
        }}
      >
        <Selection>
          <EffectComposer multisampling={8} autoClear={false}>
            <Outline
              blur
              visibleEdgeColor="white"
              edgeStrength={20}
              width={5000}
            />
          </EffectComposer>
          {config?.map((it, i) => {
            return (
              <Model
                key={it.name}
                {...it}
                setequpmentNo={setequpmentNo}
                equpmentNo={equpmentNo}
              />
            );
          })}
        </Selection>

        {/* <Robot /> */}
        <Jixiebi />

        <Floor position={[2, -0.06, 0]} />
        <gridHelper
          args={[60, 15, "#20293A", "#20293A"]}
          position={[2, 0, 0]}
        />
        <Bulb position={[0, 6, 0]} />
        <OrbitControls />
        <>
          <mesh position={[7.5, 0, 1.4]} receiveShadow>
            <boxBufferGeometry args={[45, 0.1, 4]} />
            <meshPhysicalMaterial
              color={"green"}
              roughness={0.5}
              transmission={0.5}
            />
          </mesh>
          <mesh position={[6, 0, 15]} receiveShadow>
            <boxBufferGeometry args={[4, 0.1, 30]} />
            <meshPhysicalMaterial
              color={"green"}
              roughness={0.5}
              transmission={0.5}
            />
          </mesh>
          <mesh position={[-15, 0, 0]} receiveShadow>
            <boxBufferGeometry args={[4, 0.1, 60]} />
            <meshPhysicalMaterial
              color={"green"}
              roughness={0.5}
              transmission={0.5}
            />
          </mesh>
          <mesh position={[28, 0, 0]} receiveShadow>
            <boxBufferGeometry args={[4, 0.1, 60]} />
            <meshPhysicalMaterial
              color={"green"}
              roughness={0.5}
              transmission={0.5}
            />
          </mesh>

          <mesh position={[-7, 0, -6]} receiveShadow>
            <boxBufferGeometry args={[8, 0.1, 8]} />
            <meshPhysicalMaterial color={"grey"} map={texture} roughness={1} />
          </mesh>
          <mesh position={[-7, 0, -15.6]} receiveShadow>
            <boxBufferGeometry args={[8, 0.1, 8]} />
            <meshPhysicalMaterial color={"grey"} map={texture} roughness={1} />
          </mesh>
          <mesh position={[3, 0, -10.75]} receiveShadow>
            <boxBufferGeometry args={[9, 0.1, 17.75]} />
            <meshPhysicalMaterial color={"grey"} map={texture} roughness={1} />
          </mesh>
        </>
      </Canvas>
    </>
  );
}

export default Threedire;