-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.axaml.cs
More file actions
91 lines (84 loc) · 2.68 KB
/
MainWindow.axaml.cs
File metadata and controls
91 lines (84 loc) · 2.68 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
using Avalonia.Controls;
using Avalonia.Threading;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Weather.NET;
using Weather.NET.Enums;
namespace dotnetPiPictureFrame
{
public partial class MainWindow : Window
{
FrameViewModel viewModel = new FrameViewModel();
WeatherClient client = new WeatherClient(Config.OpenWeather);
const int checkPeriod = 60*30;
int periodSeconds = 0;
int updatePhotoPeriod = 1;
int photoPeriodSeconds = 0;
Image? photoImage;
string[] photoFiles;
public MainWindow()
{
InitializeComponent();
photoImage = this.FindControl<Image>("PhotoImage");
DataContext = viewModel;
Task.Run(async () => await UpdateGUI());
}
void UpdatePhotos()
{
if (Directory.Exists(Config.PhotosFolder))
{
photoFiles = (Directory.GetFiles(Config.PhotosFolder, "*.jpg").Union(Directory.GetFiles(Config.PhotosFolder, "*.png"))).ToArray();
}
else
{
photoFiles = new string[0];
}
}
async Task UpdateWeather()
{
try
{
viewModel.CurrentWeather = await client.GetCurrentWeatherAsync(cityName: Config.WeatherCity, measurement: Measurement.Metric);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
async Task UpdateGUI()
{
int currentPhoto = 0;
_ = Task.Run(() => UpdatePhotos());
_ = Task.Run(() => UpdateWeather());
while (true)
{
viewModel.Time = DateTime.Now;
if (periodSeconds > checkPeriod)
{
_ = Task.Run(() => UpdatePhotos());
_ = Task.Run(() => UpdateWeather());
periodSeconds = 0;
}
periodSeconds++;
if (photoPeriodSeconds > updatePhotoPeriod)
{
if (photoFiles.Length > 0)
{
viewModel.PhotoPath = photoFiles[currentPhoto];
currentPhoto++;
if (currentPhoto >= photoFiles.Length)
{
currentPhoto = 0;
}
updatePhotoPeriod = 10;
}
photoPeriodSeconds = 0;
}
photoPeriodSeconds++;
await Task.Delay(1000);
}
}
}
}