r03.js 22 KB

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