243 lines
10 KiB
C#

using Npgsql;
using NpgsqlTypes;
using skyscraper8.DvbNip;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace skyscraper5.Data.PostgreSql
{
public partial class PostgresqlDataStore
{
private Dictionary<NipActualCarrierInformation, int> _knownNipCarriers;
public bool DvbNipTestForCarrier(NipActualCarrierInformation currentCarrierInformation)
{
if (_knownNipCarriers != null)
{
if (_knownNipCarriers.ContainsKey(currentCarrierInformation))
return true;
}
bool result = false;
using (NpgsqlConnection connection = new NpgsqlConnection(connectionStringBuilder.ToString()))
{
connection.Open();
NpgsqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT serial FROM dvbnip_carrier_information WHERE nid = @nid AND cid = @cid AND lid = @lid AND sid = @sid";
command.Parameters.AddWithValue("@nid", NpgsqlDbType.Integer, (int)currentCarrierInformation.NipNetworkId);
command.Parameters.AddWithValue("@cid", NpgsqlDbType.Integer, (int)currentCarrierInformation.NipCarrierId);
command.Parameters.AddWithValue("@lid", NpgsqlDbType.Integer, (int)currentCarrierInformation.NipLinkId);
command.Parameters.AddWithValue("@sid", NpgsqlDbType.Integer, (int)currentCarrierInformation.NipServiceId);
NpgsqlDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
if (_knownNipCarriers == null)
_knownNipCarriers = new Dictionary<NipActualCarrierInformation, int>();
int value = dataReader.GetInt32(0);
_knownNipCarriers.Add(currentCarrierInformation, value);
result = true;
}
dataReader.Close();
}
return result;
}
public void DvbNipInsertCarrier(NipActualCarrierInformation currentCarrierInformation)
{
EnqueueTask(x => DvbNipInsertCarrierEx(x, currentCarrierInformation));
EnqueueTask(CommitTransaction);
}
private void DvbNipInsertCarrierEx(NpgsqlConnection connection, NipActualCarrierInformation currentCarrierInformation)
{
NpgsqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO dvbnip_carrier_information VALUES (DEFAULT,DEFAULT,@nid,@cid,@lid,@sid,@spn)";
command.Parameters.AddWithValue("@nid", NpgsqlDbType.Integer, (int)currentCarrierInformation.NipNetworkId);
command.Parameters.AddWithValue("@cid", NpgsqlDbType.Integer, (int)currentCarrierInformation.NipCarrierId);
command.Parameters.AddWithValue("@lid", NpgsqlDbType.Integer, (int)currentCarrierInformation.NipLinkId);
command.Parameters.AddWithValue("@sid", NpgsqlDbType.Integer, (int)currentCarrierInformation.NipServiceId);
command.Parameters.AddWithValue("@spn", NpgsqlDbType.Text, currentCarrierInformation.NipStreamProviderName);
int a = command.ExecuteNonQuery();
if (a != 1)
throw new DataException(String.Format("Wanted to insert 1 row, got {0}", a));
command.Dispose();
}
public bool DvbNipPrivateDataSpecifier(NipActualCarrierInformation currentCarrierInformation, DateTime versionUpdate,
uint privateDataSpecifier, List<string> privateDataSessions)
{
throw new NotImplementedException();
}
private bool[] _dvbNipKnownNetworks;
public bool DvbNipTestForNetwork(BroadcastNetworkType network)
{
if (_dvbNipKnownNetworks == null)
_dvbNipKnownNetworks = new bool[ushort.MaxValue];
if (_dvbNipKnownNetworks[network.NIPNetworkID])
return true;
bool result = false;
using (NpgsqlConnection connection = new NpgsqlConnection(connectionStringBuilder.ToString()))
{
connection.Open();
NpgsqlCommand command = connection.CreateCommand();
command.CommandText = "SELECT dateadded FROM dvbnip_network WHERE nid = @nid";
command.Parameters.AddWithValue("@nid", NpgsqlDbType.Integer, (int)network.NIPNetworkID);
NpgsqlDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
_dvbNipKnownNetworks[network.NIPNetworkID] = true;
result = true;
}
dataReader.Close();
connection.Close();
}
return result;
}
public void DvbNipInsertNetwork(BroadcastNetworkType network)
{
_dvbNipKnownNetworks[network.NIPNetworkID] = true;
EnqueueTask(x => DvbNipInsertNetworkEx(x, network));
}
private void DvbNipInsertNetworkEx(NpgsqlConnection connection, BroadcastNetworkType network)
{
NpgsqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO dvbnip_network VALUES (@nid,DEFAULT,@pn,@nn,@nt,@op,@wef)";
command.Parameters.AddWithValue("@nid", NpgsqlDbType.Integer, (int)network.NIPNetworkID);
command.Parameters.AddWithValue("@pn", NpgsqlDbType.Text, network.NIPNetworkProviderName);
command.Parameters.AddWithValue("@nn", NpgsqlDbType.Text, network.NetworkName);
command.Parameters.AddWithValue("@nt", NpgsqlDbType.Integer, (int)network.NetworkType);
command.Parameters.AddWithValue("@op", NpgsqlDbType.Double, network.SatellitePosition.OrbitalPosition);
command.Parameters.AddWithValue("@wef", NpgsqlDbType.Integer, (int)network.SatellitePosition.West_East_flag);
command.ExecuteNonQuery();
command.Dispose();
foreach (NIPStreamType nipStreamType in network.NIPStream)
{
DvbNipInsertNetworkStreamType(connection, network.NIPNetworkID, nipStreamType);
}
}
private void DvbNipInsertNetworkStreamType(NpgsqlConnection connection, ushort networkNipNetworkId, NIPStreamType nipStreamType)
{
NpgsqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO dvbnip_network_stream VALUES (@nid,@cid,@lid,@sid,@spn,DEFAULT,@llf,@bt,@s)";
command.Parameters.AddWithValue("@nid", NpgsqlDbType.Integer, (int)networkNipNetworkId);
command.Parameters.AddWithValue("@cid", NpgsqlDbType.Integer, int.Parse(nipStreamType.NIPCarrierID));
command.Parameters.AddWithValue("@lid", NpgsqlDbType.Integer, int.Parse(nipStreamType.NIPLinkID));
command.Parameters.AddWithValue("@sid", NpgsqlDbType.Integer, (int)nipStreamType.NIPServiceID);
command.Parameters.AddWithValue("@spn", NpgsqlDbType.Text, nipStreamType.NIPStreamProviderName);
command.Parameters.AddWithValue("@llf", NpgsqlDbType.Integer, (int)nipStreamType.LinkLayerFormat);
command.Parameters.AddWithValue("@bt", NpgsqlDbType.Integer, (int)nipStreamType.BootstrapStream.BootstrapType);
command.Parameters.AddWithValue("@s", NpgsqlDbType.Integer, (int)nipStreamType.BootstrapStream.Status);
command.ExecuteNonQuery();
command.Dispose();
}
private HashSet<Tuple<ushort, ushort, ushort, ushort>> _knownDvbNipServices;
public bool DvbNipTestForService(BroadcastMediaStreamType broadcastMediaStreamType)
{
if (_knownDvbNipServices == null)
_knownDvbNipServices = new HashSet<Tuple<ushort, ushort, ushort, ushort>>();
Tuple<ushort, ushort, ushort, ushort> coordinates = new Tuple<ushort, ushort, ushort, ushort>(
broadcastMediaStreamType.NIPNetworkID, ushort.Parse(broadcastMediaStreamType.NIPCarrierID),
ushort.Parse(broadcastMediaStreamType.NIPLinkID), broadcastMediaStreamType.NIPServiceID);
if (_knownDvbNipServices.Contains(coordinates))
return true;
bool result = false;
using (NpgsqlConnection connection = new NpgsqlConnection(connectionStringBuilder.ToString()))
{
connection.Open();
NpgsqlCommand command = connection.CreateCommand();
command.CommandText =
"SELECT dateadded FROM dvbnip_services WHERE nid = @nid AND cid = @cid AND lid = @lid AND sid = @sid";
command.Parameters.AddWithValue("@nid", NpgsqlDbType.Integer, (int)coordinates.Item1);
command.Parameters.AddWithValue("@cid", NpgsqlDbType.Integer, (int)coordinates.Item2);
command.Parameters.AddWithValue("@lid", NpgsqlDbType.Integer, (int)coordinates.Item3);
command.Parameters.AddWithValue("@sid", NpgsqlDbType.Integer, (int)coordinates.Item4);
NpgsqlDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
_knownDvbNipServices.Add(coordinates);
result = true;
}
dataReader.Close();
command.Dispose();
connection.Close();
}
return result;
}
public void DvbNipInsertService(BroadcastMediaStreamType broadcastMediaStreamType)
{
EnqueueTask(x => DvbNipInsertServiceEx(x,broadcastMediaStreamType));
}
private void DvbNipInsertServiceEx(NpgsqlConnection connection, BroadcastMediaStreamType broadcastMediaStreamType)
{
if (broadcastMediaStreamType.BroadcastMedia.InteractiveApplications != null)
throw new NotImplementedException(nameof(broadcastMediaStreamType.BroadcastMedia.InteractiveApplications));
if (_knownDvbNipServices == null)
_knownDvbNipServices = new HashSet<Tuple<ushort, ushort, ushort, ushort>>();
Tuple<ushort, ushort, ushort, ushort> coordinates = new Tuple<ushort, ushort, ushort, ushort>(
broadcastMediaStreamType.NIPNetworkID, ushort.Parse(broadcastMediaStreamType.NIPCarrierID),
ushort.Parse(broadcastMediaStreamType.NIPLinkID), broadcastMediaStreamType.NIPServiceID);
NpgsqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO dvbnip_services VALUES (@nid,@cid,@lid,@sid,DEFAULT,@uri)";
command.Parameters.AddWithValue("@nid", NpgsqlDbType.Integer, (int)coordinates.Item1);
command.Parameters.AddWithValue("@cid", NpgsqlDbType.Integer, (int)coordinates.Item2);
command.Parameters.AddWithValue("@lid", NpgsqlDbType.Integer, (int)coordinates.Item3);
command.Parameters.AddWithValue("@sid", NpgsqlDbType.Integer, (int)coordinates.Item4);
command.Parameters.AddWithValue("@uri", NpgsqlDbType.Json, JsonConvert.SerializeObject(broadcastMediaStreamType.BroadcastMedia.URI));
int rowsInserted = command.ExecuteNonQuery();
if (rowsInserted != 1)
throw new DataException(String.Format("Expected to insert {0} rows, but it were {1}", 1, rowsInserted));
_knownDvbNipServices.Add(coordinates);
}
public bool DvbNipTestForMulticastSession(MulticastSessionType multicastSession)
{
throw new NotImplementedException();
}
public void DvbNipInsertMulticastSession(MulticastSessionType multicastSession)
{
throw new NotImplementedException();
}
public bool DvbNipTestForMulticastGatewayConfigurationTransportSession(NipActualCarrierInformation carrier,
MulticastEndpointAddressType multicastEndpointAddressType)
{
throw new NotImplementedException();
}
public void DvbNipInsertMulticastGatewayConfigurationTransportSession(NipActualCarrierInformation carrier,
MulticastGatewayConfigurationTransportSessionType multicastGatewayConfigurationTransportSession)
{
throw new NotImplementedException();
}
}
}