private void ThreadFunctionImportTVGuide(object aparam)
{
SetStandbyAllowed(false);
//System.Diagnostics.Debugger.Launch();
FileStream streamIn = null;
StreamReader fileIn = null;
try
{
if (GlobalServiceProvider.Instance.IsRegistered<IPowerScheduler>() &&
GlobalServiceProvider.Instance.Get<IPowerScheduler>().IsSuspendInProgress())
{
return;
}
ThreadParams param = (ThreadParams)aparam;
Setting setting;
TvBusinessLayer layer = new TvBusinessLayer();
string folder = layer.GetSetting("xmlTv", DefaultOutputFolder).Value;
// Allow for deleting of all existing programs before adding the new ones.
// Already imported programs might have incorrect data depending on the grabber & setup
// f.e when grabbing programs many days ahead
bool deleteBeforeImport = (layer.GetSetting("xmlTvDeleteBeforeImport", "true").Value == "true");
//if (layer.GetSetting("xmlTvDeleteBeforeImport", "true").Value == "true")
//{
// SqlBuilder sb = new SqlBuilder(StatementType.Delete, typeof(Program));
// SqlStatement stmt = sb.GetStatement();
// stmt.Execute();
//}
int numChannels = 0, numPrograms = 0;
string errors = "";
try
{
if (param._importXML)
{
string fileName = folder + @"\tvguide.xml";
Log.Write("plugin:xmltv importing " + fileName);
XMLTVImport import = new XMLTVImport(10); // add 10 msec dely to the background thread
import.Import(fileName, deleteBeforeImport, false);
numChannels += import.ImportStats.Channels;
numPrograms += import.ImportStats.Programs;
if (import.ErrorMessage.Length != 0)
errors += "tvguide.xml:" + import.ErrorMessage + "; ";
}
if (param._importLST)
{
string fileName = folder + @"\tvguide.lst";
Log.Write("plugin:xmltv importing files in " + fileName);
Encoding fileEncoding = Encoding.Default;
streamIn = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
fileIn = new StreamReader(streamIn, fileEncoding, true);
while (!fileIn.EndOfStream)
{
string tvguideFileName = fileIn.ReadLine();
if (tvguideFileName.Length == 0) continue;
if (!System.IO.Path.IsPathRooted(tvguideFileName))
{
// extend by directory
tvguideFileName = System.IO.Path.Combine(folder, tvguideFileName);
}
Log.WriteFile(@"plugin:xmltv importing " + tvguideFileName);
XMLTVImport import = new XMLTVImport(10); // add 10 msec dely to the background thread
import.Import(tvguideFileName, deleteBeforeImport, false);
numChannels += import.ImportStats.Channels;
numPrograms += import.ImportStats.Programs;
if (import.ErrorMessage.Length != 0)
errors += tvguideFileName + ": " + import.ErrorMessage + "; ";
}
}
setting = layer.GetSetting("xmlTvResultLastImport", "");
setting.Value = DateTime.Now.ToString();
setting.Persist();
setting = layer.GetSetting("xmlTvResultChannels", "");
setting.Value = numChannels.ToString();
setting.Persist();
setting = layer.GetSetting("xmlTvResultPrograms", "");
setting.Value = numPrograms.ToString();
setting.Persist();
setting = layer.GetSetting("xmlTvResultStatus", "");
setting.Value = errors;
setting.Persist();
Log.Write("Xmltv: imported {0} channels, {1} programs status:{2}", numChannels, numPrograms, errors);
}
catch (Exception ex)
{
Log.Error(@"plugin:xmltv import failed");
Log.Write(ex);
}
setting = layer.GetSetting("xmlTvLastUpdate", "");
setting.Value = param._importDate.ToString();
setting.Persist();
Log.Info("Xmltv: waiting for database to finish inserting imported programs.");
layer.WaitForInsertPrograms();
}
finally
{
Log.WriteFile(@"plugin:xmltv import done");
if (streamIn != null)
{
streamIn.Close();
streamIn.Dispose();
}
if (fileIn != null)
{
fileIn.Close();
fileIn.Dispose();
}
_workerThreadRunning = false;
SetStandbyAllowed(true);
}
}