vzlogger/backend/lib/Model/Channel.php

139 lines
4 KiB
PHP
Raw Normal View History

<?php
/**
* @copyright Copyright (c) 2010, The volkszaehler.org project
2010-07-22 18:37:14 +02:00
* @package default
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
*/
/*
* This file is part of volkzaehler.org
*
* volkzaehler.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* volkzaehler.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Volkszaehler\Model;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Channel entity
*
* @author Steffen Vogel <info@steffenvogel.de>
2010-07-22 18:37:14 +02:00
* @package default
2010-07-22 10:32:51 +02:00
*
* @Entity
* @Table(name="channels")
*/
class Channel extends Entity {
/** @Column(type="string", nullable=false) */
protected $name;
/** @Column(type="string", nullable=true) */
protected $description;
2010-07-22 10:32:51 +02:00
/** @Column(type="string", nullable=false) */
protected $indicator;
/**
* @OneToMany(targetEntity="Data", mappedBy="channel", cascade={"remove"})
*/
protected $data = NULL;
2010-07-22 10:32:51 +02:00
/** @Column(type="integer", nullable=true) */
protected $resolution;
/** @Column(type="decimal", precision="5", scale="2", nullable=true) */
protected $cost;
2010-07-22 10:32:51 +02:00
/** @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', '%')
);
2010-07-22 10:32:51 +02:00
/**
* constructor
*/
public function __construct($indicator) {
parent::__construct();
2010-07-22 10:32:51 +02:00
if (!in_array($indicator, array_keys(self::$indicators))) {
throw new \Exception($indicator . ' is no known indicator');
}
2010-07-22 10:32:51 +02:00
$this->indicator = $indicator;
$this->data = new ArrayCollection();
$this->groups = new ArrayCollection();
}
2010-07-22 10:32:51 +02:00
/**
2010-07-29 00:04:33 +02:00
* Add a new data to the database
* @todo move to Logger\Logger?
*/
public function addData(\Volkszaehler\Model\Data $data) {
$this->data->add($data);
}
2010-07-22 10:32:51 +02:00
/**
2010-07-29 00:04:33 +02:00
* 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);
}
2010-07-22 10:32:51 +02:00
2010-07-29 00:04:33 +02:00
/**
* 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; }
2010-07-22 10:32:51 +02:00
}
?>