Skip to content

Commit

Permalink
Fixed bugs when no previously saved config file is found
Browse files Browse the repository at this point in the history
  • Loading branch information
mmckechney committed Jan 27, 2021
1 parent 13100f6 commit 6deb06e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
94 changes: 62 additions & 32 deletions RingVideos/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,18 @@ static void Main(string[] args)
{
app = serviceProvider.GetService<RingVideoApplication>();
auth = Configuration.GetSection("Authentication").Get<Authentication>();
auth.Decrypt();
if (auth != null)
{
auth.Decrypt();
}else
{
auth = new Authentication();
}
filter = Configuration.GetSection("Filter").Get<Filter>();
if(filter == null)
{
filter = new Filter();
}

Task<int> val = rootCommand.InvokeAsync(args);
val.Wait();
Expand All @@ -123,37 +133,38 @@ static void Main(string[] args)

private static void SaveSettings(Filter f, Authentication a, int runResult)
{
if (f != null && a != null){
a.Encrypt();
//Set "next dates" on filter
if (runResult == 0 && !f.Snapshots)
{
f.StartDateTime = f.EndDateTime.Value.AddDays(-1);
f.EndDateTime = null;
}

a.Encrypt();
//Set "next dates" on filter
if (runResult == 0 && !f.Snapshots)
{
f.StartDateTime = f.EndDateTime.Value.AddDays(-1);
f.EndDateTime = null;
}

var folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"RingVideos");
if(!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
var folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"RingVideos");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}

string settingsFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"RingVideos\RingVideosConfig.json");
var serializeOptions = new JsonSerializerOptions
{
WriteIndented = true
};
var conf = new Config()
{
Authentication = a,
Filter = f
};
var config = JsonSerializer.Serialize(conf, serializeOptions);
string settingsFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"RingVideos\RingVideosConfig.json");
var serializeOptions = new JsonSerializerOptions
{
WriteIndented = true
};
var conf = new Config()
{
Authentication = a,
Filter = f
};
var config = JsonSerializer.Serialize(conf, serializeOptions);

File.WriteAllText(settingsFile, config);
File.WriteAllText(settingsFile, config);
}
}

private static void SetAuthenticationValues(ref Authentication a)
private static bool SetAuthenticationValues(ref Authentication a)
{
if(string.IsNullOrWhiteSpace(a.UserName))
{
Expand All @@ -164,7 +175,8 @@ private static void SetAuthenticationValues(ref Authentication a)
}
else
{
throw new ArgumentException("A Ring username is requires");
Console.WriteLine("A Ring username is required");
return false;
}
}
if (string.IsNullOrWhiteSpace(a.ClearTextPassword))
Expand All @@ -176,7 +188,8 @@ private static void SetAuthenticationValues(ref Authentication a)
}
else
{
throw new ArgumentException("A Ring password is requires");
Console.WriteLine("A Ring password is required");
return false;
}
}

Expand All @@ -188,6 +201,7 @@ private static void SetAuthenticationValues(ref Authentication a)
a.RefreshToken = rt;
}
}
return true;
}

private static void ConfigureServices(ServiceCollection services, string[] args)
Expand Down Expand Up @@ -267,20 +281,36 @@ private static async Task<int> GetVideos(string username, string password, strin
{
filter.StartDateTimeUtc = TimeZoneInfo.ConvertTimeToUtc(filter.StartDateTime.Value, TimeZoneInfo.Local);
}
else
{
filter.StartDateTime = DateTime.MinValue;
filter.StartDateTimeUtc = DateTime.MinValue;
}
if (filter.EndDateTime.HasValue)
{
filter.EndDateTimeUtc = TimeZoneInfo.ConvertTimeToUtc(filter.EndDateTime.Value, TimeZoneInfo.Local);
}
else
{
filter.EndDateTime = DateTime.MaxValue;
filter.EndDateTimeUtc = DateTime.MaxValue;
}

if (Program.filter.SetDebug)
{
var logConfig = serviceProvider.GetService<ILoggingBuilder>();
logConfig.SetMinimumLevel(LogLevel.Debug);
}

SetAuthenticationValues(ref Program.auth);

return await app.Run(Program.filter, Program.auth);
if (SetAuthenticationValues(ref Program.auth))
{
return await app.Run(Program.filter, Program.auth);
}
else
{
return -200;
}


}

Expand Down
2 changes: 1 addition & 1 deletion RingVideos/RingVideoApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal async Task<int> Run(Filter filter, Authentication auth)
try
{
this.ringSession = await Authenicate(auth);
if (this.ringSession == null)
if (this.ringSession == null || !this.ringSession.IsAuthenticated)
{
return 999;
}
Expand Down

0 comments on commit 6deb06e

Please sign in to comment.