Adicionado custom parser para faturas onde não existe período de leitura.

Faturas sem data de leitura são classificadas como erro e salva na pasta de erros.
This commit is contained in:
Adriano Serighelli 2025-06-10 09:55:09 -03:00
parent d16a799ebc
commit 52a32b9279
4 changed files with 47 additions and 14 deletions

View File

@ -0,0 +1,34 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Download_Faturas
{
public class DefaultDateTimeConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
string? str = reader.GetString();
if (string.IsNullOrWhiteSpace(str))
{
return DateTime.MinValue;
}
if (DateTime.TryParse(str, out var date))
{
return date;
}
}
return DateTime.MinValue;
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value);
}
}
}

View File

@ -9,6 +9,10 @@
<Platforms>AnyCPU</Platforms> <Platforms>AnyCPU</Platforms>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="Class1.cs" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" /> <None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup> </ItemGroup>
@ -25,11 +29,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Update="DataSet1.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>DataSet1.xsd</DependentUpon>
</Compile>
<Compile Update="Properties\Resources.Designer.cs"> <Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
@ -44,11 +43,4 @@
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="DataSet1.xsd">
<Generator>MSDataSetGenerator</Generator>
<LastGenOutput>DataSet1.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
</Project> </Project>

View File

@ -85,8 +85,13 @@
this.faturaParsed.TryGetProperty("json", out a); this.faturaParsed.TryGetProperty("json", out a);
} }
// var val = a.ToString().Contains("\"measured\": ["); Rootobject parsedResult = JsonSerializer.Deserialize<Rootobject>(a)!;
Rootobject parsedResult = JsonSerializer.Deserialize<Rootobject>(a) !;
if (parsedResult.dates.reading.periodUntil == DateTime.MinValue)
{
return;
}
dadosTusd.Mes = int.Parse(parsedResult.dates.reading.periodUntil.AddDays(-15).ToString("yMM")); dadosTusd.Mes = int.Parse(parsedResult.dates.reading.periodUntil.AddDays(-15).ToString("yMM"));
string uc = new Regex("^0+").Replace(parsedResult.locationNumber, string.Empty).Replace("/", string.Empty).Replace("-", string.Empty).Replace(".", string.Empty); string uc = new Regex("^0+").Replace(parsedResult.locationNumber, string.Empty).Replace("/", string.Empty).Replace("-", string.Empty).Replace(".", string.Empty);

View File

@ -88,8 +88,10 @@
public class Reading public class Reading
{ {
[JsonConverter(typeof(DefaultDateTimeConverter))]
public DateTime periodFrom { get; set; } public DateTime periodFrom { get; set; }
[JsonConverter(typeof(DefaultDateTimeConverter))]
public DateTime periodUntil { get; set; } public DateTime periodUntil { get; set; }
} }