PageRenderTime 35ms CodeModel.GetById 18ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/yitai/json-rpc/ehomeRPC.php

http://jqian.googlecode.com/
PHP | 300 lines | 246 code | 24 blank | 30 comment | 21 complexity | c92b15605ce728a066b9cee599a0ebb9 MD5 | raw file
  1<?php
  2// ehomeRPC.php --- Time-stamp: <2010-10-28 13:34:25 ??? by lancer>
  3// Copyright 2010 Julian Qian
  4// Author: lancer@yitai01
  5// Version: $Id: ehomerpc.php,v 0.0 2010/09/30 02:15:03 lancer Exp $
  6// Keywords: 
  7
  8require_once "libdb.php";
  9require_once "libsession.php";
 10require_once "config.php";
 11
 12class EhomeServer {
 13    private $_uid;
 14    private $_db;
 15    private $_session;
 16    private $_alarm_types;
 17    private $_max_phones = 10;
 18    private $_max_devices = 10;
 19
 20    public function __construct(){
 21        $this->_db = new Database(DBHOST, DBUSER, DBPASS, DBNAME);
 22        $this->_session = new SessionDB(DBHOST, DBUSER, DBPASS, DBNAME);
 23        $this->_uid = $this->_session->get("uid");
 24        $this->_alarm_types = array("wurgency", "wdoor", "winfra", "wwindow",
 25                                    "whelp", "wsmoke", "wgas", "wglass",
 26                                    "wwater", "wdefen");
 27    }
 28
 29    public function test(){
 30        return "test connection!";
 31    }
 32    
 33    // Login
 34    // paramter: $username, $password
 35    // return: true
 36    public function login($username, $password){
 37        $this->_db->connect();
 38        $sql = "select uid, passwd, locked from user where username='$username' limit 1";
 39        $user = $this->_db->fetch_first($sql);
 40        $this->_db->close();
 41        
 42        if($user) {
 43            if($password == $user["passwd"] && $user["locked"] == 0){
 44                // authorized OK!
 45                $this->_session->set("uid", $user["uid"]);
 46                return true;
 47            }
 48        }
 49        $this->_session->set("uid", -1);
 50        return false;
 51    }
 52    // Get device list
 53    // return: array(array("device"=>"11094",
 54    //                     "address" =>"some place"),
 55    //              ...)
 56    public function getDeviceList(){
 57        $this->_db->connect();
 58        $sql = "select devid,addr from device where uid=". $this->_uid;
 59        $ret = $this->_db->fetch_all($sql);
 60        $this->_db->close();
 61        $devices = array();
 62        foreach($ret as $item){
 63            array_push($devices, array("device" => $item["devid"],
 64                                       "address" => $item["addr"]));
 65        }
 66        return $devices;
 67    }
 68
 69    // Get phones binded to device
 70    // paramter: $device
 71    // return: array(array("phone"=>"18611101110",
 72    //                     "alarm-types"=>"urgency,help,defen"),
 73    //               ...)
 74    public function getBindPhones($device){
 75        $sql = "select * from phone where devid='$device' and uid=". $this->_uid;
 76        $this->_db->connect();
 77        $ret = $this->_db->fetch_all($sql);
 78        $this->_db->close();
 79        $phones = array();
 80        foreach($ret as $item){
 81            $types = array();
 82            foreach($item as $k=>$v){
 83                if(in_array($k, $this->_alarm_types)){
 84                    if($v){
 85                        array_push($types, substr($k, 1));
 86                    }
 87                }
 88            }
 89            array_push($phones, array("phone" => $item["mobile"],
 90                                      "alarm-types" => implode(",", $types)));
 91        }
 92        return $phones;
 93    }
 94
 95    // $alarm_types = array("urgency"=>1,"door"=>0)
 96    public function setBindPhone($device, $phone, $alarm_types){
 97        
 98        $sql = "select pid from phone where devid='$device' and mobile='$phone' and uid=".$this->_uid;
 99        $this->_db->connect();
100        $row = $this->_db->fetch_first($sql);
101        $sql = "";
102        if( empty($row) ){
103            // check max binded phones
104            $sql = "select count(pid) cnt from phone where devid='$device' and uid=".$this->_uid;
105            $row = $this->_db->fetch_first($sql);
106            $cnt = $row["cnt"];
107            if($cnt >= $this->_max_phones){
108                return false;
109            }
110
111            $sql1 = array();
112            $sql2 = array();
113            foreach($alarm_types as $k => $v){
114                array_push($sql1, "w".$k); // urgency => wurgency
115                array_push($sql2, $v);
116            }
117            $sql_1 = implode(",", $sql1);
118            $sql_2 = implode(",", $sql2);
119
120            if($sql_1) $sql_1 = ",". $sql_1;
121            if($sql_2) $sql_2 = ",". $sql_2;
122            
123            $sql = "insert into phone (devid, mobile, uid".$sql_1.") values (".$device.",'".$phone."',".$this->_uid.$sql_2.");";
124        } else {
125
126            $sqls = array();
127            foreach($alarm_types as $k => $v){
128                array_push($sqls, "w".$k."=".$v);
129            }
130            $sql_1 = implode(",", $sqls);
131            
132            $pid = $row["pid"];
133            if($sql_1){
134                $sql = "update phone set ".$sql_1." where pid=$pid";
135            }
136        }
137        $this->_db->query($sql);
138        $this->_db->close();
139
140        return true;
141    }
142
143    public function delBindPhone($device, $phone){
144        $sql = "delete from phone where devid=$device and mobile='$phone' and uid=".$this->_uid;
145        $this->_db->connect();
146        $this->_db->query($sql);
147        $this->_db->close();
148        return true;
149    }
150    
151    // Watch camera video
152    public function getVideoUri(){
153        return "not implement yet";
154    }
155    // Check defense status
156    public function getDeviceStatus($device){
157        include_once "/var/tmp/common_header.php";
158        include_once "/home/www/yitai/system/application/language/english/yitai_lang.php";
159        $this->_db->connect();
160        $sql = "select stat, up_time from status where devid='$device' order by up_time desc limit 20";
161        $ret = array();
162        $index = 0;
163        foreach($this->_db->fetch_all($sql) as $row){
164            $stat = strtolower($stat_def[$row["stat"]]);
165            $ret[$index]["status"] = $lang[$stat];
166            $ret[$index]["up_time"] = $row["up_time"];
167            $index++;
168        }
169        $this->_db->close();
170        return $ret;
171    }
172    // Defense control
173    public function setDeviceDefence($device, $defend){
174        switch($defend){
175            case true:
176                $cmd = "bf";
177                break;
178            case false:
179                $cmd = "cf";
180                break;
181            default:
182                return false;
183        }
184        define('EHOME_APP', "/home/lancer/projects/fhomeyzd/fhomed");
185        $run = EHOME_APP." -a $device,$cmd";
186
187        /*
188        error_log("\nsetDeviceDefence: ".
189                  var_export($run,1)
190                  , 3, "/tmp/php.log");
191        */
192        
193        $ret = @exec($run);
194        if($ret == 1){
195            return true;
196        }
197        return false;
198    }
199    // Modify user profile
200    public function getUserProfile(){
201        $sql = "select u.*,l.name lname from user u left join location l on u.location=l.lid where uid=". $this->_uid;
202        $this->_db->connect();
203        $row = $this->_db->fetch_first($sql);
204        $this->_db->close();
205        return array("username" => $row["username"],
206                     "realname" => $row["realname"],
207                     "email" => $row["email"],
208                     "phone" => $row["mobile"],
209                     "address" => $row["addr"],
210                     "location" => $row["lname"]);
211    }
212
213    public function setUserProfile($password,$email,$phone,$address){
214        $st = array();
215        if(!empty($password)){
216            $st[] = "passwd='$password'";
217        }
218        if(!empty($email)){
219            $st[] = "email='$email'";
220        }
221        if(!empty($phone)){
222            $st[] = "mobile='$phone'";
223        }
224        if(!empty($address)){
225            $st[] = "addr='$address'";
226        }
227        $sql = "update user set ".implode(", ", $st)." where uid=". $this->_uid;
228        $this->_db->connect();
229        $ret = $this->_db->query($sql);
230        $this->_db->close();
231        if($ret){
232            return true;
233        }
234        return false;
235    }
236    
237    // Push waring message
238    public function checkAlarmMessage(){
239        $this->_db->connect();
240        $sql = "select devid from device where uid=". $this->_uid;
241        $ret = $this->_db->fetch_all($sql);
242        $devlist = array();
243        foreach($ret as $k){
244            $devlist[] = $k["devid"];
245        }
246        $sql = "select stat,devid,up_time from status where devid IN (".implode(",",$devlist).") and stat IN (5,6,7,27,14,15,16,17,18) and up_time > UNIX_TIMESTAMP() - 900";
247        $ret = $this->_db->fetch_all($sql);
248        $alarms = array();
249        $devids = array();
250        foreach($ret as $k){
251            if(in_array($k["devid"], $devids)){
252                continue;               // unique devid
253            }else{
254                array_push($devids, $k["devid"]);
255            }
256            array_push($alarms, array("device"=>$k["devid"],
257                                      "alarm"=>$this->statusMessage_($k["stat"]),
258                                      "up_time"=>$k["up_time"]));
259        }
260        $this->_db->close();
261        return $alarms;
262    }
263
264    private function statusMessage_($stat){
265        $msg = "";
266        switch($stat){
267            case 5:
268                $msg = "urgency";
269                break;
270            case 6:
271                $msg = "door";
272                break;
273            case 7:
274                $msg = "infra";
275                break;
276            case 27:
277                $msg = "window";
278                break;
279            case 14:
280                $msg = "help";
281                break;
282            case 15:
283                $msg = "smoke";
284                break;
285            case 16:
286                $msg = "gas";
287                break;
288            case 17:
289                $msg = "glass";
290                break;
291            case 18:
292                $msg = "water";
293                break;
294        }
295        return $msg;
296    }
297}
298
299
300?>