You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
7.6 KiB
C#

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2026 WenJY
* CLR4.0.30319.42000
* Mr.Wen's MacBook Pro
* Sln.Wcs.HoistSdk
* 6BE2CE90-15BA-43B1-88B0-78853870ECDA
*
* WenJY
*
* 2026-05-06 14:09:47
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sln.Wcs.HoistSdk.Config;
using Sln.Wcs.HoistSdk.Dto.GetHoistStatus;
using Sln.Wcs.HoistSdk.Dto.HoistControl;
using Sln.Wcs.HoistSdk.Dto.SetHoistAction;
using Sln.Wcs.HoistSdk.Dto.SetHoistAlarm;
using Sln.Wcs.HoistSdk.Dto.SetHoistTask;
using Sln.Wcs.Model.Configs;
using Sln.Wcs.Plc.Service;
namespace Sln.Wcs.HoistSdk;
public class HoistSdk:IHoistSdk
{
private readonly HoistConfig _hoistConfig;
private readonly List<IPlc> _plcs;
public HoistSdk(HoistConfig hoistConfig, List<IPlc> plcs)
{
_hoistConfig = hoistConfig;
_plcs = plcs;
}
/// <summary>
/// 设置提升机动作
/// </summary>
/// <param name="setHoistActionDto"></param>
/// <returns></returns>
public SetHoistActionResultDto SetHoistAction(SetHoistActionDto setHoistActionDto)
{
SetHoistActionResultDto result = new SetHoistActionResultDto();
try
{
string paramAddress = GetPlcParam(setHoistActionDto.hoistCode,setHoistActionDto.deviceSerialNo,"放置完成");
var plc = _plcs.Where(x => x.ConfigKey.Equals(setHoistActionDto.hoistCode)).ToList().First();
var res = plc.writeInt16ByAddress(paramAddress, (int)setHoistActionDto.action);
if (res)
{
result.code = "0";
result.message = "写入成功";
}
else
{
result.code = "99";
result.message = "写入失败";
}
}
catch (Exception e)
{
result.code = "99";
result.message = e.Message;
}
return result;
}
/// <summary>
/// 设置提升机任务
/// </summary>
/// <param name="setHoistTaskDto"></param>
/// <returns></returns>
public SetHoistTaskResultDto SetHoistTask(SetHoistTaskDto setHoistTaskDto)
{
SetHoistTaskResultDto result = new SetHoistTaskResultDto();
try
{
string paramAddress = GetPlcParam(setHoistTaskDto.hoistCode,setHoistTaskDto.deviceSerialNo,"任务执行指令");
var plc = _plcs.Where(x => x.ConfigKey.Equals(setHoistTaskDto.hoistCode)).ToList().First();
var res = plc.writeInt16ByAddress(paramAddress, setHoistTaskDto.endPoint);
if (res)
{
result.code = "0";
result.message = "写入成功";
}
else
{
result.code = "99";
result.message = "写入失败";
}
}
catch (Exception e)
{
result.code = "99";
result.message = e.Message;
}
return result;
}
/// <summary>
/// 获取提升机状态
/// </summary>
/// <param name="hoistStatusDto"></param>
/// <returns></returns>
public GetHoistStatusResultDto GetHoistStatus(GetHoistStatusDto hoistStatusDto)
{
GetHoistStatusResultDto result = new GetHoistStatusResultDto();
try
{
List<dynamic> paramStr = JsonConvert.DeserializeObject<List<dynamic>>(_hoistConfig.hoist_plc_param);
List<dynamic> deviceParams = paramStr.Where(x => x.hostCode == hoistStatusDto.hoistCode).ToList();
if (string.IsNullOrEmpty(hoistStatusDto.hoistCode))
{
deviceParams = paramStr;
}
if (deviceParams != null && deviceParams.Count > 0)
{
foreach (var item in deviceParams)
{
if (item.hostCode == hoistStatusDto.hoistCode)
{
var plc = _plcs.Where(x => x.ConfigKey.Equals(item.hostCode)).FirstOrDefault();
if (plc == null)
{
result.code = "99";
result.message = $"未找到PLC: {item.hostCode}";
result.hoistCode = hoistStatusDto.hoistCode;
return result;
}
foreach (var param in item.deviceParams)
{
int value = plc.readInt16ByAddress(param.paramAddress);
param.paramValue = param.objId;
}
}
}
string jsonOutput = JsonConvert.SerializeObject(deviceParams, Formatting.Indented);
result.code = "0";
result.message = "状态更新成功";
result.hoistCode = hoistStatusDto.hoistCode;
result.deviceParamStr = jsonOutput;
}
else
{
result.code = "99";
result.message = $"未获取到设备参数:{hoistStatusDto.hoistCode}";
result.hoistCode = hoistStatusDto.hoistCode;
}
}
catch (Exception e)
{
result.code = "99";
result.message = e.Message;
}
return result;
}
/// <summary>
/// 设置提升机报警
/// </summary>
/// <param name="hoistAlarmDto"></param>
/// <returns></returns>
public SetHoistAlarmResultDto SetHoistAlarm(SetHoistAlarmDto hoistAlarmDto)
{
SetHoistAlarmResultDto result = new SetHoistAlarmResultDto();
try
{
string paramAddress = GetPlcParam(hoistAlarmDto.hoistCode,hoistAlarmDto.deviceSerialNo,hoistAlarmDto.key);
var plc = _plcs.Where(x => x.ConfigKey.Equals(hoistAlarmDto.hoistCode)).ToList().First();
var res = plc.writeInt16ByAddress(paramAddress, hoistAlarmDto.value);
if (res)
{
result.code = "0";
result.message = "设置成功";
}
else
{
result.code = "99";
result.message = "设置失败";
}
}
catch (Exception e)
{
result.code = "99";
result.message = e.Message;
}
return result;
}
/// <summary>
/// 获取参数地址
/// </summary>
/// <param name="hostCode"></param>
/// <param name="deviceSerialNo"></param>
/// <param name="paramName"></param>
/// <returns></returns>
private string GetPlcParam(string hostCode,int deviceSerialNo,string paramName)
{
List<dynamic> paramStr = JsonConvert.DeserializeObject<List<dynamic>>(_hoistConfig.hoist_plc_param);
dynamic deviceInfo = paramStr.Where(x => x.hostCode == hostCode && x.deviceSerialNo == deviceSerialNo)
.FirstOrDefault();
List<dynamic> deviceParams = (deviceInfo.deviceParams as JArray)?.ToObject<List<dynamic>>();
dynamic deviceParam = deviceParams
.Where(x => ((string)x.paramName).Contains(paramName) && (int)x.operationType == 2).FirstOrDefault();
return deviceParam.paramAddress;
}
}