mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-02-16 10:58:32 -05:00
Switch to SQL Server
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
using ChrisKaczor.HomeMonitor.Weather.Models;
|
using ChrisKaczor.HomeMonitor.Weather.Models;
|
||||||
using Dapper;
|
using Dapper;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Npgsql;
|
using System.Data.SqlClient;
|
||||||
|
|
||||||
namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
||||||
{
|
{
|
||||||
@@ -16,22 +16,22 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
|||||||
|
|
||||||
public void EnsureDatabase()
|
public void EnsureDatabase()
|
||||||
{
|
{
|
||||||
var connectionStringBuilder = new NpgsqlConnectionStringBuilder
|
var connectionStringBuilder = new SqlConnectionStringBuilder
|
||||||
{
|
{
|
||||||
Host = _configuration["Weather:Database:Host"],
|
DataSource = _configuration["Weather:Database:Host"],
|
||||||
Username = _configuration["Weather:Database:User"],
|
UserID = _configuration["Weather:Database:User"],
|
||||||
Password = _configuration["Weather:Database:Password"],
|
Password = _configuration["Weather:Database:Password"],
|
||||||
Database = "postgres"
|
InitialCatalog = "master"
|
||||||
};
|
};
|
||||||
|
|
||||||
using (var connection = new NpgsqlConnection(connectionStringBuilder.ConnectionString))
|
using (var connection = new SqlConnection(connectionStringBuilder.ConnectionString))
|
||||||
{
|
{
|
||||||
var command = new NpgsqlCommand { Connection = connection };
|
var command = new SqlCommand { Connection = connection };
|
||||||
|
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
// Check to see if the database exists
|
// Check to see if the database exists
|
||||||
command.CommandText = $"SELECT TRUE from pg_database WHERE datname='{_configuration["Weather:Database:Name"]}'";
|
command.CommandText = $"SELECT CAST(1 as bit) from sys.databases WHERE name='{_configuration["Weather:Database:Name"]}'";
|
||||||
var databaseExists = (bool?)command.ExecuteScalar();
|
var databaseExists = (bool?)command.ExecuteScalar();
|
||||||
|
|
||||||
// Create database if needed
|
// Create database if needed
|
||||||
@@ -52,17 +52,17 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private NpgsqlConnection CreateConnection()
|
private SqlConnection CreateConnection()
|
||||||
{
|
{
|
||||||
var connectionStringBuilder = new NpgsqlConnectionStringBuilder
|
var connectionStringBuilder = new SqlConnectionStringBuilder
|
||||||
{
|
{
|
||||||
Host = _configuration["Weather:Database:Host"],
|
DataSource = _configuration["Weather:Database:Host"],
|
||||||
Username = _configuration["Weather:Database:User"],
|
UserID = _configuration["Weather:Database:User"],
|
||||||
Password = _configuration["Weather:Database:Password"],
|
Password = _configuration["Weather:Database:Password"],
|
||||||
Database = _configuration["Weather:Database:Name"]
|
InitialCatalog = _configuration["Weather:Database:Name"]
|
||||||
};
|
};
|
||||||
|
|
||||||
var connection = new NpgsqlConnection(connectionStringBuilder.ConnectionString);
|
var connection = new SqlConnection(connectionStringBuilder.ConnectionString);
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
return connection;
|
return connection;
|
||||||
@@ -74,7 +74,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service.Data
|
|||||||
{
|
{
|
||||||
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.CreateReading.sql");
|
var query = ResourceReader.GetString("ChrisKaczor.HomeMonitor.Weather.Service.Data.Resources.CreateReading.sql");
|
||||||
|
|
||||||
connection.Execute(query, weatherMessage);
|
connection.Query(query, weatherMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
INSERT INTO weather_reading (timestamp, wind_direction, wind_speed, humidity, humidity_temperature, rain, pressure,
|
INSERT INTO Reading (Timestamp, WindDirection, WindSpeed, Humidity, HumidityTemperature, Rain, Pressure,
|
||||||
pressure_temperature, battery_level, light_level, latitude, longitude, altitude,
|
PressureTemperature, BatteryLevel, LightLevel, Latitude, Longitude, Altitude,
|
||||||
satellite_count, gps_timestamp)
|
SatelliteCount, GpsTimestamp)
|
||||||
VALUES (:timestamp, :windDirection, :windSpeed, :humidity, :humidityTemperature, :rain, :pressure, :pressureTemperature,
|
VALUES (@Timestamp, @WindDirection, @WindSpeed, @Humidity, @HumidityTemperature, @Rain, @Pressure, @PressureTemperature,
|
||||||
:batteryLevel, :lightLevel, :latitude, :longitude, :altitude, :satelliteCount, :gpsTimestamp)
|
@BatteryLevel, @LightLevel, @Latitude, @Longitude, @Altitude, @SatelliteCount, @GpsTimestamp)
|
||||||
ON CONFLICT DO NOTHING
|
|
||||||
@@ -1,24 +1,21 @@
|
|||||||
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
|
IF NOT EXISTS(SELECT 1 FROM sys.tables WHERE name = 'Reading')
|
||||||
|
CREATE TABLE Reading
|
||||||
CREATE TABLE IF NOT EXISTS weather_reading
|
(
|
||||||
(
|
Timestamp datetimeoffset NOT NULL
|
||||||
timestamp timestamptz NOT NULL
|
CONSTRAINT reading_pk
|
||||||
CONSTRAINT weather_reading_pk
|
PRIMARY KEY,
|
||||||
PRIMARY KEY,
|
WindDirection int NOT NULL,
|
||||||
wind_direction int NOT NULL,
|
WindSpeed decimal(4, 1) NOT NULL,
|
||||||
wind_speed double precision NOT NULL,
|
Humidity decimal(4, 1) NOT NULL,
|
||||||
humidity double precision NOT NULL,
|
HumidityTemperature decimal(4, 1) NOT NULL,
|
||||||
humidity_temperature double precision NOT NULL,
|
Rain decimal(2, 2) NOT NULL,
|
||||||
rain double precision NOT NULL,
|
Pressure decimal(8, 2) NOT NULL,
|
||||||
pressure double precision NOT NULL,
|
PressureTemperature decimal(4, 1) NOT NULL,
|
||||||
pressure_temperature double precision NOT NULL,
|
BatteryLevel decimal(3, 2) NOT NULL,
|
||||||
battery_level double precision NOT NULL,
|
LightLevel decimal(3, 2) NOT NULL,
|
||||||
light_level double precision NOT NULL,
|
Latitude decimal(9, 6) NOT NULL,
|
||||||
latitude double precision NOT NULL,
|
Longitude decimal(9, 6) NOT NULL,
|
||||||
longitude double precision NOT NULL,
|
Altitude decimal(5, 1) NOT NULL,
|
||||||
altitude double precision NOT NULL,
|
SatelliteCount int NOT NULL,
|
||||||
satellite_count double precision NOT NULL,
|
GpsTimestamp datetimeoffset NOT NULL
|
||||||
gps_timestamp timestamptz NOT NULL
|
);
|
||||||
);
|
|
||||||
|
|
||||||
SELECT create_hypertable('weather_reading', 'timestamp', if_not_exists => TRUE);
|
|
||||||
|
|||||||
@@ -55,7 +55,8 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
|
|||||||
|
|
||||||
_queueModel.BasicConsume(_configuration["Weather:Queue:Name"], true, consumer);
|
_queueModel.BasicConsume(_configuration["Weather:Queue:Name"], true, consumer);
|
||||||
|
|
||||||
_hubConnection = new HubConnectionBuilder().WithUrl(_configuration["Hub:Weather"]).Build();
|
if (!string.IsNullOrEmpty(_configuration["Hub:Weather"]))
|
||||||
|
_hubConnection = new HubConnectionBuilder().WithUrl(_configuration["Hub:Weather"]).Build();
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
@@ -64,7 +65,7 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
|
|||||||
{
|
{
|
||||||
WriteLog("MessageHandler: Stop");
|
WriteLog("MessageHandler: Stop");
|
||||||
|
|
||||||
_hubConnection.StopAsync(cancellationToken).Wait(cancellationToken);
|
_hubConnection?.StopAsync(cancellationToken).Wait(cancellationToken);
|
||||||
|
|
||||||
_queueModel.Close();
|
_queueModel.Close();
|
||||||
_queueConnection.Close();
|
_queueConnection.Close();
|
||||||
@@ -100,6 +101,9 @@ namespace ChrisKaczor.HomeMonitor.Weather.Service
|
|||||||
|
|
||||||
_database.StoreWeatherData(weatherMessage);
|
_database.StoreWeatherData(weatherMessage);
|
||||||
|
|
||||||
|
if (_hubConnection == null)
|
||||||
|
return;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (_hubConnection.State == HubConnectionState.Disconnected)
|
if (_hubConnection.State == HubConnectionState.Disconnected)
|
||||||
|
|||||||
@@ -19,12 +19,12 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.0.7" />
|
<PackageReference Include="ChrisKaczor.HomeMonitor.Weather.Models" Version="1.0.8" />
|
||||||
<PackageReference Include="Dapper" Version="1.60.6" />
|
<PackageReference Include="Dapper" Version="1.60.6" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="1.1.0" />
|
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="1.1.0" />
|
||||||
<PackageReference Include="Npgsql" Version="4.0.8" />
|
|
||||||
<PackageReference Include="RabbitMQ.Client" Version="5.1.0" />
|
<PackageReference Include="RabbitMQ.Client" Version="5.1.0" />
|
||||||
|
<PackageReference Include="System.Data.SqlClient" Version="4.6.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
},
|
},
|
||||||
"Weather": {
|
"Weather": {
|
||||||
"Database": {
|
"Database": {
|
||||||
"Name": "weather"
|
"Name": "Weather"
|
||||||
},
|
},
|
||||||
"Queue": {
|
"Queue": {
|
||||||
"Name": "weather"
|
"Name": "weather"
|
||||||
|
|||||||
@@ -19,26 +19,25 @@ spec:
|
|||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: weather-database
|
- name: weather-database
|
||||||
image: timescale/timescaledb
|
image: mcr.microsoft.com/mssql/server
|
||||||
terminationMessagePath: "/dev/termination-log"
|
terminationMessagePath: "/dev/termination-log"
|
||||||
terminationMessagePolicy: File
|
terminationMessagePolicy: File
|
||||||
imagePullPolicy: IfNotPresent
|
imagePullPolicy: IfNotPresent
|
||||||
env:
|
env:
|
||||||
- name: POSTGRES_USER
|
- name: SA_PASSWORD
|
||||||
valueFrom:
|
|
||||||
secretKeyRef:
|
|
||||||
name: weather-database-credentials
|
|
||||||
key: username
|
|
||||||
- name: POSTGRES_PASSWORD
|
|
||||||
valueFrom:
|
valueFrom:
|
||||||
secretKeyRef:
|
secretKeyRef:
|
||||||
name: weather-database-credentials
|
name: weather-database-credentials
|
||||||
key: password
|
key: password
|
||||||
- name: POSTGRES_DB
|
- name: ACCEPT_EULA
|
||||||
value: weather
|
value: "Y"
|
||||||
|
- name: MSSQL_PID
|
||||||
|
value: Express
|
||||||
|
- name: TZ
|
||||||
|
value: America/New_York
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- name: data
|
- name: data
|
||||||
mountPath: /var/lib/postgresql/data
|
mountPath: /var/opt/mssql
|
||||||
restartPolicy: Always
|
restartPolicy: Always
|
||||||
terminationGracePeriodSeconds: 30
|
terminationGracePeriodSeconds: 30
|
||||||
dnsPolicy: ClusterFirst
|
dnsPolicy: ClusterFirst
|
||||||
@@ -53,7 +52,7 @@ spec:
|
|||||||
storageClassName: local-path
|
storageClassName: local-path
|
||||||
resources:
|
resources:
|
||||||
requests:
|
requests:
|
||||||
storage: 4Gi
|
storage: 4Gi
|
||||||
---
|
---
|
||||||
kind: Service
|
kind: Service
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
@@ -62,10 +61,10 @@ metadata:
|
|||||||
spec:
|
spec:
|
||||||
ports:
|
ports:
|
||||||
- name: client
|
- name: client
|
||||||
port: 5432
|
port: 1433
|
||||||
selector:
|
selector:
|
||||||
app: weather-database
|
app: weather-database
|
||||||
type: ClusterIP
|
type: LoadBalancer
|
||||||
---
|
---
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
|
|||||||
Reference in New Issue
Block a user