sql.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /*
  49. * HOME CONTROL SYSTEM - SQL FUNCTIONS
  50. * This file contains all the necessary functions
  51. * for Home Control Systems database connectivity.
  52. */
  53. // Important variables
  54. var theRoom = new Array();
  55. var roomx = new Array();
  56. var cur_roomCode = "";
  57. var cur_info = Array();
  58. var theme = 0;
  59. var lockStat = false;
  60. /**
  61. * Holds information for a Room object
  62. *
  63. * @param roomCode Rooms unique ID
  64. * @param roomName Rooms name
  65. * @param temp Room temperature
  66. * @param light Room lights level
  67. * @param volume Room audio volume
  68. * @param mute Room audio mute
  69. * @param fireAlarm True if there's a fire in the room
  70. * @param burglarAlarm True if burglar alarm is triggered in this room
  71. * @return Nothing
  72. */
  73. function Room(roomCode, roomName, temp, light, volume, mute, fireAlarm, burglarAlarm) {
  74. this.light = light;
  75. this.volume = volume;
  76. this.mute = mute;
  77. this.temperature = temp;
  78. this.fireAlarm = fireAlarm;
  79. this.burglarAlarm = burglarAlarm;
  80. this.name = roomName;
  81. this.code = roomCode;
  82. this.lightMin = 0;
  83. this.lightMax = 100;
  84. this.volumeMin = 0;
  85. this.volumeMax = 100;
  86. this.temperatureMin = 18;
  87. this.temperatureMax = 30;
  88. }
  89. /**
  90. * Resets demo application info
  91. *
  92. * @return Nothing
  93. */
  94. function setDefaults() {
  95. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  96. db.transaction(
  97. function(tx) {
  98. // Create the database if it doesn't already exist
  99. tx.executeSql('CREATE TABLE IF NOT EXISTS RoomTable(roomCode TEXT, roomName TEXT, temp INT, tempMin INT, tempMax INT, light INT, lightMin INT, lightMax INT, volume INT, volumeMin INT, volumeMax INT, mute BOOL, fireAlarm BOOL, burglarAlarm BOOL)');
  100. tx.executeSql('CREATE TABLE IF NOT EXISTS Themes(style INT)');
  101. tx.executeSql('CREATE TABLE IF NOT EXISTS Sauna(saunaOn BOOLEAN default false, temperature DECIMAL default 0)');
  102. tx.executeSql('CREATE TABLE IF NOT EXISTS Security(alarmOn BOOLEAN default false, doorsLocked BOOLEAN default false)');
  103. // Delete all existing info
  104. var deleteResult = tx.executeSql('DELETE FROM RoomTable');
  105. deleteResult = tx.executeSql('DELETE FROM Themes');
  106. deleteResult = tx.executeSql('DELETE FROM Sauna');
  107. deleteResult = tx.executeSql('DELETE FROM Security');
  108. // Add all the rooms with default values
  109. tx.executeSql('INSERT INTO RoomTable VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 'room1', 'Bedroom 1', 18, 18, 30, 80, 0, 100, 10, 0, 100, true, false, false ]);
  110. tx.executeSql('INSERT INTO RoomTable VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 'room2', 'Bedroom 2', 19, 18, 30, 70, 0, 100, 20, 0, 100, false, false, false ]);
  111. tx.executeSql('INSERT INTO RoomTable VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 'room3', 'Master bedroom', 20, 18, 30, 60, 0, 100, 30, 0, 100, true, false, false ]);
  112. tx.executeSql('INSERT INTO RoomTable VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 'kitchen', 'Kitchen', 21, 18, 40, 50, 0, 100, 40, 0, 100, true, false, false ]);
  113. tx.executeSql('INSERT INTO RoomTable VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 'livingroom', 'Living room', 22, 18, 30, 40, 0, 100, 50, 0, 100, true, false, false ]);
  114. tx.executeSql('INSERT INTO RoomTable VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 'khh', 'Utility room', 23, 18, 30, 30, 0, 100, 60, 0, 100, true, false, false ]);
  115. tx.executeSql('INSERT INTO RoomTable VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 'kph', 'Bathroom', 24, 18, 30, 20, 0, 100, 70, 0, 100, false, false, false ]);
  116. tx.executeSql('INSERT INTO RoomTable VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ 'sauna', 'Sauna', 25, 18, 30, 10, 0, 100, 80, 0, 100, true, false, false ]);
  117. tx.executeSql('INSERT INTO Themes VALUES(?)', [ 1 ]);
  118. tx.executeSql('INSERT INTO Sauna VALUES(?,?)', [false,0]);
  119. tx.executeSql('INSERT INTO Security VALUES(?,?)', [false,false]);
  120. }
  121. )
  122. return;
  123. }
  124. /**
  125. * Saves changes to a room
  126. *
  127. * @param roomCode Unique room ID
  128. * @param temp Room temperature
  129. * @param Room lights level
  130. * @param Room audio volume
  131. * @param Room audio mute
  132. * @return Nothing
  133. */
  134. function saveRoomInfo(roomCode, temp, light, volume, mute) {
  135. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  136. db.transaction(
  137. function(tx) {
  138. // Show all rooms
  139. var RoomTable = tx.executeSql("SELECT fireAlarm, burglarAlarm, roomName FROM RoomTable WHERE roomCode='"+roomCode+"' LIMIT 1");
  140. var returnThis = new Array(RoomTable.rows.item(0).fireAlarm, RoomTable.rows.item(0).burglarAlarm, RoomTable.rows.item(0).roomName);
  141. // Delete existing room info
  142. var deleteResult = tx.executeSql("DELETE FROM RoomTable WHERE roomCode='"+roomCode+"'");
  143. // Save new info
  144. tx.executeSql('INSERT INTO RoomTable VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ roomCode, returnThis[2], temp, 18, 30, light, 0, 100, volume, 0, 100, mute, returnThis[0], returnThis[1] ]);
  145. }
  146. )
  147. }
  148. /**
  149. * Creates a new room object with all the infor of that room
  150. *
  151. * @param roomCode Unique room ID
  152. * @return Nothing
  153. */
  154. function getTheRoom(roomCode) {
  155. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  156. db.transaction(
  157. function(tx) {
  158. // Get all rooms
  159. var RoomTable = tx.executeSql("SELECT * FROM RoomTable WHERE roomCode='"+roomCode+"'");
  160. theRoom = new Room(RoomTable.rows.item(0).roomCode, RoomTable.rows.item(0).roomName, RoomTable.rows.item(0).temp, RoomTable.rows.item(0).light, RoomTable.rows.item(0).volume, RoomTable.rows.item(0).mute, RoomTable.rows.item(0).fireAlarm, RoomTable.rows.item(0).burglarAlarm);
  161. }
  162. )
  163. }
  164. /**
  165. * Gets some info from a specific room and stores it in an array.
  166. *
  167. * @param roomCode Unique room ID
  168. * @return Array
  169. */
  170. function getRoom(roomCode) {
  171. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  172. return db.transaction(
  173. function(tx) {
  174. // Show all rooms
  175. var RoomTable = tx.executeSql("SELECT light, volume, mute, fireAlarm, burglarAlarm FROM RoomTable WHERE roomCode='"+roomCode+"' LIMIT 1");
  176. var returnThis = new Array(RoomTable.rows.item(0).light, RoomTable.rows.item(0).volume, RoomTable.rows.item(0).mute, RoomTable.rows.item(0).fireAlarm, RoomTable.rows.item(0).burglarAlarm);
  177. roomx = returnThis;
  178. }
  179. )
  180. return roomx;
  181. }
  182. /**
  183. * Checks door locks status
  184. *
  185. * @return boolean
  186. */
  187. function getLockStatus() {
  188. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  189. return db.transaction(
  190. function(tx) {
  191. var RoomTable = tx.executeSql("SELECT doorsLocked FROM Security LIMIT 1");
  192. lockStat = RoomTable.rows.item(0).doorsLocked;
  193. }
  194. )
  195. return lockStat;
  196. }
  197. /**
  198. * Sets door lock status
  199. *
  200. * @param newLocks New status for door locks
  201. * @return Nothing
  202. */
  203. function setLocks( newLocks ) {
  204. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  205. return db.transaction(
  206. function(tx) {
  207. tx.executeSql('UPDATE Security SET doorsLocked="'+newLocks+'"');
  208. }
  209. )
  210. }
  211. /**
  212. * Sets HCS theme
  213. *
  214. * @param style Name of the new theme
  215. * @return Nothing
  216. */
  217. function setTheme(style) {
  218. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  219. db.transaction(
  220. function(tx) {
  221. var themes = tx.executeSql("SELECT style FROM Themes");
  222. var returnThis = themes.rows.item(0).style;
  223. // Delete existing style info
  224. var deleteResult = tx.executeSql("DELETE FROM Themes");
  225. // Save new info
  226. tx.executeSql('INSERT INTO Themes VALUES(?)', [style]);
  227. }
  228. )
  229. }
  230. /**
  231. * Gets HCS current theme
  232. *
  233. * @return string
  234. */
  235. function getTheme() {
  236. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  237. return db.transaction(
  238. function(tx) {
  239. // fetch theme
  240. var themes = tx.executeSql("SELECT style FROM Themes");
  241. theme = themes.rows.item(0).style;
  242. }
  243. )
  244. return theme;
  245. }
  246. /**
  247. * Sets sauna on
  248. *
  249. * @param saunaOn
  250. * @return Nothing
  251. */
  252. function setSaunaOn(saunaOn) {
  253. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  254. db.transaction(
  255. function(tx) {
  256. //Updating information
  257. tx.executeSql('UPDATE Sauna SET saunaOn="'+saunaOn+'"');
  258. }
  259. )
  260. }
  261. /**
  262. * Gets sauna status
  263. *
  264. * @return boolean
  265. */
  266. function isSaunaOn() {
  267. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  268. var saunaOn = false;
  269. db.transaction(
  270. function(tx) {
  271. //Updating information
  272. saunaOn = tx.executeSql('SELECT saunaOn FROM Sauna').rows.item(0).saunaOn;
  273. }
  274. )
  275. if(saunaOn == "true") return true
  276. if(saunaOn == "false") return false
  277. }
  278. /**
  279. * Sets sauna temperature
  280. *
  281. * @param temperature New sauna temperature
  282. * @return Nothing
  283. */
  284. function setSaunaTemperature(temperature) {
  285. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  286. db.transaction(
  287. function(tx) {
  288. //Updating information
  289. tx.executeSql('UPDATE Sauna SET temperature='+temperature);
  290. }
  291. )
  292. }
  293. /**
  294. * Gets sauna temperature
  295. *
  296. * @return int
  297. */
  298. function getSaunaTemperature() {
  299. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  300. var temperature = -1
  301. db.transaction(
  302. function(tx) {
  303. //Updating information
  304. temperature = tx.executeSql('SELECT temperature FROM Sauna').rows.item(0).temperature;
  305. }
  306. )
  307. return temperature;
  308. }
  309. /**
  310. * Changes door locks status
  311. *
  312. * @param doors Locks status
  313. * @return Nothing
  314. */
  315. function setDoorsLocked(doors) {
  316. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  317. db.transaction(
  318. function(tx) {
  319. //Updating information
  320. tx.executeSql('UPDATE Security SET doorsLocked="'+doors+'"');
  321. }
  322. )
  323. }
  324. /**
  325. * Checks whether the doors are locked
  326. *
  327. * @return boolean
  328. */
  329. function isDoorsLocked() {
  330. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  331. var doorsLocked = false;
  332. db.transaction(
  333. function(tx) {
  334. //Updating information
  335. doorsLocked = tx.executeSql('SELECT doorsLocked FROM Security').rows.item(0).doorsLocked;
  336. }
  337. )
  338. if(doorsLocked == "true") return true
  339. if(doorsLocked == "false") return false
  340. }
  341. /**
  342. * Sets alarm system mode
  343. *
  344. * @param alarm New status for the alarm system
  345. * @return Nothing
  346. */
  347. function setAlarmSystemOn(alarm) {
  348. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  349. db.transaction(
  350. function(tx) {
  351. //Updating information
  352. tx.executeSql('UPDATE Security SET alarmOn="'+alarm+'"');
  353. }
  354. )
  355. }
  356. /**
  357. * Checks whether the alarm system is on
  358. *
  359. * @return Nothing
  360. */
  361. function isAlarmSystemOn() {
  362. var db = openDatabaseSync("HomeControl", "1.0", "CoDe Qt Quick application", 1000000);
  363. var alarmOn = false;
  364. db.transaction(
  365. function(tx) {
  366. //Updating information
  367. alarmOn = tx.executeSql('SELECT alarmOn FROM Security').rows.item(0).alarmOn;
  368. }
  369. )
  370. if(alarmOn == "true") return true
  371. if(alarmOn == "false") return false
  372. }