Skip to content

Commit

Permalink
- another attempt to get Samsung 1352.0 format working
Browse files Browse the repository at this point in the history
  It seems that when a user changes a program number through the TVs menu, it creates a cloned record and sets the SRV_EXT_APP.recState=1
  So when there are 2 channels with the same program number, the one with recState=NULL should be ignored
  • Loading branch information
Horst Beham committed Mar 20, 2020
1 parent 3c33b57 commit abb2c90
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
4 changes: 2 additions & 2 deletions source/ChanSort.Loader.SamsungJ/DbChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal DbChannel(SQLiteDataReader r, IDictionary<string, int> field, DataRoot
(this.SignalSource & SignalSource.DvbC) == SignalSource.DvbC ? LookupData.Instance.GetDvbcTransponder(this.FreqInMhz).ToString() :
(this.SignalSource & SignalSource.DvbS) == SignalSource.DvbS ? LookupData.Instance.GetAstraTransponder((int)this.FreqInMhz).ToString() :
"";
this.Name = DbSerializer.ReadUtf16(r, 6);
this.Name = DbSerializer.ReadUtf16(r, field["srvName"]);
this.Hidden = r.GetBoolean(field["hidden"]);
this.Encrypted = r.GetBoolean(field["scrambled"]);
this.Lock = r.GetBoolean(field["lockMode"]);
Expand Down Expand Up @@ -58,7 +58,7 @@ private void ReadAnalogData(SQLiteDataReader r, IDictionary<string, int> field)
#region ReadDvbData()
protected void ReadDvbData(SQLiteDataReader r, IDictionary<string, int> field, DataRoot dataRoot, Dictionary<long, string> providers)
{
this.ShortName = DbSerializer.ReadUtf16(r, 16);
this.ShortName = DbSerializer.ReadUtf16(r, field["srvName"]);
this.RecordOrder = r.GetInt32(field["major"]);
int serviceType = r.GetInt32(field["srvType"]);
this.ServiceType = serviceType;
Expand Down
63 changes: 41 additions & 22 deletions source/ChanSort.Loader.SamsungJ/DbSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DbSerializer : SerializerBase
private readonly Dictionary<ChannelList, string> dbPathByChannelList = new Dictionary<ChannelList, string>();
private readonly List<string> tableNames = new List<string>();

private enum FileType { Unknown, SatDb, ChannelDbDvb, ChannelDbAnalog }
private enum FileType { Unknown, SatDb, ChannelDbDvb, ChannelDbAnalog, ChannelDbIp }

#region ctor()
public DbSerializer(string inputFile) : base(inputFile)
Expand All @@ -32,7 +32,7 @@ public DbSerializer(string inputFile) : base(inputFile)
this.Features.CanHideChannels = true;
this.Features.SupportedFavorites = Favorites.A | Favorites.B | Favorites.C | Favorites.D | Favorites.E;
this.Features.SortedFavorites = true;
this.Features.AllowGapsInFavNumbers = false;
this.Features.AllowGapsInFavNumbers = true;
}
#endregion

Expand Down Expand Up @@ -81,12 +81,12 @@ public override void Load()

switch (type)
{
case FileType.SatDb: break;
case FileType.ChannelDbAnalog:
ReadChannelDatabase(conn, filePath, false);
case FileType.SatDb:
break;
case FileType.ChannelDbAnalog:
case FileType.ChannelDbDvb:
ReadChannelDatabase(conn, filePath, true);
case FileType.ChannelDbIp:
ReadChannelDatabase(conn, filePath, type);
break;
}
}
Expand Down Expand Up @@ -132,6 +132,9 @@ private FileType DetectFileType(SQLiteCommand cmd)
if (tableNames.Contains("CHNL") && tableNames.Contains("SRV") && tableNames.Contains("SRV_ANL"))
return FileType.ChannelDbAnalog;

//if (tableNames.Contains("CHNL") && tableNames.Contains("SRV") && tableNames.Contains("SRV_IP"))
// return FileType.ChannelDbIp;

return FileType.Unknown;
}
#endregion
Expand Down Expand Up @@ -194,13 +197,13 @@ private void ReadTransponders(SQLiteCommand cmd)


#region ReadChannelDatabase()
private void ReadChannelDatabase(SQLiteConnection conn, string dbPath, bool digital)
private void ReadChannelDatabase(SQLiteConnection conn, string dbPath, FileType fileType)
{
this.channelById.Clear();
using (var cmd = conn.CreateCommand())
{
var providers = digital ? this.ReadProviders(cmd) : null;
var channelList = this.ReadChannels(cmd, dbPath, providers, digital);
var providers = fileType == FileType.ChannelDbDvb ? this.ReadProviders(cmd) : null;
var channelList = this.ReadChannels(cmd, dbPath, providers, fileType);
this.ReadFavorites(cmd);
this.dbPathByChannelList.Add(channelList, dbPath);
}
Expand Down Expand Up @@ -228,36 +231,45 @@ private Dictionary<long, string> ReadProviders(SQLiteCommand cmd)
#endregion

#region ReadChannels()
private ChannelList ReadChannels(SQLiteCommand cmd, string dbPath, Dictionary<long, string> providers, bool digital)
private ChannelList ReadChannels(SQLiteCommand cmd, string dbPath, Dictionary<long, string> providers, FileType fileType)
{
var signalSource = DetectSignalSource(cmd, digital);
var signalSource = DetectSignalSource(cmd, fileType);

string name = Path.GetFileName(dbPath);
ChannelList channelList = new ChannelList(signalSource, name);
string table = digital ? "SRV_DVB" : "SRV_ANL";
string table = fileType == FileType.ChannelDbDvb ? "SRV_DVB" : fileType == FileType.ChannelDbAnalog ? "SRV_ANL" : "SRV_IP";
List<string> fieldNames = new List<string> {
"chType", "chNum", "freq", // CHNL
"SRV.srvId", "major", "progNum", "cast(srvName as blob)", "srvType", "hidden", "scrambled", "lockMode", "numSel", // SRV
"SRV.srvId", "major", "progNum", "cast(srvName as blob) srvName", "srvType", "hidden", "scrambled", "lockMode", "numSel", "elim" // SRV
};
if (digital)
fieldNames.AddRange(new[] {"onid", "tsid", "vidPid", "provId", "cast(shrtSrvName as blob)", "lcn"}); // SRV_DVB
if (fileType == FileType.ChannelDbDvb)
fieldNames.AddRange(new[] {"onid", "tsid", "vidPid", "provId", "cast(shrtSrvName as blob) shrtSrvName", "lcn"}); // SRV_DVB

var sql = this.BuildQuery(table, fieldNames);
var fields = this.GetFieldMap(fieldNames);

cmd.CommandText = sql;
using (var r = cmd.ExecuteReader())
{
int prevNr = 0;
while (r.Read())
{
if (r.GetInt32(fields["elim"]) != 0)
continue;

// 171027 - ohuseyinoglu: With our change in transponder indexing, we can directly look it up by "chNum" now!
var tp = this.DataRoot.Transponder.TryGet(r.GetInt32(1));
// ... and get the satellite from that transponder - if set
// Note that we can have channels from multiple satellites in the same list, so this is a loop variable now
var sat = tp?.Satellite;
var channel = new DbChannel(r, fields, this.DataRoot, providers, sat, tp);

if (channel.OldProgramNr == prevNr) // when there is a SRV_EXT_APP table in the database, the service with the highest ext_app "recState" takes priority
continue;

this.DataRoot.AddChannel(channelList, channel);
this.channelById.Add(channel.RecordIndex, channel);
prevNr = channel.OldProgramNr;
}
}

Expand All @@ -267,9 +279,11 @@ private ChannelList ReadChannels(SQLiteCommand cmd, string dbPath, Dictionary<lo
#endregion

#region DetectSignalSource()
private static SignalSource DetectSignalSource(SQLiteCommand cmd, bool digital)
private static SignalSource DetectSignalSource(SQLiteCommand cmd, FileType fileType)
{
var signalSource = digital ? SignalSource.Digital : SignalSource.Analog;
if (fileType == FileType.ChannelDbIp)
return SignalSource.IP|SignalSource.Digital;
var signalSource = fileType == FileType.ChannelDbAnalog ? SignalSource.Analog : SignalSource.Digital;
cmd.CommandText = "select distinct chType from CHNL";
using (var r = cmd.ExecuteReader())
{
Expand Down Expand Up @@ -312,8 +326,8 @@ private string BuildQuery(string table, IList<string> fieldNames)
}
sql += " from " + table + " inner join SRV on SRV.srvId="+table+".srvId inner join CHNL on CHNL.chId=SRV.chId";

if (this.tableNames.Contains("SRV_EXT_APP")) // in format 1352.0 there are duplicate "major" values in SRV and this recState seems to be the only indicator for "deleted" channels
sql += " inner join SRV_EXT_APP on SRV_EXT_APP.srvId=SRV.srvId and SRV_EXT_APP.recState is null";
if (this.tableNames.Contains("SRV_EXT_APP")) // in format 1352.0 there are duplicate "major" values in SRV and this recState seems to be the only difference
sql += " inner join SRV_EXT_APP on SRV_EXT_APP.srvId=SRV.srvId order by SRV.major, ifnull(SRV_EXT_APP.recState,0) desc";

return sql;
}
Expand All @@ -324,7 +338,11 @@ private IDictionary<string, int> GetFieldMap(IList<string> fieldNames)
{
Dictionary<string, int> field = new Dictionary<string, int>();
for (int i = 0; i < fieldNames.Count; i++)
field[fieldNames[i]] = i;
{
var idx = fieldNames[i].LastIndexOf(' ') + 1;
field[fieldNames[i].Substring(idx)] = i;
}

return field;
}
#endregion
Expand All @@ -334,17 +352,18 @@ private void ReadFavorites(SQLiteCommand cmd)
{
cmd.CommandText = "select srvId, fav, pos from SRV_FAV";
var r = cmd.ExecuteReader();
int favPosAdjust = tableNames.Contains("SRV_EXT_APP") ? 0 : 1;
while (r.Read())
{
var channel = this.channelById.TryGet(r.GetInt64(0));
if (channel == null)
continue;
int fav = r.GetInt32(1) - 1; // fav values start with 1 in the table
int pos = r.GetInt32(2); // pos values start with 0
int pos = r.GetInt32(2) + favPosAdjust; // pos values start with 0 or 1
if (pos >= 0)
{
channel.Favorites |= (Favorites) (1 << fav);
channel.OldFavIndex[fav] = pos + 1;
channel.OldFavIndex[fav] = pos;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions source/changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
ChanSort Change Log
===================

2020-03-20
- another attempt to get Samsung 1352.0 format working

2020-03-15
- repackaged to include Polish translation files

Expand Down

0 comments on commit abb2c90

Please sign in to comment.