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