jy-plc/Services/DeviceSerive.cs

400 lines
14 KiB
C#
Raw Permalink Normal View History

2024-07-24 13:30:21 +00:00
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
{
/// <summary>
///
/// </summary>
public class DeviceSerive : BaseService<Device>
{
private AlertService _alertService;
private PlcHelper _hslPlc;
/// <summary>
///
/// </summary>
/// <param name="config"></param>
/// <param name="alertService"></param>
public DeviceSerive(IConfiguration config, AlertService alertService,
PlcHelper plcHelper) : base(config, nameof(Device))
{
_alertService = alertService;
_hslPlc = plcHelper;
}
/// <summary>
/// 新加设备
/// </summary>
/// <param name="device"></param>
/// <returns></returns>
public async Task<bool> Add(Device device)
{
Expression<Func<Device, bool>> 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;
}
/// <summary>
/// 取状态
/// </summary>
/// <param name="Ingroup"></param>
/// <returns></returns>
public async Task<ApiResult> GetlistByGroupState(bool Ingroup)
{
Expression<Func<Device, bool>> expression = filter => filter.InGroup == Ingroup && filter.IsDelete == false;
var result = (await base.GetListWithExp(expression)).ToList();
return new ApiResult() { code = 0, msg = "", data = result };
}
/// <summary>
/// 更新设备
/// </summary>
/// <param name="device"></param>
/// <returns></returns>
public async Task<ApiResult> Update(Device device)
{
Expression<Func<Device, bool>> 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 = "对象为空" };
}
/// <summary>
/// 更新设备组的状态
/// </summary>
/// <param name="ids"></param>
/// <param name="Ingroup"></param>
/// <returns></returns>
public async Task UpdataGroupState(IEnumerable<string> ids, bool Ingroup)
{
Expression<Func<Device, bool>> 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);
}
}
/// <summary>
/// 检测是否已经在组中
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
public async Task<bool> CheckGroupState(IEnumerable<string> ids)
{
Expression<Func<Device, bool>> exp = num => ids.Contains(num.Id);
var items = (await base.GetListWithExp(exp));
foreach (var item in items)
{
if (item.InGroup)
{
return false;
}
}
return true;
}
/// <summary>
/// 分页数据
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<ApiResult> GetList(RqeustPaging request)
{
request.pageSize = request.pageSize == 0 ? 10 : request.pageSize;
Expression<Func<Device, bool>> 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 }
};
}
/// <summary>
/// 取多条数据
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
public async Task<List<Device>> GetDeviceByIds(IEnumerable<string> ids)
{
FilterDefinitionBuilder<Device> builderFilter = Builders<Device>.Filter;
FilterDefinition<Device> filter = builderFilter.In("Id", ids);
var result = await base.FindListyFilter(filter);
return result;
}
/// <summary>
/// 取全部数据
/// </summary>
/// <returns></returns>
public async Task<List<object>> GeAllList()
{
Expression<Func<Device, bool>> 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<object> obj = new List<object>();
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; }
}
/// <summary>
/// 更新设备状态
/// </summary>
/// <param name="device"></param>
/// <returns></returns>
public async Task<bool> UpdateState(Device device)
{
Expression<Func<Device, bool>> 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;
}
/// <summary>
/// 开启
/// </summary>
/// <param name="deviceId">设备id</param>
/// <param name="point">是否定点</param>
/// <returns></returns>
public async Task<bool> startdevice(string deviceId, bool point = false)
{
Expression<Func<Device, bool>> 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;
}
/// <summary>
/// 停止
/// </summary>
/// <param name="deviceId"></param>
/// <returns></returns>
public async Task<bool> stopDevice(string deviceId)
{
Expression<Func<Device, bool>> 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<object> 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<object> getval(string deviceId)
{
Expression<Func<Device, bool>> 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<object> setval(string deviceId, bool val)
{
Expression<Func<Device, bool>> 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;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="t"></param>
/// <param name="val"></param>
/// <returns></returns>
public async Task<object> 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;
}
/// <summary>
/// 归零
/// </summary>
/// <param name="id"></param>
/// <param name="val"></param>
/// <returns></returns>
public async Task<object> 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;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="t"></param>
/// <param name="val"></param>
/// <returns></returns>
public async Task<object> 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;
}
/// <summary>
/// 设备角度
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<object> GetUpOrDown(string id)
{
var entity = await base.GetAsync(id);
if (entity != null)
{
return await _hslPlc.GetAngleVal1(entity.deviceIp, "VW4034");
}
return null;
}
/// <summary>
/// 设备角度
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<object> GetLeftOrRight(string id)
{
var entity = await base.GetAsync(id);
if (entity != null)
{
return await _hslPlc.GetAngleVal1(entity.deviceIp, "VD4030");
}
return null;
}
}
}