-
Notifications
You must be signed in to change notification settings - Fork 0
/
CtLists.cs
228 lines (173 loc) · 6.86 KB
/
CtLists.cs
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using HtmlAgilityPack;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NUnit.Framework.Internal.Execution;
using TCore.KeyVault;
using TCore;
using StatusBox;
using TCore.UI;
namespace CtLists
{
public partial class CtLists : Form
{
private string m_username;
private string m_password;
private HttpClient m_client = new HttpClient();
private StatusRpt m_srpt;
public CtLists(string username, string password)
{
m_username = username;
m_password = password;
InitializeComponent();
m_headingWineList.Text = "";
m_headingCellarTrackerUpdate.Text = "";
m_srpt = new StatusRpt(m_recStatus);
//m_srpt.SetLogLevel(5);
//m_srpt.SetFilter(StatusRpt.MSGT.Body);
}
private void RenderHeadingLine(object sender, PaintEventArgs e)
{
RenderSupp.RenderHeadingLine(sender, e);
}
private Cellar m_cellar;
private async Task DownloadCellar()
{
string sRequest = $"https://www.cellartracker.com/xlquery.asp?table=Inventory&User={m_username}&Password={m_password}";
String sHtml = null;
try
{
HttpResponseMessage response = await m_client.GetAsync(sRequest);
response.EnsureSuccessStatusCode();
sHtml = await response.Content.ReadAsStringAsync();
}
catch (Exception exc)
{
MessageBox.Show($"Couldn't get stream from cellartracker: {exc.Message}");
return;
}
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(sHtml);
m_cellar = Cellar.BuildFromDocument(doc);
PopulateFilters();
}
private async void DoDownloadCellar(object sender, EventArgs e)
{
await DownloadCellar();
}
private void PopulateFilters()
{
m_lbxColor.Items.Clear();
m_lbxLocation.Items.Clear();
HashSet<string> locations = new HashSet<string>();
HashSet<string> colors = new HashSet<string>();
foreach (Bottle bottle in m_cellar.Bottles)
{
if (!locations.Contains(bottle.Location))
locations.Add(bottle.Location);
if (!colors.Contains(bottle.Color))
colors.Add(bottle.Color);
}
foreach (string sLocation in locations)
m_lbxLocation.Items.Add(sLocation);
foreach (string sColor in colors)
m_lbxColor.Items.Add(sColor);
}
string[] BuildStringArrayFromCheckedListbox(CheckedListBox lbx)
{
string[] rgs = null;
if (lbx.CheckedItems.Count > 0)
{
rgs = new string[lbx.CheckedItems.Count];
int i = 0;
foreach (string s in lbx.CheckedItems)
rgs[i++] = s;
}
return rgs;
}
private void MakeList(object sender, EventArgs e)
{
// build the filters
string[] rgsLocations = null;
string[] rgsColors = null;
bool fGroupByVarietal = m_fVarietalGrouping.Checked;
rgsLocations = BuildStringArrayFromCheckedListbox(m_lbxLocation);
rgsColors = BuildStringArrayFromCheckedListbox(m_lbxColor);
WineList list = WineList.BuildFromCellar(m_cellar, rgsLocations, rgsColors, fGroupByVarietal);
list.CreateFile(m_ebOutFile.Text, fGroupByVarietal, rgsColors == null || rgsColors.Length == 1);
}
private CtSql m_ctsql;
void EnsureCtSql()
{
if (m_ctsql == null)
m_ctsql = new CtSql();
}
async Task EnsureCellarDownloaded()
{
if (m_cellar == null)
await DownloadCellar();
}
private async void UpdateSql(object sender, EventArgs e)
{
await EnsureCellarDownloaded();
EnsureCtSql();
await m_ctsql.UpdateLocalDatabaseFromDownloadedCellar(m_cellar, false, false);
}
private CellarTrackerWeb m_ctWeb;
private async void DoDrinkWines(object sender, EventArgs e)
{
await EnsureCellarDownloaded();
if (m_ctWeb == null)
m_ctWeb = new CellarTrackerWeb(m_username, m_password, m_srpt);
WineDrinker drinker = new WineDrinker(m_ctWeb);
EnsureCtSql();
await drinker.FindAndDrinkWines(m_cellar, m_ctsql, false);
}
private async void DoRelocateWines(object sender, EventArgs e)
{
await EnsureCellarDownloaded();
if (m_ctWeb == null)
m_ctWeb = new CellarTrackerWeb(m_username, m_password, m_srpt);
WineMover uhaul = new WineMover(m_ctWeb);
EnsureCtSql();
await uhaul.FindAndRelocateWines(m_cellar, m_ctsql, false);
}
private async void DoSyncCheck(object sender, EventArgs e)
{
await EnsureCellarDownloaded();
WineMover uhaul = new WineMover(null);
WineDrinker drinker = new WineDrinker(null);
EnsureCtSql();
int leadingZero1, leadingZero2, inOurs, inTheirs;
(leadingZero1, leadingZero2, inOurs, inTheirs) = await m_ctsql.UpdateLocalDatabaseFromDownloadedCellar(m_cellar, true, true);
int cNeedToDrink = await drinker.FindAndDrinkWines(m_cellar, m_ctsql, true);
int cNeedToMove = await uhaul.FindAndRelocateWines(m_cellar, m_ctsql, true);
StringBuilder sb = new StringBuilder();
if (cNeedToDrink > 0)
sb.Append($"Need to drink: {cNeedToDrink}. RUN DRINKWINES! ");
if (cNeedToMove > 0)
sb.Append($"Need to move: {cNeedToDrink}. RUN RELOCATE! ");
if (leadingZero1 > 0)
sb.Append($"Leading zeroes broken: {leadingZero1}. FIX LEADING ZEROS! ");
if (leadingZero2 > 0)
sb.Append($"Leading double zeroes broken: {leadingZero2}. FIX LEADING ZEROS! ");
if (inOurs > 0)
sb.Append($"In our celler, but not CellarTracker: {inOurs}. DON'T KNOW HOW TO FIX!! ");
if (inTheirs > 0)
sb.Append($"In CellarTracker but not ours: {inTheirs}. RUN UPDATELOCAL!");
if (sb.Length == 0)
MessageBox.Show("Everything is up to date!!");
else
MessageBox.Show(sb.ToString());
}
}
}