r03.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. var unityName = "R03Monitor";
  2. var errorTable;
  3. var sites = {};
  4. var display_info = {};
  5. $.ajaxSettings.async = false;
  6. var jqxhr = $.getJSON("/BIM-Monitor/assets/errorTable.json", function (json) {
  7. errorTable = json;
  8. });
  9. var C3Json = $.getJSON("/BIM-Monitor/assets/json/c3-monitorList.json", function (json) {
  10. sites["c3"] = json;
  11. });
  12. var R03Json = $.getJSON("/BIM-Monitor/assets/json/r03-monitorList.json", function (json) {
  13. sites["r03"] = json;
  14. });
  15. $.ajaxSettings.async = true;
  16. var lastNode;
  17. var switchNumber = 0;
  18. passString('SIS');
  19. var wellTBA = [];
  20. var wellNameLists = [];
  21. var isAll = true;
  22. var dataTable;
  23. var sparklinePieData = [];
  24. $(document).ready(function () {
  25. GetCheckAllAJAXValue();
  26. LoadPage();
  27. $("#menu > ul > li.nav-parent.nav-active.nav-expanded > ul > li.nav-parent.nav-active.nav-expanded > ul > li:nth-child(1) > a").css("color", "#0088cc");
  28. $(".monitor").on('click', function () {
  29. $('.monitor').css('color', '#abb4be')
  30. let site = $(this).parent().parent().parent().attr("monitor");
  31. $(this).css("color", "#0088cc");
  32. if (site != 'r03') {
  33. location.href = `../${site}/Index.php`;
  34. } else {
  35. let option = $(this).attr("monitor");
  36. isAll = true;
  37. passString(option);
  38. document.getElementById("monitorName").innerHTML = option + "監測儀器";
  39. LoadPage();
  40. }
  41. });
  42. })
  43. //Pass monitor equipment's name to index.php select form
  44. function passString(option) {
  45. window.localStorage.setItem("option", option);
  46. }
  47. //unity選擇到一個監測儀器後,在selectWell.cs會給一個監測儀器名稱(例:OW-1)給網頁
  48. function GetWellName(wellName) {
  49. showWell(wellName, true);
  50. }
  51. //網頁用SendMessage()傳Initialization()給unity時,unity會回傳一個所有監測儀器名稱(例:OW-1,OW-2 ...)給網頁
  52. function GetWellNameList(wellNameList) {
  53. window.localStorage.setItem("wellNameList", wellNameList);
  54. }
  55. //display well name and send its name to showWell function
  56. function getEquipmentName(equipment) {
  57. $.ajaxSettings.async = false;
  58. $.ajax({
  59. url: '/BIM-Monitor/script/php/getEquipmentName.php',
  60. type: "GET",
  61. data: {
  62. equipment: equipment,
  63. }
  64. }).success(function (data) {
  65. GetWellNameList(data);
  66. }).error(function (error) {
  67. console.log("錯誤");
  68. });
  69. $.ajaxSettings.async = true;
  70. }
  71. //unity載入後會先跑LoadPage();
  72. function LoadPage() {
  73. var option = window.localStorage.getItem("option");
  74. getEquipmentName(option);
  75. $('#' + window.localStorage.getItem("option")).removeClass('hidden').removeAttr('disabled');
  76. document.getElementById("monitorSelect2").innerHTML = "選擇觀測儀器";
  77. var wellTBALocal = getCheckValue();
  78. var dataValue = option;
  79. for (i = 0; i < wellTBALocal.length; i++) {
  80. dataValue = dataValue.concat(',', wellTBALocal[i]);
  81. }
  82. if (isLoad) {
  83. //dataValue = 監測儀器代號,超出警戒值的儀器名稱
  84. //AddDataTable會存option:監測儀器代號(string)
  85. // abnormal:超出警戒值的儀器名稱(array)
  86. // gameObjects:所有監測儀器名稱(array)
  87. // optionGameObjects:包含監測儀器代號的所有監測儀器名稱(array)
  88. // 到unity的DataTable.cs裡面
  89. myGameInstance.SendMessage("Monitor", "AddDataTable", dataValue);
  90. //原本的模型牆被挖空,SID、RS、VG都有建各自的牆,若選擇一種監測儀器則另外兩種隱藏掉
  91. //unity會跑addData(DataTable.option);
  92. //功能是依照DataTable的option將所有optionGameObjects改成normal的顏色
  93. //unity會跑Search(DataTable.option, DataTable.abnormal);
  94. //功能是將所有abnormal的儀器改成abnormal的顏色
  95. myGameInstance.SendMessage("Monitor", "Initialization");
  96. }
  97. var wellNameList = window.localStorage.getItem("wellNameList");
  98. var wellNameListsLocal = wellNameList.split(",");
  99. var order = new Array(wellNameListsLocal.length);
  100. for (i = 0; i < wellNameListsLocal.length; i++) {
  101. order[i] = wellNameListsLocal[i].match(/\d+/g);
  102. }
  103. for (i = 0; i < order.length; i++) {
  104. for (j = 0; j < order.length; j++) {
  105. if (order[j] > order[j + 1]) {
  106. let temp = order[j];
  107. order[j] = order[j + 1];
  108. order[j + 1] = temp;
  109. let temp2 = wellNameListsLocal[j];
  110. wellNameListsLocal[j] = wellNameListsLocal[j + 1];
  111. wellNameListsLocal[j + 1] = temp2;
  112. }
  113. }
  114. }
  115. wellNameLists = wellNameListsLocal;
  116. wellTBA = wellTBALocal;
  117. SetList();
  118. ResetCamera();
  119. showWell(wellNameLists[0], false);
  120. }
  121. //get well info from sql server
  122. function showWell(str, changeCamera) {
  123. var strLocal = "";
  124. strLocal = str;
  125. $("#monitorSelect2").innerHTML = strLocal;
  126. getTable(str);
  127. getChart(str);
  128. if (isLoad) {
  129. //把現在選擇的儀器名稱(例:OW-1)傳到unity,unity找模型名稱相同的改變模型顏色以及鏡頭置中模型
  130. myGameInstance.SendMessage("MainCamera", "GetDataFromWebGL", str);
  131. }
  132. if (!changeCamera) {
  133. ResetCamera();
  134. }
  135. }
  136. function getTable(monitor) {
  137. $.ajax({
  138. url: '/BIM-Monitor/script/php/get_monitor_table.php',
  139. type: "GET",
  140. dataType: "JSON",
  141. data: {
  142. monitor: monitor
  143. }
  144. }).success(function (result) {
  145. let table = result.table;
  146. let unit = result.unit;
  147. let haveDepth = result.haveDepth;
  148. let haveDirection = result.haveDirection;
  149. var header = "";
  150. if (haveDepth) {
  151. for (i = 0; i < table[0].length - 1; i++) {
  152. head = i * 0.5;
  153. if (head == 0)
  154. head = "TOP";
  155. header += `<th>${head}</th>`;
  156. }
  157. } else if (haveDirection) {
  158. header = `<th>${monitor}(1~3向)</th><th>${monitor}(2~4向)</th>`;
  159. } else {
  160. header = `<th id="unit">測量值 單位:</th>`;
  161. }
  162. $("#tabData").empty();
  163. $("#tabData").append(`<table id="dataTable" class="display">
  164. <thead>
  165. <tr id="dataTableCol">
  166. <th>測量日期</th>
  167. ${header}
  168. </tr>
  169. </thead>
  170. <tbody id="table">
  171. </tbody>
  172. </table>`);
  173. dataTable = $('#dataTable').DataTable({
  174. bProcessing: true,
  175. responsive: true,
  176. "searching": true,
  177. "pageLength": 10,
  178. "language": {
  179. "processing": "處理中...",
  180. "loadingRecords": "載入中...",
  181. "lengthMenu": "顯示 _MENU_ 項結果",
  182. "zeroRecords": "沒有符合的結果或是沒有資料",
  183. "info": "顯示第 _START_ 至 _END_ 項結果,共 _TOTAL_ 項",
  184. "infoEmpty": "顯示第 0 至 0 項結果,共 0 項",
  185. "infoFiltered": "(從 _MAX_ 項結果中過濾)",
  186. "infoPostFix": "",
  187. "search": "",
  188. "paginate": {
  189. "first": '<i class="fa fa-step-backward"></i>',
  190. "previous": '<i class="fa fa-backward"></i>',
  191. "next": '<i class="fa fa-forward"></i>',
  192. "last": '<i class="fa fa-step-forward"></i>'
  193. },
  194. "aria": {
  195. "sortAscending": ": 升冪排列",
  196. "sortDescending": ": 降冪排列"
  197. }
  198. },
  199. });
  200. if (dataTable != null) {
  201. dataTable.clear();
  202. dataTable.draw();
  203. }
  204. if (monitor == '') {
  205. monitor = '查無監測儀器或';
  206. }
  207. $("#tableName").text(monitor + "資料表");
  208. $("#unit").text(`測量值(${unit})`);
  209. table.forEach(function (data) {
  210. dataTable.row.add(data)
  211. dataTable.draw();
  212. });
  213. }).error(function (error) {
  214. console.log(error);
  215. }).complete(function (e) {
  216. });
  217. }
  218. //建立pie chart
  219. function getCheckValue() {
  220. var dataArray = [];
  221. var option = window.localStorage.getItem("option");
  222. $.ajax({
  223. url: "/BIM-Monitor/script/php/get_abnormal.php",
  224. data: {
  225. type: option
  226. },
  227. async: false,
  228. contentType: "application/json",
  229. dataType: "json"
  230. })
  231. .success(function (response) {
  232. dataArray = response['wellTBA'];
  233. var normal_count = response['normal_count'];
  234. var abnormal_count = response['abnormal_count'];
  235. sparklinePieData = [normal_count, abnormal_count];
  236. var all = normal_count + abnormal_count;
  237. var percent = abnormal_count / all;
  238. var url = "/BIM-Monitor/assets/javascripts/ui-elements/pie.charts.js";
  239. var script = document.createElement('script');
  240. script.setAttribute('type', 'text/javascript');
  241. script.setAttribute('src', url);
  242. document.getElementsByTagName('head')[0].appendChild(script);
  243. document.getElementById("percent").innerHTML = Math.round(percent * 100) + "%";
  244. })
  245. .error(function () {
  246. console.log("錯誤統計未正常獲取");
  247. })
  248. .complete(function () {});
  249. return dataArray;
  250. }
  251. //建立左側監測儀器(異常數量/總數量)
  252. function GetCheckAllAJAXValue() {
  253. for (i = 0; i < sites["r03"].length; i++) {
  254. var option = sites["r03"][i].code;
  255. $.ajax({
  256. url: "/BIM-Monitor/script/php/get_abnormal.php",
  257. data: {
  258. type: option
  259. },
  260. async: false,
  261. contentType: "application/json",
  262. dataType: "json"
  263. })
  264. .success(function (response) {
  265. var normal_count = response['normal_count'];
  266. var abnormal_count = response['abnormal_count'];
  267. var all_count = 0 + normal_count + abnormal_count;
  268. $(`.${option}`).html("(<font color='#FF0000'>" + abnormal_count + "</font>/" + all_count + ")");
  269. })
  270. .error(function () {
  271. console.log("錯誤統計未正常獲取");
  272. })
  273. .complete(function () {});
  274. }
  275. }
  276. function CheckError() {
  277. var checkBox = document.getElementById("errorCheck");
  278. if (checkBox.checked == true) {
  279. isAll = false;
  280. } else {
  281. isAll = true;
  282. }
  283. SetList();
  284. }
  285. //Display chart
  286. function getChart(monitor) {
  287. type = monitor.split("-")[0];
  288. $.ajax({
  289. url: "/BIM-Monitor/script/php/get_monitor_chart.php",
  290. type: "GET",
  291. async: false,
  292. contentType: "application/json",
  293. dataType: "json",
  294. data: {
  295. monitor: monitor
  296. }
  297. }).done(function (response) {
  298. charts = response.chart;
  299. label = response.label;
  300. unit = response.unit;
  301. displayChart(monitor, label, unit, charts);
  302. }).error(function (error) {
  303. console.log(error);
  304. });
  305. }
  306. function displayChart(monitor, label, unit, charts) {
  307. var ctx = document.getElementById("myChart").getContext("2d");
  308. var date = [];
  309. var data = [];
  310. var warning1 = [];
  311. var action1 = [];
  312. var warning2 = [];
  313. var action2 = [];
  314. var type = monitor.split("-")[0];
  315. var SIS_data = {};
  316. if (window.chart != undefined) {
  317. window.chart.destroy();
  318. }
  319. if (type == "SIS" || type == "SID") {
  320. SIS_data["depth"] = [];
  321. SIS_data["warning1"] = [];
  322. SIS_data["warning2"] = [];
  323. SIS_data["action1"] = [];
  324. SIS_data["action2"] = [];
  325. SIS_data["data"] = {};
  326. charts.forEach(element => {
  327. var sis_date = element["Date"];
  328. var sis_warning = element["WarningValue"].split("±")[1];
  329. var sis_action = element["ActionValue"].split("±")[1];
  330. delete element["Date"];
  331. delete element["WarningValue"];
  332. delete element["ActionValue"];
  333. var sis_data_length = Object.keys(element).length;
  334. SIS_data["data"][sis_date] = [];
  335. for (i = 1; i <= sis_data_length; i++) {
  336. SIS_data["data"][sis_date].push(element["" + i * 0.5]);
  337. if (SIS_data["depth"].length < sis_data_length) {
  338. SIS_data["depth"].push(i * 0.5);
  339. SIS_data["warning1"].push(+sis_warning);
  340. SIS_data["warning2"].push(-sis_warning);
  341. SIS_data["action1"].push(+sis_action);
  342. SIS_data["action2"].push(-sis_action);
  343. }
  344. }
  345. });
  346. var datasets = [{
  347. label: "警戒值+",
  348. data: SIS_data["warning1"],
  349. borderColor: "#FF0000",
  350. borderDash: [5, 5],
  351. pointRadius: 0
  352. }, {
  353. label: "警戒值-",
  354. data: SIS_data["warning2"],
  355. borderColor: "#FF0000",
  356. borderDash: [5, 5],
  357. pointRadius: 0
  358. }, {
  359. label: "行動值+",
  360. data: SIS_data["action1"],
  361. borderColor: "#FF0000",
  362. pointRadius: 0
  363. }, {
  364. label: "行動值-",
  365. data: SIS_data["action2"],
  366. borderColor: "#FF0000",
  367. pointRadius: 0
  368. }];
  369. Object.keys(SIS_data["data"]).forEach(element => {
  370. datasets.push({
  371. label: element,
  372. data: SIS_data["data"][element],
  373. borderColor: randomColorBlue(),
  374. pointRadius: 0
  375. });
  376. });
  377. window.chart = new Chart(ctx, {
  378. //Line chart
  379. type: 'line',
  380. data: {
  381. labels: SIS_data["depth"],
  382. datasets: datasets
  383. },
  384. options: {
  385. spanGaps: true,
  386. maintainAspectRatio: false,
  387. responsive: true,
  388. indexAxis: 'y',
  389. scales: {
  390. y: {
  391. title: {
  392. display: true,
  393. text: "觀測深度(m)"
  394. }
  395. },
  396. x: {
  397. title: {
  398. display: true,
  399. text: label + '(' + unit + ')'
  400. }
  401. }
  402. }
  403. }
  404. });
  405. } else {
  406. charts.forEach(function (chart) {
  407. Object.keys(chart).forEach(function (key) {
  408. if (key == "Date") {
  409. date.push(chart[key]);
  410. } else if (key.includes("WarningValue")) {
  411. let value = 0;
  412. if (chart[key].includes("±")) {
  413. value = chart[key].split("±")[1];
  414. warning1.push(+value);
  415. warning2.push(-value);
  416. }
  417. if (key.includes("High")) {
  418. warning1.push(chart[key]);
  419. } else if (key.includes("Low")) {
  420. warning2.push(chart[key]);
  421. }
  422. } else if (key.includes("ActionValue")) {
  423. let value = 0;
  424. if (chart[key].includes("±")) {
  425. value = chart[key].split("±")[1];
  426. action1.push(+value);
  427. action2.push(-value);
  428. }
  429. if (key.includes("High")) {
  430. action1.push(chart[key]);
  431. } else if (key.includes("Low")) {
  432. action2.push(chart[key]);
  433. }
  434. } else {
  435. data.push(chart[key]);
  436. }
  437. });
  438. })
  439. var datasets = [{
  440. label: "警戒值+",
  441. data: warning1,
  442. borderColor: "#FF0000",
  443. borderDash: [5, 5],
  444. pointRadius: 0
  445. }, {
  446. label: "警戒值-",
  447. data: warning2,
  448. borderColor: "#FF0000",
  449. borderDash: [5, 5],
  450. pointRadius: 0
  451. }, {
  452. label: "行動值+",
  453. data: action1,
  454. borderColor: "#FF0000",
  455. pointRadius: 0
  456. }, {
  457. label: "行動值-",
  458. data: action2,
  459. borderColor: "#FF0000",
  460. pointRadius: 0
  461. }];
  462. if (type == "TI") {
  463. var data1 = [];
  464. var data2 = [];
  465. for (i = 0; i < data.length; i += 2) {
  466. data1.push(data[i]);
  467. data2.push(data[i + 1]);
  468. }
  469. datasets.push({
  470. label: monitor + "(1-3)",
  471. data: data1,
  472. borderColor: "#3e95cd"
  473. });
  474. datasets.push({
  475. label: monitor + "(2-4)",
  476. data: data2,
  477. borderColor: "#000000"
  478. });
  479. } else {
  480. datasets.push({
  481. label: monitor,
  482. data: data,
  483. borderColor: "#3e95cd"
  484. });
  485. }
  486. window.chart = new Chart(ctx, {
  487. //Line chart
  488. type: 'line',
  489. data: {
  490. //X-axis displays the date
  491. labels: date,
  492. datasets: datasets
  493. },
  494. options: {
  495. spanGaps: true,
  496. maintainAspectRatio: false,
  497. responsive: true,
  498. scales: {
  499. x: {
  500. title: {
  501. display: true,
  502. text: "日期"
  503. }
  504. },
  505. y: {
  506. title: {
  507. display: true,
  508. text: label + '(' + unit + ')'
  509. }
  510. }
  511. }
  512. }
  513. });
  514. }
  515. }
  516. function ResetCamera() {
  517. if (isLoad) {
  518. myGameInstance.SendMessage("MainCamera", "ResetCamera");
  519. }
  520. }
  521. //選擇觀測儀器
  522. function SetList() {
  523. var wellNameListsLocal = "";
  524. var isError = false;
  525. var monitorSelect = document.getElementById("monitorSelect");
  526. var monitorButton = document.getElementById("monitorButton");
  527. while (monitorSelect.hasChildNodes()) {
  528. monitorSelect.removeChild(monitorSelect.firstChild);
  529. }
  530. while (monitorButton.hasChildNodes()) {
  531. monitorButton.removeChild(monitorButton.firstChild);
  532. }
  533. monitorButton.innerHTML = "綠色為正常|紅色為異常<br>";
  534. GetCheckAllAJAXValue();
  535. for (i = 0; i < wellNameLists.length; i++) {
  536. wellNameListsLocal = wellNameLists[i];
  537. var option = document.createElement("li");
  538. var createA = document.createElement("a");
  539. var createAText = document.createTextNode(wellNameListsLocal);
  540. var createButton = document.createElement("button");
  541. var createButtonText = document.createTextNode(wellNameListsLocal);
  542. createButton.setAttribute('class', "mb-xs mt-xs mr-xs modal-with-zoom-anim btn btn-success modal-dismiss");
  543. for (j = 0; j < wellTBA.length; j++) {
  544. if (wellNameLists[i] == (wellTBA[j])) {
  545. createA.style = "color:red;";
  546. createButton.setAttribute('class', "mb-xs mt-xs mr-xs btn btn-danger modal-dismiss");
  547. isError = true;
  548. }
  549. }
  550. createA.setAttribute('onclick', "showWell('" + wellNameLists[i] + "',true)");
  551. createButton.setAttribute('onclick', "showWell('" + wellNameLists[i] + "',true)");
  552. createButton.setAttribute('type', "button");
  553. if (isAll || (!isAll && isError)) {
  554. createA.appendChild(createAText);
  555. createButton.appendChild(createButtonText);
  556. option.appendChild(createA);
  557. monitorSelect.appendChild(option);
  558. monitorButton.appendChild(createButton);
  559. }
  560. isError = false;
  561. }
  562. }
  563. function UpdateGPS(GPS) {
  564. if (isLoad) {
  565. myGameInstance.SendMessage("PlayerArmature", "UpdateGPS", GPS);
  566. }
  567. }
  568. function randomColorBlue() {
  569. var mid = Math.floor(Math.random() * 255);
  570. return "#30" + mid.toString(16) + "ff";
  571. }