Beispiel #1
0
        /// <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);
            });
        }
Beispiel #2
0
        /// <summary>
        /// 同步控制设备
        /// </summary>
        /// <returns></returns>
        private bool SyncControlDevices()
        {
            var syncResult = false;

            //控制设备
            var controlDevices =
                FacilityControlDeviceUnit.FindAllWithCache()
                .ToList()
                .Where(c => c.Code1.Substring(13, 1).EqualIgnoreCase("K"));
            var facilityControlDeviceUnits = controlDevices as FacilityControlDeviceUnit[] ?? controlDevices.ToArray();

            if (facilityControlDeviceUnits.Any())
            {
                //逐个上传
                facilityControlDeviceUnits.ToList().ForEach(cd =>
                {
                    #region 构建DeviceModel

                    var code = cd.Code1;
                    var devs = cd.ControlDeviceUnits; //改设施控制设备所拥有的所有控制设备继电器的集合
                    if (devs != null)
                    {
                        var originalValue  = CalcControlDeviceValue.CalcOriginal(cd);
                        var processedValue = CalcControlDeviceValue.CalcProcessValue(cd);
                        var deviceModel    = new DeviceModel
                        {
                            Serialnum           = code,
                            Name                = cd.Name,
                            DeviceTypeSerialnum = devs[0].DeviceTypeSerialnum,
                            FacilitySerialnum   = cd.Facility.Code1,
                            ProcessedValue      = originalValue,
                            ShowValue           = processedValue,
                            UpdateTime          = DateTime.Now,
                            RelayType           = devs[0].RelayTypeName
                        };

                        #endregion 构建DeviceModel

                        var entity = AwEntityHelper.GetEntity(cd.Facility.Farm.Code1);
#if DEBUG
                        var sw = new Stopwatch();
                        sw.Start();
#endif
                        try
                        {
                            var trans          = ApiTransportHelper.GetTransport();
                            var responseResult = _deviceApi.QueryDevice(entity, trans, deviceModel.Serialnum);
                            if (responseResult.Data != null)
                            {
                                var entity1 = AwEntityHelper.GetEntity(cd.Facility.Farm.Code1);
                                var result  = _deviceApi.UpdateDevice(entity1, trans, deviceModel);
                                trans.Dispose();
                                LogHelper.Debug("更新控制设备{0}:{1}", deviceModel.Serialnum,
                                                result ? "成功" : "失败");
                                syncResult = result;
                            }
                            else
                            {
                                var entity1 = AwEntityHelper.GetEntity(cd.Facility.Farm.Code1);
                                var result  = _deviceApi.AddDevice(entity1, trans, deviceModel);
                                trans.Dispose();
                                LogHelper.Debug("添加控制设备{0}:{1}", deviceModel.Serialnum,
                                                result ? "成功" : "失败");
                                syncResult = result;
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Fatal(ex.ToString());
                        }
#if DEBUG
                        sw.Stop();
                        var apiAccesslog = new ApiAccessLog
                        {
                            ApiName    = "同步控制设备",
                            Result     = syncResult,
                            CreateTime = DateTime.Now,
                            CostTime   = Convert.ToInt32(sw.ElapsedMilliseconds)
                        };
                        apiAccesslog.Save();
                        LogHelper.Debug("同步控制设备耗时" + sw.ElapsedMilliseconds.ToString() + "ms");
#endif
                    }
                });
            }
            //return syncResult;
            return(true);
        }