first commit
This commit is contained in:
commit
b31e9a7261
38 files changed
+3822
No files matched your search
@@ -0,0 +1,164 @@
|
||||
using Gateway.Api.Options;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Gateway.Api.Services;
|
||||
|
||||
public sealed class GatewayDatabase
|
||||
{
|
||||
private readonly string _connectionString;
|
||||
|
||||
public string DataDirectory { get; }
|
||||
|
||||
public GatewayDatabase(IOptions<GatewayOptions> options)
|
||||
{
|
||||
DataDirectory = Path.GetFullPath(options.Value.DataDirectory);
|
||||
Directory.CreateDirectory(DataDirectory);
|
||||
_connectionString = new SqliteConnectionStringBuilder
|
||||
{
|
||||
DataSource = Path.Combine(DataDirectory, "seiugw.db"),
|
||||
Mode = SqliteOpenMode.ReadWriteCreate,
|
||||
Cache = SqliteCacheMode.Shared
|
||||
}.ToString();
|
||||
}
|
||||
|
||||
public SqliteConnection OpenConnection()
|
||||
{
|
||||
var connection = new SqliteConnection(_connectionString);
|
||||
connection.Open();
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = "PRAGMA foreign_keys = ON; PRAGMA busy_timeout = 5000;";
|
||||
command.ExecuteNonQuery();
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
using var connection = OpenConnection();
|
||||
using var command = connection.CreateCommand();
|
||||
command.CommandText = """
|
||||
PRAGMA journal_mode = WAL;
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
key TEXT NOT NULL PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS admin_account (
|
||||
id INTEGER NOT NULL PRIMARY KEY CHECK(id = 1),
|
||||
username TEXT NOT NULL UNIQUE CHECK(username = 'admin'),
|
||||
password_hash BLOB NOT NULL,
|
||||
password_salt BLOB NOT NULL,
|
||||
password_iterations INTEGER NOT NULL,
|
||||
changed_at_utc TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS points (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
display_name TEXT NOT NULL,
|
||||
protocol TEXT NOT NULL,
|
||||
scope TEXT NOT NULL,
|
||||
value_type TEXT NOT NULL,
|
||||
readable INTEGER NOT NULL,
|
||||
writable INTEGER NOT NULL,
|
||||
external_identity TEXT NULL,
|
||||
address_json TEXT NOT NULL,
|
||||
created_at_utc TEXT NOT NULL,
|
||||
bacnet_object_kind TEXT NULL,
|
||||
bacnet_object_instance INTEGER NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_points_external_identity
|
||||
ON points(protocol, external_identity)
|
||||
WHERE external_identity IS NOT NULL;
|
||||
CREATE TABLE IF NOT EXISTS mappings (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
source_point_id TEXT NOT NULL REFERENCES points(id) ON DELETE RESTRICT,
|
||||
target_point_id TEXT NOT NULL REFERENCES points(id) ON DELETE RESTRICT,
|
||||
direction TEXT NOT NULL,
|
||||
enabled INTEGER NOT NULL,
|
||||
bacnet_priority INTEGER NULL,
|
||||
acknowledge_multiple_external_writers INTEGER NOT NULL,
|
||||
transform_json TEXT NOT NULL,
|
||||
created_at_utc TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS mqtt_mappings (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
point_id TEXT NOT NULL REFERENCES points(id) ON DELETE RESTRICT,
|
||||
format TEXT NOT NULL,
|
||||
direction TEXT NOT NULL,
|
||||
quality_of_service TEXT NOT NULL,
|
||||
retain INTEGER NOT NULL,
|
||||
project_year INTEGER NULL,
|
||||
project_number TEXT NULL,
|
||||
isp_number TEXT NULL,
|
||||
se_category TEXT NULL,
|
||||
topic_name TEXT NULL,
|
||||
json_topic TEXT NULL,
|
||||
json_property TEXT NULL,
|
||||
created_at_utc TEXT NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_mqtt_mapping_point_topic
|
||||
ON mqtt_mappings(point_id, format, project_year, project_number, isp_number, se_category, topic_name, json_topic, json_property);
|
||||
CREATE TABLE IF NOT EXISTS point_values (
|
||||
point_id TEXT NOT NULL PRIMARY KEY REFERENCES points(id) ON DELETE CASCADE,
|
||||
value_json TEXT NOT NULL,
|
||||
source_protocol TEXT NOT NULL,
|
||||
updated_at_utc TEXT NOT NULL,
|
||||
status TEXT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS audit_log (
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
occurred_at_utc TEXT NOT NULL,
|
||||
category TEXT NOT NULL,
|
||||
message TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS trend_logs (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
source_point_id TEXT NOT NULL REFERENCES points(id) ON DELETE RESTRICT,
|
||||
name TEXT NOT NULL,
|
||||
bacnet_object_instance INTEGER NOT NULL UNIQUE,
|
||||
interval_seconds INTEGER NOT NULL,
|
||||
max_records INTEGER NOT NULL,
|
||||
enabled INTEGER NOT NULL,
|
||||
created_at_utc TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS trend_records (
|
||||
trend_log_id TEXT NOT NULL REFERENCES trend_logs(id) ON DELETE CASCADE,
|
||||
sequence_number INTEGER NOT NULL,
|
||||
timestamp_utc TEXT NOT NULL,
|
||||
value_json TEXT NOT NULL,
|
||||
source_protocol TEXT NOT NULL,
|
||||
status TEXT NULL,
|
||||
PRIMARY KEY(trend_log_id, sequence_number)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_trend_records_query
|
||||
ON trend_records(trend_log_id, sequence_number DESC);
|
||||
""";
|
||||
command.ExecuteNonQuery();
|
||||
EnsureColumn(connection, "points", "bacnet_object_kind", "TEXT NULL");
|
||||
EnsureColumn(connection, "points", "bacnet_object_instance", "INTEGER NULL");
|
||||
using var bacnetObjectIndex = connection.CreateCommand();
|
||||
bacnetObjectIndex.CommandText = """
|
||||
DROP INDEX IF EXISTS idx_points_bacnet_object;
|
||||
CREATE UNIQUE INDEX idx_points_bacnet_object
|
||||
ON points(bacnet_object_kind, bacnet_object_instance)
|
||||
WHERE scope = 'Gateway' AND bacnet_object_kind IS NOT NULL AND bacnet_object_instance IS NOT NULL;
|
||||
""";
|
||||
bacnetObjectIndex.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
private static void EnsureColumn(SqliteConnection connection, string tableName, string columnName, string definition)
|
||||
{
|
||||
using var pragma = connection.CreateCommand();
|
||||
pragma.CommandText = $"PRAGMA table_info({tableName});";
|
||||
using var reader = pragma.ExecuteReader();
|
||||
while (reader.Read())
|
||||
{
|
||||
if (string.Equals(reader.GetString(1), columnName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
using var alter = connection.CreateCommand();
|
||||
alter.CommandText = $"ALTER TABLE {tableName} ADD COLUMN {columnName} {definition};";
|
||||
alter.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user