123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- BemsWebApplication.ServiceUsageByEnergy = function(params, viewInfo) {
- "use strict";
- var initialized = false,
- shouldReload = false,
- dataSource;
- var CO2 = $Code.FuelType.WATER + 1;
- var fuelTypesDataSource = BemsWebApplication.db.createDataSource('BemsFuelType'),
- //var fuelTypesDataSource = BemsWebApplication.db.createDataSource('BemsFacilityTypeId'),
- factorCo2DataSource = BemsWebApplication.db.createDataSource('BemsFactorCo2'),
- serviceTypesDataSource = BemsWebApplication.db.createDataSource('BemsServiceType'),
- fuelTypesForSearch = ko.observableArray(),
- timeTypesForSearch = ko.observableArray(),
- timeTypeIdForSearch = ko.observable(0),
- fuelTypeIdForSearch = ko.observable(),
- fuelTypeNameForSearch = ko.observable(),
- deferredForSearch = new $.Deferred();
- var factorCo2 = null;
- //var energyUsageStatusGet = new BWA.Chart.EnergyUsageStatusGet(BWA.UserInfo.SiteId()),
- var energyServiceGet = new BWA.Chart.PointHistoryServiceTypeGet(BWA.UserInfo.SiteId()),
- timeBoxForSearch = BWA.SearchView.createDateTimeBox(),
- dataArray = ko.observableArray();
- var chart = BWA.Chart.Instance('chart', 'dxChart');
- timeTypesForSearch($G.TimeTypes);
- var m = moment();
- dataArray([
- { DateTime: new Date(m.year(), m.month(), m.date(), 0), Value: 30, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
- { DateTime: new Date(m.year(), m.month(), m.date(), 0, 15), Value: 32 },
- { DateTime: new Date(m.year(), m.month(), m.date(), 0, 30), Value: 33 },
- { DateTime: new Date(m.year(), m.month(), m.date(), 0, 45), Value: 34 },
- { DateTime: new Date(m.year(), m.month(), m.date(), 1), Value: 50, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
- { DateTime: new Date(m.year(), m.month(), m.date(), 2), Value: 60, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
- { DateTime: new Date(m.year(), m.month(), m.date(), 3), Value: 80, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
- { DateTime: new Date(m.year(), m.month(), m.date(), 4), Value: 100, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
- { DateTime: new Date(m.year(), m.month(), m.date(), 5), Value: 40, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 },
- { DateTime: new Date(m.year(), m.month(), m.date(), 6), Value: 120, PowerCo2: 40, GasCo2: 40, WaterCo2: 40 }
- ]);
- function handleViewShown() {
- if (initialized === false) {
- chart = chart(BWA.ChartOptions.ServiceUsageByEnergy.getChartOptions($Code.TimeType.HOUR));
- //chart = chart(BWA.ChartOptions.UsageByEnergy.getChartOptions($Code.TimeType.MONTH));
- initialized = true;
- }
- if (shouldReload) {
- //shouldReload = false;
- //dataSource.pageIndex(0);
- //dataSource.load();
- }
- factorCo2DataSource.load().done(function(dbFactorCo2) {
- factorCo2 = BWA.Bems.refineFactorCo2DataArray(dbFactorCo2);
- });
- }
- function handleViewHidden() {
- $SearchView.visibleObservable(false); // hcLee 2015 03 23
- //hideOverlay();
- // var dataGrid = $('#gridContainer').dxDataGrid('instance');
- // dataGrid.clearSelection();
- }
- function handleViewShowing() {
- $.when(fuelTypesDataSource.load()).done(function (fuelTypes) {
- fuelTypes.push({
- FuelTypeId: ko.observable(CO2),
- Name: ko.observable('CO2')
- });
- fuelTypesForSearch($SearchView.filterObservableArrayForSelectBox('FuelTypeId', fuelTypes));
- deferredForSearch.resolve();
- handleSearchInSearchView();
- });
- }
- function refreshList() {
- //dataSource.pageIndex(0);
- //dataSource.load();
- }
- var searchViewOptions = {
- searchViewItems: [
- { id: 'TimeTypeId', defaultValue: 0, value: timeTypeIdForSearch, dataSource: timeTypesForSearch },
- { id: 'FuelTypeId', ignoreValue: 0, defaultValue: 1, value: fuelTypeIdForSearch, dataSource: fuelTypesForSearch },
- { id: 'DateForHour', type: 'dateRange', isOnlyDate: true },
- { id: 'TimeForHour', type: 'dateRange', isOnlyDate: true },
- ],
- promiseDataInSearchView: deferredForSearch.promise()
- };
- function handleSearchInSearchView() {
- var date = timeBoxForSearch.getDate(),
- timeType = timeBoxForSearch.type(),
- fuelTypeId = fuelTypeIdForSearch();
- // fuelTypeName= fuelTypeNameForSearch(); // hcLee 2015 03 23
- //alert(fuelTypeId);
- var commonParameters = {
- TimeIntervalType: timeBoxForSearch.type(),
- StartDate: date.startDate,
- EndDate: date.endDate
- };
- var pointHistoryParameters = {
- BuildingId: 0,
- FloorId: null,
- ZoneId: null,
- FuelTypeId: fuelTypeIdForSearch()
- };
- var options = BWA.ChartOptions.ServiceUsageByEnergy.getSeriesOptions(
- fuelTypeId,
- //fuelTypeName,
- timeBoxForSearch.type()
- );
- $.when(
- energyServiceGet.apiGet(_.extend(pointHistoryParameters, commonParameters)))
- //formulaGet.apiGet(_.extend(temperatureParameters, commonParameters)),
- //formulaGet.apiGet(_.extend(humidityParameters, commonParameters)))
- .done(function (values) {
- var dataArray = [];
- _.each(values, function (x) {
- dataArray.push({
- DateTime: moment(x.DateTime).toDate(),
- Value: x.Value
- //Value: 100
- });
- });
- options.dataSource = dataArray;
- chart().option(options);
- // chart().option('dataSource', dataArray);
- // chart().option('dataSource', chartDataArray());
- // chartDataArray(values);
- });
- //var request = energyServiceGet.requestGetForEnergyService.bind(energyServiceGet);
- }
- function handleSearchInSearchView_old() {
- var date = timeBoxForSearch.getDate(),
- timeType = timeBoxForSearch.type(),
- fuelTypeId = fuelTypeIdForSearch();
- // fuelTypeName= fuelTypeNameForSearch(); // hcLee 2015 03 23
- //alert(fuelTypeId);
- var options = BWA.ChartOptions.ServiceUsageByEnergy.getSeriesOptions(
- fuelTypeId,
- //fuelTypeName,
- timeBoxForSearch.type()
- );
- var request = energyServiceGet.requestGetForEnergyService.bind(energyServiceGet);
- if (fuelTypeId === CO2) {
- var promises = [];
- var fuelTypes = _.values($Code.FuelType);
- _.each(fuelTypes, function(fuelTypeId) {
- promises.push(request(fuelTypeId, timeType, 7)(date.startDate, date.endDate));
- });
- var calculate = factorCo2.calculate;
- $.when.apply(this, promises)
- .done(function(electricityArray, gasArray, waterArray) {
- var chartDataSource = [];
- var co2Fields = ['ECo2', 'GCo2', 'WCo2'];
- _.each(arguments, function(array, i) {
- var field = co2Fields[i];
- _.each(array[0], function(x) {
- var data = {
- DateTime: moment(x.DateTime).toDate(),
- };
- data[field] = calculate(x.Value, fuelTypes[i]);
- chartDataSource.push(data);
- });
- });
- options.dataSource = chartDataSource;
- chart().option(options);
- });
- }
- else {
- request(fuelTypeId, timeType, 7)(date.startDate, date.endDate)
- .done(function(dataArray) {
- var list = _.map(dataArray, function(x) {
- x.DateTime = moment(x.DateTime).toDate();
- return x;
- });
- var options = BWA.ChartOptions.ServiceUsageByEnergy.getSeriesOptions(
- //fuelTypeIdForSearch(),
- fuelTypeId,
- timeBoxForSearch.type()
- );
- options.dataSource = list;
- chart().option(options);
- });
- }
- //chart().option('equalBarWidth', false);
- }
- var viewModel = $.extend(BWA.CommonView.create(params, viewInfo, searchViewOptions, ko.observable(null), handleViewShown, null, handleSearchInSearchView), {
- refreshList: refreshList,
- viewShowing: handleViewShowing,
- viewHidden: handleViewHidden, // hcLee 2015 03 23
- });
- viewModel.timeBoxForSearch = timeBoxForSearch;
- return viewModel;
- };
|