using LangGuan.Command.Extension; using LangGuan.Command.Model; using LangGuan.Command.Model.EntityModel; using Microsoft.Extensions.Configuration; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Net.NetworkInformation; using System.Threading.Tasks; namespace LangGuan.Services { /// /// /// public class DeviceSerive : BaseService { private AlertService _alertService; private PlcHelper _hslPlc; /// /// /// /// /// public DeviceSerive(IConfiguration config, AlertService alertService, PlcHelper plcHelper) : base(config, nameof(Device)) { _alertService = alertService; _hslPlc = plcHelper; } /// /// 新加设备 /// /// /// public async Task Add(Device device) { Expression> expression = filter => filter.deviceId == device.deviceId; var entity = (await base.GetListWithExp(expression)).ToList().FirstOrDefault(); if (entity == null) { await base.CreateAsync(device); return true; } return false; } /// /// 取状态 /// /// /// public async Task GetlistByGroupState(bool Ingroup) { Expression> expression = filter => filter.InGroup == Ingroup && filter.IsDelete == false; var result = (await base.GetListWithExp(expression)).ToList(); return new ApiResult() { code = 0, msg = "", data = result }; } /// /// 更新设备 /// /// /// public async Task Update(Device device) { Expression> exp = num => num.Id == device.Id; var entity = (await base.GetListWithExp(exp)).FirstOrDefault(); if (entity != null) { entity.lng = device.lng; entity.lat = device.lat; entity.NickName = device.NickName; entity.deviceIp = device.deviceIp; entity.deviceId = device.deviceId; await base.UpdateAsync(entity.Id, entity); return new ApiResult() { code = 0, msg = "" }; } return new ApiResult() { code = 1, msg = "对象为空" }; } /// /// 更新设备组的状态 /// /// /// /// public async Task UpdataGroupState(IEnumerable ids, bool Ingroup) { Expression> exp = num => ids.Contains(num.Id); var items = (await base.GetListWithExp(exp)); foreach (var item in items) { item.InGroup = Ingroup; await base.UpdateAsync(item.Id, item); } } /// /// 检测是否已经在组中 /// /// /// public async Task CheckGroupState(IEnumerable ids) { Expression> exp = num => ids.Contains(num.Id); var items = (await base.GetListWithExp(exp)); foreach (var item in items) { if (item.InGroup) { return false; } } return true; } /// /// 分页数据 /// /// /// public async Task GetList(RqeustPaging request) { request.pageSize = request.pageSize == 0 ? 10 : request.pageSize; Expression> exp = filter => true; var query = await base.GetListWithExp(exp); var total = query.Count(); var items = query.OrderByDescending(s => s.CreateDateTime) .Skip(request.pageSize * (request.current - 1)).Take(request.pageSize) .Select(s => new { s.Id, s.deviceId, s.NickName, s.lat, s.lng, s.deviceIp }).ToList(); return new ApiResult() { code = 0, data = new { total = total, items = items } }; } /// /// 取多条数据 /// /// /// public async Task> GetDeviceByIds(IEnumerable ids) { FilterDefinitionBuilder builderFilter = Builders.Filter; FilterDefinition filter = builderFilter.In("Id", ids); var result = await base.FindListyFilter(filter); return result; } /// /// 取全部数据 /// /// public async Task> GeAllList() { Expression> exp = num => true; var query = await base.GetListWithExp(exp); var list = query.OrderByDescending(s => s.CreateDateTime).ToList(); // .Select(s => new { s.Id, s.deviceId, s.NickName, s.deviceIp, s.lat, s.lng, status = s.state, s.InGroup, s.angle }).ToList(); List obj = new List(); foreach (var item in list) { Ping pingSender = new Ping(); PingReply reply = await pingSender.SendPingAsync(item.deviceIp, 500); // var temp = await _hslPlc.GetAngleVal1(item.deviceIp, "VD4030"); obj.Add(new { item.Id, item.deviceId, item.NickName, item.deviceIp, item.lat, item.lng, angle = "0.0", online = reply.Status == IPStatus.Success ? true : false }); } //foreach (var item in list) //{ // /// 远程获取设备是否在线 // var temp = await _hslPlc.GetAngleVal1(item.deviceIp, "VD4030"); // obj.Add(new indexdevice // { // id = item.Id, // deviceId = item.deviceId, // NickName = item.NickName, // lat = item.lat, // lng = item.lng, // angle = temp, // online = temp != "" ? true : false, // deviceIp = item.deviceIp // }); // //obj.Add(new // //{ // // item.Id, // // item.deviceId, // // item.NickName, // // item.deviceIp, // // item.lat, // // item.lng, // // angle = temp, // // online = temp != "" ? true : false // //}); //} return obj; } public class indexdevice { public string id { get; set; } public string deviceId { get; set; } public string NickName { get; set; } public string deviceIp { get; set; } public string lat { get; set; } public string lng { get; set; } public string angle { get; set; } public bool online { get; set; } } /// /// 更新设备状态 /// /// /// public async Task UpdateState(Device device) { Expression> expression = filter => filter.deviceId == device.deviceId; var entity = (await base.GetListWithExp(expression)).ToList().FirstOrDefault(); if (entity != null) { if (entity.state != device.state) { string content = "设备离线"; if (entity.state == 0) { content = "设备上线"; } entity.state = device.state; await base.UpdateAsync(device.Id, entity); await _alertService.Add(new Alert { NickName = entity.NickName, deviceId = entity.deviceId, content = content }); return true; } } return false; } /// /// 开启 /// /// 设备id /// 是否定点 /// public async Task startdevice(string deviceId, bool point = false) { Expression> exp = num => num.Id == deviceId; var entity = await base.GetAsync(deviceId); if (entity != null) { await _hslPlc.SetVal(entity.deviceIp, "V4000.1", true); return await _hslPlc.SetVal(entity.deviceIp, "V4000.2", point); } return false; } /// /// 停止 /// /// /// public async Task stopDevice(string deviceId) { Expression> exp = num => num.Id == deviceId; var entity = await base.GetAsync(deviceId); if (entity != null) { return await _hslPlc.SetVal(entity.deviceIp, "V4000.1", false); } return false; } public async Task readkey(string ip, string key) { return await _hslPlc.read(ip, key); } public async Task wrirekey(string ip, string key, int val) { await _hslPlc.WriteVal(ip, key, val); } public async Task getval(string deviceId) { Expression> exp = num => num.Id == deviceId; var entity = await base.GetAsync(deviceId); if (entity != null) { return await _hslPlc.GetVal(entity.deviceIp, "V4000.1"); } return false; } public async Task setval(string deviceId, bool val) { Expression> exp = num => num.Id == deviceId; var entity = await base.GetAsync(deviceId); if (entity != null) { return await _hslPlc.SetVal(entity.deviceIp, "V4000.1", val); } return false; } /// /// /// /// /// /// /// public async Task setUporDown(string id, int t = 1, bool val = true) { var entity = await base.GetAsync(id); if (entity != null) { string key = "V4000.5";//上 if (t != 1) { key = "V4000.6";//下 } return await _hslPlc.SetVal(entity.deviceIp, key, val); } return false; } /// /// 归零 /// /// /// /// public async Task Zero(string id, bool val = true) { var entity = await base.GetAsync(id); if (entity != null) { string key = "V4000.7";//归零 Console.WriteLine("归零操作"); return await _hslPlc.SetVal(entity.deviceIp, key, val); } return false; } /// /// /// /// /// /// /// public async Task setLeftorRight(string id, int t = 1, bool val = true) { var entity = await base.GetAsync(id); if (entity != null) { string key = "V4000.3";//左 if (t != 1) { key = "V4000.4";//右 } return await _hslPlc.SetVal(entity.deviceIp, key, val); } return false; } /// /// 设备角度 /// /// /// public async Task GetUpOrDown(string id) { var entity = await base.GetAsync(id); if (entity != null) { return await _hslPlc.GetAngleVal1(entity.deviceIp, "VW4034"); } return null; } /// /// 设备角度 /// /// /// public async Task GetLeftOrRight(string id) { var entity = await base.GetAsync(id); if (entity != null) { return await _hslPlc.GetAngleVal1(entity.deviceIp, "VD4030"); } return null; } } }