energyView.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * Copyright © 2010 Digia Plc
  3. * Copyright © 2010 Nokia Corporation
  4. *
  5. * All rights reserved.
  6. *
  7. * Nokia and Nokia Connecting People are registered trademarks of
  8. * Nokia Corporation.
  9. * Java and all Java-based marks are trademarks or registered
  10. * trademarks of
  11. * Sun Microsystems, Inc. Other product and company names
  12. * mentioned herein may be
  13. * trademarks or trade names of their respective owners.
  14. *
  15. *
  16. * Subject to the conditions below, you may, without charge:
  17. *
  18. * · Use, copy, modify and/or merge copies of this software and
  19. * associated documentation files (the "Software")
  20. *
  21. * · Publish, distribute, sub-licence and/or sell new software
  22. * derived from or incorporating the Software.
  23. *
  24. *
  25. * This file, unmodified, shall be included with all copies or
  26. * substantial portions
  27. * of the Software that are distributed in source code form.
  28. *
  29. * The Software cannot constitute the primary value of any new
  30. * software derived
  31. * from or incorporating the Software.
  32. *
  33. * Any person dealing with the Software shall not misrepresent
  34. * the source of the Software.
  35. *
  36. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  37. * KIND, EXPRESS OR IMPLIED,
  38. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  39. * MERCHANTABILITY, FITNESS FOR A
  40. * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  41. * AUTHORS OR COPYRIGHT
  42. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  43. * WHETHER IN AN ACTION
  44. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  45. * CONNECTION WITH THE
  46. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  47. */
  48. var currentWeek = null;
  49. var availableWeeks = null;
  50. var maxConsumption = null;
  51. var weekData = null;
  52. var startWeek = null;
  53. var startYear = null;
  54. /**
  55. * Set-up function for EnergyView
  56. *
  57. * @return boolean
  58. */
  59. function onStartUp() {
  60. currentWeek = 0;
  61. startWeek = weekNumber();
  62. startYear = yearNumber();
  63. availableWeeks = weekData.length;
  64. maxConsumption = getMaxConsumption();
  65. histogram.maxValue = maxConsumption;
  66. return showWeek( currentWeek );
  67. }
  68. /**
  69. * Changes current week (changed to represent years in HomeControlSystem)
  70. *
  71. * @param weekID The index of the dataset in data table
  72. * @return boolean
  73. */
  74. function showWeek(weekID) {
  75. if (weekID < 0) weekID = weekData.length - 1;
  76. if (weekID >= weekData.length) weekID = 0;
  77. currentWeek = weekID;
  78. var yearToUse = startYear - weekID;
  79. weekYear.text = "Year " + yearToUse;
  80. histogram.setValues( weekData[weekID] );
  81. return true;
  82. }
  83. /**
  84. * Finds and return the largest consumption
  85. *
  86. * @return real
  87. */
  88. function getMaxConsumption() {
  89. var a = 0;
  90. var b = 0;
  91. var currentMax = 0;
  92. for(a = 0; a < availableWeeks; a++) {
  93. for(b = 0; b < 7; b++) {
  94. if (weekData[a][b][0] > currentMax) currentMax = weekData[a][b][0];
  95. }
  96. }
  97. return currentMax;
  98. }
  99. /**
  100. * Expands Date class (Picked from the Internet)
  101. *
  102. * @return int
  103. */
  104. Date.prototype.getWeek = function() {
  105. var onejan = new Date(this.getFullYear(),0,1);
  106. return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
  107. }
  108. /**
  109. * Gets the number of current week
  110. *
  111. * @return int
  112. */
  113. function weekNumber() {
  114. var theDate = new Date();
  115. return theDate.getWeek();
  116. }
  117. /**
  118. * Gets the number of current year
  119. *
  120. * @return int
  121. */
  122. function yearNumber() {
  123. var theDate = new Date();
  124. return theDate.getFullYear();
  125. }
  126. /**
  127. * Counts the width for label items (month initials on EnergyView)
  128. *
  129. * @return real
  130. */
  131. function columnWidth() {
  132. var amount = histogram.countVisible();
  133. return Math.round( ( (histogram.width/(2*histogram.spacing)) - (histogram.spacing*(amount-1)) ) / amount );
  134. }
  135. /**
  136. * SQL FUNCTIONS
  137. */
  138. /**
  139. * Checks whether sauna is on
  140. *
  141. * @return bool
  142. */
  143. function isSaunaOn() {
  144. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  145. var saunaOn = false;
  146. db.transaction(
  147. function(tx) {
  148. //Updating information
  149. saunaOn = tx.executeSql('SELECT saunaOn FROM Sauna').rows.item(0).saunaOn;
  150. }
  151. )
  152. return saunaOn;
  153. }
  154. /**
  155. * Gets sauna temperature
  156. *
  157. * @return Nothing
  158. */
  159. function getSaunaTemperature() {
  160. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  161. var temperature = -1
  162. db.transaction(
  163. function(tx) {
  164. //Updating information
  165. temperature = tx.executeSql('SELECT temperature FROM Sauna').rows.item(0).temperature;
  166. }
  167. )
  168. return temperature;
  169. }
  170. /**
  171. * Counts the total consumption and adds the values to the table
  172. *
  173. * @return Nothing
  174. */
  175. function loadConsumption() {
  176. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  177. db.transaction(
  178. function(tx) {
  179. var info = tx.executeSql("SELECT COUNT(*) as allrooms, SUM(light) as lightsum FROM RoomTable");
  180. var info2 = tx.executeSql("SELECT volume, mute, temp FROM RoomTable");
  181. var usedVolume = 0;
  182. var usedHeat = 0;
  183. var heatingPower = 80; // W/m^2
  184. var houseArea = 120; // m^2
  185. for (var i=0; i < info2.rows.length; i++) {
  186. if (info2.rows.item(i).mute == "false") usedVolume += parseInt(info2.rows.item(i).volume);
  187. usedHeat += parseInt(info2.rows.item(i).temp);
  188. }
  189. var saunaWatts = ( isSaunaOn() ) ? ((getSaunaTemperature()/120)*6000) : 0;
  190. var machines = 12500;
  191. var misc = 8350;
  192. var whole = 0;
  193. whole += Math.round(info.rows.item(0).lightsum*(3/5)*3+6000);
  194. whole += Math.round(usedVolume*(1.2)+5000);
  195. whole += machines;
  196. whole += misc;
  197. whole += saunaWatts;
  198. whole += Math.round( (usedHeat/(info.rows.item(0).allrooms*30)) * 80 * 120 ) + 4000;
  199. var theValues = Array();
  200. var maxPercentage = 0;
  201. lightsW.text = " " + Math.round(info.rows.item(0).lightsum*(3/5)*3+6000)/1000;
  202. theValues[0] = Array();
  203. theValues[0][0] = Math.round( ((info.rows.item(0).lightsum*(3/5)*3+6000)/whole) * 100 );
  204. if (theValues[0][0] > maxPercentage) maxPercentage = theValues[0][0];
  205. theValues[0][1] = "Lights";
  206. avW.text = " " + Math.round(usedVolume*(1.2)+5000)/1000;
  207. theValues[1] = Array();
  208. theValues[1][0] = Math.round( ((usedVolume*(1.2)+5000)/whole) * 100 );
  209. if (theValues[1][0] > maxPercentage) maxPercentage = theValues[1][0];
  210. theValues[1][1] = "AV";
  211. machinesW.text = " " + Math.round(machines)/1000;
  212. theValues[2] = Array();
  213. theValues[2][0] = Math.round( ((machines)/whole) * 100 );
  214. if (theValues[2][0] > maxPercentage) maxPercentage = theValues[2][0];
  215. theValues[2][1] = "Appliances";
  216. saunaW.text = " " + saunaWatts/1000;
  217. theValues[3] = Array();
  218. theValues[3][0] = Math.round( (saunaWatts/whole) * 100 );
  219. if (theValues[3][0] > maxPercentage) maxPercentage = theValues[3][0];
  220. theValues[3][1] = "Sauna";
  221. heatingW.text = " " + Math.round( (usedHeat/(info.rows.item(0).allrooms*30)) * 80 * 120 + 4000 )/1000;
  222. theValues[4] = Array();
  223. theValues[4][0] = Math.round( (( (usedHeat/(info.rows.item(0).allrooms*30)) * 80 * 120 + 4000 )/whole) * 100 );
  224. if (theValues[4][0] > maxPercentage) maxPercentage = theValues[4][0];
  225. theValues[4][1] = "Heating";
  226. miscW.text = " " + Math.round(misc)/1000;
  227. theValues[5] = Array();
  228. theValues[5][0] = Math.round( ((misc)/whole) * 100 );
  229. if (theValues[5][0] > maxPercentage) maxPercentage = theValues[5][0];
  230. theValues[5][1] = "Misc.";
  231. histogram2.maxValue = maxPercentage;
  232. histogram2.setValues( theValues );
  233. total.text = (whole/1000);
  234. }
  235. )
  236. }
  237. /**
  238. * Monthly energy consumption data.
  239. */
  240. weekData = Array();
  241. // Week #1 // Week #2 // Week #3
  242. weekData[0] = Array(); weekData[1] = Array(); weekData[2] = Array();
  243. weekData[0][0] = Array(); weekData[1][0] = Array(); weekData[2][0] = Array();
  244. weekData[0][0][0] = 183; weekData[1][0][0] = 360; weekData[2][0][0] = 300;
  245. weekData[0][0][1] = "M"; weekData[1][0][1] = "M"; weekData[2][0][1] = "M";
  246. weekData[0][1] = Array(); weekData[1][1] = Array(); weekData[2][1] = Array();
  247. weekData[0][1][0] = 280; weekData[1][1][0] = 260; weekData[2][1][0] = 282;
  248. weekData[0][1][1] = "T"; weekData[1][1][1] = "T"; weekData[2][1][1] = "T";
  249. weekData[0][2] = Array(); weekData[1][2] = Array(); weekData[2][2] = Array();
  250. weekData[0][2][0] = 346; weekData[1][2][0] = 250; weekData[2][2][0] = 265;
  251. weekData[0][2][1] = "W"; weekData[1][2][1] = "W"; weekData[2][2][1] = "W";
  252. weekData[0][3] = Array(); weekData[1][3] = Array(); weekData[2][3] = Array();
  253. weekData[0][3][0] = 330; weekData[1][3][0] = 265; weekData[2][3][0] = 288;
  254. weekData[0][3][1] = "T"; weekData[1][3][1] = "T"; weekData[2][3][1] = "T";
  255. weekData[0][4] = Array(); weekData[1][4] = Array(); weekData[2][4] = Array();
  256. weekData[0][4][0] = 285; weekData[1][4][0] = 280; weekData[2][4][0] = 180;
  257. weekData[0][4][1] = "F"; weekData[1][4][1] = "F"; weekData[2][4][1] = "F";
  258. weekData[0][5] = Array(); weekData[1][5] = Array(); weekData[2][5] = Array();
  259. weekData[0][5][0] = 270; weekData[1][5][0] = 313; weekData[2][5][0] = 130;
  260. weekData[0][5][1] = "S"; weekData[1][5][1] = "S"; weekData[2][5][1] = "S";
  261. weekData[0][6] = Array(); weekData[1][6] = Array(); weekData[2][6] = Array();
  262. weekData[0][6][0] = 290; weekData[1][6][0] = 330; weekData[2][6][0] = 120;
  263. weekData[0][6][1] = "S"; weekData[1][6][1] = "S"; weekData[2][6][1] = "S";
  264. weekData[0][7] = Array(); weekData[1][7] = Array(); weekData[2][7] = Array();
  265. weekData[0][7][0] = 183; weekData[1][7][0] = 360; weekData[2][7][0] = 300;
  266. weekData[0][7][1] = "M"; weekData[1][7][1] = "M"; weekData[2][7][1] = "M";
  267. weekData[0][8] = Array(); weekData[1][8] = Array(); weekData[2][8] = Array();
  268. weekData[0][8][0] = 280; weekData[1][8][0] = 260; weekData[2][8][0] = 282;
  269. weekData[0][8][1] = "T"; weekData[1][8][1] = "T"; weekData[2][8][1] = "T";
  270. weekData[0][9] = Array(); weekData[1][9] = Array(); weekData[2][9] = Array();
  271. weekData[0][9][0] = 346; weekData[1][9][0] = 250; weekData[2][9][0] = 265;
  272. weekData[0][9][1] = "W"; weekData[1][9][1] = "W"; weekData[2][9][1] = "W";
  273. weekData[0][10] = Array(); weekData[1][10] = Array(); weekData[2][10] = Array();
  274. weekData[0][10][0] = 330; weekData[1][10][0] = 265; weekData[2][10][0] = 288;
  275. weekData[0][10][1] = "T"; weekData[1][10][1] = "T"; weekData[2][10][1] = "T";
  276. weekData[0][11] = Array(); weekData[1][11] = Array(); weekData[2][11] = Array();
  277. weekData[0][11][0] = 285; weekData[1][11][0] = 280; weekData[2][11][0] = 180;
  278. weekData[0][11][1] = "F"; weekData[1][11][1] = "F"; weekData[2][11][1] = "F";