using GuideScreen.Common.Common; using GuideScreen.Common.Constants; using GuideScreen.Common.Repositories; using GuideScreen.Common.Repositories.Entities; using GuideScreen.Common.Services; using GuideScreen.Common.Services.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GuideScreen.Common.Services.Impl { /// /// PLC点位服务 实现 /// internal class PLCPointService : IPLCPointService { private IPLCPointRepository plcPointRepository; private IPLCMonitorService plcMonitorService; public PLCPointService(IPLCPointRepository plcPointRepository, IPLCMonitorService pLCMonitorService) { this.plcPointRepository = plcPointRepository; this.plcMonitorService = pLCMonitorService; } /// /// 获取 PLC 点位 列表 /// /// /// /// /// public PageModel> GetPLCPointList(string deviceName, string note, int pageSize, int pageIndex) { var pageModel = plcPointRepository.GetPLCPointList(deviceName, note, pageSize, pageIndex); if (pageModel.Content != null && plcMonitorService.IsRun) { foreach (var model in pageModel.Content) { model.PointValue = plcMonitorService.ReadValue(model.PointAddress, Type.GetType(model.PointDataType))?.ToString(); } } return pageModel; } /// /// 获取 控制器时间参数显示及设置 列表 /// /// public List GetControllerTimeParametersList() { var list = plcPointRepository.GetPLCPointList(PLCDeviceNames.SetControllerTimeParameters, "", 0, 0); var points = plcMonitorService.Points; if (points != null && points.Count > 0) { foreach (var c in list.Content) { var point = points.Where(f => f.Address == c.PointAddress).FirstOrDefault(); if (point != null) { c.PointValue = point.ObjectValue?.ToString(); } } } return list.Content; } /// /// 获取设备名称 /// /// public List GetDeviceNames() { var list = plcPointRepository.GetDeviceNames(); return list; } /// /// 获取PLC点位明细 /// /// /// public PLCPointModel GetPLCPointDetail(int id) { var entity = plcPointRepository.GetPLCPointDetail(id); var model = ModelTools.PubClone.Trans(entity); if (model != null && plcMonitorService.IsRun) { model.PointValue = plcMonitorService.ReadValue(model.PointAddress, Type.GetType(model.PointDataType))?.ToString(); } return model; } /// /// 更新PLC点位信息 /// /// public void UpdatePLCPoint(PLCPointModel model) { var entity = ModelTools.PubClone.Trans(model); plcPointRepository.UpdatePLCPoint(entity); if (model != null && plcMonitorService.IsRun) { model.PointValue = plcMonitorService.WriteValue(model.PointAddress, Type.GetType(model.PointDataType), model.PointValue)?.ToString(); } } } }