private IEnumerable<CloudPrinter> UpdatePrintQueues()
{
if (PrintQueuesLastUpdated + PrintQueueUpdateInterval - TimeSpan.FromSeconds(5) < DateTime.Now)
{
try
{
Dictionary<string, string> printerIds;
if (_Queues.Count == 0)
{
IEnumerable<dynamic> printers = HTTPHelper.PostCloudPrintUrlEncodedRequest(OAuthTicket, "list", new { proxy = Config.CloudPrintProxyID }).printers;
printerIds = printers.ToDictionary(p => (string)p.name, p => (string)p.id);
}
else
{
printerIds = _Queues.ToDictionary(p => p.Name, p => p.PrinterID);
}
List<CloudPrinter> queues = new List<CloudPrinter>();
foreach (CloudPrinter queue in PrintJobProcessor.GetPrintQueues())
{
if (!printerIds.ContainsKey(queue.Name))
{
RegisterCloudPrinter(queue);
}
else
{
queue.SetPrinterID(printerIds[queue.Name]);
UpdateCloudPrinter(queue);
}
queues.Add(queue);
}
foreach (KeyValuePair<string, string> printer_kvp in printerIds)
{
if (queues.Count(q => q.PrinterID == printer_kvp.Value) == 0)
{
DeleteCloudPrinter(printer_kvp.Value);
}
}
_Queues = queues;
PrintQueuesLastUpdated = DateTime.Now;
UpdateCloudPrintJobs();
return queues.AsEnumerable();
}
catch (Exception ex)
{
Logger.Log(LogLevel.Warning, "Caught exception while updating printer queues:\n{0}", ex.ToString());
return _Queues.AsEnumerable();
}
}
else
{
return _Queues.AsEnumerable();
}
}