ly/Controllers/HomeController.cs

108 lines
3.8 KiB
C#
Raw Normal View History

2025-03-22 12:16:22 +00:00
using LY.App.Common.Redis;
using LY.App.Device;
using LY.App.Model;
using LY.App.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2025-06-16 09:09:38 +00:00
using StackExchange.Redis;
using System.Reactive.Joins;
2025-03-22 12:16:22 +00:00
namespace LY.App.Controllers
{
2025-03-24 02:59:34 +00:00
/// <summary>
/// 首页控制器
/// </summary>
2025-03-22 12:16:22 +00:00
[Route("api/[controller]")]
[ApiController]
public class HomeController : ControllerBase
{
private readonly DeviceService _deviceService;
private readonly DeviceManager deviceManager = DeviceManager.Instance;
private readonly PositionService _positionService;
private readonly RedisService _redisService;
2025-03-27 16:34:20 +00:00
private readonly AlarmService _alarmService;
private readonly WeatherService _weatherService;
2025-03-24 02:59:34 +00:00
/// <summary>
///
/// </summary>
/// <param name="deviceService"></param>
/// <param name="positionService"></param>
/// <param name="redisService"></param>
2025-03-27 16:34:20 +00:00
/// <param name="alarmService"></param>
/// <param name="weatherService"></param>
public HomeController(DeviceService deviceService, PositionService positionService,
RedisService redisService, AlarmService alarmService, WeatherService weatherService)
2025-03-22 12:16:22 +00:00
{
_deviceService = deviceService;
_positionService = positionService;
_redisService = redisService;
2025-03-27 16:34:20 +00:00
_alarmService = alarmService;
_weatherService = weatherService;
2025-03-22 12:16:22 +00:00
}
/// <summary>
/// 首页
/// </summary>
/// <returns></returns>
2025-04-01 16:05:41 +00:00
[HttpGet("index")]
2025-03-22 12:16:22 +00:00
public async Task<IActionResult> view()
{
ApiResult result = new ApiResult();
var positions = await _positionService.Index();
2025-03-27 16:34:20 +00:00
var alarmCount = await _redisService.GetOrSetAsync(RedisKeyList.index_data(),
async () => await _alarmService.IndexCount(),
2025-06-30 01:07:06 +00:00
TimeSpan.FromSeconds(GetLeftTime()));
2025-03-27 16:34:20 +00:00
var weather = await _redisService.GetOrSetAsync(RedisKeyList.Index_Weather,
async () => await _weatherService.GetWeather(),
2025-06-16 09:09:38 +00:00
TimeSpan.FromHours(3));
var data = await GetUserLocation();
2025-03-22 12:16:22 +00:00
result.data = new
{
2025-03-27 16:34:20 +00:00
positions,
alarmCount,
2025-06-16 09:09:38 +00:00
weather,
userLocation = data
2025-03-22 12:16:22 +00:00
};
return Ok(result);
}
2025-06-16 09:09:38 +00:00
2025-06-30 01:07:06 +00:00
private int GetLeftTime() {
DateTime now = DateTime.Now;
// 今天的结束时间 23:59:59
DateTime endOfDay = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
// 相差时间
TimeSpan remaining = endOfDay - now;
// 剩余秒数
return (int)remaining.TotalSeconds;
}
2025-06-16 09:09:38 +00:00
private async Task<List<SyncLocation>> GetUserLocation()
{
var keys = await _redisService.GetAllKeysAsync("user_locationbyid_*");
List<SyncLocation> result = new List<SyncLocation>();
if (keys.Count > 0)
{
foreach (var item in keys)
{
result.Add(await _redisService.GetAsync<SyncLocation>(item));
}
}
return result;
}
/// <summary>
/// 同步移动端位置信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("syncLocation")]
public async Task<IActionResult> SyncLocation(SyncLocation input)
{
string key = RedisKeyList.UserLocation(input.userId);
await _redisService.SetAsync(key, new SyncLocation() { userId = input.userId, lat = input.lat, lon = input.lon }, TimeSpan.FromMinutes(1));
return Ok(new ApiResult());
}
2025-03-22 12:16:22 +00:00
}
}