Skip to content

Commit

Permalink
Finnished getStopTIme
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubAndrysek committed Apr 30, 2020
1 parent a9cf69b commit e08fcb7
Show file tree
Hide file tree
Showing 8 changed files with 429 additions and 77 deletions.
218 changes: 167 additions & 51 deletions app/CoreModule/Model/ThisSensorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@
use Nette\Utils\DateTime;
//use DateTime;
use DateTimeImmutable;
use Nette\Database\UniqueConstraintViolationException;
use App\Utils\Pretty;

class ThisSensorManager
{
use Nette\SmartObject;

const

public const
START = "2000-01-01 00:00:00",
MINUTE = 60;

public const
WORK = '1', // Machine is working
STOP = "0", // Machine is not working
REWORK = "2"; //State after end of stoji
OLDWORK = '1', // Machine is working
OLDSTOP = "0", // Machine is not working
OLDREWORK = "2"; //State after end of stoji

public const
FINISHED = 'FINISHED', // Machine is working
STOP = "STOP", // Machine is not working
REWORK = "REWORK", // State after end of STOP
ON = 'ON', // ON machine
OFF = 'OFF'; // OFF machine


public const STATES = [
self::WORK,
self::STOP,
self::REWORK,
];

private $database;
private $defaultMsgLanguage;
Expand All @@ -42,9 +47,13 @@ public function __construct($defaultMsgLanguage,$defaultAPILanguage, Context $da
$this->defaultMsgLanguage = $defaultMsgLanguage;
$this->defaultAPILanguage = $defaultAPILanguage;
$this->sensorManager = $sensorManager;

}

public function pretty($state = true, $main, $englishMsg = "", $czechMsg = "" )
{
return( array($state, $main, $englishMsg, $czechMsg));
}

/**
* Save sensor status to database
Expand All @@ -55,99 +64,206 @@ public function addEvent($sName, $state)
{
if(!$this->sensorManager->sensorIsExist($sName))
{
return array(false, "Sensor with name ".$sName." delete does not exist", "Senzor s názvem".$sName." neexistuje");
// return array(false, "Sensor with name ".$sName." delete does not exist", "Senzor s názvem".$sName." neexistuje");
return $this->pretty(0,"","Sensor with name ".$sName." does not exist", "Senzor s názvem".$sName." neexistuje");
}

if($succes = $this->database->table($sName)->insert([
'state' => $state,
]))
{
return array(true, "Event created", "Záznam byl vytvořen", $sName, $state);
{
return $this->pretty(true, "Event created", "Záznam byl vytvořen", $sName, $state);
}
else
{
return array(false, "ERROR!!!", "ERROR!!!");
return $this->pretty(false, "ERROR!!!", "ERROR!!!");
}
}
//BY NAME//

public function isEmpty($array)
{
if(count($array)<1)
{
return true;
}
return false;
}


//////////////
// Gets
// Gets
//////////////

public function getAllEvents($sName)
{
return $this->database->table($sName)->fetchAll();
}

public function getAllEventsState($sName, self::)
public function getAllEventsState($sName, $state)
{
return $this->database->table($sName)->where("state", $state)->fetchAll();
}
}

public function getAllEventsOlder($sName, $time)
{
return $this->database->table($sName)->where("time >=?", $time)->fetchAll();
}
}

public function getAllEventsYounger($sName, $time)
{
return $this->database->table($sName)->where("time <=?", $time)->fetchAll();
}

//////////////
// Counts
//////////////

public function countAllEvents($sName)
{
return $this->database->table($sName)->count();
}
}

public function countAllEventsState($sName, $state)
{
return $this->database->table($sName)->where("state", $state)->count();
}
}




//////////////
// ID
//////////////

public function getFirstId($sName, $time, $state)
public function getFirstIdState($sName, $time, $state)
{
return $this->database->table($sName)->where("time >=? AND state=?", $time, $state)->fetch()->id;
}
}

public function getLastId($sName, $time, $state)
public function getLastIdState($sName, $time, $state)
{
return $this->database->table($sName)->where("time <=? AND state=?", $time, $state)->order("id DESC")->fetch()->id;
}

public function getAllId($sName, $from , $to, $state)
// public function getAllIdState($sName, $from , $to, $state = self::FINISHED)
// {
// $ids = $this->database->table($sName)->where("time >=? AND time <=? AND state=?", $from, $to, $state)->fetchAll();

// $out = array();
// foreach($ids as $id)
// {
// $out[] = $id->id;
// }
// return $out;
// }


public function getAllIdState($sName, $from , $to, $state = self::FINISHED)
{
$ids = $this->database->table($sName)->where("time >=? AND time <=? AND state=?", $from, $to, $state)->fetchAll();


$out = array();
foreach($ids as $id)
{
// $out[] = $id->id => $id->state;
array_push($out, array($id->id => $id->state));
}
return $out;
}

public function getAllId($sName, $from , $to)
{
$ids = $this->database->table($sName)->where("time >=? AND time <=?", $from, $to)->fetchAll();

$out = array();
foreach($ids as $id)
{
$out[] = $id->id;
// $out[] = $id->id => $id->state;
array_push($out, array($id->id => $id->state));
}
return $out;
}

public function getAllTime($sName, $ids)
{
$first = $this->database->table($sName)->where("id=?", $ids[0])->fetch()->time->getTimestamp();
$last = $this->database->table($sName)->where("id=?", end($ids))->fetch()->time->getTimestamp();
//dump($ids);
//echo "First key".array_key_first($ids);
//dump( array_key_last($ids)+1);
$first = $this->database->table($sName)->where("id=?", array_key_first($ids[0]) )->fetch()->time->getTimestamp();
$last = $this->database->table($sName)->where("id=?", array_key_last($ids)+1)->fetch()->time->getTimestamp();
$res = $last-$first;
return new DateInterval("PT".$res."S");

}

public function getRunTime($sName, $ids): DateInterval
public function testPretty()
{
// dump($x = Pretty::return(true, array(1,2,3), "IT is ok", "Je to ok"));
// $y = PrettyReturn::return(true, array(1,2,3), "IT is ok", "Je to ok");
dump(Pretty::fromParts(2000,1,11,1));
// dump($y);
return 1;//$x;
}

public function getStopTime($sName, $ids)
{
if(!$this->sensorManager->sensorIsExist($sName))
{
return $this->pretty(false,"", "Sensor with name ".$sName." delete does not exist", "V poli neni žádný prvek");
}

if($this->isEmpty($ids))
{
return $this->pretty(false,"", "IDs is empty", "V poli neni žádný prvek");
}

$sState = 0;
$time = 0;
// foreach ($variable as $key => $value) {
// # code...
// }

foreach($ids as $id => $state)
{

// dump($state);
//dump($id);
//dump( $state[key($state)]);
//echo "ID: ".key($state)."STATE ->".$state[key($state)];
switch ($sState) {
case 0:
if($state[key($state)] == self::STOP)
{
$sState++;
$start = $this->database->table($sName)->where("id=?", $id+1)->fetch()->time->getTimestamp();
//echo " -> STOP -> ".$start;
}
break;
case 1:
if($state[key($state)] == self::REWORK)
{
$stop = $this->database->table($sName)->where("id=?", $id+1)->fetch()->time->getTimestamp();
$sState = 0;
$time += $stop-$start;
//echo " -> REWORK -> ".$stop."-> ALL: ". $time;
}
break;

// default:
// # code...
// break;
}
//echo "<br><br>";
}

// $first = $this->database->table($sName)->where("id=?", $ids[0])->fetch()->time->getTimestamp();
// $last = $this->database->table($sName)->where("id=?", end($ids))->fetch()->time->getTimestamp();
// $res = $last-$first;
return new DateInterval("PT".$time."S");

}

public function getWorkTimeOld($sName, $ids): DateInterval
{
$last = $this->database->table($sName)->where("id=?", 1)->fetch();
//echo $out = $last->time->getTimestamp();
Expand All @@ -167,7 +283,7 @@ public function getRunTime($sName, $ids): DateInterval
foreach($ids as $id)
{
$actual = $this->database->table($sName)->where("id=?", $id)->fetch();

//dump($last->actWork);

// First loop
Expand All @@ -193,45 +309,45 @@ public function getRunTime($sName, $ids): DateInterval
//echo "NEXT - BIG";
$actWork = $lastWork;
$workTime+=$actWork;

}
if($actWork<$lastWork-1.5*self::MINUTE)
{
//echo "NEXT - SMALL";
$actWork = 0;
$workTime+=$actWork;
}

}
else
{
$workTime+=$actWork;
//echo "NEXT - NORMAL";

}
$this->database->table($sName)->where("id=?", $id)->update(['work'=>$actWork, 'time'=>$actual->time]);

//echo " -ID:".$id." Last: ".$last->time." Actual: ".$actual->time."<br>";
//echo "Add:".$actWork."->T:".gmdate("H:i:s",$actWork)." -> result: ".$workTime."->T:".gmdate("H:i:s",$workTime)."<br><br><br>";

$last = $actual;
$lastWork = $actWork;

}

}
$this->database->table($sName)->where("id=?", 10)->update(['work'=>$workTime]);
return new DateInterval("PT".$workTime."S");
}


public function dateIntervalToSeconds($dateInterval)
{
$reference = new DateTimeImmutable;
$endTime = $reference->add($dateInterval);

return $endTime->getTimestamp() - $reference->getTimestamp();
}



////////////////////
Expand All @@ -255,21 +371,21 @@ public function getRunTime2($sName, $ids)
$workTime = $last->work;
foreach($ids as $id)
{


//dump($last->work);
if($id < $ids[1])
{
//echo "<br>Add: ".$start->add( $workTime);
$this->database->table($sName)->where("id=?", $id)->insert(['work'=>$workTime]);

}
else if($id >= $ids[1])
{
$actual = $this->database->table($sName)->where("id=?", $id)->fetch();

//echo "<br>ID:".$id."->";

//echo $last->time." - ".$actual->time;
$add = date_diff(DateTime::from($last->time), DateTime::from($actual->time));
dump($add);
Expand All @@ -282,15 +398,15 @@ public function getRunTime2($sName, $ids)
//echo " *NORMAL*".$add->i." ->".$start->add($add);
$this->database->table($sName)->where("id=?", $id)->insert(['work'=>$add]);
}

$last = $actual;
}

}
$out = date_diff(DateTime::from(self::START),$start);
return $out;
}
}

public function resetDB($sName)
{
for ($i = 0;$i<=8;$i++ )
Expand Down
Loading

0 comments on commit e08fcb7

Please sign in to comment.