bb sync commit
[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                 $('#thermo').click(function() {
17                         thermo.draw_thermos();
18                 });
19         },
20         draw: function() {
21                 thermo.get_thermos();
22         },
23         get_thermos: function() {
24                 xhr("POST","cgi-bin/fhemwrapper","list",
25                     function(ret) {
26                         // extract thermos
27                         var thermos=ret.match(/\ \ MAX_[0-9,a-f]{6}\ /g);
28                         thermo.get_thermo_details(thermos);
29                 });
30         },
31         thermos: {},
32         get_thermo_details: function(thermos) {
33                 for(var th in thermos) {
34                         var id=thermos[th];
35                         cl("xhr: list "+id);
36                         xhr("POST","cgi-bin/fhemwrapper","list "+id,
37                             function(ret) {
38                                 var id=ret.match(/NAME.*/)+'';
39                                 id=id.replace(/NAME\s+/,"");
40                                 var alias=ret.match(/alias.*/)+'';
41                                 alias=alias.replace(/alias\s+/,"");
42                                 var state=ret.match(/STATE.*/)+'';
43                                 state=state.replace(/STATE\s+/,"");
44                                 var eco=ret.match(/ecoTemperature.*/)+'';
45                                 eco=eco.replace(/.*ecoTemperature\s+/,"");
46                                 var comfort=ret.match(/comfortTem.*/)+'';
47                                 comfort=comfort.replace(/.*comfortTem.*\s+/,"");
48                                 thermo.thermos[id]={};
49                                 thermo.thermos[id].alias=alias;
50                                 thermo.thermos[id].state=state;
51                                 thermo.thermos[id].eco=eco;
52                                 thermo.thermos[id].comfort=comfort;
53                                 thermo.draw_thermos();
54                         });
55                 }
56         },
57         draw_thermo: function(alias,name,state,eco,comfort,type) {
58                 var html="<div class=thermo>";
59                 switch(type) {
60                 case 'd':
61                         html+="<div class=thermoalias>"+alias+"</div>";
62                         html+="<div class=thermoname>"+name+"</div>";
63                         html+="<div class=thermostate>"+state+"</div>";
64                         html+="<div class=thermoeco>"+eco+"</div>";
65                         html+="<div class=thermocomfort>"+comfort+"</div>";
66                         break;
67                 case 'h':
68                         html+="<div class=thermoalias><b>"+alias+"</b></div>";
69                         html+="<div class=thermoname><b>"+name+"</b></div>";
70                         html+="<div class=thermostate><b>"+state+"</b></div>";
71                         html+="<div class=thermoeco><b>"+eco+"</b></div>";
72                         html+="<div class=thermocomfort><b>"+comfort+
73                               "</b></div>";
74                         break;
75                 }
76                 html+="</div>";
77                 return html;
78         },
79         draw_thermos: function() {
80                 var html=thermo.draw_thermo('Alias','Name','State',
81                                             'Eco','Comfort','h');
82                 for(var i in thermo.thermos) {
83                         var th=thermo.thermos[i];
84                         html+=thermo.draw_thermo(th.alias,i,th.state,
85                                                  th.eco,th.comfort,'d');
86                 }
87                 $('div#thermobody').html(html);
88         }
89 };
90
91 var cmd = {
92         init: function() {
93                 $('#cmdout').css('display','none');
94                 $('#cmdin').keydown(function(key) {
95                         if(key.keyCode==13)
96                                 cmd.execute();
97                 });
98         },
99         execute: function() {
100                 var cmdline=$('#cmdin').val();
101                 xhr("POST","cgi-bin/fhemwrapper",cmdline,function(ret) {
102                         $('#cmdout').css('display','block');
103                         $('#cmdouttxt').val(ret);
104                 });
105                 $('#cmdin').val("");
106         }
107 };
108
109 var intres = {
110         init: function() {
111                 document.addEventListener('deviceready',intres.startup,false);
112         },
113         startup: function() {
114                 // cmd
115                 cmd.init();
116                 // config
117                 //config.init();
118                 // thermostat
119                 thermo.init();
120         }
121 };
122
123 $(document).ready(function() {
124         if(Modernizr.hasEvent('deviceready')) {
125                 intres.init();
126         }
127         else {
128                 intres.startup();
129         }
130 });
131