d40c8d0312035bf8af0a6eb1a046ba5c22317766.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. BemsWebApplication.ViewIndoorTH = function (params, viewInfo) {
  2. "use strict";
  3. var BUILDING_DEPTH = 1,
  4. FLOOR_DEPTH = 2,
  5. ZONE_DEPTH = 3;
  6. var CODE = $Code;
  7. var visiblePopup = ko.observable(false);
  8. //var timer;
  9. var initialized = false,
  10. dataSource;
  11. var buildingDataSource = BemsWebApplication.db.createDataSource('CmBuilding', [('SiteId', '=', BWA.UserInfo.SiteId())]),
  12. floorDataSource = BemsWebApplication.db.createDataSource('CmFloor'),
  13. buildings = ko.observableArray(),
  14. floorsForSearch = ko.observableArray(),
  15. buildingIdForSearch = ko.observable(),
  16. buildingNameForSearch = ko.observable(),
  17. floorIdForSearch = ko.observable(),
  18. floorNameForSearch = ko.observable(),
  19. deferredForSearch = new $.Deferred(),
  20. deferredForImageLoading = new $.Deferred();
  21. var deferredForLoadData = new $.Deferred();
  22. var locationEnableForSearch = ko.observable(true);
  23. var imageArray_prev = new Array();
  24. var BemsFloorStatusDataSource = BemsWebApplication.db.createDataSource('BemsFloorStatus');
  25. var BemsFloorPlanDataSource = BemsWebApplication.db.createDataSource('BemsFloorPlan');
  26. var imagefileUrl = ko.observable();
  27. function handleSearchViewInitializeUpdate() {
  28. //alert('handleSearchViewInitializeUpdate');
  29. $('#locationTreeViewInSearchView8').cwTreeView({
  30. width: '40%',
  31. height: '100%',
  32. maxDepth: 2,
  33. onClickTreeItem: handleClickTreeItem,
  34. delegateDataSource: BWA.DataUtil.getLocationDataSourceDelegateForTreeView(buildingDataSource, floorDataSource)
  35. });
  36. }
  37. function handleClickTreeItem(element, data) {
  38. var eq = BWA.DataUtil.constructEqualFilter;
  39. var depth = data.depth;
  40. //data = data.data;
  41. switch (depth) {
  42. case BUILDING_DEPTH: // building
  43. data = data.data;
  44. (data.BuildingId());
  45. buildingNameForSearch(data.Name());
  46. floorIdForSearch(null);
  47. floorNameForSearch('');
  48. break;
  49. case FLOOR_DEPTH: // floor
  50. buildingNameForSearch(data.parentData.data.Name());
  51. //alert(buildingIdForSearch());
  52. data = data.data;
  53. buildingIdForSearch(data.BuildingId());
  54. floorIdForSearch(data.FloorId());
  55. floorNameForSearch(data.Name());
  56. break;
  57. }
  58. }
  59. function handleViewShown() {
  60. if (initialized === false) {
  61. floorNameForSearch('');
  62. initialized = true;
  63. }
  64. /*
  65. timer = setInterval(function () {
  66. var now = new Date();
  67. if ((now.getMinutes() % 15 == 0) && (now.getSeconds() == 58)) {
  68. handleSearchInSearchView();
  69. }
  70. }, 1000);*/
  71. }
  72. function handleViewHidden() {
  73. $SearchView.visibleObservable(false); // hcLee 2015 03 23
  74. visiblePopup(false);
  75. // clearInterval(timer); // hcLee 2015 06 12
  76. }
  77. function handleViewShowing() {
  78. //$.when(buildingDataSource.load(), floorDataSource.load()).done(function (dbBuildings, dbFloors) {
  79. // deferredForSearch.resolve();
  80. // dbBuildings = dbBuildings[0];
  81. // dbFloors = dbFloors[0];
  82. // if (_.isEmpty(dbBuildings) === false) {
  83. // buildingIdForSearch(dbBuildings[0].BuildingId());
  84. // buildingNameForSearch(dbBuildings[0].Name());
  85. // floorIdForSearch(dbFloors[0].FloorId());
  86. // floorNameForSearch(dbFloors[0].Name());
  87. // handleSearchInSearchView();
  88. // }
  89. //});
  90. }
  91. function refreshList() {
  92. }
  93. var searchViewOptions = {
  94. searchViewItems: [
  95. {}
  96. ],
  97. promiseDataInSearchView: deferredForSearch.promise()
  98. };
  99. var mapType = {
  100. coordinate: { font: "fill: black; font-weight: bold; font-size: 15px;", fixed: 1 },
  101. temperature: { font: "fill: red; font-weight: bold; font-size: 20px;", fixed: 1 },
  102. humidity: { font: "fill: blue; font-weight: bold; font-size: 20px;", fixed: 0 },
  103. carboneDioxide: { font: "fill: green ; font-weight: bold; font-size: 20px;", fixed: 1 },
  104. concentration: { font: "fill: darkgreen; font-weight: bold; font-size: 20px;", fixed: 1 },
  105. ozone: { font: "fill: purple ; font-weight: bold; font-size: 20px;", fixed: 2 },
  106. dust: { font: "fill: orangered ; font-weight: bold; font-size: 20px;", fixed: 0 }
  107. };
  108. function setTextInSVG(values) {
  109. $('#imageContext').find('text').remove();
  110. // image 기존 이모티콘 지우기
  111. for (var i = 0; i < imageArray_prev.length; i++) {
  112. imageArray_prev[i].parentNode.removeChild(imageArray_prev[i]);
  113. }
  114. imageArray_prev = new Array();
  115. var floorTextLocationInfo = new Array();
  116. var cnt = 0;
  117. BemsFloorStatusDataSource.filter([
  118. ["SiteId", "=", BWA.UserInfo.SiteId()],
  119. "and",
  120. ["FloorId", "=", floorIdForSearch()]
  121. ]);
  122. BemsFloorStatusDataSource.load()
  123. .done(function (result) {
  124. for (var i = 0; i < result.length; i++) {
  125. floorTextLocationInfo[i] = {
  126. "floorId": 0,
  127. "x": 0,
  128. "y": 0,
  129. "type": "",
  130. "property": 0
  131. };
  132. floorTextLocationInfo[i].floorId = result[i].FloorId();
  133. floorTextLocationInfo[i].x = result[i].Xvalue();
  134. floorTextLocationInfo[i].y = result[i].Yvalue();
  135. floorTextLocationInfo[i].property = result[i].PropertyId();
  136. if (result[i].ServiceTypeId() == 100)
  137. floorTextLocationInfo[i].type = 'temperature';
  138. else if (result[i].ServiceTypeId() == 110)
  139. floorTextLocationInfo[i].type = 'humidity';
  140. else if (result[i].ServiceTypeId() == 130)
  141. floorTextLocationInfo[i].type = 'carboneDioxide';
  142. else if (result[i].ServiceTypeId() == 140)
  143. floorTextLocationInfo[i].type = 'concentration';
  144. else if (result[i].ServiceTypeId() == 150)
  145. floorTextLocationInfo[i].type = 'ozone';
  146. else if (result[i].ServiceTypeId() == 160)
  147. floorTextLocationInfo[i].type = 'dust';
  148. cnt++;
  149. }
  150. for (var i = 0; i < cnt; i++) {
  151. var locationInfo = floorTextLocationInfo[i];
  152. var type = mapType[locationInfo.type];
  153. var value = values[i].toFixed(type.fixed);
  154. if (value != -9999) addText(locationInfo.x, locationInfo.y, value, type.font, locationInfo.type);
  155. }
  156. });
  157. }
  158. function handleSearchInSearchView() {
  159. //$('#st').text('층계별 도면' + " : " + buildingNameForSearch() + " - " +
  160. if (floorIdForSearch() == null) {
  161. utils.toast.show('층을 선택해야 합니다.');
  162. return;
  163. }
  164. BemsFloorPlanDataSource.filter([
  165. ["SiteId", "=", BWA.UserInfo.SiteId()]
  166. ]);
  167. BemsFloorPlanDataSource.load()
  168. .done(function (result) {
  169. var idx = 0;
  170. for (var i = 0; i < result.length; i++) {
  171. if (result[i].FloorId() == floorIdForSearch() && result[i].ImageFileId() != null) {
  172. idx++;
  173. var imageFileId = result[i].ImageFileId();
  174. }
  175. }
  176. $('#st').text(buildingNameForSearch() + " - " +
  177. floorNameForSearch());
  178. if (idx == 0) {
  179. imagefileUrl('');
  180. loadImage(imagefileUrl());
  181. utils.toast.show('해당 층의 평면도가 없습니다.','error');
  182. }
  183. else {
  184. imagefileUrl('{0}/CmFile(SiteId={1},FileId={2})/$value'.formati(BWA.db._url, BWA.UserInfo.SiteId(), imageFileId));
  185. loadImage(imagefileUrl());
  186. }
  187. var promises = [];
  188. promises.push(BemsWebApplication.api.get('BemsMonitoringPointHistory/FloorEachPointValue', {
  189. SiteId: BWA.UserInfo.SiteId(),
  190. FloorId: floorIdForSearch(),
  191. }));
  192. $.when.apply(this, promises).done(function (values) {
  193. setTextInSVG(values);
  194. }).fail(function (error) {
  195. visiblePopup(false);
  196. });
  197. });
  198. }
  199. function loadImage(imageUrl) {
  200. $('#imageContext').find('text').remove();
  201. //var imageUrl = url;//'/Images/Blueprint/' + viewModel.dataModel.FloorId() + '.jpg';
  202. var image = document.getElementById("image");
  203. var newImage = new Image();
  204. newImage.onload = function () {
  205. var imageSize = getImageSize(newImage.width, newImage.height);
  206. $('#imageContext').css('width', imageSize.width).css('height', imageSize.height);
  207. $('#image').attr('width', imageSize.width).attr('height', imageSize.height); // 2016 05 30 추가, IE 문제해결, 종화
  208. }
  209. newImage.src = imageUrl;
  210. image.setAttributeNS('http://www.w3.org/1999/xlink', 'href', imageUrl);
  211. }
  212. function getImageSize(width, height) {
  213. var viewIndoor = $('#viewIndoor');
  214. var maxWidth = parseFloat(viewIndoor.css('width')), maxHeight = parseFloat(viewIndoor.css('height'));
  215. var widthRatio = width / maxWidth;
  216. var heightRatio = height / maxHeight;
  217. var maxRatio = max(widthRatio, heightRatio);
  218. if (maxRatio > 1) {
  219. return { width: width / maxRatio, height: height / maxRatio }
  220. }
  221. return { width: width, height: height }
  222. }
  223. function max(x, y) {
  224. if (x > y) return x;
  225. return y;
  226. }
  227. function addText(x, y, text, style, type) {
  228. if (type == 'temperature')
  229. var imageUrl = '/Images/Blueprint/object_1.png';
  230. else if (type == 'concentration')
  231. var imageUrl = '/Images/Blueprint/object_2.png';
  232. else if (type == 'carboneDioxide')
  233. var imageUrl = '/Images/Blueprint/object_3.png';
  234. else if (type == 'humidity')
  235. var imageUrl = '/Images/Blueprint/object_4.png';
  236. else if (type == 'dust')
  237. var imageUrl = '/Images/Blueprint/object_5.png';
  238. else if (type == 'ozone')
  239. var imageUrl = '/Images/Blueprint/object_6.png';
  240. var image = document.createElementNS("http://www.w3.org/2000/svg", "image");
  241. image.setAttributeNS(null, 'x', x);
  242. image.setAttributeNS(null, 'y', y);
  243. image.setAttributeNS('http://www.w3.org/1999/xlink', 'href', imageUrl);
  244. $('#imageContext').append(image);
  245. imageArray_prev.push(image);
  246. //************************
  247. var image = document.createElementNS("http://www.w3.org/2000/svg", "text");
  248. image.textContent = text;
  249. image.setAttributeNS(null, 'x', x + 45);
  250. image.setAttributeNS(null, 'y', y + 23);
  251. if (style) {
  252. image.setAttributeNS(null, 'style', style);
  253. }
  254. $('#imageContext').append(image);
  255. }
  256. function handleMenuItemClick(e) {
  257. var chartIntance = chart();
  258. var chartOptions = chartIntance._options;
  259. var series = chartIntance.getAllSeries();
  260. var table = [];
  261. var columns = [chartOptions.argumentAxis.title];
  262. var values = {};
  263. _.each(chartOptions.valueAxis, function (a) {
  264. columns.push(a.title.text);
  265. });
  266. _.each(series, function (s, i) {
  267. _.each(s.pointsByArgument, function (point, argument) {
  268. var array = values[argument];
  269. if (_.isArray(array) === false) {
  270. array = values[argument] = [];
  271. }
  272. array[i] = point.value;
  273. });
  274. });
  275. var csvRows = [columns.join(',')];
  276. _.each(values, function (values, argument) {
  277. csvRows.push([$G.dateTime(moment(Number(argument))), values.join(',')].join(','));
  278. });
  279. var csvContent = csvRows.join("\r\n");
  280. var agent = navigator.userAgent.toLowerCase();
  281. if (window.navigator.msSaveOrOpenBlob) {
  282. var fileData = ['\uFEFF' + csvContent];
  283. var blobObject = new Blob(fileData);
  284. window.navigator.msSaveOrOpenBlob(blobObject, 'export.csv');
  285. }
  286. else {
  287. var csvString = 'data:text/csv;utf-8,\uFEFF' + encodeURI(csvContent);
  288. var a = $('#csvExporter');
  289. a.attr({
  290. 'href': csvString,
  291. 'target': '_blank',
  292. 'download': 'export.csv'
  293. });
  294. // a.trigger('click');
  295. var aa = document.getElementById('csvExporter');
  296. aa.click();
  297. }
  298. }
  299. var viewModel = $.extend(BWA.CommonView.create(
  300. params, viewInfo, searchViewOptions, ko.observable(null), handleViewShown, null,
  301. handleSearchInSearchView, handleSearchViewInitializeUpdate), {
  302. refreshList: refreshList,
  303. viewShowing: handleViewShowing,
  304. viewHidden: handleViewHidden, // hcLee 2015 03 23
  305. //viewDisposing: handleViewHidden, // hcLee 2015 03 23
  306. locationEnableForSearch: locationEnableForSearch,
  307. handleMenuItemClick: handleMenuItemClick,
  308. menuItems: [{
  309. text: '엑셀(csv) 내보내기'
  310. }]
  311. });
  312. $HourGlassPopup(viewModel, visiblePopup);
  313. return viewModel;
  314. };