Skip to content

Commit

Permalink
Add connected scale support (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
HoverEpic committed Feb 21, 2024
1 parent 32827c3 commit e1864c4
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 5 deletions.
5 changes: 4 additions & 1 deletion core/composants.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ function config_types3_form_modif(array $props): string {
<div class="col-md-4"><?= textInput(['name' => 'description', 'text' => "Description:"], $props['description']) ?></div>
<div class="col-md-1"><label for="couleur">Couleur:</label><input id="couleur" name="couleur" type="color" value="<?= $props['couleur'] ?>" class="form-control" required></div>
<php if (array_key_exists('position', $props)) { ?>
<div class="col-md-4"><?= array_key_exists('position', $props) ? numberInput(['name' => 'position', 'text' => "Position:"], $props['position']) : "" ?></div>
<div class="col-md-1"><?= array_key_exists('position', $props) ? numberInput(['name' => 'position', 'text' => "Position:"], $props['position']) : "" ?></div>
<php } ?>
<php if (array_key_exists('serial_balance', $props)) { ?>
<!--<div class="col-md-1"><label for="serial_balance">Balance série:</label><?= array_key_exists('serial_balance', $props) ? checkBox(['name' => 'serial_balance', 'text' => 'balance'], $props['serial_balance']) : "" ?></div>-->
<php } ?>
<div class="col-md-1"><br><button class="btn btn-default">Modifier</button></div>
</form>
Expand Down
4 changes: 2 additions & 2 deletions core/requetes.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function convention_sortie_by_id(PDO $bdd, int $id): array {
}

function points_collecte_id(PDO $bdd, int $id): array {
$sql = 'SELECT pesee_max, nom
$sql = 'SELECT pesee_max, nom, serial_balance
FROM points_collecte
WHERE id = :id';
return fetch_id($bdd, $sql, $id);
Expand All @@ -205,7 +205,7 @@ function points_sorties_id(PDO $bdd, int $id): array {

function points_collectes(PDO $bdd): array {
$sql = 'SELECT id, nom, adresse, couleur, pesee_max,
visible, timestamp, commentaire, pesee_max
visible, timestamp, commentaire, pesee_max, serial_balance
FROM points_collecte';
return fetch_all($sql, $bdd);
}
Expand Down
52 changes: 52 additions & 0 deletions ifaces/collecte.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
conteneurs: <?= json_encode(filter_visibles(types_contenants($bdd)), JSON_NUMERIC_CHECK); ?>,
types_action: <?= json_encode($types_action, JSON_NUMERIC_CHECK); ?>,
localites: <?= json_encode(filter_visibles(localites($bdd)), JSON_NUMERIC_CHECK); ?>,
serial_balance: <?= $point_collecte['serial_balance'] === 1 ? 'true' : 'false' ?>,
};
</script>

Expand Down Expand Up @@ -124,7 +125,58 @@
initUI('../api/collectes.php', encaisse);

window.OressourceEnv.tickets = [ ticketItem ];

if (window.OressourceEnv.serial_balance) {
const button = document.createElement('button');
button.setAttribute('class', 'btn btn-success');
button.innerHTML = "Connecter balance"
document.getElementById('numpad').append(button);
button.addEventListener('click', () => setup_scale(numpad), true);
}

}, false);

async function setup_scale(numpad) {
if ("serial" in navigator) {
console.log('Serial is supported, try to connect to scale......')

// Test set value
//numpad.input.value = 15

// Filter on devices with the Arduino Uno USB Vendor/Product IDs.
const filters = [
{ usbVendorId: 0x2341, usbProductId: 0x0043 },
{ usbVendorId: 0x2341, usbProductId: 0x0001 }
];

// Prompt user to select any serial port.
const port = await navigator.serial.requestPort(/*{filters}*/);

// Wait for the serial port to open.
await port.open({ baudRate: 9600 });
while (port.readable) {
const reader = port.readable.getReader();

try {
while (true) {
const { value, done } = await reader.read();
if (done) {
// Allow the serial port to be closed later.
reader.releaseLock();
break;
}
if (value) {
console.log(value);
}
}
} catch (error) {
// TODO: Handle non-fatal read error.
}
}
} else {
console.log('Serial scale NOT supported')
}
}
</script>
<?php
include_once 'pied.php';
Expand Down
3 changes: 3 additions & 0 deletions ifaces/edition_points_collecte.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<div class="col-md-2"><label for="commentaire">Commentaire:</label><br><br> <input type="text" value ="<?= $_GET['commentaire'] ?? ''; ?>" name="commentaire" id="commentaire" class="form-control" required></div>
<div class="col-md-2"><label for="pesee_max">Masse maxi. d'une pesée (Kg):</label> <input type="text" value ="<?= $_GET['pesee_max'] ?? ''; ?>" name="pesee_max" id="pesee_max" class="form-control" required></div>
<div class="col-md-1"><label for="couleur">Couleur:</label><br><br><input type="color" value="#<?= $_GET['couleur'] ?? ''; ?>" name="couleur" id="couleur" class="form-control" required></div>
<div class="col-md-1"><label for="serial_balance">Balance série:</label><input type="checkbox" <?= $_GET['serial_balance'] == 1 ? 'selected' : ''; ?> name="serial_balance" id="serial_balance" class="form-control"></div>
<div class="col-md-1"><br><br><button class="btn btn-default">Creer!</button></div>
</form>
</div>
Expand All @@ -54,6 +55,7 @@
<th>Commentaire</th>
<th>Pesée maxi.</th>
<th>Visible</th>
<th>Balance série</th>
<th>Modifier</th>
</tr>
</thead>
Expand All @@ -69,6 +71,7 @@
<td><?= $donnees['commentaire']; ?></td>
<td><?= $donnees['pesee_max']; ?></td>
<td><?= configBtnVisible(['url' => 'points_collecte', 'id' => $donnees['id'], 'visible' => $donnees['visible']]) ?></td>
<td><?= $donnees['serial_balance'] === 1 ? "OUI" : "NON"; ?></td>
<td><a style="appearance: button" class="btn btn-warning btn-sm" href="../ifaces/modification_points_collecte.php?id=<?= $donnees['id'] ?>">Modifier</a></td>
</tr>
<?php } ?>
Expand Down
1 change: 1 addition & 0 deletions ifaces/modification_points_collecte.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<div class="col-md-2"><label for="commentaire">Commentaire:</label><br><br><input type="text" value ="<?= $_GET['commentaire'] ?? $donnees['commentaire'] ?>" name="commentaire" id="commentaire" class="form-control" required></div>
<div class="col-md-1"><label for="pesee_max">Pesée maxi:</label><input type="text" value="<?= $_GET['pesee_max'] ?? $donnees['pesee_max'] ?>" name="pesee_max" id="pesee_max" class="form-control" required></div>
<div class="col-md-1"><label for="couleur">Couleur:</label><br><br><input type="color" value="<?= $donnees['couleur'] ?>" name="couleur" id="couleur" class="form-control" required></div>
<div class="col-md-1"><label for="serial_balance">Balance série:</label><input type="checkbox" name="serial_balance" id="serial_balance" class="form-control" <?= $donnees['serial_balance'] === 1 ? 'checked' : ''; ?>></div>
<div class="col-md-1"><br><br><button class="btn btn-warning">Modifier</button></div>
</form>
<br><br>
Expand Down
4 changes: 4 additions & 0 deletions moteur/dbconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,9 @@ function updateDB(PDO $bdd) {
if (!count($bdd->query("SHOW COLUMNS FROM `utilisateurs` LIKE 'page_defaut'")->fetchAll())) {
$bdd -> exec("ALTER TABLE `utilisateurs` ADD COLUMN page_defaut text");
}
// Check for utilisateurs.page_defaut
if (!count($bdd->query("SHOW COLUMNS FROM `points_collecte` LIKE 'serial_balance'")->fetchAll())) {
$bdd -> exec("ALTER TABLE `points_collecte` ADD COLUMN serial_balance tinyint(1) NOT NULL DEFAULT '0'");
}
// Check for other sql updates....
}
5 changes: 3 additions & 2 deletions moteur/modification_points_collecte_post.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
session_start();
if (isset($_SESSION['id']) && $_SESSION['systeme'] === 'oressource' && (strpos($_SESSION['niveau'], 'k') !== false)) {
require_once '../moteur/dbconfig.php';
$balance = isset($_POST['serial_balance']) ? 1 : 0;
try {
$req = $bdd->prepare('UPDATE points_collecte SET nom = :nom, adresse = :adresse , commentaire = :commentaire, pesee_max = :pesee_max, couleur = :couleur WHERE id = :id');
$req->execute(['nom' => $_POST['nom'], 'adresse' => $_POST['adresse'], 'commentaire' => $_POST['commentaire'], 'pesee_max' => $_POST['pesee_max'], 'couleur' => $_POST['couleur'], 'id' => $_POST['id']]);
$req = $bdd->prepare('UPDATE points_collecte SET nom = :nom, adresse = :adresse , commentaire = :commentaire, pesee_max = :pesee_max, couleur = :couleur, serial_balance = :serial_balance WHERE id = :id');
$req->execute(['nom' => $_POST['nom'], 'adresse' => $_POST['adresse'], 'commentaire' => $_POST['commentaire'], 'pesee_max' => $_POST['pesee_max'], 'couleur' => $_POST['couleur'], 'serial_balance' => $balance, 'id' => $_POST['id']]);
$req->closeCursor();
header('Location:../ifaces/edition_points_collecte.php');
} catch (PDOException $e) {
Expand Down

0 comments on commit e1864c4

Please sign in to comment.