monitor_20201022174447.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <template>
  2. <div id="container" class="box">
  3. <div id="cesiumContainer"></div>
  4. <div class="menu">
  5. <el-dropdown @command="handleCommand">
  6. <span class="el-dropdown-link">
  7. 下拉菜单<i class="el-icon-arrow-down el-icon--right"></i>
  8. </span>
  9. <el-dropdown-menu slot="dropdown" class="opt">
  10. <el-dropdown-item command="a">天气开关</el-dropdown-item>
  11. <el-dropdown-item command="b">地图中文标记</el-dropdown-item>
  12. <el-dropdown-item command="c">回到初始位置</el-dropdown-item>
  13. </el-dropdown-menu>
  14. </el-dropdown>
  15. </div>
  16. <div class="promsg" v-show="promsg">
  17. <div class="promsg_bar">状态:<span>在线</span></div>
  18. <div class="promsg_bar">
  19. 姓名:<span>{{ msg[userid].username }}</span>
  20. </div>
  21. <div class="promsg_bar">
  22. 疲劳度:<span>{{ msg[userid].pld }}</span>
  23. </div>
  24. <div class="promsg_bar">
  25. 收缩血压:<span>{{ msg[userid].szxy }}</span>
  26. </div>
  27. <div class="promsg_bar">
  28. 舒张血压:<span>{{ msg[userid].ssxy }}</span>
  29. </div>
  30. <div class="promsg_bar">
  31. 血氧浓度:<span>{{ msg[userid].xynd }}</span>
  32. </div>
  33. <div class="promsg_bar">
  34. 心跳频率:<span>{{ msg[userid].xtpl }}</span>
  35. </div>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. var Cesium = require("cesium/Cesium");
  41. var widgets = require("cesium/Widgets/widgets.css");
  42. import addCircleWave from "../../public/static/CiecleScan";
  43. export default {
  44. name: "cesiumPage",
  45. data() {
  46. return {
  47. promsg: false,
  48. labelEntities: [],
  49. weather: null,
  50. publicPath: process.env.BASE_URL,
  51. viewer: null,
  52. msg: [
  53. {
  54. bs: 200,
  55. isrun: 0,
  56. lat: 38.49372100830078,
  57. lng: 106.24307250976562,
  58. pld: 36,
  59. ssxy: 72,
  60. szxy: 110,
  61. userid: 1941,
  62. username: "孙亚飞",
  63. xtpl: 94,
  64. xynd: 98,
  65. },
  66. ],
  67. userid: 1,
  68. };
  69. },
  70. created() {
  71. if (this.websock) {
  72. this.websock.close(); // 关闭websocket连接
  73. }
  74. this.initWebSocket();
  75. },
  76. destroyed() {
  77. //页面销毁时关闭ws连接
  78. if (this.websock) {
  79. this.websock.close(); // 关闭websocket
  80. }
  81. },
  82. mounted() {
  83. var viewer = new Cesium.Viewer("cesiumContainer", {
  84. animation: false, //是否显示动画控件
  85. shouldAnimate: true,
  86. homeButton: false, //是否显示Home按钮
  87. fullscreenButton: false, //是否显示全屏按钮
  88. baseLayerPicker: false, //是否显示图层选择控件
  89. geocoder: false, //是否显示地名查找控件http://localhost:8080/#/cesiumScene
  90. timeline: false, //是否显示时间线控件
  91. sceneModePicker: false, //是否显示投影方式控件
  92. navigationHelpButton: false, //是否显示帮助信息控件
  93. infoBox: false, //是否显示点击要素之后显示的信息
  94. requestRenderMode: true, //启用请求渲染模式
  95. scene3DOnly: false, //每个几何实例将只能以3D渲染以节省GPU内存
  96. sceneMode: 3, //初始场景模式 1 2D模式 2 2D循环模式 3 3D模式 Cesium.SceneMode
  97. fullscreenElement: document.body, //全屏时渲染的HTML元素 暂时没发现用处
  98. imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
  99. url:
  100. "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer",
  101. baseLayerPicker: false,
  102. }),
  103. });
  104. viewer.scene.camera.setView({
  105. // 初始化相机经纬度
  106. destination: new Cesium.Cartesian3.fromDegrees(
  107. 106.24307250976562,
  108. 38.49372100830078,
  109. 2000
  110. ),
  111. orientation: {
  112. heading: Cesium.Math.toRadians(0.0),
  113. pitch: Cesium.Math.toRadians(-90.0), //从上往下看为-90
  114. roll: 0,
  115. },
  116. });
  117. var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
  118. handler.setInputAction((movement) => {
  119. var pick = viewer.scene.pick(movement.position);
  120. if (Cesium.defined(pick) && pick.id.id) {
  121. this.userid = pick.id.id;
  122. this.promsg = true;
  123. }
  124. }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  125. this.viewer = viewer;
  126. },
  127. methods: {
  128. addpoint(res) {
  129. switch (res.isrun) {
  130. case 0:
  131. if (!this.viewer.entities.getById(res.userid))
  132. this.viewer.entities.add({
  133. id: res.userid,
  134. position: Cesium.Cartesian3.fromDegrees(res.lng, res.lat),
  135. point: {
  136. pixelSize: 10,
  137. color: Cesium.Color.YELLOW,
  138. },
  139. label: {
  140. text: res.username,
  141. font: "normal 14px MicroSoft YaHei",
  142. color: Cesium.Color.fromCssColorString("#fff"),
  143. outlineWidth: 2,
  144. verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
  145. pixelOffset: new Cesium.Cartesian2(0, -9),
  146. },
  147. });
  148. break;
  149. case 1:
  150. if (this.viewer.entities.getById(res.userid)) {
  151. this.viewer.entities.removeById(res.userid);
  152. addCircleWave(this.viewer, res, Cesium.Color.GREEN);
  153. } else {
  154. addCircleWave(this.viewer, res, Cesium.Color.GREEN);
  155. }
  156. break;
  157. default:
  158. if (this.viewer.entities.getById(res.userid)) {
  159. this.viewer.entities.removeById(res.userid);
  160. addCircleWave(this.viewer, res, Cesium.Color.RED);
  161. } else {
  162. addCircleWave(this.viewer, res, Cesium.Color.RED);
  163. }
  164. break;
  165. }
  166. },
  167. addRain() {
  168. // 雨雪天气添加
  169. // scene.postProcessStages.add(Cesium.PostProcessStageLibrary.createRainStage())
  170. var scene = this.viewer.scene;
  171. if (this.weather == null) {
  172. scene.postProcessStages.add(
  173. (this.weather = Cesium.PostProcessStageLibrary.createSnowStage())
  174. );
  175. } else {
  176. scene.postProcessStages.remove(this.weather);
  177. this.weather = null;
  178. }
  179. // scene.skyAtmosphere.hueShift = -0.8;
  180. // scene.skyAtmosphere.saturationShift = -0.7;
  181. // scene.skyAtmosphere.brightnessShift = -0.33;
  182. // scene.fog.density = 0.001;
  183. // scene.fog.minimumBrightness = 0.8;
  184. },
  185. geoJSON() {
  186. //jeojson 加载宁夏地图
  187. Cesium.Math.setRandomNumberSeed(0);
  188. var promise = this.viewer.dataSources.add(
  189. Cesium.GeoJsonDataSource.load(
  190. `${this.publicPath}static/mapgeoJsonnx.json`,
  191. {
  192. stroke: Cesium.Color.green,
  193. fill: Cesium.Color.TRANSPARENT,
  194. strokeWidth: 3,
  195. markerSymbol: "?",
  196. }
  197. )
  198. );
  199. promise.then((dataSource) => {
  200. const entities = dataSource.entities.values;
  201. console.log(this.labelEntities.length);
  202. if (this.labelEntities.length == 0) {
  203. for (let i = 0; i < entities.length; i++) {
  204. const entity = entities[i];
  205. const name = entity.name;
  206. const location = entity.properties._centroid._value;
  207. const labelEntity = this.viewer.entities.add({
  208. position: Cesium.Cartesian3.fromDegrees(location[0], location[1]),
  209. label: {
  210. text: name,
  211. font: "14px Helvetica",
  212. },
  213. });
  214. this.labelEntities.push(labelEntity);
  215. }
  216. } else {
  217. for (let i = 0; i < this.labelEntities.length; i++) {
  218. this.viewer.entities.remove(this.labelEntities[i]);
  219. }
  220. // for (let i = 0; i < entities.length; i++) {
  221. // entities[i].polygon.outline = false;
  222. // }
  223. }
  224. });
  225. },
  226. flyto() {
  227. this.viewer.camera.flyTo({
  228. destination: Cesium.Cartesian3.fromDegrees(
  229. 106.24307250976562,
  230. 38.49372100830078,
  231. 2000
  232. ),
  233. //duration:5, // 设置飞行持续时间,默认会根据距离来计算
  234. complete: function () {
  235. // 到达位置后执行的回调函数
  236. console.log("到达目的地");
  237. },
  238. cancle: function () {
  239. // 如果取消飞行则会调用此函数
  240. console.log("飞行取消");
  241. },
  242. pitchAdjustHeight: -90, // 如果摄像机飞越高于该值,则调整俯仰俯仰的俯仰角度,并将地球保持在视口中。
  243. maximumHeight: 5000, // 相机最大飞行高度
  244. });
  245. },
  246. handleCommand(command) {
  247. switch (command) {
  248. case "a":
  249. this.addRain();
  250. break;
  251. case "b":
  252. this.geoJSON();
  253. break;
  254. default:
  255. this.flyto();
  256. break;
  257. }
  258. },
  259. //初始化weosocket
  260. initWebSocket() {
  261. if (typeof WebSocket === "undefined") {
  262. alert("您的浏览器不支持WebSocket");
  263. return false;
  264. }
  265. const wsuri = "ws://10.155.32.4:8010/shbracelet";
  266. this.websock = new WebSocket(wsuri);
  267. this.websock.onopen = this.websocketonopen;
  268. this.websock.onmessage = this.websocketonmessage;
  269. this.websock.onerror = this.websocketonerror;
  270. this.websock.onclose = this.websocketclose;
  271. },
  272. //连接成功
  273. websocketonopen() {
  274. console.log("WebSocket连接成功");
  275. // 添加心跳检测,每30秒发一次数据,防止连接断开(这跟服务器的设置有关,如果服务器没有设置每隔多长时间不发消息断开,可以不进行心跳设置)
  276. let self = this;
  277. this.timer = setInterval(() => {
  278. try {
  279. self.websock.send("test");
  280. console.log("发送消息");
  281. } catch (err) {
  282. console.log("断开了:" + err);
  283. self.connection();
  284. }
  285. }, 30000);
  286. },
  287. //接收后端返回的数据,可以根据需要进行处理
  288. websocketonmessage(e) {
  289. var vm = this;
  290. let res = JSON.parse(e.data);
  291. console.log(res);
  292. this.msg = res;
  293. for (let i in res) {
  294. this.addpoint(res[i]);
  295. }
  296. },
  297. //连接建立失败重连
  298. websocketonerror(e) {
  299. console.log(`连接失败的信息:`, e);
  300. this.initWebSocket(); // 连接失败后尝试重新连接
  301. },
  302. //关闭连接
  303. websocketclose(e) {
  304. console.log("断开连接", e);
  305. },
  306. },
  307. };
  308. </script>
  309. <!-- Add "scoped" attribute to limit CSS to this component only -->
  310. <style lang="scss" scoped>
  311. html,
  312. body,
  313. #cesiumContainer {
  314. width: 100%;
  315. height: 100%;
  316. margin: 0;
  317. padding: 0;
  318. overflow: hidden;
  319. }
  320. .box {
  321. height: 100%;
  322. .menu {
  323. position: absolute;
  324. left: 20px;
  325. top: 20px;
  326. background-color: rgba(0, 0, 0, 0.5);
  327. box-sizing: border-box;
  328. padding: 6px 20px;
  329. border-radius: 10px;
  330. .el-dropdown-link {
  331. color: #fff;
  332. }
  333. }
  334. .promsg {
  335. position: absolute;
  336. width: 8vw;
  337. right: 20px;
  338. top: 20px;
  339. background-color: rgba(0, 0, 0, 0.5);
  340. box-sizing: border-box;
  341. padding: 6px 20px;
  342. border-radius: 10px;
  343. display: flex;
  344. flex-wrap: wrap;
  345. .promsg_bar {
  346. color: #fff;
  347. width: 100%;
  348. box-sizing: border-box;
  349. padding: 10px 6px;
  350. display: flex;
  351. justify-content: space-between;
  352. }
  353. }
  354. }
  355. .opt {
  356. background-color: rgba(0, 0, 0, 0.5) !important;
  357. color: #fff !important;
  358. border: none;
  359. }
  360. </style>