Added washinsa script

This commit is contained in:
Arnaud Vergnet 2020-03-14 15:52:27 +01:00 committed by keplyx
parent 21f03322c6
commit f586c10dd2
2 changed files with 180 additions and 0 deletions

179
washinsa/index.php Normal file
View file

@ -0,0 +1,179 @@
<?php
/**
* Wash-INSA PARSER
* This program get all information about the laundry
* Author : Gabin NOBLET (Promo 55)
* Date : 07/2019
* Version 1.0
*/
/**Get Type of machines (1 for dryers, 2 for washers, 0 else)
* @param $str (string)
* @return int
*/
function mType($str)
{
$a = explode(' ', $str);
if (substr($a[0], -5) == "SECHE") {
return 1;
} elseif (substr($a[0], -4) == "LAVE") {
return 2;
} else {
return 0;
}
}
/**Get Status of machines (1 for available, 2 for current, 3 for finished, 0 for out of order)
* @param $node (DOMNode)
* @return int
*/
function mStatus($node)
{
$a = explode(' ', $node[2]->nodeValue);
if (substr($a[0], -10) == "DISPONIBLE") {
return 1;
} elseif (substr($a[0], -7) == "TERMINE") {
return 3;
} elseif (isset($node[4])) { //If there is time information, it is current
return 2;
} else {
return 0;
}
}
/** Clean strings (removes spaces, tabs end return before and behind the string)
* @param $str (string)
* @return string
*/
function clean($str)
{
return rtrim(ltrim(htmlspecialchars_decode($str)));
}
function generateJson()
{
$page = new DOMDocument();
$page->loadHTMLFile("https://www.proxiwash.com/weblaverie/ma-laverie-2?s=cf4f39&16d33a57b3fb9a05d4da88969c71de74=1"); //Get page
$division = $page->getElementById("liste-machines");
$liste_machines = $division->childNodes[0]->childNodes;
//Remove titles
$titles = $liste_machines->item(0);
$titles->parentNode->removeChild($titles);
$return = array(
'dryers' => array(),
'washers' => array()
);
foreach ($liste_machines as $machines) {
$specs = $machines->childNodes;
//Format array
$template = array(
'number' => "",
'state' => "",
'startTime' => "",
'endTime' => "",
'donePercent' => "",
'remainingTime' => ""
);
//Get info from HTML
$specsArray = array();
foreach ($specs as $info) {
if (isset($info->tagName)) {
if ($info->tagName == "td") {
array_push($specsArray, $info);
}
}
}
//Get Type
$type = mType(clean($specsArray[0]->nodeValue));
//Get Number
preg_match('/\d{1,2}/', $specsArray[1]->nodeValue, $number);
$template['number'] = $number[0];
//Get Status and DonePercent
$status = mStatus($specsArray);
switch ($status) {
case 1:
$template['state'] = "DISPONIBLE";
break;
case 2:
$template['state'] = "EN COURS";
foreach ($specsArray[2]->childNodes as $child) {
if (isset($child->tagName)) {
if ($child->tagName == "table") {
$progressBar = $child->childNodes[0]->childNodes[0];
if ($progressBar->getAttribute('bgcolor') == "Green") {
$template['donePercent'] = substr($progressBar->getAttribute('width'), 0, -1);
}
}
}
}
break;
case 3:
$template['state'] = "TERMINE";
break;
default :
$template['state'] = "HORS SERVICE";
break;
}
if ($template['state'] === "EN COURS") { // We set Times only when they could exist
//Get StartTime
$template['startTime'] = $specsArray[4]->nodeValue;
//Get EndTime
$template['endTime'] = $specsArray[5]->nodeValue;
$template['remainingTime'] = get_remaining_time($template['startTime'], $template['endTime'], $template['donePercent']);
}
if ($type == 1) {
array_push($return['dryers'], $template);
} elseif ($type == 2) {
array_push($return['washers'], $template);
}
// echo "<pre>";
// echo "Type : ".$type."\n" ;
// print_r($template);
// echo "</pre>";
}
$jsonData = json_encode($return);
file_put_contents('washinsa.json', $jsonData);
}
/**
* Get remaining time for the current machine
*
* @param $startTime
* @param $endTime
* @return string
*/
function get_remaining_time($startTime, $endTime, $percentDone)
{
$startArray = explode(':', $startTime);
$endArray = explode(':', $endTime);
$unixStart = mktime($startArray[0], $startArray[1], 0, 1, 0);
$unixEnd = mktime($endArray[0], $endArray[1], 0, 1, 0);
if ($unixStart > $unixEnd) { // Machine ends the following day
$unixEnd = mktime($endArray[0], $endArray[1], 0, 1, 1);
}
$deltaMinutes = ($unixEnd - $unixStart)/60;
$remainingTime = $deltaMinutes * (1 - $percentDone/100);
return strval(round($remainingTime, 0));
}
generateJson();

1
washinsa/washinsa.json Normal file
View file

@ -0,0 +1 @@
{"dryers":[{"number":"1","state":"DISPONIBLE","startTime":"","endTime":"","donePercent":"","remainingTime":""},{"number":"2","state":"EN COURS","startTime":"15:24","endTime":"15:59","donePercent":"62.9","remainingTime":"13"},{"number":"3","state":"TERMINE","startTime":"","endTime":"","donePercent":"","remainingTime":""}],"washers":[{"number":"4","state":"EN COURS","startTime":"\u00a0","endTime":"\u00a0","donePercent":"","remainingTime":"0"},{"number":"5","state":"EN COURS","startTime":"15:35","endTime":"16:15","donePercent":"26.9","remainingTime":"29"},{"number":"6","state":"DISPONIBLE","startTime":"","endTime":"","donePercent":"","remainingTime":""},{"number":"7","state":"EN COURS","startTime":"15:34","endTime":"16:19","donePercent":"26","remainingTime":"33"},{"number":"8","state":"DISPONIBLE","startTime":"","endTime":"","donePercent":"","remainingTime":""},{"number":"9","state":"DISPONIBLE","startTime":"","endTime":"","donePercent":"","remainingTime":""},{"number":"10","state":"DISPONIBLE","startTime":"","endTime":"","donePercent":"","remainingTime":""},{"number":"11","state":"DISPONIBLE","startTime":"","endTime":"","donePercent":"","remainingTime":""},{"number":"12","state":"EN COURS","startTime":"15:02","endTime":"15:47","donePercent":"97.7","remainingTime":"1"}]}