r03.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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. console.log("monitor: " + monitor);
  288. type = monitor.split("-")[0];
  289. $.ajax({
  290. url: "/BIM-Monitor/script/php/get_monitor_chart.php",
  291. type: "GET",
  292. async: false,
  293. contentType: "application/json",
  294. dataType: "json",
  295. data: {
  296. monitor: monitor
  297. }
  298. }).done(function (response) {
  299. charts = response.chart;
  300. label = response.label;
  301. unit = response.unit;
  302. displayChart(monitor, label, unit, charts);
  303. }).error(function (error) {
  304. console.log(error);
  305. });
  306. }
  307. function displayChart(monitor, label, unit, charts) {
  308. var ctx = document.getElementById("myChart").getContext("2d");
  309. var date = [];
  310. var data = [];
  311. var warning1 = [];
  312. var action1 = [];
  313. var warning2 = [];
  314. var action2 = [];
  315. var type = monitor.split("-")[0];
  316. var SIS_data = {};
  317. if (window.chart != undefined) {
  318. window.chart.destroy();
  319. }
  320. if (type == "SIS" || type == "SID") {
  321. SIS_data["depth"] = [];
  322. SIS_data["warning1"] = [];
  323. SIS_data["warning2"] = [];
  324. SIS_data["action1"] = [];
  325. SIS_data["action2"] = [];
  326. SIS_data["data"] = {};
  327. charts.forEach(element => {
  328. var sis_date = element["Date"];
  329. var sis_warning = element["WarningValue"].split("±")[1];
  330. var sis_action = element["ActionValue"].split("±")[1];
  331. delete element["Date"];
  332. delete element["WarningValue"];
  333. delete element["ActionValue"];
  334. var sis_data_length = Object.keys(element).length;
  335. SIS_data["data"][sis_date] = [];
  336. for (i = 1; i <= sis_data_length; i++) {
  337. SIS_data["data"][sis_date].push(element["" + i * 0.5]);
  338. if (SIS_data["depth"].length < sis_data_length) {
  339. SIS_data["depth"].push(i * 0.5);
  340. SIS_data["warning1"].push(+sis_warning);
  341. SIS_data["warning2"].push(-sis_warning);
  342. SIS_data["action1"].push(+sis_action);
  343. SIS_data["action2"].push(-sis_action);
  344. }
  345. }
  346. });
  347. var datasets = [{
  348. label: "警戒值+",
  349. data: SIS_data["warning1"],
  350. borderColor: "#FF0000",
  351. borderDash: [5, 5],
  352. pointRadius: 0
  353. }, {
  354. label: "警戒值-",
  355. data: SIS_data["warning2"],
  356. borderColor: "#FF0000",
  357. borderDash: [5, 5],
  358. pointRadius: 0
  359. }, {
  360. label: "行動值+",
  361. data: SIS_data["action1"],
  362. borderColor: "#FF0000",
  363. pointRadius: 0
  364. }, {
  365. label: "行動值-",
  366. data: SIS_data["action2"],
  367. borderColor: "#FF0000",
  368. pointRadius: 0
  369. }];
  370. Object.keys(SIS_data["data"]).forEach(element => {
  371. datasets.push({
  372. label: element,
  373. data: SIS_data["data"][element],
  374. borderColor: randomColorBlue(),
  375. pointRadius: 0
  376. });
  377. });
  378. window.chart = new Chart(ctx, {
  379. //Line chart
  380. type: 'line',
  381. data: {
  382. labels: SIS_data["depth"],
  383. datasets: datasets
  384. },
  385. options: {
  386. spanGaps: true,
  387. maintainAspectRatio: false,
  388. responsive: true,
  389. indexAxis: 'y',
  390. scales: {
  391. y: {
  392. title: {
  393. display: true,
  394. text: "觀測深度(m)"
  395. }
  396. },
  397. x: {
  398. title: {
  399. display: true,
  400. text: label + '(' + unit + ')'
  401. }
  402. }
  403. }
  404. }
  405. });
  406. } else {
  407. charts.forEach(function (chart) {
  408. Object.keys(chart).forEach(function (key) {
  409. if (key == "Date") {
  410. date.push(chart[key]);
  411. } else if (key.includes("WarningValue")) {
  412. let value = 0;
  413. if (chart[key].includes("±")) {
  414. value = chart[key].split("±")[1];
  415. warning1.push(+value);
  416. warning2.push(-value);
  417. }
  418. if (key.includes("High")) {
  419. warning1.push(chart[key]);
  420. } else if (key.includes("Low")) {
  421. warning2.push(chart[key]);
  422. }
  423. } else if (key.includes("ActionValue")) {
  424. let value = 0;
  425. if (chart[key].includes("±")) {
  426. value = chart[key].split("±")[1];
  427. action1.push(+value);
  428. action2.push(-value);
  429. }
  430. if (key.includes("High")) {
  431. action1.push(chart[key]);
  432. } else if (key.includes("Low")) {
  433. action2.push(chart[key]);
  434. }
  435. } else {
  436. data.push(chart[key]);
  437. }
  438. });
  439. })
  440. var datasets = [{
  441. label: "警戒值+",
  442. data: warning1,
  443. borderColor: "#FF0000",
  444. borderDash: [5, 5],
  445. pointRadius: 0
  446. }, {
  447. label: "警戒值-",
  448. data: warning2,
  449. borderColor: "#FF0000",
  450. borderDash: [5, 5],
  451. pointRadius: 0
  452. }, {
  453. label: "行動值+",
  454. data: action1,
  455. borderColor: "#FF0000",
  456. pointRadius: 0
  457. }, {
  458. label: "行動值-",
  459. data: action2,
  460. borderColor: "#FF0000",
  461. pointRadius: 0
  462. }];
  463. if (type == "TI") {
  464. var data1 = [];
  465. var data2 = [];
  466. for (i = 0; i < data.length; i += 2) {
  467. data1.push(data[i]);
  468. data2.push(data[i + 1]);
  469. }
  470. datasets.push({
  471. label: monitor + "(1-3)",
  472. data: data1,
  473. borderColor: "#3e95cd"
  474. });
  475. datasets.push({
  476. label: monitor + "(2-4)",
  477. data: data2,
  478. borderColor: "#000000"
  479. });
  480. } else {
  481. datasets.push({
  482. label: monitor,
  483. data: data,
  484. borderColor: "#3e95cd"
  485. });
  486. }
  487. window.chart = new Chart(ctx, {
  488. //Line chart
  489. type: 'line',
  490. data: {
  491. //X-axis displays the date
  492. labels: date,
  493. datasets: datasets
  494. },
  495. options: {
  496. spanGaps: true,
  497. maintainAspectRatio: false,
  498. responsive: true,
  499. scales: {
  500. x: {
  501. title: {
  502. display: true,
  503. text: "日期"
  504. }
  505. },
  506. y: {
  507. title: {
  508. display: true,
  509. text: label + '(' + unit + ')'
  510. }
  511. }
  512. }
  513. }
  514. });
  515. }
  516. }
  517. function ResetCamera() {
  518. if (isLoad) {
  519. myGameInstance.SendMessage("MainCamera", "ResetCamera");
  520. }
  521. }
  522. //選擇觀測儀器
  523. function SetList() {
  524. var wellNameListsLocal = "";
  525. var isError = false;
  526. var monitorSelect = document.getElementById("monitorSelect");
  527. var monitorButton = document.getElementById("monitorButton");
  528. while (monitorSelect.hasChildNodes()) {
  529. monitorSelect.removeChild(monitorSelect.firstChild);
  530. }
  531. while (monitorButton.hasChildNodes()) {
  532. monitorButton.removeChild(monitorButton.firstChild);
  533. }
  534. monitorButton.innerHTML = "綠色為正常|紅色為異常<br>";
  535. GetCheckAllAJAXValue();
  536. for (i = 0; i < wellNameLists.length; i++) {
  537. wellNameListsLocal = wellNameLists[i];
  538. var option = document.createElement("li");
  539. var createA = document.createElement("a");
  540. var createAText = document.createTextNode(wellNameListsLocal);
  541. var createButton = document.createElement("button");
  542. var createButtonText = document.createTextNode(wellNameListsLocal);
  543. createButton.setAttribute('class', "mb-xs mt-xs mr-xs modal-with-zoom-anim btn btn-success modal-dismiss");
  544. for (j = 0; j < wellTBA.length; j++) {
  545. if (wellNameLists[i] == (wellTBA[j])) {
  546. createA.style = "color:red;";
  547. createButton.setAttribute('class', "mb-xs mt-xs mr-xs btn btn-danger modal-dismiss");
  548. isError = true;
  549. }
  550. }
  551. createA.setAttribute('onclick', "showWell('" + wellNameLists[i] + "',true)");
  552. createButton.setAttribute('onclick', "showWell('" + wellNameLists[i] + "',true)");
  553. createButton.setAttribute('type', "button");
  554. if (isAll || (!isAll && isError)) {
  555. createA.appendChild(createAText);
  556. createButton.appendChild(createButtonText);
  557. option.appendChild(createA);
  558. monitorSelect.appendChild(option);
  559. monitorButton.appendChild(createButton);
  560. }
  561. isError = false;
  562. }
  563. }
  564. function UpdateGPS(GPS) {
  565. if (isLoad) {
  566. myGameInstance.SendMessage("PlayerArmature", "UpdateGPS", GPS);
  567. }
  568. }
  569. function randomColorBlue() {
  570. var mid = Math.floor(Math.random() * 255);
  571. return "#30" + mid.toString(16) + "ff";
  572. }