edbf31725b19d8ff636c9fc35b4bfac8ed973c73
[outofuni/intres.git] / js / intres.js
1 /*
2  * intres - intelligent reside
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  */
7
8 var config = {
9         init: function() {
10         }
11 };
12
13 var thermo = {
14         init: function() {
15                 thermo.draw();
16         },
17         draw: function() {
18                 thermo.get_thermos();
19         },
20         get_thermos: function() {
21                 xhr("POST","cgi-bin/fhemwrapper","list",
22                     function(ret) {
23                         // extract thermos
24                         var thermos=ret.match(/\ \ MAX_[0-9,a-f]{6}\ /g);
25                         thermo.get_thermo_details(thermos);
26                 });
27         },
28         thermos: {},
29         get_thermo_details: function(thermos) {
30                 for(var th in thermos) {
31                         var id=thermos[th];
32                         cl("xhr: list "+id);
33                         xhr("POST","cgi-bin/fhemwrapper","list "+id,
34                             function(ret) {
35                                 var id=ret.match(/NAME.*/)+'';
36                                 id=id.replace(/NAME\s+/,"");
37                                 var alias=ret.match(/alias.*/)+'';
38                                 alias=alias.replace(/alias\s+/,"");
39                                 var state=ret.match(/STATE.*/)+'';
40                                 state=state.replace(/STATE\s+/,"");
41                                 var eco=ret.match(/ecoTemperature.*/)+'';
42                                 eco=eco.replace(/.*ecoTemperature\s+/,"");
43                                 var comfort=ret.match(/comfortTemperature.*/)+'';
44                                 comfort=comfort.replace(/.*comfortTemperature\s+/,"");
45                                 thermo.thermos[id]={};
46                                 thermo.thermos[id].alias=alias;
47                                 thermo.thermos[id].state=state;
48                                 thermo.thermos[id].eco=eco;
49                                 thermo.thermos[id].comfort=comfort;
50                                 thermo.draw_thermos();
51                         });
52                 }
53         },
54         draw_thermo: function(alias,name,state,eco,comfort) {
55                 var html="";
56                 html+="<div class=thermoalias>"+alias+"</div>";
57                 html+="<div class=thermoname>"+name+"</div>";
58                 html+="<div class=thermostate>"+state+"</div>";
59                 html+="<div class=thermoeco>"+eco+"</div>";
60                 html+="<div class=thermocomfort>"+comfort+"</div>";
61                 return html;
62         },
63         draw_thermos: function() {
64                 var html="";
65                 for(var i in thermo.thermos) {
66                         var th=thermo.thermos[i];
67                         html+=thermo.draw_thermo(th.alias,i,th.state,
68                                                  th.eco,th.comfort);
69                 }
70                 $('div#thermobody').html(html);
71         }
72 };
73
74 var cmd = {
75         init: function() {
76                 $('#cmdout').css('display','none');
77                 $('#cmdin').keydown(function(key) {
78                         if(key.keyCode==13)
79                                 cmd.execute();
80                 });
81         },
82         execute: function() {
83                 var cmdline=$('#cmdin').val();
84                 xhr("POST","cgi-bin/fhemwrapper",cmdline,function(ret) {
85                         $('#cmdout').css('display','block');
86                         $('#cmdouttxt').val(ret);
87                 });
88                 $('#cmdin').val("");
89         }
90 };
91
92 var intres = {
93         init: function() {
94                 document.addEventListener('deviceready',intres.startup,false);
95         },
96         startup: function() {
97                 // cmd
98                 cmd.init();
99                 // config
100                 //config.init();
101                 // thermostat
102                 thermo.init();
103         }
104 };
105
106 $(document).ready(function() {
107         if(Modernizr.hasEvent('deviceready')) {
108                 intres.init();
109         }
110         else {
111                 intres.startup();
112         }
113 });
114