Skip to content

Instantly share code, notes, and snippets.

@Masatoshi
Last active August 29, 2015 14:01
Show Gist options
  • Save Masatoshi/aa4ec16c38a80a408733 to your computer and use it in GitHub Desktop.
Save Masatoshi/aa4ec16c38a80a408733 to your computer and use it in GitHub Desktop.
var underscore = angular.module('underscore', []);
underscore.factory('_', function() {
return window._; // assumes underscore has already been loaded on the page
});
//angular.module('webApp', ['underscore','ui.bootstrap'])
//.controller('calDenki', function ($scope, $http, $filter, _) {
angular.module('webApp', ['ui.bootstrap'])
.controller('calDenki', function ($scope, $http, $filter) {
$scope.powerCompanies = [
{name: '北海道電力', url:['http://www.hepco.co.jp/userate/price/unitprice/unitprice01.html'],steps:[[0,19.88],[120,26.06],[300,29.46]]},
{name: '東北電力', url:['http://www.tohoku-epco.co.jp/dprivate/unit/index.html'],steps:[[0,18.24],[120,24.87],[300,28.75]]},
{name: '東京電力',
url: 'http://www.tepco.co.jp/e-rates/individual/basic/charge/charge01-j.html',
prices:{
'10A':{color:'赤',price:273.00},
'15A':{color:'桃',price:409.50},
'20A':{color:'黄',price:546.00},
'30A':{color:'緑',price:819.00},
'40A':{color:'灰',price:1092.00},
'50A':{color:'茶',price:1365.00},
'60A':{color:'紫',price:1638.00}
},
steps:[[0,19.43],[120,25.91],[300,29.93]],
discount: 54,
},
{name: '中部電力',
url: ['http://www.chuden.co.jp/ryokin/shikumi/shi_keiyaku/kei_ryokin/keisan/',
'http://www.chuden.co.jp/ryokin/home_menu/home_basic/hba_timeplan/'],
prices:{
'10A':{color:'赤',price:280.80},
'15A':{color:'桃',price:421.20},
'20A':{color:'黄',price:561.60},
'30A':{color:'緑',price:842.40},
'40A':{color:'灰',price:1123.20},
'50A':{color:'茶',price:1404.00},
'60A':{color:'紫',price:1684.80}
},
steps:[[0,20.68],[120,25.08],[300,27.97]],
night:{90:24.16,night:13.45},
minimum: 253.80,
discount: 54,
surcharge: 0.69,
recycle: 0.75,
solor: 0.03
},
{name: '北陸電力',url:['http://www.rikuden.co.jp/ryokin/minsei.html'],steps:[[0,17.44],[120,21.25],[300,22.94]], minimum: 177.44},
{name: '関西電力', url:['http://www.kepco.co.jp/home/ryoukin/'],steps:[[0,20.84],[120,27.27],[300,31.09]]},
{name: '中国電力', url:['http://www.energia.co.jp/elec/h_menu/pricelist/pricelist1.html'],steps:[[0,20.34],[120,26.90],[300,28.98]]},
{name: '四国電力', url:['http://www.yonden.co.jp/ryoukin/'],steps:[[0,20.00],[120,26.50],[300,29.95]]},
{name: '九州電力', url:['http://www.kyuden.co.jp/rate_mein-menu_4_2_h26_5.html'],steps:[[0,17.13],[120,22.63],[300,25.57]]},
{name: '沖縄電力', url:['http://www.okiden.co.jp/service/individual/menu_specific.html'],steps:[[0,22.49],[120,27.93],[300,29.07]]}
];
$scope.presetCompany = $scope.powerCompanies[2];
$scope.setCompany = function(num){
$scope.presetCompany = $scope.powerCompanies[num];
}
$scope.presets = [
{id:0, name:'テレビ',watt:60, idlewatt:1, hours:3},
{id:1, name:'DVD・HDプレイヤー',watt:60, idlewatt:1, hours:3},
{id:2, name:'冷蔵庫 40L(単身)',watt:60, idlewatt:1, hours:24},
{id:3, name:'冷蔵庫 80L(家庭)',watt:60, idlewatt:1, hours:24},
{id:4, name:'洗濯・乾燥機', watt:60, idlewatt:1, hours:1},
{id:5, name:'パソコン(デスクトップ)',watt:150, idlewatt:1, hours:3},
{id:6, name:'パソコン(ノート)',watt:100, idlewatt:1, hours:3},
{id:7, name:'エアコン',watt:100, idlewatt:1, hours:4},
{id:8, name:'こたつ',watt:100, idlewatt:1, hours:3},
{id:9, name:'電気ストーブ',watt:100, idlewatt:1, hours:3},
{id:10, name:'空気清浄機',watt:100, idlewatt:1, hours:6},
{id:11, name:'掃除機',watt:100, idlewatt:1, hours:0.5},
{id:12, name:'扇風機',watt:50, idlewatt:1, hours:3},
{id:13, name:'ドライヤー',watt:1200, idlewatt:1, hours:1},
{id:14, name:'照明(8畳LED)',watt:100, idlewatt:1, hours:6},
{id:15, name:'電子レンジ',watt:1200, idlewatt:1, hours:0.5},
{id:16, name:'IH',watt:100, idlewatt:1, hours:1},
{id:16, name:'自動食器洗い機',watt:1300, idlewatt:1, hours:0.5}];
$scope.selectedPreset = {};
$scope.$watch('selectedPreset', function(){
$scope.selectedPreset.amount = $scope.selectedPreset.amount||1;
})
$scope.myPresets = [{name:"単身世帯",indexs:[{id:0,amount:1},{id:2,amount:1},{id:15,amount:1}]},
{name:"二人世帯",indexs:[{id:0,amount:1},{id:3,amount:1},{id:11,amount:1},{id:15,amount:1},{id:16,amount:1}]},
{name:"四人世帯",indexs:[{id:0,amount:1},{id:2,amount:1},{id:9,amount:1},{id:11,amount:1},{id:15,amount:1},{id:16,amount:1}]}
]
$scope.setPreset = function(num){
$scope.devices = [];
$scope.myPresets[num].indexs.map(function(idx){
var found = $filter('getById')($scope.presets, idx.id);
if(found) {
found.amount = idx.amount;
$scope.devices.push(found);
}
})
}
$scope.devices = [
{name:'テレビ',watt:60, amount:1, hours:3},
{name:'パソコン(デスクトップ)',watt:100, amount:1, hours:6},
];
$scope.deviceMng = {
add: function(dev){
if(dev.watt && dev.amount && dev.hours)
/*
var base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var len = base.length;
dev.id = Math.floor(Math.random()*10000);
*/
$scope.devices.push(dev);
$scope.selectedPreset = {};
},
del: function(num){if($scope.devices[num]) $scope.devices.splice(num,1)}
};
$scope.downLoadCSV = function(){
window.open( "data:text/csv;charset=utf-8," + escape(JSON.stringify($scope.devices)));;
// 計算結果も
// "=IF(A2=B2,""PASS"", ""FAIL"")"
}
$scope.total = {};
$scope.$watch('devices', function(){
$scope.total.watt = function(){
return $scope.devices.map(function(dev){return dev.watt*dev.amount*dev.hours})
.reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
},0)}()
},true)
/*
$scope.$watchCollection('devices', function(){
$scope.total.watt = function(){
return $scope.devices.map(function(dev){return dev.watt*dev.amount*dev.hours})
.reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
},0)}()
})*/
$scope.$watchCollection('[total.watt, presetCompany]', function(){
function price(watt){
var watt = Math.round(watt / 1000);
var result = $scope.presetCompany.steps.map(function(price){
if(watt > price[0]) return price[1];
return 0;
})
return Math.max.apply(null,result)*watt;
}
$scope.total.charge = price($scope.total.watt);
$scope.total.monthCharge = price($scope.total.watt*365/12);
$scope.total.yearCharge = $scope.total.monthCharge*12;
},true)
})
.directive('presetOption', function() {
return {
replace : true,
restrict: 'AEC',
transclude: true,
template: '<div><select ng-model="selectedPreset" ng-options="preset.name for preset in presets" class="form-control on-repeat-done><option value="">-- 選択してください --</option></select>' +
'<div class="form-inline"><div class="input-group input-group-sm col-lg-5 pull-left"><input type="text" ng-model="selectedPreset.name" class="form-control" placeholder="家電名"></div>' +
'<div class="input-group input-group-sm col-lg-2 input-mini pull-left"><input type="number" ng-model="selectedPreset.watt" class="form-control col-lg-6" placeholder="消費電力" min="0" max="10000" step="5" required><span class="input-group-addon">W</span></div>' +
'<div class="input-group input-group-sm col-lg-2 input-mini pull-left"><input type="number" ng-model="selectedPreset.amount" class="form-control col-lg-6" placeholder="台数" min="1" max="99" required><span class="input-group-addon">台</span></div>' +
'<div class="input-group input-group-sm col-lg-2 input-mini pull-left"><input type="number" ng-model="selectedPreset.hours" class="form-control" placeholder="利用時間" min="0" max="24" step="0.25" required><span class="input-group-addon">H</span></div>' +
'<button type="button" class="btn btn-primary btn-xs" ng-click="deviceMng.add(selectedPreset)" ng-disabled="!selectedPreset.watt||!selectedPreset.hours">追加</button></div></div>'
};
})
.directive('deviceTable', function() {
return {
replace : true,
restrict: 'AEC',
transclude: true,
template: '<table class="table table-striped table-hover ">' +
'<tr><th colspan="2">デバイス名</th><th>消費電力(W)</th><th>台数</th><th>時間(H)</th><th>小計</th></tr>' +
'<tr ng-repeat="device in devices track by $index" on-repeat-done>' +
'<td><button type="button" class="btn btn-danger btn-xs" ng-click="deviceMng.del($index)">削除</button></td>' +
'<td><input type="text" ng-model="device.name"></td>' +
'<td><input type="number" ng-model="device.watt" class=""></td>' +
'<td><input type="number" ng-model="device.amount" min="1" max="99" class=""></td>' +
'<td><input type="number" ng-model="device.hours" min="0" max="24" step="0.25" class=""></td>' +
'<td class="text-right">{{device.watt * device.amount * device.hours}}</td></tr>' +
'<tr><th colspan="5" class="text-right">総計</td><td class="text-right">{{total.watt}}</td></tr>' +
'</table>'
};
})
.directive('companyProfile', function() {
return {
replace : true,
restrict: 'AEC',
template: '<dl><dt>住所</dt><dd>愛知県あま市下萱津坪井36−1</dd></dl>'
};
})
.filter('sumByKey', function () {
return function (data, key) {
if (typeof (data) === 'undefined' || typeof (key) === 'undefined') {
return 0;
}
var sum = 0;
for (var i = data.length - 1; i >= 0; i--) {
sum += Number(data[i][key]);
}
return sum;
}
})
.filter('getById', function() {
return function(items, id) {
var result;
items.map(function(item){
if(item.id == id) {
result = item;
}
})
return result;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment