Guest User

Solarlog

a guest
Aug 17th, 2013
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Author: Niko Will
  2. function SolarLog(host) {
  3.     this.host = host;
  4.     this.months = ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'Septemb
  5. er', 'Oktober', 'November', 'Dezember' ];
  6.     this.shortmonths = ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov
  7. ', 'Dez' ];
  8.     this.vars = {};
  9.  
  10.     // define all necessary base vars you need
  11.     // unfortunely I found no better way to wrap all vars
  12.     // without defining them as global vars
  13.     this.vars._base_vars_js_needed = [
  14.         'time_start',
  15.         'time_end',
  16.         'Verguetung',
  17.         'AnlagenKWP',
  18.         'StatusCodes',
  19.         'FehlerCodes',
  20.         'SollYearKWP',
  21.         'sollMonth',
  22.     ];
  23.  
  24.     this.vars._min_cur_js_needed = [
  25.         'Datum',
  26.         'Uhrzeit',
  27.         'Pac',
  28.         'aPdc',
  29.         'curStatusCode',
  30.         'curFehlerCode'
  31.     ];
  32. };
  33.  
  34. SolarLog.prototype.get = function(filename, options, callback, parseResponse) {
  35.     var varname = filename.replace('.', '_').replace('?', '_');
  36.     if (this.vars['_'+varname+'_pending']) {
  37.         this.vars['_'+varname+'_callbacks'].push(callback);
  38.     } else {
  39.         this.vars['_'+varname+'_pending'] = true;
  40.         this.vars['_'+varname+'_callbacks'] = new Array();
  41.         this.vars['_'+varname+'_callbacks'].push(callback);
  42.         var self = this;
  43.         var data = options || {};
  44.         data.url = this.host + filename
  45.         $.ajax({
  46.             url: 'pages/quad/proxy.php',
  47.             data: data,
  48.             dataType: 'text'
  49.         })
  50.         .done(function(response) {
  51.             parseResponse.call(self, response, varname);
  52.             self.vars['_'+varname+'_pending'] = false;
  53.             for (var i = 0; i < self.vars['_'+varname+'_callbacks'].length; i++) {
  54.                 self.vars['_'+varname+'_callbacks'][i].call(self, self.vars['_'+varname]);
  55.             }
  56.         });
  57.     }
  58. };
  59.  
  60. SolarLog.prototype.localEval = function(response, preCode, postCode) {
  61.     var self = this;
  62.     var js = '(function() {';
  63.     if (preCode)
  64.         js += preCode;
  65.     js += response;
  66.     if (postCode)
  67.         js += postCode;
  68.     js += '})();';
  69.     eval(js);
  70. };
  71.  
  72. SolarLog.prototype.getVars = function(f, callback) {
  73.     this.get(f, null, callback, function(response, varname) {
  74.         var postCode = 'self.vars._'+varname+' = {';
  75.         for (var i in this.vars['_'+varname+'_needed']) {
  76.             postCode += this.vars['_'+varname+'_needed'][i] + ': ';
  77.             postCode += this.vars['_'+varname+'_needed'][i] + ', ';
  78.         }
  79.         postCode += '};';
  80.         this.localEval(response, null, postCode);
  81.     });
  82. };
  83.  
  84. SolarLog.prototype.getBaseVars = function(callback) {
  85.     this.getVars('base_vars.js', callback);
  86. };
  87.  
  88. SolarLog.prototype.getMinCur = function(callback) {
  89.     this.getVars('min_cur.js', callback);
  90. };
  91.  
  92. SolarLog.prototype.getList = function(f, options, a, i, callback) {
  93.     this.get(f, options, callback, function(response, varname) {
  94.         this.localEval(response, 'var '+a+' = []; var '+i+' = 0;', 'self.vars._'+varname+' = '+a+'
  95. ;');
  96.     });
  97. };
  98.  
  99. SolarLog.prototype.getMinDay = function(callback) {
  100.     this.getList('min_day.js', null, 'm', 'mi', callback);
  101. };
  102.  
  103. SolarLog.prototype.getDay = function(day, callback) {
  104.     var day_str = ('0' + day.getFullYear()).slice(-2)
  105.         + ('0' + (day.getMonth()+1)).slice(-2)
  106.         + ('0' + day.getDate()).slice(-2);
  107.     this.getList('min.js?'+day_str, null, 'm', 'mi', callback);
  108. };
  109.  
  110. SolarLog.prototype.getDays = function(callback) {
  111.     this.getList('days.js', null, 'da', 'dx', callback);
  112. };
  113.  
  114. SolarLog.prototype.getCurrent = function(callback) {
  115.     this.getDays(function(da) {
  116.         var s1 = da[0].split('|');
  117.         var s2 = s1[1].split(';');
  118.         var current = [this.str2date(s1[0].substr(0,8) + ' 00:00:00').valueOf(), parseInt(s2[0])];
  119.         callback.call(this, current);
  120.     });
  121. };
  122.  
  123. SolarLog.prototype.getDaysHistory = function(date, callback) {
  124.     var options = {};
  125.     if (date) {
  126.         options.contains = ('0' + (date.getMonth()+1)).slice(-2)
  127.             + '.' + ('0' + date.getFullYear()).slice(-2) + '|';
  128.     }
  129.     this.getList('days_hist.js', options, 'da', 'dx', callback);
  130. };
  131.  
  132. SolarLog.prototype.getMonths = function(date, callback) {
  133.     var options = {};
  134.     if (date) {
  135.         options.contains = '.' + ('0' + date.getFullYear()).slice(-2) + '|';
  136.     }
  137.     this.getList('months.js', options, 'mo', 'mx', callback);
  138. };
  139.  
  140. SolarLog.prototype.getYears = function(callback) {
  141.     this.getList('years.js', null, 'ye', 'yx', callback);
  142. };
  143.  
  144. SolarLog.prototype.date2utc = function(date) {
  145.     return new Date(date.getTime() + date.getTimezoneOffset() * 60000);
  146. };
  147.  
  148. var i = 0;
  149. SolarLog.prototype.str2utc = function(s) {
  150.     return this.str2date(s).valueOf();
  151. };
  152.  
  153. SolarLog.prototype.str2date = function(s) {
  154.     var s1 = s.split(' ');
  155.     var d = s1[0].split('.');
  156.     var t = s1[1].split(':');
  157.     var year = parseInt(d[2]) + 2000;
  158.     var month = parseInt(d[1]) - 1;
  159.     var day = parseInt(d[0]);
  160.     var hour = parseInt(t[0]);
  161.     var minute = parseInt(t[1]);
  162.     var second = parseInt(t[2]);
  163.     return new Date(year, month, day, hour, minute, second);
  164. };
  165.  
  166. SolarLog.prototype.countDaysInMonth = function(year, month) {
  167.     var monthStart = new Date(year, month, 1);
  168.     var monthEnd = new Date(year, month + 1, 1);
  169.     return (monthEnd - monthStart) / (1000 * 60 * 60 * 24);
  170. };
  171.  
  172. SolarLog.prototype.countDaysInYear = function(year) {
  173.     var yearStart = new Date(year, 1, 1);
  174.     var yearEnd = new Date(year + 1, 1, 1);
  175.     return (yearEnd - yearStart) / (1000 * 60 * 60 * 24);
  176. };
  177.  
  178. SolarLog.prototype.getSollForYear = function(year, month, day, base) {
  179.     var d = new Date();
  180.     if (d.getFullYear() == year) {
  181.         var soll = 0;
  182.         for (var i = 0; i <= month; i++) {
  183.             soll += this.getSollForMonth(year, i, day, base);
  184.         }
  185.         return soll;
  186.     } else {
  187.         return ((base.AnlagenKWP / 1000.0) * base.SollYearKWP);
  188.     }
  189. };
  190.  
  191. SolarLog.prototype.getSollForMonth = function(year, month, day, base) {
  192.     var d = new Date();
  193.     var soll = ((base.AnlagenKWP / 1000.0) * base.SollYearKWP) / 100.0 * base.sollMonth[month];
  194.     if (d.getMonth() == month && d.getFullYear() == year) {
  195.         return soll / this.countDaysInMonth(year, month) * day;
  196.     } else {
  197.         return soll;
  198.     }
  199. };
  200.  
  201. SolarLog.prototype.getSollForDay = function(year, month, base) {
  202.     var soll = ((base.AnlagenKWP / 1000.0) * base.SollYearKWP) / 100.0 * base.sollMonth[month];
  203.     return soll / this.countDaysInMonth(year, month);
  204. };
  205.  
  206. SolarLog.prototype.loading = function(finished) {
  207.     finished = finished || false;
  208.     this.loadingCounter = this.loadingCounter || 0;
  209.     if (!finished) {
  210.         if (this.loadingCounter == 0) {
  211.             $.mobile.loading('show');
  212.         }
  213.         this.loadingCounter++;
  214.     } else {
  215.         this.loadingCounter--;
  216.         if (this.loadingCounter <= 0) {
  217.             $.mobile.loading('hide');
  218.             this.loadingCounter = 0;
  219.         }
  220.     }
  221. };
  222.  
  223. SolarLog.prototype.prepare = function() {
  224.     if ($('#pv_plot_day').highcharts()) {
  225.         $('#pv_plot_day').highcharts().destroy();
  226.     }
  227.     if ($('#pv_plot_month').highcharts()) {
  228.         $('#pv_plot_month').highcharts().destroy();
  229.     }
  230.     if ($('#pv_plot_year').highcharts()) {
  231.         $('#pv_plot_year').highcharts().destroy();
  232.     }
  233. };
  234.  
  235. SolarLog.prototype.renderDay = function(date, base, isCurrent) {
  236.     this.loading();
  237.     var cb = function(m) {
  238.         var day = date.getDate();
  239.         var month = date.getMonth();
  240.         var year = date.getFullYear();
  241.         var xmin = this.date2utc(new Date(year, month, day, base.time_start[month], 0, 0)).valueOf
  242. ();
  243.         var xmax = this.date2utc(new Date(year, month, day, base.time_end[month], 0, 0)).valueOf();
  244.         var dayMax = 0;
  245.         var enabled = [true, false, false, true];
  246.         var types = ['area', 'line', 'line', 'area'];
  247.         var names = ['PAC', 'String 1', 'String 2', 'Ertrag'];
  248.         var yAxis = [0, 0, 0, 1];
  249.         var index = [1, 2, 3, 0];
  250.         var legendIndex = [0, 2, 3, 1];
  251.         var data = new Array(types.length);
  252.         var curPac = 0;
  253.         for (var i = 0; i < types.length; i++) {
  254.             data[i] = new Array();
  255.         }
  256.         for (var mi = (m.length - 1); mi >= 0; mi--) {
  257.             var s1 = m[mi].split('|');
  258.             var s2 = s1[1].split(';');
  259.             curPac = parseInt(s2[3]);
  260.             var pac = parseInt(s2[0]);
  261.             if (dayMax < pac) {
  262.                 dayMax = pac;
  263.             }
  264.             for (var i = 0; i < types.length; i++) {
  265.                 data[i].push([this.str2utc(s1[0]), parseInt(s2[i])]);
  266.             }
  267.         }
  268.         var series = Array();
  269.         for (var i = 0; i < types.length; i++) {
  270.             if (enabled[i]) {
  271.                 series.push({
  272.                     type: types[i],
  273.                     name: names[i],
  274.                     yAxis: yAxis[i],
  275.                     index: index[i],
  276.                     legendIndex: legendIndex[i],
  277.                     data: data[i]
  278.                 });
  279.             }
  280.         }
  281.         // draw the plot
  282.         var id = '#pv_plot_day';
  283.         $(id).highcharts({
  284.             chart: {
  285.                 height: $(id).height(),
  286.                 width: $(id).width(),
  287.                 marginRight: 50,
  288.                 marginLeft: 50,
  289.                 marginTop: 40,
  290.             },
  291.             colors: ["#f9a028", '#AAAAAA', '#FF0000', '#0000FF'],
  292.             series: series,
  293.             xAxis: {
  294.                 type: 'datetime',
  295.                 min: xmin,
  296.                 max: xmax
  297.             },
  298.             yAxis: [{
  299.                 min: 0,
  300.                 max: 12000,
  301.                 title: null,
  302.             }, {
  303.                 min: 0,
  304.                 max: 80000,
  305.                 title: null,
  306.                 opposite: true
  307.             }]
  308.         });
  309.  
  310.         // output day specific values
  311.         $('#pv_day').html(day + '.');
  312.         $('#pv_day_pac').html((curPac / 1000.0).toFixed(2));
  313.         $('#pv_day_eur').html(((curPac / 1000.0) * (base.Verguetung / 10000.0)).toFixed(2));
  314.         $('#pv_day_pac2').html((curPac / base.AnlagenKWP).toFixed(2));
  315.         $('#pv_day_max').html((dayMax / 1000.0).toFixed(2));
  316.         var soll = this.getSollForDay(year, month, base);
  317.         $('#pv_day_soll').html(soll.toFixed(2));
  318.         $('#pv_day_ist').html((100.0 / soll * (curPac / 1000.0)).toFixed(1) + " %");
  319.         this.loading(true);
  320.     };
  321.        
  322.     if (!isCurrent) {
  323.         this.getDay(date, cb);
  324.     } else {
  325.         this.getMinDay(cb);
  326.     }
  327. };
  328.  
  329. SolarLog.prototype.renderMonth = function(date, base, current) {
  330.     this.loading();
  331.     this.getDaysHistory(date, function(da) {
  332.         var monthPac = current ? current[1] : 0;
  333.         var monthMax = current ? current[1] : 0;
  334.         var year = date.getFullYear();
  335.         var month = date.getMonth();
  336.         var day = date.getDate();
  337.         var xmin = this.date2utc(new Date(year, month, 1)).valueOf();
  338.         var xmax = this.date2utc(new Date(year, month + 1, 1)).valueOf();
  339.         var data = new Array();
  340.         for (var dx = (da.length - 1); dx >= 0; dx--) {
  341.             var s1 = da[dx].split('|');
  342.             var s2 = s1[1].split(';');
  343.             var pac = parseInt(s2[0]);
  344.             monthPac += pac;
  345.             if (monthMax < pac) {
  346.                 monthMax = pac;
  347.             }
  348.             data.push([this.str2date(s1[0] + ' 00:00:00').valueOf(), pac]);
  349.         }
  350.         // also push the current value if there is one
  351.         if (current) {
  352.             data.push(current);
  353.         }
  354.         // draw the plot
  355.         var id = '#pv_plot_month';
  356.         $(id).highcharts({
  357.             chart: {
  358.                 height: $(id).height(),
  359.                 width: $(id).width(),
  360.                 marginRight: 50,
  361.                 marginLeft: 50,
  362.                 marginTop: 40,
  363.             },
  364.             plotOptions: {
  365.                 series: {
  366.                     point: {
  367.                         events: {
  368.                             click: function() {
  369.                                 solarlog.render(new Date(this.x));
  370.                                 return false;
  371.                             }
  372.                         }
  373.                     }
  374.                 }
  375.             },
  376.             series: [{
  377.                 data: data,
  378.                 type: 'column',
  379.                 name: 'Tageserträge ' + this.months[month]
  380.             }],
  381.             xAxis: {
  382.                 type: 'datetime',
  383.                 min: xmin,
  384.                 max: xmax
  385.             },
  386.             yAxis: {
  387.                 min: 0,
  388.                 max: 80000,
  389.                 title: null,
  390.                 plotLines: [{
  391.                     value: this.getSollForDay(year, month, base) * 1000,
  392.                     color : 'red',
  393.                     width : 2,
  394.                 }],
  395.             }
  396.         });
  397.         // output the month specific values
  398.         $('#pv_month').html(this.shortmonths[month]);
  399.         $('#pv_month_pac').html((monthPac / 1000.0).toFixed(2));
  400.         $('#pv_month_eur').html(((monthPac / 1000.0) * (base.Verguetung / 10000.0)).toFixed(2));
  401.         $('#pv_month_pac2').html((monthPac / base.AnlagenKWP).toFixed(2));
  402.         $('#pv_month_max').html((monthMax / 1000.0).toFixed(2));
  403.         var soll = 0;
  404.         if (current) {
  405.             var d = new Date(current[0]);
  406.             soll = this.getSollForMonth(d.getFullYear(), d.getMonth(), d.getDate(), base);
  407.         } else {
  408.             soll = this.getSollForMonth(year, month, day, base);
  409.         }
  410.         $('#pv_month_soll').html(soll.toFixed(2));
  411.         $('#pv_month_ist').html((100.0 / soll * (monthPac / 1000.0)).toFixed(1) + " %");
  412.         this.loading(true);
  413.     });
  414. };
  415.  
  416. SolarLog.prototype.renderYear = function(date, base, current) {
  417.     this.loading();
  418.     this.getMonths(date, function(mo) {
  419.         var curPac = current ? current[1] : 0;
  420.         var yearPac = curPac;
  421.         var yearMax = 0;
  422.         var year = date.getFullYear();
  423.         var month = date.getMonth();
  424.         var day = date.getDate();
  425.         var xmin = this.date2utc(new Date(year, 1, 1)).valueOf();
  426.         var xmax = this.date2utc(new Date(year, 12, 31)).valueOf();
  427.         var data = new Array();
  428.         for (var mx = (mo.length - 1); mx > 0; mx--) {
  429.             var mo1 = mo[mx].split('|');
  430.             var pac = parseInt(mo1[1]);
  431.             yearPac += pac;
  432.             if (yearMax < pac) {
  433.                 yearMax = pac;
  434.             }
  435.             data.push([
  436.                 this.str2date('01' + mo1[0].substr(2) + ' 00:00:00').valueOf(),
  437.                 pac
  438.             ]);
  439.         }
  440.         var mo1 = mo[0].split('|');
  441.         var pac = parseInt(mo1[1]);
  442.         yearPac += pac;
  443.         if (yearMax < (pac + curPac)) {
  444.             yearMax = pac + curPac;
  445.         }
  446.         data.push([
  447.             this.str2date('01' + mo1[0].substr(2) + ' 00:00:00').valueOf(),
  448.             pac + curPac
  449.         ]);
  450.         // draw the plot
  451.         var id = '#pv_plot_year';
  452.         $(id).highcharts({
  453.             chart: {
  454.                 height: $(id).height(),
  455.                 width: $(id).width(),
  456.                 marginRight: 50,
  457.                 marginLeft: 50,
  458.                 marginTop: 40,
  459.             },
  460.             plotOptions: {
  461.                 series: {
  462.                     point: {
  463.                         events: {
  464.                             click: function() {
  465.                                 solarlog.render(new Date(this.x));
  466.                                 return false;
  467.                             }
  468.                         }
  469.                     }
  470.                 }
  471.             },
  472.             series: [{
  473.                 data: data,
  474.                 type: 'column',
  475.                 name: 'Monatserträge ' + year
  476.             }],
  477.             xAxis: {
  478.                 type: 'datetime',
  479.                 min: xmin,
  480.                 max: xmax,
  481.             startOnTick: true,
  482.             endOnTick: true
  483.             },
  484.             yAxis: {
  485.                 min: 0,
  486.                 max: 2200000,
  487.                 title: null,
  488.             }
  489.         });
  490.  
  491.         // output year specific values
  492.         $('#pv_year').html(year);
  493.         $('#pv_year_pac').html((yearPac / 1000.0).toFixed(2));
  494.         $('#pv_year_eur').html(((yearPac / 1000.0) * (base.Verguetung / 10000.0)).toFixed(2));
  495.         $('#pv_year_pac2').html((yearPac / base.AnlagenKWP).toFixed(2));
  496.         $('#pv_year_max').html((yearMax / 1000.0).toFixed(2));
  497.         var soll = 0;
  498.         if (current) {
  499.             var d = new Date(current[0]);
  500.             soll = this.getSollForYear(d.getFullYear(), d.getMonth(), d.getDate(), base);
  501.         } else {
  502.             soll = this.getSollForYear(year, month, day, base);
  503.         }
  504.         $('#pv_year_soll').html(soll.toFixed(2));
  505.         $('#pv_year_ist').html((100.0 / soll * (yearPac / 1000.0)).toFixed(1) + " %");
  506.         this.loading(true);
  507.     });
  508. };
  509.  
  510. SolarLog.prototype.render = function(date) {
  511.     this.loading();
  512.     this.getBaseVars(function(base) {
  513.         this.getMinCur(function(cur) {
  514.             $('#pv_cur_pac').html(cur.Pac);
  515.             $('#pv_cur_string1').html(cur.aPdc[0]);
  516.             $('#pv_cur_string2').html(cur.aPdc[1]);
  517.             if (cur.Pac > 0 && (cur.aPdc[0] > 0 || cur.aPdc[1] > 0)) {
  518.                 $('#pv_cur_percent').html((100.0 / (cur.aPdc[0] + cur.aPdc[1]) * cur.Pac).toFixed(
  519. 1) + ' %');
  520.             } else {
  521.                 $('#pv_cur_percent').html('---');
  522.             }
  523.             $('#pv_cur_status').html(base.StatusCodes[0].split(',')[cur.curStatusCode[0]]);
  524.             $('#pv_cur_error').html(base.FehlerCodes[0].split(',')[cur.curFehlerCode[0]]);
  525.  
  526.             var modified = this.str2date(cur.Datum + ' ' + cur.Uhrzeit);
  527.             var isCurrentYear = !date || date.getFullYear() == modified.getFullYear();
  528.             var isCurrentMonth = !date || (isCurrentYear && date.getMonth() == modified.getMonth()
  529. );
  530.  
  531.             if (!date || date >= modified || (isCurrentMonth && date.getDate() == modified.getDate
  532. ())) {
  533.                 date = modified;
  534.             }
  535.  
  536.             var yearChanged = !this.date || date.getFullYear() != this.date.getFullYear();
  537.             var monthChanged = !this.date || yearChanged || date.getMonth() != this.date.getMonth(
  538. );
  539.             var dayChanged = !this.date || monthChanged || date.getDate() != this.date.getDate();
  540.  
  541.             if (dayChanged) {
  542.                 this.renderDay(date, base, date == modified);
  543.                 this.getCurrent(function(current) {
  544.                     if (monthChanged) this.renderMonth(date, base, isCurrentMonth ? current : null
  545. );
  546.                     if (yearChanged) this.renderYear(date, base, isCurrentYear ? current : null);
  547.                 });
  548.             }
  549.  
  550.             this.date = date;
  551.             this.loading(true);
  552.         });
  553.     });
  554. };
Advertisement
Add Comment
Please, Sign In to add comment