<?php
// DeviceCounterList.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Api\Server;use DateTimeImmutable;use DateTimeInterface;use DateTimeZone;use Docuform\Api\TableMapper;use Docuform\Api\Exception\ApiHttpBadRequestException;use Docuform\Database\DbSQL;use Docuform\Lib\ValidateParam;class DeviceCounterList extends TableMapper{public $deviceId=0;public $timestamp;private $mappings=[];public function getColumnMappings():array{if($this->mappings===[]){$this->mappings[]=['name'=>'DeviceId','type'=>DbSQL::cst_intornull,'alias'=>'deviceId'];$this->mappings[]=['name'=>'TimeUTC','type'=>DbSQL::cst_date,'alias'=>'timestamp'];$user=$this->getContainer()->get('user');$serverApi=$this->getContainer()->get('RestApi');$request=$serverApi->getRequest();$andExtended='';if(!empty($request->getQueryParams()['countersScope'])){$countersScope=explode(',',$request->getQueryParams()['countersScope']);if(count($countersScope)===1)$andExtended=' AND Extended='.($countersScope[0]==='extended'?1:0);}$device=$serverApi->getDevice();$customerId=$device->customerId;$database=$this->getContainer()->get('database');$cursor=null;$userWhere="";$params=[$customerId];if(!$user->isAdmin){if($user->isDealer){$userWhere.=" AND U.DeviceManagerId=?";$params[]=$user->id;}else{$userWhere.=" AND U.Id=?";$params[]=$user->id;}}$sql='SELECT CT.ColPos,CD.Extended,CD.JsonName'.' FROM ACCMIBCOUNTERDEF CD,ACCMIBCOUNTERTEMPLATE CT'.' WHERE CT.UserId=(SELECT U.DeviceManagerId FROM ACCUSERS U WHERE U.Id=?'.$userWhere.')'.$andExtended.' AND TemplateName IS NULL AND CD.PrMibName=CT.PrMibName'.' ORDER BY CT.ColPos';$row=$database->FetchFirst($cursor,$sql,$params);while($row!==false){$m=['name'=>'C'.$row['ColPos'],'type'=>DbSQL::cst_intornull];$m['alias']=((int)$row['Extended']===0)?['standardCounters'=>$row['JsonName']]:['extendedCounters'=>$row['JsonName']];$this->mappings[]=$m;$row=$database->FetchNext($cursor);}$database->FetchClose($cursor);}return $this->mappings;}public function getParameterConstraints(string $method):array{if($method==='GET')return[['name'=>'countersScope','type'=>ValidateParam::STRINGLIST,'maxLength'=>128,'allowedContent'=>['standard','extended']],['name'=>'date','type'=>ValidateParam::DATE_ISO8601]];return[];}public function read(int $id):void{$params=array();$serverApi=$this->getContainer()->get('RestApi');$request=$serverApi->getRequest();$this->checkGetParameters($request,$request->getQueryParams());$device=$serverApi->getDevice();if($device===null||$device->id!==$id){throw new ApiHttpBadRequestException($request);}if($device->isLicensed===false){throw new ApiHttpBadRequestException($request,'Device not licensed');}$this->__set('DeviceId',$device->id);$params[]=$device->id;$andTimeUTC='';if(!empty($request->getQueryParams()['date'])){$timeUTC=$request->getQueryParams()['date'];if(!empty($timeUTC)){$dateTime=DateTimeImmutable::createFromFormat(DateTimeInterface::ATOM,$timeUTC,new DateTimeZone('UTC'));$errors=DateTimeImmutable::getLastErrors();if($dateTime===false){throw new ApiHttpBadRequestException($request,'Parameter "date" not a valid ISO-8601 date.');}$andTimeUTC=' AND TimeUTC < CONVERT(DATETIME2, \''.$dateTime->format('Y-m-d H:i:s').'\', 120)';}}$database=$this->getContainer()->get('database');$cursor=null;$sql='SELECT * FROM ACCMIBCOUNTERVALUES WHERE DeviceId=?';if(!empty($andTimeUTC))$sql.=$andTimeUTC;$sql.=' ORDER BY TimeUTC DESC';$row=$database->FetchFirst($cursor,$sql,$params);if($row!==false){foreach($this->mappings as $colDef){if(isset($colDef['name'],$row[$colDef['name']])){$name=$colDef['name'];if($name==='timestamp'&&isset($this->$name))continue;$value=$row[$name];$this->__set($name,$value);}}}if($cursor!==null)$database->FetchClose($cursor);}}