yaahm.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //########################################################################################
  2. // yaahm.js
  3. // Version 1.45
  4. // See 95_YAAHM for licensing
  5. //########################################################################################
  6. //# Prof. Dr. Peter A. Henning
  7. //------------------------------------------------------------------------------------------------------
  8. // Determine csrfToken
  9. //------------------------------------------------------------------------------------------------------
  10. var req = new XMLHttpRequest();
  11. req.open('GET', document.location.href, false);
  12. req.send(null);
  13. var csrfToken = req.getResponseHeader('X-FHEM-csrfToken');
  14. if( csrfToken == null ){
  15. csrfToken = "null";
  16. }
  17. //------------------------------------------------------------------------------------------------------
  18. // encode Parameters for URL
  19. //------------------------------------------------------------------------------------------------------
  20. function encodeParm(oldval) {
  21. var newval;
  22. newval = oldval.replace(/"/g, '%27');
  23. newval = newval.replace(/#/g, '%23');
  24. newval = newval.replace(/\+/g, '%2B');
  25. newval = newval.replace(/&/g, '%26');
  26. newval = newval.replace(/'/g, '%27');
  27. newval = newval.replace(/=/g, '%3D');
  28. newval = newval.replace(/\?/g, '%3F');
  29. newval = newval.replace(/\|/g, '%7C');
  30. return newval;
  31. }
  32. // Tool Tips
  33. // $( function() {
  34. // $( document ).tooltip();
  35. // } );
  36. //------------------------------------------------------------------------------------------------------
  37. // Expand text box
  38. //------------------------------------------------------------------------------------------------------
  39. $(function () {
  40. $(".expand").focus(function () {
  41. $(this).animate({
  42. width: '200px'
  43. },
  44. "slow")
  45. });
  46. });
  47. $(function () {
  48. $(".expand").blur(function () {
  49. $(this).animate({
  50. width: '100px'
  51. },
  52. "slow")
  53. });
  54. });
  55. //------------------------------------------------------------------------------------------------------
  56. // Write the Attribute Value
  57. //------------------------------------------------------------------------------------------------------
  58. function yaahm_setAttribute(name, attr, val) {
  59. //set Yaahm Attribute
  60. var location = document.location.pathname;
  61. if (location.substr(location.length -1, 1) == '/') {
  62. location = location.substr(0, location.length -1);
  63. }
  64. var url = document.location.protocol + "//" + document.location.host + location;
  65. FW_cmd(url + '?XHR=1&fwcsrf=' + csrfToken + '&cmd.' + name + '=attr ' + name + ' ' + encodeParm(attr) + ' ' + encodeParm(val));
  66. }
  67. //------------------------------------------------------------------------------------------------------
  68. // Change mode and state, set next time
  69. //------------------------------------------------------------------------------------------------------
  70. var hsold;
  71. var hmold;
  72. function yaahm_mode(name, targetmode) {
  73. var location = document.location.pathname;
  74. if (location.substr(location.length -1, 1) == '/') {
  75. location = location.substr(0, location.length -1);
  76. }
  77. var url = document.location.protocol + "//" + document.location.host + location;
  78. FW_cmd(url + '?XHR=1&fwcsrf=' + csrfToken + '&cmd.' + name + '={main::YAAHM_mode("' + name + '","' + targetmode + '")}');
  79. }
  80. function yaahm_state(name, targetstate) {
  81. var location = document.location.pathname;
  82. if (location.substr(location.length -1, 1) == '/') {
  83. location = location.substr(0, location.length -1);
  84. }
  85. var url = document.location.protocol + "//" + document.location.host + location;
  86. FW_cmd(url + '?XHR=1&fwcsrf=' + csrfToken + '&cmd.' + name + '={main::YAAHM_state("' + name + '","' + targetstate + '")}');
  87. }
  88. function yaahm_setnext(name, i) {
  89. var location = document.location.pathname;
  90. if (location.substr(location.length -1, 1) == '/') {
  91. location = location.substr(0, location.length -1);
  92. }
  93. var url = document.location.protocol + "//" + document.location.host + location;
  94. var nval;
  95. if (document.getElementById('wt' + i + '_n') !== null) {
  96. nval = document.getElementById('wt' + i + '_n').value;
  97. } else {
  98. nval = "undef";
  99. }
  100. FW_cmd(url + '?XHR=1&fwcsrf=' + csrfToken + '&cmd.' + name + '={main::YAAHM_nextWeeklyTime("' + name + '","next_' + i + '","' + nval + '")}');
  101. }
  102. //------------------------------------------------------------------------------------------------------
  103. // Write field value for next - first two here, the others dynamically
  104. //------------------------------------------------------------------------------------------------------
  105. $("body").on('DOMSubtreeModified', "#wt0_o",
  106. function () {
  107. nval = document.getElementById("wt0_o").innerHTML;
  108. document.getElementById("wt0_n").value = nval;
  109. })
  110. $("body").on('DOMSubtreeModified', "#wt1_o",
  111. function () {
  112. nval = document.getElementById("wt1_o").innerHTML;
  113. document.getElementById("wt1_n").value = nval;
  114. })
  115. //------------------------------------------------------------------------------------------------------
  116. // Animate housestate icon
  117. //------------------------------------------------------------------------------------------------------
  118. var blinker;
  119. var hsfill;
  120. var hscolor;
  121. function blinkhs() {
  122. var w = document.getElementById("wid_hs");
  123. if (w) {
  124. if (hsfill == hscolor) {
  125. hsfill = "white";
  126. w.getElementsByClassName("hs_is")[0].setAttribute("fill", "white");
  127. } else {
  128. hsfill = hscolor;
  129. w.getElementsByClassName("hs_is")[0].setAttribute("fill", hscolor);
  130. }
  131. }
  132. }
  133. $("body").on('DOMSubtreeModified', "#sym_hs",
  134. function () {
  135. var w = document.getElementById("wid_hs");
  136. if (w) {
  137. var symnew = document.getElementById("sym_hs").innerHTML;
  138. if (blinking == 1 && symnew.includes("green")) {
  139. clearInterval(blinker);
  140. blinking = 0;
  141. w.getElementsByClassName("hs_is")[0].setAttribute("fill", hscolor);
  142. } else {
  143. if (blinking == 0 && ! symnew.includes("green")) {
  144. hscolor = w.getElementsByClassName("hs_is")[0].getAttribute("fill");
  145. blinker = setInterval('blinkhs()', 1000);
  146. blinking = 1;
  147. }
  148. }
  149. }
  150. })
  151. $("body").on('DOMSubtreeModified', "#hid_hs",
  152. function () {
  153. var hsnew = document.getElementById("hid_hs").innerHTML;
  154. if (hsnew != hsold) {
  155. hsold = hsnew;
  156. var w = document.getElementById("wid_hs");
  157. if (w) {
  158. switch (hsnew) {
  159. case "unsecured":
  160. hscolor = csstate[0];
  161. w.getElementsByClassName("hs_is")[0].setAttribute("fill", csstate[0]);
  162. w.getElementsByClassName("hs_smb")[0].setAttribute("visibility", "hidden");
  163. w.getElementsByClassName("hs_unlocked")[0].setAttribute("visibility", "visible");
  164. w.getElementsByClassName("hs_locked")[0].setAttribute("visibility", "hidden");
  165. w.getElementsByClassName("hs_eye")[0].setAttribute("visibility", "hidden");
  166. break;
  167. case "secured":
  168. hscolor = csstate[1];
  169. w.getElementsByClassName("hs_is")[0].setAttribute("fill", csstate[1]);
  170. w.getElementsByClassName("hs_smb")[0].setAttribute("visibility", "hidden");
  171. w.getElementsByClassName("hs_unlocked")[0].setAttribute("visibility", "hidden");
  172. w.getElementsByClassName("hs_locked")[0].setAttribute("visibility", "visible");
  173. w.getElementsByClassName("hs_eye")[0].setAttribute("visibility", "hidden");
  174. break;
  175. case "protected":
  176. hscolor = csstate[2];
  177. w.getElementsByClassName("hs_is")[0].setAttribute("fill", csstate[2]);
  178. w.getElementsByClassName("hs_smb")[0].setAttribute("visibility", "visible");
  179. w.getElementsByClassName("hs_unlocked")[0].setAttribute("visibility", "hidden");
  180. w.getElementsByClassName("hs_locked")[0].setAttribute("visibility", "visible");
  181. w.getElementsByClassName("hs_eye")[0].setAttribute("visibility", "hidden");
  182. break;
  183. case "guarded":
  184. hscolor = csstate[3];
  185. w.getElementsByClassName("hs_is")[0].setAttribute("fill", csstate[3]);
  186. w.getElementsByClassName("hs_smb")[0].setAttribute("visibility", "visible");
  187. w.getElementsByClassName("hs_unlocked")[0].setAttribute("visibility", "hidden");
  188. w.getElementsByClassName("hs_locked")[0].setAttribute("visibility", "visible");
  189. w.getElementsByClassName("hs_eye")[0].setAttribute("visibility", "visible");
  190. break;
  191. }
  192. } else {
  193. alert("state widget not found");
  194. }
  195. }
  196. });
  197. $("body").on('DOMSubtreeModified', "#hid_hm",
  198. function () {
  199. var hmnew = document.getElementById("hid_hm").innerHTML;
  200. if (hmnew != hmold) {
  201. hmold = hmnew;
  202. var w = document.getElementById("wid_hm");
  203. if (w) {
  204. switch (hmnew) {
  205. case "normal":
  206. w.getElementsByClassName("hm_is")[0].setAttribute("fill", csmode[0]);
  207. w.getElementsByClassName("hm_n")[0].setAttribute("visibility", "visible");
  208. w.getElementsByClassName("hm_p")[0].setAttribute("visibility", "hidden");
  209. w.getElementsByClassName("hm_a")[0].setAttribute("visibility", "hidden");
  210. w.getElementsByClassName("hm_dnd")[0].setAttribute("visibility", "hidden");
  211. break;
  212. case "party":
  213. w.getElementsByClassName("hm_is")[0].setAttribute("fill", csmode[1]);
  214. w.getElementsByClassName("hm_n")[0].setAttribute("visibility", "hidden");
  215. w.getElementsByClassName("hm_p")[0].setAttribute("visibility", "visible");
  216. w.getElementsByClassName("hm_a")[0].setAttribute("visibility", "hidden");
  217. w.getElementsByClassName("hm_dnd")[0].setAttribute("visibility", "hidden");
  218. break;
  219. case "absence":
  220. w.getElementsByClassName("hm_is")[0].setAttribute("fill", csmode[2]);
  221. w.getElementsByClassName("hm_n")[0].setAttribute("visibility", "hidden");
  222. w.getElementsByClassName("hm_p")[0].setAttribute("visibility", "hidden");
  223. w.getElementsByClassName("hm_a")[0].setAttribute("visibility", "visible");
  224. w.getElementsByClassName("hm_dnd")[0].setAttribute("visibility", "hidden");
  225. break;
  226. case "donotdisturb":
  227. w.getElementsByClassName("hm_is")[0].setAttribute("fill", csmode[3]);
  228. w.getElementsByClassName("hm_n")[0].setAttribute("visibility", "hidden");
  229. w.getElementsByClassName("hm_p")[0].setAttribute("visibility", "hidden");
  230. w.getElementsByClassName("hm_a")[0].setAttribute("visibility", "hidden");
  231. w.getElementsByClassName("hm_dnd")[0].setAttribute("visibility", "visible");
  232. break;
  233. }
  234. } else {
  235. alert("mode widget not found");
  236. }
  237. }
  238. });
  239. //------------------------------------------------------------------------------------------------------
  240. // Start the daily timer
  241. //------------------------------------------------------------------------------------------------------
  242. function yaahm_startDayTimer(name) {
  243. var location = document.location.pathname;
  244. if (location.substr(location.length -1, 1) == '/') {
  245. location = location.substr(0, location.length -1);
  246. }
  247. var url = document.location.protocol + "//" + document.location.host + location;
  248. // saving start and end times
  249. for (var i = 0; i < dailyno; i++) {
  250. var sval, eval, xval, aval1, aval2;
  251. if ((dailykeys[i] != 'wakeup') && (dailykeys[i] != 'sleep')) {
  252. if (document.getElementById('dt' + dailykeys[i] + '_s') !== null) {
  253. sval = document.getElementById('dt' + dailykeys[i] + '_s').value;
  254. } else {
  255. sval = "undef"
  256. }
  257. if (document.getElementById('dt' + dailykeys[i] + '_e') !== null) {
  258. eval = document.getElementById('dt' + dailykeys[i] + '_e').value;
  259. } else {
  260. eval = "undef"
  261. }
  262. if (document.getElementById('dt' + dailykeys[i] + '_x') !== null) {
  263. xval = encodeParm(document.getElementById('dt' + dailykeys[i] + '_x').value);
  264. } else {
  265. xval = "undef"
  266. }
  267. aval1 = $("input[name='actim" + dailykeys[i] + "']:checked").map(function () {
  268. return $(this).val();
  269. }).get();
  270. aval2 = $("input[name='actid" + dailykeys[i] + "']:checked").map(function () {
  271. return $(this).val();
  272. }).get();
  273. FW_cmd(url + '?XHR=1&fwcsrf=' + csrfToken + '&cmd.' + name + '={main::YAAHM_setParm("' + name + '","dt","' + dailykeys[i] + '",' + '"' + sval + '","' + eval + '","' + xval + '","' + aval1 + ';' + aval2 + '")}');
  274. }
  275. }
  276. // really start it now
  277. FW_cmd(url + '?XHR=1&fwcsrf=' + csrfToken + '&cmd.' + name + ' ={main::YAAHM_startDayTimer("' + name + '")}');
  278. // change link
  279. $('#dtlink').html('<a href="/fhem?detail=' + name + '.dtimer.IF">' + name + '.dtimer.IF</a>');
  280. }
  281. //------------------------------------------------------------------------------------------------------
  282. // Weekly profile
  283. //------------------------------------------------------------------------------------------------------
  284. function yaahm_startWeeklyTimer(name) {
  285. var location = document.location.pathname;
  286. if (location.substr(location.length -1, 1) == '/') {
  287. location = location.substr(0, location.length -1);
  288. }
  289. var url = document.location.protocol + "//" + document.location.host + location;
  290. // saving start weekly times
  291. // iterate over different weekly tables
  292. for (var i = 0; i < weeklyno; i++) {
  293. var xval;
  294. var nval;
  295. var aval1, aval2;
  296. var sval =[ "", "", "", "", "", "", ""];
  297. //action
  298. if (document.getElementById('wt' + i + '_x') !== null) {
  299. xval = encodeParm(document.getElementById('wt' + i + '_x').value);
  300. } else {
  301. xval = "undef"
  302. }
  303. //next time - attention, field is in toptable
  304. if (document.getElementById('wt' + i + '_n') !== null) {
  305. nval = document.getElementById('wt' + i + '_n').value;
  306. } else {
  307. nval = "undef"
  308. }
  309. //activity party/absence
  310. aval1 = $("input[name='acti_" + i + "_m']:checked").map(function () {
  311. return $(this).val();
  312. }).get();
  313. //activity vacation/holiday
  314. aval2 = $("input[name='acti_" + i + "_d']:checked").map(function () {
  315. return $(this).val();
  316. }).get();
  317. //iterate over days of week
  318. for (var j = 0; j < 7; j++) {
  319. if (document.getElementById('wt' + weeklykeys[j] + i + '_s') !== null) {
  320. sval[j] = document.getElementById('wt' + weeklykeys[j] + i + '_s').value;
  321. } else {
  322. sval[j] = "undef";
  323. }
  324. }
  325. FW_cmd(url + '?XHR=1&fwcsrf=' + csrfToken + '&cmd.' + name + '={main::YAAHM_setParm("' + name + '","wt","' + i + '","' + xval + '","' + nval + '","' + aval1 + '","' + aval2 + '","' + sval.join('","') + '")}');
  326. }
  327. // really start it now
  328. FW_cmd(url + '?XHR=1&fwcsrf=' + csrfToken + '&cmd.' + name + ' ={main::YAAHM_startWeeklyTimer("' + name + '")}');
  329. // change links
  330. for (var i = 0; i < weeklyno; i++) {
  331. $('#wt' + i + 'link').html('<a href="fhem?detail=' + name + '.wtimer_' + i + '.IF">' + name + '.wtimer_' + i + '.IF</a>');
  332. }
  333. }