diff --git a/DataTableStorages/skyscraper5.Data.PostgreSql/DvbNip.cs b/DataTableStorages/skyscraper5.Data.PostgreSql/DvbNip.cs index b66fa44..5e0d7d4 100644 --- a/DataTableStorages/skyscraper5.Data.PostgreSql/DvbNip.cs +++ b/DataTableStorages/skyscraper5.Data.PostgreSql/DvbNip.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; +using Newtonsoft.Json; namespace skyscraper5.Data.PostgreSql { @@ -147,14 +148,73 @@ namespace skyscraper5.Data.PostgreSql } + private HashSet> _knownDvbNipServices; public bool DvbNipTestForService(BroadcastMediaStreamType broadcastMediaStreamType) { - throw new NotImplementedException(); + if (_knownDvbNipServices == null) + _knownDvbNipServices = new HashSet>(); + + Tuple coordinates = new Tuple( + 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) { - throw new NotImplementedException(); + 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 coordinates = new Tuple( + 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)