Skip to content
Snippets Groups Projects
SmartData.php 3.72 KiB
Newer Older
root's avatar
root committed
<?php

namespace SmartData;

require_once( __DIR__ . '/Exception.php');
require_once( __DIR__ . '/Packable.php');

abstract class SmartData extends Packable
{
    const STATIC_VERSION = "1.1";
    const MOBILE_VERSION = "1.2";

    public $version;
    private $unit;
    public $value;
    public $error;
    public $confidence;
    public $x;
    public $y;
    public $z;
    public $time;

    public static function fields() : array {
        return array(
                'unit'       => Unpack::UNIT,
                'value'      => Unpack::VALUE,
                'error'      => Unpack::BYTE,
                'confidence' => Unpack::BYTE,
                'x'          => Unpack::DIMENSION,
                'y'          => Unpack::DIMENSION,
                'z'          => Unpack::DIMENSION,
                'time'       => Unpack::TIME,
                'd'          => Unpack::DIMENSION
            );
    }

    public $gw;

    public function __construct($u, $v, $e, $c, $x,$y,$z, $t, $d){
        $this->unit = ($u instanceof Unit) ? $u : Unit::interpret($u);
        $this->value      = $v;
        $this->error      = $e;
        $this->confidence = $c;
        $this->x          = $x;
        $this->y          = $y;
        $this->z          = $z;
        $this->time       = $t;
        $this->d          = $d;

        if((Config::DEBUGGED[__CLASS__] ?? false) || Config::HYSTERICALLY_DEBUGGED)
            Logger::debug(__CLASS__.": $this");
    }

    public function toArray(){
        $json = array();
        $json['timestamp']  = $this->time;
        $json['value']      = $this->value;
        $json['error']      = $this->error;
        $json['confidence'] = $this->confidence;
        $json['x']          = $this->x;
        $json['y']          = $this->y;
        $json['z']          = $this->z;
        $json['d']          = $this->d;
        return $json;
    }

    public function toJson(){
        $json = json_encode($this->toArray());
        $json = preg_replace('/\"timestamp\":\"/', "\"timestamp\":", $json);
        $json = preg_replace('/\",\"value\":/', ",\"value\":", $json);
        return $json;
    }

    public function unit(){
        return $this->unit;
    }

    public function __get($property){
        if (property_exists($this, $property)) {
            switch ($property) {
                case 'unit':
                    if($this->unit instanceof Unit)
                        return $this->unit->cod;
                    else
                        return $this->unit;
                default:
                    return $this->$property;
            }
        }
    }

    public function __toString() {
        $string  = "u={$this->unit}, ";
        $string .= "v={$this->value}, ";
        $string .= "e={$this->error}, ";
        $string .= "c={$this->confidence}, ";
        $string .= "x={$this->x}, ";
        $string .= "y={$this->y}, ";
        $string .= "z={$this->z}, ";
        $string .= "t={$this->time}, ";
        $string .= "d={$this->d}";
        $string .= '}';
        return $string;
    }
}

class StaticSmartData extends SmartData {
    public function __construct($u, $v, $e, $c, $x,$y,$z, $t, $d) {
        parent::__construct($u, $v, $e, $c, $x,$y,$z, $t, $d);
        $this->version = SmartData::STATIC_VERSION;
    }
    public function __toString() {
        return '{S, '.(string)parent::__toString();
    }
}

class MobileSmartData extends SmartData
{
    public $signature;

    public function __construct($u, $v, $e, $c, $x,$y,$z, $t, $d, $signature=0) {
        parent::__construct($u, $v, $e, $c, $x,$y,$z, $t, $d);
        $this->signature = $signature;
        $this->version = SmartData::MOBILE_VERSION;
    }
    public function __toString() {
        return '{M, '.(string)parent::__toString();
    }
}