-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskUpdateTrackables.cs
More file actions
98 lines (91 loc) · 4.18 KB
/
Copy pathTaskUpdateTrackables.cs
File metadata and controls
98 lines (91 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskScheduler.Models;
namespace TaskScheduler
{
public class TaskUpdateTrackables : TaskBase
{
private int _tbCount = 0;
private int _tokenAccountIndex = 0;
public TaskUpdateTrackables(Manager taskManager) :
base(taskManager, typeof(TaskUpdateTrackables), "Update Trackables", 0, 1, 0)
{
_tbCount = 0;
Details = _tbCount.ToString();
}
protected override void ServiceMethod()
{
string activeCode = "";
try
{
bool isScheduledCache = false;
//first get scheduled
using (var db = new PetaPoco.Database(Manager.SchedulerConnectionString, "System.Data.SqlClient"))
{
ScheduledTrackable swp = db.FirstOrDefault<ScheduledTrackable>("select top 1 * from ScheduledTrackable");
if (swp != null)
{
activeCode = swp.Code;
isScheduledCache = true;
db.Execute("delete from ScheduledTrackable where Code=@0", swp.Code);
}
if (!isScheduledCache)
{
List<string> gcidList = db.Fetch<string>("SELECT LastTrackableCode FROM TaskUpdateTrackable");
if (gcidList.Count > 0)
{
activeCode = gcidList[0];
}
activeCode = db.FirstOrDefault<string>(string.Format("select top 1 Code from [{0}].[dbo].[GCEuTrackable] where Code>@0 and Updated<=@1 order by Code", GCEuDataSupport.GlobalcachingDatabaseName), activeCode, DateTime.Now.AddDays(-1));
if (string.IsNullOrEmpty(activeCode))
{
activeCode = "";
activeCode = db.FirstOrDefault<string>(string.Format("select top 1 Code from [{0}].[dbo].[GCEuTrackable] where Code>@0 and Updated<=@1 order by Code", GCEuDataSupport.GlobalcachingDatabaseName), activeCode, DateTime.Now.AddDays(-1));
}
if (!string.IsNullOrEmpty(activeCode))
{
if (gcidList.Count > 0)
{
db.Execute("update TaskUpdateTrackable set LastTrackableCode=@0", activeCode);
}
else
{
db.Execute("insert into TaskUpdateTrackable (LastTrackableCode) values (@0)", activeCode);
}
}
}
if (!string.IsNullOrEmpty(activeCode))
{
//update
string token = GeocachingAPI.Instance.GetServiceToken(ref _tokenAccountIndex);
if (token.Length > 0)
{
var tb = GeocachingAPI.GetTrackable(token, activeCode);
if (tb != null)
{
var logs = GeocachingAPI.GetTrackableLogs(token, tb.Code);
var tbTravel = GeocachingAPI.GetTrackableTravel(token, tb.Code);
if (logs != null)
{
//update tb
DataSupport.Instance.AddTrackable(tb, logs, tbTravel);
_tbCount++;
}
}
}
}
}
Details = string.Format("C:{0} T:{1}", activeCode??"", _tbCount);
ServiceInfo.ErrorInLastRun = false;
}
catch (Exception e)
{
ServiceInfo.ErrorInLastRun = true;
Details = string.Format("{0} - {1}", activeCode??"", e.Message);
}
}
}
}