. */ namespace Volkszaehler\Model; use Doctrine\Common\Collections\ArrayCollection; /** * Channel entity * * @author Steffen Vogel * @package default * * @Entity * @Table(name="channels") */ class Channel extends Entity { /** @Column(type="string", nullable=false) */ protected $name; /** @Column(type="string", nullable=true) */ protected $description; /** @Column(type="string", nullable=false) */ protected $indicator; /** * @OneToMany(targetEntity="Data", mappedBy="channel", cascade={"remove"}) */ protected $data = NULL; /** @Column(type="integer", nullable=true) */ protected $resolution; /** @Column(type="decimal", precision="5", scale="2", nullable=true) */ protected $cost; /** @ManyToMany(targetEntity="Group", mappedBy="channels") */ protected $groups; /** * indicator => interpreter, unit mapping */ protected static $indicators = array( 'power' => array('meter', 'W'), 'gas' => array('meter', 'm³'), 'water' => array('meter', 'm³'), 'temperature' => array('sensor', '°C'), 'pressure' => array('sensor', 'hPa'), 'humidity' => array('sensor', '%') ); /** * constructor */ public function __construct($indicator) { parent::__construct(); if (!in_array($indicator, array_keys(self::$indicators))) { throw new \Exception($indicator . ' is no known indicator'); } $this->indicator = $indicator; $this->data = new ArrayCollection(); $this->groups = new ArrayCollection(); } /** * Add a new data to the database * @todo move to Logger\Logger? */ public function addData(\Volkszaehler\Model\Data $data) { $this->data->add($data); } /** * Obtain channel interpreter to obtain data and statistical information for a given time interval * * @param Doctrine\ORM\EntityManager $em * @param integer $from timestamp in ms since 1970 * @param integer $to timestamp in ms since 1970 * @return Interpreter */ public function getInterpreter(\Doctrine\ORM\EntityManager $em, $from, $to) { $interpreterClassName = 'Volkszaehler\Interpreter\\' . ucfirst($this->getType()) . 'Interpreter'; return new $interpreterClassName($this, $em, $from, $to); } /** * Validate * * @PrePersist @PreUpdate */ protected function validate() { if ($this->getResolution() <= 0 && $this->getType() == 'meter') { throw new Exception('resolution has to be a positive integer'); } } /** * getter & setter */ public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getDescription() { return $this->description; } public function setDescription($description) { $this->description = $description; } public function getResolution() { return $this->resolution; } public function setResolution($resolution) { $this->resolution = $resolution; } public function getCost() { return $this->cost; } public function setCost($cost) { $this->cost = $cost; } public function getType() { return self::$indicators[$this->indicator][0]; } public function getUnit() { return self::$indicators[$this->indicator][1]; } public function getIndicator() { return $this->indicator; } } ?>