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 { 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; } /// /// 取全部可用的plan /// /// public async Task> GetList() { Expression> exp = filter => filter.disable == false; return (await base.GetListWithExp(exp)).ToList(); } /// /// 更新 /// /// /// public async Task updatePlan(Plan input) { await base.UpdateAsync(input.Id, input); if (input.disable) { // QuartzUtil.DeleteJob(input.Id); } else { // QuartzUtil.StartJobWithCron(input.Id, input.name, input.cron); } return new ApiResult() { code = 0 }; } /// /// 取单条 /// /// /// public async Task 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 } }; } /// /// /// /// /// public async Task remove(IEnumerable ids) { foreach (var item in ids) { var entity = await base.GetAsync(item); await base.RemoveAsync(item); await Reload(); } return new ApiResult() { code = 0 }; } /// /// 新加plan /// /// /// public async Task AddPlan(Plan input) { var temp = await base.CreateAsync(input); await Reload(); return new ApiResult() { code = 0, }; } /// /// 更新 /// /// /// public async Task 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 }; } /// /// /// /// /// public async Task GetList(RqeustPaging request) { Expression> exp = filter => true; return await base.GetPager(request, exp); } /// /// 取所计划中所有的设备ids /// /// /// public async Task> GetDeviceIds(IEnumerable ids) { Expression> expression = filter => ids.Contains(filter.Id); var items = (await base.GetListWithExp(expression)).ToList(); List groupIds = new List(); //取设备分组列表 // 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()); } /// /// 判断是否在计划中 /// /// /// public async Task existsPlan(string id) { Expression> expression = filter => true; List groupIds = new List(); 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); } /// /// 执行 /// /// /// 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); }); } } } }