PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/classes/timer.class.php

http://domuslink.googlecode.com/
PHP | 497 lines | 380 code | 71 blank | 46 comment | 66 complexity | 2e62b93f8e9ccb832e0c00e69a76aa31 MD5 | raw file
  1. <?php
  2. /*
  3. * domus.Link :: PHP Web-based frontend for Heyu (X10 Home Automation)
  4. * Copyright (c) 2007, Istvan Hubay Cebrian (istvan.cebrian@domus.link.co.pt)
  5. * Project's homepage: http://domus.link.co.pt
  6. * Project's dev. homepage: http://domuslink.googlecode.com
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope's that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details. You should have
  17. * received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. require_once(CLASS_FILE_LOCATION."scheduleelement.class.php");
  22. require_once(CLASS_FILE_LOCATION."heyusched.const.php");
  23. /*
  24. * The Timer class decomposes a line from an heyu sched file element into an Object. It Also
  25. * allows the construction of a new timer line/object.
  26. *
  27. * Validation of elements occurs and it will throw exceptions if they cannot be parsed.
  28. * This class handles all the valid cases for a timer.
  29. *
  30. */
  31. class Timer extends ScheduleElement {
  32. private $daysOfWeek = '';
  33. private $startDate;
  34. private $stopDate;
  35. private $startTime;
  36. private $stopTime;
  37. private $startMacro = '';
  38. private $stopMacro = '';
  39. private $timerOptions;
  40. function __construct() {
  41. $args = func_get_args();
  42. if(!empty($args)) {
  43. parent::__construct($args[0]);
  44. $this->timerOptions = array();
  45. if(strtolower(trim($this->getType())) == TIMER_D) {
  46. $this->parseTimerLine($this->getElementLine());
  47. $this->rebuildElementLine();
  48. }
  49. else
  50. throw new Exception("This is not a timer line!");
  51. }
  52. else
  53. {
  54. parent::__construct();
  55. $this->setType(TIMER_D);
  56. $this->setDaysOfWeek(".......");
  57. $this->parseDates("01/01-12/31");
  58. $this->parseStartTime("00:01");
  59. $this->parseStopTime("23:59");
  60. $this->setStartMacro("null");
  61. $this->setStopMacro("null");
  62. $this->timerOptions = array();
  63. $this->rebuildElementLine();
  64. }
  65. }
  66. function rebuildElementLine() {
  67. $anArray = array($this->getType(),$this->getDaysOfWeek(),$this->getStartDate()."-".$this->getStopDate(),$this->getStartTime(),$this->getStopTime(),$this->getStartMacro(),$this->getStopMacro());
  68. if(count($this->timerOptions)) {
  69. foreach($this->timerOptions as $timerOption )
  70. array_push($anArray, "".$timerOption);
  71. }
  72. $this->setElementLine($anArray);
  73. }
  74. function parseTimerLine($timerLine) {
  75. // Split line into elements of the timer
  76. $elements = preg_split('/\s{1,}/', $timerLine);
  77. $elementCount = count($elements);
  78. if($elementCount < 7)
  79. throw new Exception("Timer line has less than 7 elements. Something is missing! ".print_r($elements, true));
  80. if($elementCount == 9 || $elementCount == 11 || $elementCount == 13 || $elementCount == 15)
  81. $this->parseTimerOptions($elements);
  82. elseif($elementCount != 7)
  83. throw new Exception("Timer line has incorrect number of options: ".print_r($elements, true));
  84. $this->setDaysOfWeek(rtrim(ltrim($elements[1])));
  85. $this->parseDates(rtrim(ltrim($elements[2])));
  86. $this->parseStartTime(rtrim(ltrim($elements[3])));
  87. $this->parseStopTime(rtrim(ltrim($elements[4])));
  88. $this->setStartMacro(rtrim(ltrim($elements[5])));
  89. $this->setStopMacro(rtrim(ltrim($elements[6])));
  90. }
  91. function getDaysOfWeek() {
  92. return $this->daysOfWeek;
  93. }
  94. function setDaysOfWeek($theDays) {
  95. if(strlen($theDays) != 7)
  96. throw new Exception("Timer line invalid Days of Week: ".$theDays);
  97. $this->daysOfWeek = $theDays;
  98. }
  99. function parseDates($dateRange) {
  100. // Dates are comprised of four components separated by :,-,/,.
  101. // The order we will use as default will be mm/dd-mm/dd
  102. // also check for the expire-dd as it is a valid construct
  103. $matchValue = preg_match('/(^(\d{1,2})[\/\.:]\d{1,2}-\d{1,2}[\/\.:]\d{1,2}$)|(^(expire-)\d{1,3}$)/', $dateRange, $matches);
  104. if($matchValue == 0)
  105. throw new Exception("Timer line invalid Date Range in timer: [".$dateRange."]");
  106. $dates = preg_split('/:|-|\/|\./', $dateRange);
  107. if(count($dates) == 2) {
  108. $this->startDate = new Date($dates[0]);
  109. $this->stopDate = new Date($dates[1]);
  110. }
  111. else {
  112. $this->startDate = new Date($dates[0], $dates[1]);
  113. $this->stopDate = new Date($dates[2], $dates[3]);
  114. }
  115. }
  116. function getStartDate() {
  117. return $this->startDate;
  118. }
  119. function getStopDate() {
  120. return $this->stopDate;
  121. }
  122. function parseStartTime($startTime) {
  123. $this->startTime = new Time($startTime);
  124. }
  125. function parseStopTime($stopTime) {
  126. $this->stopTime = new Time($stopTime);
  127. }
  128. function parseTimerOptions($theElements) {
  129. $numElements = count($theElements);
  130. $x = 0;
  131. for($i = 7; $i < $numElements; $i+=2) {
  132. if(strlen($theElements[$i])) {
  133. $anOption = new TimerOption($theElements[$i], $theElements[$i + 1]);
  134. if(!count($this->timerOptions)) {
  135. $this->timerOptions[$x] = $anOption;
  136. $x++;
  137. }
  138. else {
  139. $this->timerOptions[$x] = $anOption;
  140. $x++;
  141. }
  142. }
  143. }
  144. }
  145. function getStartTime() {
  146. return $this->startTime;
  147. }
  148. function getStopTime() {
  149. return $this->stopTime;
  150. }
  151. function setStartMacro($startMacro) {
  152. $this->startMacro = $startMacro;
  153. }
  154. function getStartMacro() {
  155. return $this->startMacro;
  156. }
  157. function setStopMacro($stopMacro) {
  158. $this->stopMacro = $stopMacro;
  159. }
  160. function getStopMacro() {
  161. return $this->stopMacro;
  162. }
  163. function getTimerOptions() {
  164. return $this->timerOptions;
  165. }
  166. function getTimerOption($aType) {
  167. if(count($this->timerOptions)) {
  168. foreach($this->timerOptions as $aTimerOption) {
  169. if($aTimerOption->getOptionType() == $aType)
  170. return $aTimerOption;
  171. }
  172. }
  173. }
  174. function removeTimerOption($aType) {
  175. if($aType && count($this->timerOptions) > 0) {
  176. for($i = 0; $i < count($this->timerOptions); $i++) {
  177. if($aType == $this->timerOptions[$i]->getOptionType())
  178. array_splice($this->timerOptions, $i, 1);
  179. }
  180. }
  181. }
  182. function addTimerOption($aTimerOption) {
  183. array_splice($this->timerOptions, -1, 0, array($aTimerOption));
  184. }
  185. }
  186. /*
  187. * Date object to handle the start/stop date constructs in heyu.
  188. */
  189. class Date {
  190. private $month;
  191. private $day;
  192. private $expire;
  193. function __construct() {
  194. $args = func_get_args();
  195. if(!empty($args)) {
  196. if(count($args) == 1) {
  197. $this->setExpire($args[0]);
  198. }
  199. else {
  200. $this->month = intval($args[0]);
  201. $this->day = intval($args[1]);
  202. $this->expire = 0;
  203. }
  204. }
  205. else {
  206. $this->month = 1;
  207. $this->day = 1;
  208. $this->expire = 0;
  209. }
  210. }
  211. function setMonth($theMonth) {
  212. $this->month = intval($theMonth);
  213. }
  214. function getMonth() {
  215. return $this->month;
  216. }
  217. function setDay($theDay) {
  218. $this->day = intval($theDay);
  219. }
  220. function getDay() {
  221. return $this->day;
  222. }
  223. function setExpire($anExpireType) {
  224. if($anExpireType == "expire") {
  225. $this->month = 0;
  226. $this->day = 0;
  227. $this->expire = 1;
  228. }
  229. elseif($anExpireType) {
  230. $this->month = 0;
  231. $this->day = intval($anExpireType);
  232. $this->expire = 2;
  233. }
  234. }
  235. function getExpire() {
  236. return $this->expire;
  237. }
  238. function __toString() {
  239. if($this->expire == 0)
  240. return ($this->month<10?"0".$this->month:$this->month)."/".($this->day<10?"0".$this->day:$this->day);
  241. elseif($this->expire == 2)
  242. return "".$this->day;
  243. elseif($this->expire == 1)
  244. return "expire";
  245. }
  246. }
  247. /*
  248. * Time object to handle the start/stop time constructs in heyu.
  249. */
  250. class Time {
  251. private $min = 0;
  252. private $hour = 0;
  253. private $dawnDusk = '';
  254. private $security = false;
  255. private $offsetMin = 0;
  256. private $plusMinus = '';
  257. private $now = '';
  258. function __construct() {
  259. $args = func_get_args();
  260. if(!empty($args))
  261. {
  262. // Need to determine if we have a time (hh:mm), dawn/dusk with opt +/-mins, now with opt +mins, and security flag
  263. $checkTime = rtrim(ltrim(strtolower($args[0])));
  264. // Validate time to rules of heyu
  265. $theMatchValue = preg_match('/(^(dawn|dusk)[\+-](\d{1,4})[s]?$)|(^(dawn|dusk)s$)|(^(dawn|dusk)$)|(^now[\+]\d{1,2}$)|(^now$)|(^\d{1,2}:\d{1,2}[s]?$)/', $checkTime, $matches);
  266. if($theMatchValue) {
  267. // match found for correct formats with restrictions
  268. if(substr($checkTime, 0, 4) == "dawn" || substr($checkTime, 0, 4) == "dusk") {
  269. if(strlen($checkTime) > 5 ) {
  270. $this->setPlusMinus(substr($checkTime, 4, 1));
  271. $this->setOffsetMin(substr($checkTime, 5, strlen($checkTime) - 5));
  272. }
  273. $this->setDawnDusk(substr($checkTime, 0, 4));
  274. }
  275. elseif(substr($checkTime, 0, 3) == "now" ) {
  276. if(strlen($checkTime) >= 5) {
  277. // now can only have now[+nums] and s is invalid, so go ahead and set values
  278. $this->setPlusMinus(substr($checkTime, 3, 1));
  279. $this->setOffsetMin(substr($checkTime, 4, strlen($checkTime) - 4));
  280. }
  281. $this->setNow();
  282. }
  283. else {
  284. // time is nums:nums[s] only
  285. $colonPos = strpos($checkTime, ":");
  286. $this->setHours(substr($checkTime, 0, $colonPos));
  287. $timeLen = strlen($checkTime);
  288. if(substr($checkTime, strlen($checkTime) - 1, 1 ) == "s" )
  289. $minLen = $timeLen - 2 - $colonPos;
  290. else
  291. $minLen = $timeLen - 1 - $colonPos;
  292. $this->setMins(substr($checkTime, $colonPos + 1, $minLen));
  293. }
  294. if(substr($checkTime, strlen($checkTime) - 1, 1 ) == "s" )
  295. $this->setSecurity(true);
  296. }
  297. else
  298. throw new Exception("Time string is not valid: ".$theTime."\n");
  299. }
  300. }
  301. function setDawnDusk($type) {
  302. $this->dawnDusk = $type;
  303. $this->now = "";
  304. $this->hour = 0;
  305. $this->min = 0;
  306. }
  307. function setSecurity($flag) {
  308. $this->security = $flag;
  309. }
  310. function setOffsetMin($minutes) {
  311. $this->offsetMin = intval($minutes);
  312. }
  313. function setPlusMinus($theValue) {
  314. $this->plusMinus = $theValue;
  315. }
  316. function setNow() {
  317. $this->now = "now";
  318. $this->hour = 0;
  319. $this->min = 0;
  320. $this->dawnDusk = "";
  321. }
  322. function setHours($hours) {
  323. $this->hour = intval($hours);
  324. $this->now = "";
  325. $this->dawnDusk = "";
  326. $this->offsetMin = 0;
  327. $this->plusMinus = "";
  328. }
  329. function setMins($mins) {
  330. $this->min = intval($mins);
  331. }
  332. function __toString() {
  333. if(strlen($this->dawnDusk))
  334. return $this->dawnDusk.$this->plusMinus.($this->offsetMin?$this->offsetMin:"").($this->security?"s":"");
  335. elseif(strlen($this->now))
  336. return $this->now.$this->plusMinus.($this->offsetMin?$this->offsetMin:"");
  337. else
  338. return ($this->hour<10?"0".$this->hour:$this->hour).":".($this->min<10?"0".$this->min:$this->min).($this->security?"s":"");
  339. }
  340. function getDawnDusk() {
  341. return $this->dawnDusk;
  342. }
  343. function isDawnDusk() {
  344. if(substr($this->dawnDusk,0,1) == "d")
  345. return true;
  346. else
  347. return false;
  348. }
  349. function getSecurity() {
  350. return $this->security;
  351. }
  352. function getOffsetMin() {
  353. return $this->offsetMin;
  354. }
  355. function getPlusMinus() {
  356. return $this->plusMinus;
  357. }
  358. function getNow() {
  359. return $this->now;
  360. }
  361. function isNow() {
  362. if($this->now == "now")
  363. return true;
  364. else
  365. return false;
  366. }
  367. function getHours() {
  368. return $this->hour;
  369. }
  370. function getMins() {
  371. return $this->min;
  372. }
  373. }
  374. /*
  375. * Timer Option object to handle the dawn/dusk modifiers
  376. */
  377. class TimerOption {
  378. private $optionType;
  379. private $optionHour;
  380. private $optionMin;
  381. function __construct() {
  382. $args = func_get_args();
  383. if(!empty($args)) {
  384. $this->parseOptions($args[0], $args[1]);
  385. }
  386. }
  387. function parseOptions($theType, $theTime) {
  388. $theType = strtolower(rtrim(ltrim($theType)));
  389. $theTime = rtrim(ltrim($theTime));
  390. // print("TimerOption args: ".$theType." ".$theTime."\n");
  391. if(preg_match('/\b(?>dawnlt|dawngt|dusklt|duskgt)\b/', $theType)) {
  392. if(preg_match('/\b\d{1,2}:\d{1,2}\b/', $theTime)) {
  393. $colonPos = strpos($theTime, ":");
  394. $this->optionType = $theType;
  395. $this->optionHour = intval(substr($theTime, 0, $colonPos));
  396. $this->optionMin = intval(substr($theTime, $colonPos + 1, strlen($theTime) - ($colonPos + 1)));
  397. }
  398. else
  399. throw new Exception("The timer option does not have a vaild time: ".$theTime);
  400. }
  401. else
  402. throw new Exception("Timer option does not have correct modifier: ".$theType);
  403. }
  404. function setOptionType($theType) {
  405. $this->optionType = $theType;
  406. }
  407. function setOptionHour($theHour) {
  408. $this->optionHour = intval($theHour);
  409. }
  410. function setOptionMin($theMin) {
  411. $this->optionMin = intval($theMin);
  412. }
  413. function getOptionType() {
  414. return $this->optionType;
  415. }
  416. function getOptionHour() {
  417. return $this->optionHour;
  418. }
  419. function getOptionMin() {
  420. return $this->optionMin;
  421. }
  422. function __toString() {
  423. return $this->optionType." ".($this->optionHour<10?"0".$this->optionHour:$this->optionHour).":".($this->optionMin<10?"0".$this->optionMin:$this->optionMin);
  424. }
  425. }
  426. ?>