d9a8ad887a3c0857822f858853b970eb88161eca.svn-base 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. BemsWebApplication.ServiceUsageByEnergy = function(params, viewInfo) {
  2. "use strict";
  3. var initialized = false,
  4. shouldReload = false,
  5. dataSource;
  6. var CO2 = $Code.FuelType.WATER + 1;
  7. var fuelTypesDataSource = BemsWebApplication.db.createDataSource('BemsFuelType'),
  8. //var fuelTypesDataSource = BemsWebApplication.db.createDataSource('BemsFacilityTypeId'),
  9. factorCo2DataSource = BemsWebApplication.db.createDataSource('BemsFactorCo2'),
  10. serviceTypesDataSource = BemsWebApplication.db.createDataSource('BemsServiceType'),
  11. fuelTypesForSearch = ko.observableArray(),
  12. timeTypesForSearch = ko.observableArray(),
  13. timeTypeIdForSearch = ko.observable(0),
  14. fuelTypeIdForSearch = ko.observable(),
  15. fuelTypeNameForSearch = ko.observable(),
  16. deferredForSearch = new $.Deferred();
  17. var factorCo2 = null;
  18. //var energyUsageStatusGet = new BWA.Chart.EnergyUsageStatusGet(BWA.UserInfo.SiteId()),
  19. var energyServiceGet = new BWA.Chart.PointHistoryServiceTypeGet(BWA.UserInfo.SiteId()),
  20. timeBoxForSearch = BWA.SearchView.createDateTimeBox(),
  21. dataArray = ko.observableArray();
  22. var chart = BWA.Chart.Instance('chart', 'dxChart');
  23. timeTypesForSearch($G.TimeTypes);
  24. var m = moment();
  25. dataArray([
  26. { DateTime: new Date(m.year(), m.month(), m.date(), 0), Value: 30, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
  27. { DateTime: new Date(m.year(), m.month(), m.date(), 0, 15), Value: 32 },
  28. { DateTime: new Date(m.year(), m.month(), m.date(), 0, 30), Value: 33 },
  29. { DateTime: new Date(m.year(), m.month(), m.date(), 0, 45), Value: 34 },
  30. { DateTime: new Date(m.year(), m.month(), m.date(), 1), Value: 50, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
  31. { DateTime: new Date(m.year(), m.month(), m.date(), 2), Value: 60, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
  32. { DateTime: new Date(m.year(), m.month(), m.date(), 3), Value: 80, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
  33. { DateTime: new Date(m.year(), m.month(), m.date(), 4), Value: 100, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
  34. { DateTime: new Date(m.year(), m.month(), m.date(), 5), Value: 40, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
  35. { DateTime: new Date(m.year(), m.month(), m.date(), 6), Value: 120, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 }
  36. ]);
  37. function handleViewShown() {
  38. if (initialized === false) {
  39. chart = chart(BWA.ChartOptions.ServiceUsageByEnergy.getChartOptions($Code.TimeType.HOUR));
  40. //chart = chart(BWA.ChartOptions.UsageByEnergy.getChartOptions($Code.TimeType.MONTH));
  41. initialized = true;
  42. }
  43. if (shouldReload) {
  44. //shouldReload = false;
  45. //dataSource.pageIndex(0);
  46. //dataSource.load();
  47. }
  48. factorCo2DataSource.load().done(function(dbFactorCo2) {
  49. factorCo2 = BWA.Bems.refineFactorCo2DataArray(dbFactorCo2);
  50. });
  51. }
  52. function handleViewHidden() {
  53. $SearchView.visibleObservable(false); // hcLee 2015 03 23
  54. //hideOverlay();
  55. // var dataGrid = $('#gridContainer').dxDataGrid('instance');
  56. // dataGrid.clearSelection();
  57. }
  58. function handleViewShowing() {
  59. $.when(fuelTypesDataSource.load()).done(function (fuelTypes) {
  60. fuelTypes.push({
  61. FuelTypeId: ko.observable(CO2),
  62. Name: ko.observable('CO2')
  63. });
  64. fuelTypesForSearch($SearchView.filterObservableArrayForSelectBox('FuelTypeId', fuelTypes));
  65. deferredForSearch.resolve();
  66. handleSearchInSearchView();
  67. });
  68. }
  69. function refreshList() {
  70. //dataSource.pageIndex(0);
  71. //dataSource.load();
  72. }
  73. var searchViewOptions = {
  74. searchViewItems: [
  75. { id: 'TimeTypeId', defaultValue: 0, value: timeTypeIdForSearch, dataSource: timeTypesForSearch },
  76. { id: 'FuelTypeId', ignoreValue: 0, defaultValue: 1, value: fuelTypeIdForSearch, dataSource: fuelTypesForSearch },
  77. { id: 'DateForHour', type: 'dateRange', isOnlyDate: true },
  78. { id: 'TimeForHour', type: 'dateRange', isOnlyDate: true },
  79. ],
  80. promiseDataInSearchView: deferredForSearch.promise()
  81. };
  82. function handleSearchInSearchView() {
  83. var date = timeBoxForSearch.getDate(),
  84. timeType = timeBoxForSearch.type(),
  85. fuelTypeId = fuelTypeIdForSearch();
  86. // fuelTypeName= fuelTypeNameForSearch(); // hcLee 2015 03 23
  87. //alert(fuelTypeId);
  88. var commonParameters = {
  89. TimeIntervalType: timeBoxForSearch.type(),
  90. StartDate: date.startDate,
  91. EndDate: date.endDate
  92. };
  93. var pointHistoryParameters = {
  94. BuildingId: 0,
  95. FloorId: null,
  96. ZoneId: null,
  97. FuelTypeId: fuelTypeIdForSearch()
  98. };
  99. var options = BWA.ChartOptions.ServiceUsageByEnergy.getSeriesOptions(
  100. fuelTypeId,
  101. //fuelTypeName,
  102. timeBoxForSearch.type()
  103. );
  104. $.when(
  105. energyServiceGet.apiGet(_.extend(pointHistoryParameters, commonParameters)))
  106. //formulaGet.apiGet(_.extend(temperatureParameters, commonParameters)),
  107. //formulaGet.apiGet(_.extend(humidityParameters, commonParameters)))
  108. .done(function (values) {
  109. var dataArray = [];
  110. _.each(values, function (x) {
  111. dataArray.push({
  112. DateTime: moment(x.DateTime).toDate(),
  113. Value: x.Value
  114. //Value: 100
  115. });
  116. });
  117. options.dataSource = dataArray;
  118. chart().option(options);
  119. // chart().option('dataSource', dataArray);
  120. // chart().option('dataSource', chartDataArray());
  121. // chartDataArray(values);
  122. });
  123. //var request = energyServiceGet.requestGetForEnergyService.bind(energyServiceGet);
  124. }
  125. function handleSearchInSearchView_old() {
  126. var date = timeBoxForSearch.getDate(),
  127. timeType = timeBoxForSearch.type(),
  128. fuelTypeId = fuelTypeIdForSearch();
  129. // fuelTypeName= fuelTypeNameForSearch(); // hcLee 2015 03 23
  130. //alert(fuelTypeId);
  131. var options = BWA.ChartOptions.ServiceUsageByEnergy.getSeriesOptions(
  132. fuelTypeId,
  133. //fuelTypeName,
  134. timeBoxForSearch.type()
  135. );
  136. var request = energyServiceGet.requestGetForEnergyService.bind(energyServiceGet);
  137. if (fuelTypeId === CO2) {
  138. var promises = [];
  139. var fuelTypes = _.values($Code.FuelType);
  140. _.each(fuelTypes, function(fuelTypeId) {
  141. promises.push(request(fuelTypeId, timeType, 7)(date.startDate, date.endDate));
  142. });
  143. var calculate = factorCo2.calculate;
  144. $.when.apply(this, promises)
  145. .done(function(electricityArray, gasArray, waterArray) {
  146. var chartDataSource = [];
  147. var co2Fields = ['ECo2', 'GCo2', 'WCo2'];
  148. _.each(arguments, function(array, i) {
  149. var field = co2Fields[i];
  150. _.each(array[0], function(x) {
  151. var data = {
  152. DateTime: moment(x.DateTime).toDate(),
  153. };
  154. data[field] = calculate(x.Value, fuelTypes[i]);
  155. chartDataSource.push(data);
  156. });
  157. });
  158. options.dataSource = chartDataSource;
  159. chart().option(options);
  160. });
  161. }
  162. else {
  163. request(fuelTypeId, timeType, 7)(date.startDate, date.endDate)
  164. .done(function(dataArray) {
  165. var list = _.map(dataArray, function(x) {
  166. x.DateTime = moment(x.DateTime).toDate();
  167. return x;
  168. });
  169. var options = BWA.ChartOptions.ServiceUsageByEnergy.getSeriesOptions(
  170. //fuelTypeIdForSearch(),
  171. fuelTypeId,
  172. timeBoxForSearch.type()
  173. );
  174. options.dataSource = list;
  175. chart().option(options);
  176. });
  177. }
  178. //chart().option('equalBarWidth', false);
  179. }
  180. var viewModel = $.extend(BWA.CommonView.create(params, viewInfo, searchViewOptions, ko.observable(null), handleViewShown, null, handleSearchInSearchView), {
  181. refreshList: refreshList,
  182. viewShowing: handleViewShowing,
  183. viewHidden: handleViewHidden, // hcLee 2015 03 23
  184. });
  185. viewModel.timeBoxForSearch = timeBoxForSearch;
  186. return viewModel;
  187. };