/// <summary> /// 初始化控制记录列表 /// </summary> private void InitDeviceControlLog() { this.listView1.Items.Clear(); List <DeviceControlLog> list = DeviceControlLog.FindAll(); foreach (var log in list) { var strings = new string[] { log.Code1, log.Code2, log.Code3, log.ControlDeviceUnitName, log.ControlResult.ToString() }; var listViewItem = new ListViewItem(strings); listViewItem.Tag = log; this.listView1.Items.Add(listViewItem); } }
/// <summary> /// 控制设备 /// </summary> /// <param name="deviceId">控制设备ID</param> /// <param name="status">设备状态</param> private void Control(int deviceId, int status) { const bool result = false; List <FacilityControlDeviceUnit> fac = FacilityControlDeviceUnit.FindAllByControlDeviceUnitGroupNum(deviceId); if (fac == null || !fac.Any()) { return; } var device = ControlDeviceUnit.FindByID(deviceId); if (device == null) { return; } string reason = null; TransportTypeEnum type; if ( !Enum.TryParse(device.ModularDevice.CommunicateDevice.CommunicateDeviceTypeName, true, out type)) { type = TransportTypeEnum.Unknow; } var host = device.ModularDevice.CommunicateDevice.Args1; var e = Convert.ToInt32(device.ModularDevice.CommunicateDevice.Args2); var timeout = Convert.ToInt32(device.ModularDevice.CommunicateDevice.Args3); //获取Transport var transport = TransportFactory.GetTransport(type, host, e, timeout); var modbusControlDevice = new ModbusControlDevice(transport, Convert.ToByte(device.ModularDevice.Address)); try { //待编写控制指令 //if (device.IsComposite) //三态设备 正转 反转 停止 //{ // if (status == -1) // { // result = modbusControlDevice.Write(device.RegisterAddress1, true); // } // else if (status == 0) // { // result = modbusControlDevice.Write(device.RegisterAddress1, false); // result = modbusControlDevice.Write(device.RegisterAddress2, false); // } // else if (status == 1) // { // result = modbusControlDevice.Write(device.RegisterAddress2, true); // } //} //else //两态设备 开 关 //{ // result = modbusControlDevice.Write(device.RegisterAddress1, status == 1 ? true : false); //} } catch (Exception ex) { reason = ex.ToString(); } LogHelper.Debug("指令执行" + (result ? "成功" : "失败")); DeviceControlCommand.FindAllByControlDeviceUnitID(deviceId).ToList().ForEach(deviceControlCommand => { var controlResult = new ControlResult { Serialnum = deviceControlCommand.Code1, //Command = status, //FacilityCode = fac[0].Facility.Code1, //DeviceCode = fac[0].Code1, //ContinueTime = 5, Time = DateTime.Now, Result = result, FailReason = reason }; var devControlLog = new DeviceControlLog { Code1 = deviceControlCommand.Code1, ControlDeviceUnitID = deviceControlCommand.ControlDeviceUnitGroupNum, ControlResult = result, CreateTime = DateTime.Now, DeviceControlCommand = deviceControlCommand, DeviceControlCommandID = deviceControlCommand.ID, DeviceValue = Convert.ToInt32(CalcControlDeviceValue.CalcProcessValue(fac[0])), ShowValue = CalcControlDeviceValue.CalcOriginal(fac[0]).ToString(), FailReason = reason }; devControlLog.Save(); UploadControlCommand(controlResult, fac[0].Facility); //上传控制指令 Thread.Sleep(50); }); }