using DeviceService.Core; using DeviceService.Core.Api; using DeviceService.Driver; using DeviceService.Entity; namespace DeviceService.Device { [DeviceDesc("PLC")] public class PLCDevice : NotifyBase, IDevice { private long _id; public long Id { get => _id; set { SetProperty(ref _id, value); } } private string _deviceId; public string DeviceId { get => _deviceId; set { SetProperty(ref _deviceId, value); } } private string _type = "PLC"; public string Type { get => _type; private set { SetProperty(ref _type, value); } } private string _brand; public string Brand { get => _brand; set { SetProperty(ref _brand, value); } } private string _connectStr; public string ConnectStr { get => _connectStr; set { SetProperty(ref _connectStr, value); } } private DeviceStatus _status; public DeviceStatus Status { get => _status; set { SetProperty(ref _status, value); } } private AccessType _accessType; public AccessType AccessType { get => _accessType; set { SetProperty(ref _accessType, value); } } private IPLCDriver driver; public bool Init(out string msg) { msg = string.Empty; try { driver = DriverManager.Instance.CreateDriverByTypeAndBrand(Type, Brand); return true; } catch (Exception ex) { msg = ex.Message; return false; } } public bool Open(out string msg) { if (driver == null) { msg = "设备尚未初始化或初始化失败。"; return false; } var res = driver.Open(out msg); if (res) { Status = DeviceStatus.Busy; } return res; } public bool Close(out string msg) { if (driver == null) { msg = "设备尚未初始化或初始化失败。"; return false; } var res = driver.Close(out msg); if (res) { Status = DeviceStatus.Idle; } return res; } public ResponseData HandleRequest(RequestData req) { return new ResponseData { Success = false, Message = "未实现" }; } } }