r03.js 21 KB

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