Added phpInterface and shellScripts

This commit is contained in:
Jens 2021-02-03 23:20:13 +01:00
parent 4c1fc4f5e7
commit 86cb486637
7 changed files with 372 additions and 0 deletions

7
phpInterface/README.md Normal file
View File

@ -0,0 +1,7 @@
# ShoppingListPHP
This is a PHP interface for the shopping list I originally intended for use with the voice assistant Rhasspy only.
Throw the files onto a webserver that has PHP and network access to your MySQL/MariaDB server and edit shoppingListConfig.php.
Afterwards simply access shoppingList.php with your browser.

View File

@ -0,0 +1,212 @@
<?php
class DBLink
{
require_once("shoppingListConfig.php");
static $sqlLink = null;
public static function getDbLink()
{
if(DBLink::$sqlLink == null)
{
DBLink::$sqlLink = new mysqli(DBLink::$mysqlserver, DBLink::$mysqluser, DBLink::$mysqlpw, DBLink::$mysqldb, DBLink::$mysqlPort);
// Verbindung überprüfen
if (mysqli_connect_errno())
{
printf("Database connection error:%s\n", mysqli_connect_error());
exit();
}
}
return DBLink::$sqlLink;
}
}
if(isset($_GET['command']) && $_GET['command'] == "printRhasspyProductList")
{
printRhasspyProductList();
exit(0);
}
echo "<html>
<head>
<title>Shopping list</title>
<style type=\"text/css\">
table
{
border-collapse:separate;
border-spacing:10px 10px;
}
.smallButton
{
font-size:10px;;
height:5000em;
width:5000em;
}
</style>
</head>
<body>";
$command = $_POST['command'];
if(isset($command))
{
switch($command)
{
case "addToList":
$productId = $_POST['productToAdd'];
addToList($productId);
break;
case "removeFromList":
$productId = $_POST['productToRemove'];
removeFromList($productId);
break;
case "printShoppingList":
printShoppingList();
break;
}
}
printShoppingList();
echo "</body></html>";
mysqli_close(DBLink::getDbLink());
function addToList($productId)
{
if($productId > 0)
{
$SQL_command;
if(oneListExists())
$SQL_command = "INSERT IGNORE INTO listEntries (listId, productId) VALUES ((SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1), ".$productId.")";
else
$SQL_command = "INSERT IGNORE INTO listEntries (listId, productId) VALUES ((INSERT INTO `lists` (creationTime) VALUES (".round(microtime(true) * 1000).") RETURN @id, ".$productId."))";
if (DBLink::getDbLink()->query($SQL_command))
echo "Product added to list.<br>";
else
echo "Product could not be added to list.<br>";
}
else
echo "Select a product.";
}
function removeFromList($productId)
{
if($productId > 0)
{
if(oneListExists())
{
$SQL_command = "DELETE FROM listEntries WHERE listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) AND productId=".$productId;
if (DBLink::getDbLink()->query($SQL_command))
echo "Product removed from list.<br>";
else
echo "Product could not be removed from list.<br>";
}
}
else
echo "Select a product.";
}
function oneListExists()
{
$SQL_command = "SELECT COUNT(id) as listAmount FROM lists";
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
if ($row = mysqli_fetch_object($mysqli_result))
{
if($row->listAmount > 0)
return true;
}
return false;
}
function printShoppingList()
{
echo " <form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"addToList\" />
<select name=\"productToAdd\" id=\"productToAdd\">
<option value=\"0\">Select product to add</option>
";
$SQL_command = "SELECT id, name FROM `products` ORDER BY name ASC";
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
while ($row = mysqli_fetch_object($mysqli_result))
{
echo "<option value=\"".$row->id."\">".$row->name."</option>";
}
echo " </select>
<input type=\"submit\" value=\"add\" />
</form>";
if(oneListExists())
{
$SQL_command = "SELECT * FROM listEntries INNER JOIN products ON listEntries.productId=products.id INNER JOIN storeTypes ON products.storeTypeId = storeTypes.id WHERE listEntries.listId=(SELECT id FROM `lists` ORDER BY creationTime DESC LIMIT 1) ORDER BY storeTypes.name, products.name ASC";
$mysqli_result = DBLink::getDbLink()->query($SQL_command);
$lastShop="";
while ($row = mysqli_fetch_array($mysqli_result))
{
if(isset($lastShop) && $lastShop != "" && $row[7] != $lastShop)
echo "</table>";
if(!isset($lastShop) || $row[7] != $lastShop)
{
$lastShop = $row[7];
echo $lastShop."<br>================";
echo "<table border=\"0\">";
}
echo "<tr>
<td style=\"vertical-align:top;\">".$row[3]."</td>
<td style=\"vertical-align:top;\">
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\">
<input type=\"hidden\" id=\"command\" name=\"command\" value=\"removeFromList\" />
<input type=\"hidden\" id=\"productToRemove\" name=\"productToRemove\" value=\"".$row[1]."\" />
<input class=\"smallButton\" type=\"submit\" value=\"remove\" />
</form>
</td>
</tr>";
}
echo "</table>";
}
else
echo "No list exists, yet.";
}
function printRhasspyProductList()
{
$SQL_Befehl = "SELECT name, synonyms FROM products ORDER BY products.name ASC";
$mysqli_result = DBLink::getDbLink()->query($SQL_Befehl);
$lastShop="";
while ($row = mysqli_fetch_array($mysqli_result))
{
if(is_null($row[1]))
echo $row[0]."\n";
else
echo "(".str_replace(";", "|", $row[1])."|".$row[0]."):".$row[0]."\n";
}
}
?>

View File

@ -0,0 +1,9 @@
<?php
static $mysqlserver = 'mysql.mydomain'; //Host
static $mysqluser = 'shoppingList'; //User [It's recommended to not use root]
static $mysqlpw = 'somePassword'; //Password
static $mysqldb = 'shoppingList'; //Database
static $mysqlPort = 3306;
?>

View File

@ -0,0 +1,63 @@
#!/bin/bash
SHOPPINGITEM=""
SITEID=""
PARAMS=""
SESSIONID=""
while (( "$#" )); do
case "$1" in
--shoppingProduct)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
SHOPPINGITEM=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
--siteId)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
SITEID=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
--sessionId)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
SESSIONID=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
if [ "$SHOPPINGITEM" = "" ]
then
echo "Was soll ich auf die Liste setzen?"
exit 1
else
java -jar /home/pi/hc_scripts/ShoppingList.jar --action addToList --shoppingProduct $SHOPPINGITEM
if [ "$?" -eq "0" ]
then
exit 0
else
exit 1
fi
fi

View File

@ -0,0 +1,63 @@
#!/bin/bash
SHOPPINGITEM=""
SITEID=""
PARAMS=""
SESSIONID=""
while (( "$#" )); do
case "$1" in
--shoppingProduct)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
SHOPPINGITEM=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
--siteId)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
SITEID=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
--sessionId)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
SESSIONID=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"
if [ "$SHOPPINGITEM" = "" ]
then
echo "Was soll ich von der Liste streichen?"
exit 1
else
java -jar /home/pi/hc_scripts/ShoppingList.jar --action removeFromList --shoppingProduct $SHOPPINGITEM
if [ "$?" -eq "0" ]
then
exit 0
else
exit 1
fi
fi

View File

@ -0,0 +1,9 @@
#!/bin/bash
java -jar /home/pi/hc_scripts/ShoppingList.jar --action resetList
if [ "$?" -eq "0" ]
then
exit 0
else
exit 1
fi

View File

@ -0,0 +1,9 @@
#!/bin/bash
java -jar /home/pi/hc_scripts/ShoppingList.jar --action sendList
if [ "$?" -eq "0" ]
then
exit 0
else
exit 1
fi