initial checkin
[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                                 thermo.thermos[id]={};
42                                 thermo.thermos[id].alias=alias;
43                                 thermo.thermos[id].temp=state;
44                                 thermo.draw_thermos();
45                         });
46                 }
47         },
48         draw_thermos: function() {
49                 var html="";
50                 for(var i in thermo.thermos) {
51                         var th=thermo.thermos[i];
52                         html+=th.alias+" "+th.temp+"<br>";
53                 }
54                 tdiv=$('div#thermobody');
55                 tdiv.html(html);
56         }
57 };
58
59 var cmd = {
60         init: function() {
61                 $('#cmdout').css('display','none');
62                 $('#cmdin').keydown(function(key) {
63                         if(key.keyCode==13)
64                                 cmd.execute();
65                 });
66         },
67         execute: function() {
68                 var cmdline=$('#cmdin').val();
69                 xhr("POST","cgi-bin/fhemwrapper",cmdline,function(ret) {
70                         $('#cmdout').css('display','block');
71                         $('#cmdouttxt').val(ret);
72                 });
73                 $('#cmdin').val("");
74         }
75 };
76
77 var intres = {
78         init: function() {
79                 document.addEventListener('deviceready',intres.startup,false);
80         },
81         startup: function() {
82                 // cmd
83                 cmd.init();
84                 // config
85                 //config.init();
86                 // thermostat
87                 thermo.init();
88         }
89 };
90
91 $(document).ready(function() {
92         if(Modernizr.hasEvent('deviceready')) {
93                 intres.init();
94         }
95         else {
96                 intres.startup();
97         }
98 });
99