Add department_relays and department_gates object classes to support relay and gate management for departments

This commit is contained in:
Jeppe Bundgaard
2026-03-03 11:47:11 +01:00
parent a62bcdb47d
commit 29b6df381e
2 changed files with 207 additions and 0 deletions
@@ -0,0 +1,100 @@
<?php
namespace objects;
use classes\db;
use classes\object_property;
use classes\selfserve;
use Exception;
use traits\db_object_t;
class department_gates_o extends db
{
use db_object_t;
public object_property $department; // The department id the gate relates to
// The reason why there's two separate boolean properties for entrance and exit is to allow for departments where the same gate can be both an entrance and an exit (e.g. a turnstile that people both enter and exit through, where the gate criteria is based on a phone call triggered by passing through the turnstile, and the same criteria applies for both entering and exiting)
public object_property $is_entrance; // Whether the gate is an entrance gate
public object_property $is_exit; // Whether the gate is an exit gate
public object_property $name; // The name of the gate
public object_property $config; // A JSON with the gate config, including the type of gate and any relevant parameters (e.g. for a phone call triggered gate, the config would include the phone number to call and the call duration threshold)
public object_property $created_at;
public object_property $updated_at;
public object_property $deleted_at;
public function structure(): void
{
$this->setTable('department_gates');
}
/**
* Add a gate
* @param departments_o $department The department for the gate
* @param bool $is_entrance Whether the gate is an entrance gate (true) or an exit gate (false)
* @param bool $is_exit Whether the gate is an exit gate (true) or an entrance gate (false)
* @param string $name The name of the gate
* @param array $config A JSON with the gate config, including the type of gate and any relevant parameters (e.g. for a phone call triggered gate, the config would include the phone number to call and the call duration threshold)
* @return department_gates_o
* @throws Exception If the object was not created successfully
*/
public function add(departments_o $department, bool $is_entrance, bool $is_exit, string $name, array $config): department_gates_o
{
// Sanitize the input
$department->requireSelected();
$name = trim($name);
if (empty($name)) {
throw new Exception('Gate name cannot be empty');
}
// Add the object
$tmp_id = self::add_object([
'department' => (int)$department->id,
'is_entrance' => (bool)$is_entrance,
'is_exit' => (bool)$is_exit,
'name' => $name,
'config' => (array)$config,
]);
$this->id = $tmp_id;
self::getObjectProperties();
self::objectChanged();
return $this;
}
public function getObjectProperties(): void
{
$this->department = new object_property($this->table, $this->id, 'department', 'int', false);
$this->is_entrance = new object_property($this->table, $this->id, 'is_entrance', 'bool', false);
$this->is_exit = new object_property($this->table, $this->id, 'is_exit', 'bool', false);
$this->name = new object_property($this->table, $this->id, 'name', 'string', false);
$this->config = new object_property($this->table, $this->id, 'config', 'json', false);
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false);
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', false);
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
}
public function objectChanged(): void
{
//TODO: Add cache invalidation
}
/**
* Get the gate as an array, including progress details if $include_progress is true
* @return array The gate as an array
* @throws Exception If the object is not valid or selected.
*/
public function asArray(): array
{
$this->requireSelected();
return [
'id' => (int)$this->id,
'department' => (int)$this->department->value(),
'is_entrance' => (bool)$this->is_entrance->value(),
'is_exit' => (bool)$this->is_exit->value(),
'name' => (string)$this->name->value(),
'config' => (array)$this->config->value(),
'created_at' => (string)$this->created_at->value(),
'updated_at' => (string)$this->updated_at->value(),
];
}
}
@@ -0,0 +1,107 @@
<?php
namespace objects;
use classes\db;
use classes\object_property;
use classes\selfserve;
use Exception;
use traits\db_object_t;
class department_relays_o extends db
{
use db_object_t;
public object_property $department; // The department id
public object_property $relay_id; // The Shelly relay id
public object_property $name; // The name of the relay
public object_property $type; // The type of relay (e.g. 'SWITCH', 'TRIGGER')
public object_property $config; // A JSON with the relay config, including any relevant parameters (e.g. for a trigger what should happen, and the trigger webhook token)
public object_property $created_at;
public object_property $updated_at;
public object_property $deleted_at;
public function structure(): void
{
$this->setTable('department_relays');
}
/**
* Add a relay
* @param departments_o $department The department for the relay
* @param string $relay_id The Shelly relay id
* @param string $name The name of the relay
* @param string $type The type of relay (e.g. 'SWITCH', 'TRIGGER')
* @param array $config A JSON with the relay config, including any relevant parameters (e.g. for a trigger what should happen, and the trigger webhook token)
* @return department_relays_o
* @throws Exception If the object was not created successfully
*/
public function add(departments_o $department, string $relay_id, string $name, string $type, array $config): department_relays_o
{
// Sanitize the input
$department->requireSelected();
$relay_id = trim($relay_id);
if (empty($relay_id)) {
throw new Exception('Relay ID cannot be empty');
}
$name = trim($name);
if (empty($name)) {
throw new Exception('Relay name cannot be empty');
}
$type = trim($type);
if (empty($type)) {
throw new Exception('Relay type cannot be empty');
}
// Add the object
$tmp_id = self::add_object([
'department' => (int)$department->id,
'relay_id' => $relay_id,
'name' => $name,
'type' => $type,
'config' => (array)$config,
]);
$this->id = $tmp_id;
self::getObjectProperties();
self::objectChanged();
return $this;
}
public function getObjectProperties(): void
{
$this->department = new object_property($this->table, $this->id, 'department', 'int', false);
$this->relay_id = new object_property($this->table, $this->id, 'relay_id', 'string', false);
$this->name = new object_property($this->table, $this->id, 'name', 'string', false);
$this->type = new object_property($this->table, $this->id, 'type', 'string', false);
$this->config = new object_property($this->table, $this->id, 'config', 'json', false);
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp', false);
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp', false);
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp', false);
}
public function objectChanged(): void
{
//TODO: Add cache invalidation
}
/**
* Get the relay as an array
* @return array The relay as an array
* @throws Exception If the object is not valid or selected.
*/
public function asArray(): array
{
$this->requireSelected();
return [
'id' => (int)$this->id,
'department' => (int)$this->department->value(),
'relay_id' => (string)$this->relay_id->value(),
'name' => (string)$this->name->value(),
'type' => (string)$this->type->value(),
'config' => (array)$this->config->value(),
'created_at' => (string)$this->created_at->value(),
'updated_at' => (string)$this->updated_at->value(),
];
}
}