r03.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. "searching": true,
  149. "pageLength": 10,
  150. "language": {
  151. "processing": "處理中...",
  152. "loadingRecords": "載入中...",
  153. "lengthMenu": "顯示 _MENU_ 項結果",
  154. "zeroRecords": "沒有符合的結果或是沒有資料",
  155. "info": "顯示第 _START_ 至 _END_ 項結果,共 _TOTAL_ 項",
  156. "infoEmpty": "顯示第 0 至 0 項結果,共 0 項",
  157. "infoFiltered": "(從 _MAX_ 項結果中過濾)",
  158. "infoPostFix": "",
  159. "search": "",
  160. "paginate": {
  161. "first": '<i class="fa fa-step-backward"></i>',
  162. "previous": '<i class="fa fa-backward"></i>',
  163. "next": '<i class="fa fa-forward"></i>',
  164. "last": '<i class="fa fa-step-forward"></i>'
  165. },
  166. "aria": {
  167. "sortAscending": ": 升冪排列",
  168. "sortDescending": ": 降冪排列"
  169. }
  170. },
  171. });
  172. if (dataTable != null) {
  173. dataTable.clear();
  174. dataTable.draw();
  175. }
  176. if (monitor == '') {
  177. monitor = '查無監測儀器或';
  178. }
  179. $("#tableName").text(monitor + "資料表");
  180. $("#unit").text(`測量值(${unit})`);
  181. table.forEach(function (data) {
  182. dataTable.row.add(data)
  183. dataTable.draw();
  184. });
  185. }).error(function (error) {
  186. console.log(error);
  187. }).complete(function (e) {
  188. });
  189. }
  190. //建立pie chart
  191. function getCheckValue() {
  192. var dataArray = [];
  193. var option = window.localStorage.getItem("option");
  194. $.ajax({
  195. url: "/BIM-Monitor/script/php/get_abnormal.php",
  196. data: {
  197. type: option
  198. },
  199. async: false,
  200. contentType: "application/json",
  201. dataType: "json"
  202. })
  203. .success(function (response) {
  204. dataArray = response['wellTBA'];
  205. var normal_count = response['normal_count'];
  206. var abnormal_count = response['abnormal_count'];
  207. sparklinePieData = [normal_count, abnormal_count];
  208. var all = normal_count + abnormal_count;
  209. var percent = abnormal_count / all;
  210. var url = "/BIM-Monitor/assets/javascripts/ui-elements/pie.charts.js";
  211. var script = document.createElement('script');
  212. script.setAttribute('type', 'text/javascript');
  213. script.setAttribute('src', url);
  214. document.getElementsByTagName('head')[0].appendChild(script);
  215. document.getElementById("percent").innerHTML = Math.round(percent * 100) + "%";
  216. })
  217. .error(function () {
  218. console.log("錯誤統計未正常獲取");
  219. })
  220. .complete(function () {});
  221. return dataArray;
  222. }
  223. //建立左側監測儀器(異常數量/總數量)
  224. function GetCheckAllAJAXValue() {
  225. for (i = 0; i < sites["r03"].length; i++) {
  226. var option = sites["r03"][i].code;
  227. $.ajax({
  228. url: "/BIM-Monitor/script/php/get_abnormal.php",
  229. data: {
  230. type: option
  231. },
  232. async: false,
  233. contentType: "application/json",
  234. dataType: "json"
  235. })
  236. .success(function (response) {
  237. var normal_count = response['normal_count'];
  238. var abnormal_count = response['abnormal_count'];
  239. var all_count = 0 + normal_count + abnormal_count;
  240. $(`.${option}`).html("(<font color='#FF0000'>" + abnormal_count + "</font>/" + all_count + ")");
  241. })
  242. .error(function () {
  243. console.log("錯誤統計未正常獲取");
  244. })
  245. .complete(function () {});
  246. }
  247. errorArray = [];
  248. var phase
  249. if (document.getElementById("phase") !== null) {
  250. phase = document.getElementById("phase").value;
  251. } else {
  252. phase = 7;
  253. }
  254. for (i = 0; i < errorTable.length; i++) {
  255. var method = errorTable[i].method;
  256. var option = errorTable[i].table_name;
  257. var table_name = errorTable[i].table_name + "data";
  258. var warning1 = errorTable[i].warning1;
  259. var warning2 = errorTable[i].warning2;
  260. var warning_Count = errorTable[i].warning_Count;
  261. $.ajax({
  262. url: "/BIM-Monitor/script/php/checkValue2.php",
  263. data: {
  264. method: method,
  265. table_name: table_name,
  266. warning1: warning1,
  267. warning2: warning2,
  268. warning_Count: warning_Count,
  269. phase: phase
  270. },
  271. async: false,
  272. contentType: "application/json",
  273. dataType: "json"
  274. })
  275. .success(function (response) {
  276. var normal_count = response['normal_count'];
  277. var abnormal_count = response['abnormal_count'];
  278. var all_count = 0 + normal_count + abnormal_count;
  279. document.getElementById(option).innerHTML = "(<font color='#FF0000'>" + abnormal_count + "</font>/" + all_count + ")";
  280. errorArray.push(abnormal_count);
  281. })
  282. .error(function () {
  283. alert("錯誤統計未正常獲取");
  284. })
  285. .complete(function () {});
  286. }
  287. }
  288. function CheckError() {
  289. var checkBox = document.getElementById("errorCheck");
  290. if (checkBox.checked == true) {
  291. isAll = false;
  292. } else {
  293. isAll = true;
  294. }
  295. SetList();
  296. }
  297. //Display chart
  298. function getChart(monitor) {
  299. type = monitor.split("-")[0];
  300. $.ajax({
  301. url: "/BIM-Monitor/script/php/get_monitor_chart.php",
  302. type: "GET",
  303. async: false,
  304. contentType: "application/json",
  305. dataType: "json",
  306. data: {
  307. monitor: monitor
  308. }
  309. }).done(function (response) {
  310. charts = response.chart;
  311. label = response.label;
  312. unit = response.unit;
  313. displayChart(monitor, label, unit, charts);
  314. }).error(function (error) {
  315. console.log(error);
  316. });
  317. }
  318. function displayChart(monitor, label, unit, charts) {
  319. var ctx = document.getElementById("myChart").getContext("2d");
  320. var date = [];
  321. var data = [];
  322. var warning1 = [];
  323. var action1 = [];
  324. var warning2 = [];
  325. var action2 = [];
  326. var type = monitor.split("-")[0];
  327. var SIS_data = {};
  328. if (window.chart != undefined) {
  329. window.chart.destroy();
  330. }
  331. if (type == "SIS" || type == "SID") {
  332. SIS_data["depth"] = [];
  333. SIS_data["warning1"] = [];
  334. SIS_data["warning2"] = [];
  335. SIS_data["action1"] = [];
  336. SIS_data["action2"] = [];
  337. SIS_data["data"] = {};
  338. charts.forEach(element => {
  339. var sis_date = element["Date"];
  340. var sis_warning = element["WarningValue"].split("±")[1];
  341. var sis_action = element["ActionValue"].split("±")[1];
  342. delete element["Date"];
  343. delete element["WarningValue"];
  344. delete element["ActionValue"];
  345. var sis_data_length = Object.keys(element).length;
  346. SIS_data["data"][sis_date] = [];
  347. for (i = 1; i <= sis_data_length; i++) {
  348. SIS_data["data"][sis_date].push(element["" + i * 0.5]);
  349. if (SIS_data["depth"].length < sis_data_length) {
  350. SIS_data["depth"].push(i * 0.5);
  351. SIS_data["warning1"].push(+sis_warning);
  352. SIS_data["warning2"].push(-sis_warning);
  353. SIS_data["action1"].push(+sis_action);
  354. SIS_data["action2"].push(-sis_action);
  355. }
  356. }
  357. });
  358. var datasets = [{
  359. label: "警戒值+",
  360. data: SIS_data["warning1"],
  361. borderColor: "#FF0000",
  362. borderDash: [5, 5],
  363. pointRadius: 0
  364. }, {
  365. label: "警戒值-",
  366. data: SIS_data["warning2"],
  367. borderColor: "#FF0000",
  368. borderDash: [5, 5],
  369. pointRadius: 0
  370. }, {
  371. label: "行動值+",
  372. data: SIS_data["action1"],
  373. borderColor: "#FF0000",
  374. pointRadius: 0
  375. }, {
  376. label: "行動值-",
  377. data: SIS_data["action2"],
  378. borderColor: "#FF0000",
  379. pointRadius: 0
  380. }];
  381. Object.keys(SIS_data["data"]).forEach(element => {
  382. datasets.push({
  383. label: element,
  384. data: SIS_data["data"][element],
  385. borderColor: randomColorBlue(),
  386. pointRadius: 0
  387. });
  388. });
  389. window.chart = new Chart(ctx, {
  390. //Line chart
  391. type: 'line',
  392. data: {
  393. labels: SIS_data["depth"],
  394. datasets: datasets
  395. },
  396. options: {
  397. plugins: {
  398. legend: {
  399. display: false
  400. },
  401. },
  402. spanGaps: true,
  403. maintainAspectRatio: false,
  404. responsive: true,
  405. indexAxis: 'y',
  406. scales: {
  407. y: {
  408. title: {
  409. display: true,
  410. text: "觀測深度(m)"
  411. }
  412. },
  413. x: {
  414. title: {
  415. display: true,
  416. text: label + '(' + unit + ')'
  417. }
  418. }
  419. }
  420. }
  421. });
  422. } else {
  423. charts.forEach(function (chart) {
  424. Object.keys(chart).forEach(function (key) {
  425. if (key == "Date") {
  426. date.push(chart[key]);
  427. } else if (key.includes("WarningValue")) {
  428. let value = 0;
  429. if (chart[key].includes("±")) {
  430. value = chart[key].split("±")[1];
  431. warning1.push(+value);
  432. warning2.push(-value);
  433. }
  434. if (key.includes("High")) {
  435. warning1.push(chart[key]);
  436. } else if (key.includes("Low")) {
  437. warning2.push(chart[key]);
  438. }
  439. } else if (key.includes("ActionValue")) {
  440. let value = 0;
  441. if (chart[key].includes("±")) {
  442. value = chart[key].split("±")[1];
  443. action1.push(+value);
  444. action2.push(-value);
  445. }
  446. if (key.includes("High")) {
  447. action1.push(chart[key]);
  448. } else if (key.includes("Low")) {
  449. action2.push(chart[key]);
  450. }
  451. } else {
  452. data.push(chart[key]);
  453. }
  454. });
  455. })
  456. var datasets = [{
  457. label: "警戒值+",
  458. data: warning1,
  459. borderColor: "#FF0000",
  460. borderDash: [5, 5],
  461. pointRadius: 0
  462. }, {
  463. label: "警戒值-",
  464. data: warning2,
  465. borderColor: "#FF0000",
  466. borderDash: [5, 5],
  467. pointRadius: 0
  468. }, {
  469. label: "行動值+",
  470. data: action1,
  471. borderColor: "#FF0000",
  472. pointRadius: 0
  473. }, {
  474. label: "行動值-",
  475. data: action2,
  476. borderColor: "#FF0000",
  477. pointRadius: 0
  478. }];
  479. if (type == "TI") {
  480. var data1 = [];
  481. var data2 = [];
  482. for (i = 0; i < data.length; i += 2) {
  483. data1.push(data[i]);
  484. data2.push(data[i + 1]);
  485. }
  486. datasets.push({
  487. label: monitor + "(1-3)",
  488. data: data1,
  489. borderColor: "#3e95cd"
  490. });
  491. datasets.push({
  492. label: monitor + "(2-4)",
  493. data: data2,
  494. borderColor: "#000000"
  495. });
  496. } else {
  497. datasets.push({
  498. label: monitor,
  499. data: data,
  500. borderColor: "#3e95cd"
  501. });
  502. }
  503. window.chart = new Chart(ctx, {
  504. //Line chart
  505. type: 'line',
  506. data: {
  507. //X-axis displays the date
  508. labels: date,
  509. datasets: datasets
  510. },
  511. options: {
  512. spanGaps: true,
  513. maintainAspectRatio: false,
  514. responsive: true,
  515. scales: {
  516. x: {
  517. title: {
  518. display: true,
  519. text: "日期"
  520. }
  521. },
  522. y: {
  523. title: {
  524. display: true,
  525. text: label + '(' + unit + ')'
  526. }
  527. }
  528. }
  529. }
  530. });
  531. }
  532. }
  533. function ResetCamera() {
  534. if (isLoad) {
  535. myGameInstance.SendMessage("MainCamera", "ResetCamera");
  536. }
  537. }
  538. //選擇觀測儀器
  539. function SetList() {
  540. var wellNameListsLocal = "";
  541. var isError = false;
  542. var monitorSelect = document.getElementById("monitorSelect");
  543. var monitorButton = document.getElementById("monitorButton");
  544. while (monitorSelect.hasChildNodes()) {
  545. monitorSelect.removeChild(monitorSelect.firstChild);
  546. }
  547. while (monitorButton.hasChildNodes()) {
  548. monitorButton.removeChild(monitorButton.firstChild);
  549. }
  550. monitorButton.innerHTML = "綠色為正常|紅色為異常<br>";
  551. GetCheckAllAJAXValue();
  552. for (i = 0; i < wellNameLists.length; i++) {
  553. wellNameListsLocal = wellNameLists[i];
  554. var option = document.createElement("li");
  555. var createA = document.createElement("a");
  556. var createAText = document.createTextNode(wellNameListsLocal);
  557. var createButton = document.createElement("button");
  558. var createButtonText = document.createTextNode(wellNameListsLocal);
  559. createButton.setAttribute('class', "mb-xs mt-xs mr-xs modal-with-zoom-anim btn btn-success modal-dismiss");
  560. for (j = 0; j < wellTBA.length; j++) {
  561. if (wellNameLists[i] == (wellTBA[j])) {
  562. createA.style = "color:red;";
  563. createButton.setAttribute('class', "mb-xs mt-xs mr-xs btn btn-danger modal-dismiss");
  564. isError = true;
  565. }
  566. }
  567. createA.setAttribute('onclick', "showWell('" + wellNameLists[i] + "',true)");
  568. createButton.setAttribute('onclick', "showWell('" + wellNameLists[i] + "',true)");
  569. createButton.setAttribute('type', "button");
  570. if (isAll || (!isAll && isError)) {
  571. createA.appendChild(createAText);
  572. createButton.appendChild(createButtonText);
  573. option.appendChild(createA);
  574. monitorSelect.appendChild(option);
  575. monitorButton.appendChild(createButton);
  576. }
  577. isError = false;
  578. }
  579. }
  580. function UpdateGPS(GPS) {
  581. if (isLoad) {
  582. myGameInstance.SendMessage("PlayerArmature", "UpdateGPS", GPS);
  583. }
  584. }
  585. function randomColorBlue() {
  586. var mid = Math.floor(Math.random() * 255);
  587. return "#30" + mid.toString(16) + "ff";
  588. }