220 lines
7.4 KiB
C#
220 lines
7.4 KiB
C#
using LangGuan.Command.Extension;
|
|
using LangGuan.Command.Model;
|
|
using LangGuan.Command.Model.EntityModel;
|
|
using LangGuan.Services.Job;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Quartz;
|
|
using Quartz.Impl;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LangGuan.Services
|
|
{
|
|
public class PlanService : BaseService<Plan>
|
|
{
|
|
private GroupService _groupService;
|
|
private DeviceSerive _device;
|
|
private DeviceUsedService _deviceUsedService;
|
|
private PlcHelper _plcHelper;
|
|
public PlanService(IConfiguration config,
|
|
GroupService group, DeviceSerive deviceSerive,
|
|
DeviceUsedService deviceUsedService, PlcHelper plcHelper) : base(config, nameof(Plan))
|
|
{
|
|
_groupService = group;
|
|
_device = deviceSerive;
|
|
_deviceUsedService = deviceUsedService;
|
|
_plcHelper = plcHelper;
|
|
}
|
|
/// <summary>
|
|
/// 取全部可用的plan
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<Plan>> GetList()
|
|
{
|
|
Expression<Func<Plan, bool>> exp = filter => filter.disable == false;
|
|
return (await base.GetListWithExp(exp)).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public async Task<ApiResult> updatePlan(Plan input)
|
|
{
|
|
await base.UpdateAsync(input.Id, input);
|
|
if (input.disable)
|
|
{
|
|
// QuartzUtil.DeleteJob(input.Id);
|
|
}
|
|
else
|
|
{
|
|
// QuartzUtil.StartJobWithCron<MyJob>(input.Id, input.name, input.cron);
|
|
}
|
|
return new ApiResult() { code = 0 };
|
|
}
|
|
/// <summary>
|
|
/// 取单条
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<ApiResult> findOne(string id)
|
|
{
|
|
var entity = await base.GetAsync(id);
|
|
var ids = await GetDeviceIds(new[] { entity.Id });
|
|
var items = await _device.GetDeviceByIds(ids);
|
|
|
|
return new ApiResult() { code = 0, data = new { plan = entity, deviceItems = items } };
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public async Task<ApiResult> remove(IEnumerable<string> ids)
|
|
{
|
|
foreach (var item in ids)
|
|
{
|
|
var entity = await base.GetAsync(item);
|
|
await base.RemoveAsync(item);
|
|
await Reload();
|
|
}
|
|
return new ApiResult() { code = 0 };
|
|
}
|
|
/// <summary>
|
|
/// 新加plan
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public async Task<ApiResult> AddPlan(Plan input)
|
|
{
|
|
var temp = await base.CreateAsync(input);
|
|
await Reload();
|
|
return new ApiResult()
|
|
{
|
|
code = 0,
|
|
};
|
|
}
|
|
/// <summary>
|
|
/// 更新
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<ApiResult> setdis(string id)
|
|
{
|
|
var entity = await base.GetAsync(id);
|
|
if (entity != null)
|
|
{
|
|
entity.disable = entity.disable == true ? false : true;
|
|
await base.UpdateAsync(id, entity);
|
|
await Reload();
|
|
}
|
|
return new ApiResult() { code = 0 };
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
public async Task<ApiResult> GetList(RqeustPaging request)
|
|
{
|
|
Expression<Func<Plan, bool>> exp = filter => true;
|
|
return await base.GetPager(request, exp);
|
|
}
|
|
/// <summary>
|
|
/// 取所计划中所有的设备ids
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public async Task<IEnumerable<string>> GetDeviceIds(IEnumerable<string> ids)
|
|
{
|
|
Expression<Func<Plan, bool>> expression = filter => ids.Contains(filter.Id);
|
|
var items = (await base.GetListWithExp(expression)).ToList();
|
|
List<string> groupIds = new List<string>();
|
|
//取设备分组列表
|
|
// var deviceIds = items.Select(s => s.deviceIds).ToList();
|
|
items.Select(s => s.groups).ToList().ForEach(s =>
|
|
{
|
|
groupIds.AddRange(s);
|
|
});
|
|
//取设备
|
|
var deviceIds = (await _groupService.GetDeviceIds(groupIds)).ToList();
|
|
items.Select(s => s.deviceIds).ToList().ForEach(o =>
|
|
{
|
|
deviceIds.AddRange(o);
|
|
});
|
|
return deviceIds;
|
|
}
|
|
private async Task Reload()
|
|
{
|
|
IServiceCollection services = new ServiceCollection();
|
|
Monitor monitor = null;
|
|
monitor = new Monitor(services.BuildServiceProvider());
|
|
monitor.SetJobs(await GetList());
|
|
}
|
|
/// <summary>
|
|
/// 判断是否在计划中
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> existsPlan(string id)
|
|
{
|
|
Expression<Func<Plan, bool>> expression = filter => true;
|
|
List<string> groupIds = new List<string>();
|
|
var items = (await base.GetListWithExp(expression)).ToList();
|
|
items.Select(s => s.groups).ForEach(s =>
|
|
{
|
|
groupIds.AddRange(s);
|
|
});
|
|
var deviceIds = (await _groupService.GetDeviceIds(groupIds)).ToList();
|
|
items.Select(s => s.deviceIds).ToList().ForEach(o =>
|
|
{
|
|
deviceIds.AddRange(o);
|
|
});
|
|
return deviceIds.Any(s => s == id);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 执行
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task exec(string id)
|
|
{
|
|
var plan = await base.GetAsync(id);
|
|
var devices = await GetDeviceIds(new[] { id });
|
|
foreach (var item in devices)
|
|
{
|
|
Console.WriteLine($"开启命令:---{id}");
|
|
//开启远程控制,再发送命令
|
|
var entity = await _device.GetAsync(item);
|
|
if (entity == null)
|
|
{
|
|
Console.WriteLine($"id:{item},没有找到该条数据");
|
|
continue;
|
|
}
|
|
await _plcHelper.SetVal(entity.deviceIp, "V4000.1", true);
|
|
await _deviceUsedService.AddDeviceUsed(new DeviceUsed()
|
|
{
|
|
DeviceId = item,
|
|
Start = DateTime.Now,
|
|
End = DateTime.Now.AddSeconds(plan.execution),
|
|
});
|
|
_ = Task.Run(async () =>
|
|
{
|
|
await Task.Delay(plan.execution);
|
|
|
|
Console.WriteLine($"关闭命令:---{id}");
|
|
await _plcHelper.SetVal(entity.deviceIp, "V4000.1", false);
|
|
});
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|