c# - 从 Json API 对变量进行更新检查
问题描述
所以我有一个UWP
项目,我处理一组房间的预订。我从Json API
.
我想做一个loop
检查房间是否被预订或不是每分钟或类似的东西,但我不知道该怎么做。
这就是我如何获得所有带有预订的房间以及它们的所有属性:
public async void addroom()
{
string url = "https://api.booking.com/api/company/07ce8f7c-f3d3-4df2-84bd-33f8fc263deb/rooms";
HttpClient client = new HttpClient();
string response = await client.GetStringAsync(url);
List<Class2> data = JsonConvert.DeserializeObject<List<Class2>>(response);
foreach (Class2 room in data)
{
string booking = $"https://api.booking.com/api/company/07ce8f7c-f3d3-4df2-84bd-33f8fc263deb/rooms/{room.id}/bookings";
HttpClient BookingClient = new HttpClient();
string BookingResponse = await BookingClient.GetStringAsync(booking);
List<Bookings> bookings = JsonConvert.DeserializeObject<List<Bookings>>(BookingResponse);
room.Bookings = bookings;
string id = room.id;
string name = room.name;
int seats = room.seats;
Uri Img = room.ImageUrl;
List<Roomattribute> roomattrib = room.roomAttributes;
var NewRoom = new Room
{
RoomID = id,
RoomName = name,
FrontImage = Img,
Seats = seats,
};
foreach (var books in bookings)
{
string note = books.note;
DateTime TimeFrom = books.timeFrom;
DateTime TimeTo = books.timeTo;
Class2 BookRoom = books.room;
string BookId = books.id;
DateTime Now = new DateTime(2018, 04, 25, 09, 40, 00);
var BeforeEnd = books.timeTo.Subtract(Now).Subtract(TimeSpan.FromMinutes(15));
var BeforeBegin = books.timeFrom.Subtract(Now).Subtract(TimeSpan.FromMinutes(15));
if (books.timeFrom <= Now && books.timeTo > Now)
{
ToRed();
DispatcherTimer ColorTimer = new DispatcherTimer();
ColorTimer.Interval = BeforeEnd;
ColorTimer.Tick += (sender, args) =>
{
ToYellow();
ColorTimer.Stop();
};
ColorTimer.Start();
}
else if (books.timeTo == Now)
{
ToGreen();
}
else
{
DispatcherTimer ColorTimer = new DispatcherTimer();
ColorTimer.Interval = BeforeBegin;
ColorTimer.Tick += (sender, args) =>
{
ToYellow();
ColorTimer.Stop();
};
ColorTimer.Start();
}
}
foreach (var attri in roomattrib)
{
int attriId = attri.id;
string attriName = attri.name;
int attriIcon = attri.icon;
if (room.roomAttributes.Any(a => a.id == 1))
{
NewRoom.Tv = Visibility.Visible;
}
else if (room.roomAttributes.Any(a => a.id != 1))
{
NewRoom.Tv = Visibility.Collapsed;
}
if (room.roomAttributes.Any(a => a.id == 2))
{
NewRoom.Wifi = Visibility.Visible;
}
else if (room.roomAttributes.Any(a => a.id != 2))
{
NewRoom.Wifi = Visibility.Collapsed;
}
if (room.roomAttributes.Any(a => a.id == 3))
{
NewRoom.Projector = Visibility.Visible;
}
else if (room.roomAttributes.Any(a => a.id != 3))
{
NewRoom.Projector = Visibility.Collapsed;
}
if (room.roomAttributes.Any(a => a.id == 4))
{
NewRoom.Wboard = Visibility.Visible;
}
else if (room.roomAttributes.Any(a => a.id != 4))
{
NewRoom.Wboard = Visibility.Collapsed;
}
}
Rooms.Add(NewRoom);
}
}
现在我的所有代码都运行良好(除此之外,所有预订都转到所有房间,但这是题外话......),当一个房间空置时,它有一个绿色LinearGredientBrush
,当一个房间被预订时,它正在改变颜色变为红色,当房间无人居住 15 分钟后,颜色变为黄色。
我需要检查的是,例如,如果一个房间在时间用完之前被取消。
我在想把所有这些都放在一个For loop
可能的解决方案中:
var BeforeEnd = books.timeTo.Subtract(Now).Subtract(TimeSpan.FromMinutes(15));
var BeforeBegin = books.timeFrom.Subtract(Now).Subtract(TimeSpan.FromMinutes(15));
if (books.timeFrom <= Now && books.timeTo > Now)
{
ToRed();
DispatcherTimer ColorTimer = new DispatcherTimer();
ColorTimer.Interval = BeforeEnd;
ColorTimer.Tick += (sender, args) =>
{
ToYellow();
ColorTimer.Stop();
};
ColorTimer.Start();
}
else if (books.timeTo == Now)
{
ToGreen();
}
else
{
DispatcherTimer ColorTimer = new DispatcherTimer();
ColorTimer.Interval = BeforeBegin;
ColorTimer.Tick += (sender, args) =>
{
ToYellow();
ColorTimer.Stop();
};
ColorTimer.Start();
}
我希望我对这个问题的描述足够好,并且很高兴能在我的问题上得到一些帮助。
提前致谢!
解决方案
无需DispatcherTimer
为每个预订单独创建一个,您可以只创建一个按时间间隔触发一次的单个预订,例如每分钟一次(取决于您希望看到颜色变化的频率)。
在Tick
处理程序中,您可以检查每个房间:
- 预订处于活动状态:红色- 可以通过
foreach
对给定房间的预订进行简单循环来完成此检查 - 预订在不到 15 分钟内开始:黄色- 再次简单
foreach
循环,检查所有预订的开始时间,如果距离不到 15 分钟,我们有一个匹配 - 否则:绿色
推荐阅读
- azure-ad-b2c - Azure AD B2C - Android 中的回调方法问题
- sql-server - SQL 执行任务部分有效
- c++ - ASIO使用删除函数C++
- reactjs - 重置 React 组件状态
- python - 如何将字典添加到数据框列?
- c++ - 为什么重载 operator new 被认为是静态的
- python - 如何将 DataFrame 中的一行与另一个 df 中的所有其他行分开?
- android - 如何为微调器设置动画,使其随选项扩展
- google-analytics - Google Analytics - 排除经过身份验证的用户
- java - 本地存储静态数据