예제 #1
0
        public static async Task <bool> InvokerQueryDiaitalSwitchWithAutoUpload(MachineButtonItem boardItem, int queryTime)
        {
            var boardSendCommand = new NetTcpWcfClientService <ITLAutoDevice>(boardItem.ServiceAddress, TimeSpan.FromMilliseconds(queryTime));
            var result           = await boardSendCommand.SendAsync(async proxy =>
            {
                var controlInfo = new ControlInfo {
                    ServiceKey = CommonConfigHelper.PLCServiceKey
                };
                var serviceData = new PLCControlServiceData
                {
                    ControlPLCType         = ControlPLCType.QueryDiaitalSwitchWithAutoUpload,
                    DeviceNumber           = boardItem.DeviceNumber,
                    Number                 = new[] { boardItem.Number },
                    PortSignName           = boardItem.SignName,
                    QueryTimeForAutoUpload = queryTime
                };
                controlInfo.Data = serviceData.ToBytes();
                var resultInfo   = await proxy.ControlDevice(controlInfo);
                if (!resultInfo.IsError && (resultInfo.Data != null))
                {
                    var switchItems = resultInfo.Data.ToObject <IEnumerable <SwitchItem> >().ToList();
                    var switchItem  = switchItems.Find(s => (s != null) && (s.SwitchNumber == boardItem.Number) && s.SwitchStatus.ToInt32().ToBoolean());
                    if (switchItem != null)
                    {
                        return(true);
                    }
                }
                return(false);
            });

            return(result);
        }
예제 #2
0
        public static async Task <bool> InvokerControlRelay(MachineRelayItem relayItem, bool isNo)
        {
            var notificationService = new NetTcpWcfClientService <ITLAutoDevice>(relayItem.ServiceAddress);

            RegWcfEvents(notificationService);
            var result = await notificationService.SendAsync(async proxy =>
            {
                var controlInfo = new ControlInfo {
                    ServiceKey = CommonConfigHelper.PLCServiceKey
                };
                var serviceData = new PLCControlServiceData
                {
                    ControlPLCType = ControlPLCType.ControlRelay,
                    DeviceNumber   = relayItem.DeviceNumber,
                    Number         = new[] { relayItem.Number },
                    PortSignName   = relayItem.SignName,
                    RelayStatus    = isNo
                };
                controlInfo.Data = serviceData.ToBytes();
                var resultInfo   = await proxy.ControlDevice(controlInfo);
                if (!resultInfo.IsError && (resultInfo.Data != null))
                {
                    return(true);
                }
                return(false);
            });

            UnregWcfEvents(notificationService);
            return(result);
        }
예제 #3
0
        public static async Task <bool> InvokerSimulationDigitalSwitch(SwitchItem boardItem)
        {
            var sendWcfCommand = new NetTcpWcfClientService <ITLAutoDevice>(boardItem.ServiceAddress);
            var result         = await sendWcfCommand.SendAsync(async proxy =>
            {
                var controlInfo = new ControlInfo {
                    ServiceKey = CommonConfigHelper.PLCServiceKey
                };
                var serviceData = new PLCControlServiceData
                {
                    ControlPLCType = ControlPLCType.SimulationDigitalSwitch,
                    DeviceNumber   = boardItem.DeviceNumber,
                    Number         = new[] { boardItem.Number },
                    PortSignName   = boardItem.SignName
                };
                controlInfo.Data = serviceData.ToBytes();
                var resultInfo   = await proxy.ControlDevice(controlInfo);
                if (!resultInfo.IsError && (resultInfo.Data != null))
                {
                    var result1 = resultInfo.Data[0].ToBoolean();
                    return(result1);
                }
                return(false);
            });

            return(result);
        }
예제 #4
0
        public static async Task <WcfResultInfo> Send(PLCControlServiceData serviceData)
        {
            var sendWcfCommand = new SendWcfCommandEx(PLCDeviceService.GetPLCDeviceService().WcfServiceAddress);

            sendWcfCommand.Error += SendWcfCommand_Error;
            sendWcfCommand.CommunicationError += SendWcfCommand_CommunicationError;
            sendWcfCommand.TimeoutError       += SendWcfCommand_TimeoutError;
            var result = await sendWcfCommand.SendEx(PLCDeviceService.Key, serviceData.ToBytes());

            sendWcfCommand.Error -= SendWcfCommand_Error;
            sendWcfCommand.CommunicationError -= SendWcfCommand_CommunicationError;
            sendWcfCommand.TimeoutError       -= SendWcfCommand_TimeoutError;
            return(result);
        }
예제 #5
0
        public override async Task <bool> Excute(Action <string> writeLogMsgAction)
        {
            var tasks = new List <bool>();

            foreach (var boardItemInfo in BoardItemInfos)
            {
                var serviceAddress = ProjectHelper.GetBoardServiceAddress(boardItemInfo.DeviceNumber, BoardType);
                var portName       = ProjectHelper.GetBoardPortName(boardItemInfo.DeviceNumber, BoardType);
                var wcfCommand     = new SendWcfCommand <ITLAutoDevice>(serviceAddress, writeLogMsgAction);
                var task           = await wcfCommand.SendAsync(async proxy =>
                {
                    try
                    {
                        var controlInfo = new ControlInfo {
                            ServiceKey = ConfigHelper.PLCServiceKey
                        };
                        var serviceData = new PLCControlServiceData
                        {
                            ControlPLCType = ControlPLCType.ControlRelay,
                            DeviceNumber   = boardItemInfo.DeviceNumber,
                            Number         = new[] { boardItemInfo.Number },
                            PortSignName   = portName,
                            RelayStatus    = boardItemInfo.IsNo
                        };
                        controlInfo.Data = serviceData.ToBytes();
                        var result       = await proxy.ControlDevice(controlInfo);
                        if (!result.IsError && (result.Data != null))
                        {
                            return(true);
                        }
                    }
                    catch (Exception ex)
                    {
                        writeLogMsgAction("执行任务时出错,原因为:" + ex.Message);
                    }
                    return(false);
                });

                tasks.Add(task);
            }
            return(tasks.All(s => s));
        }
예제 #6
0
        public override async Task <bool> Excute(Action <string> writeLogMsgAction)
        {
            _isStop  = false;
            _isBreak = false;
            var newTasks = new List <Tuple <Task <bool>, BoardItemInfo> >();

            bool[] result;
            do
            {
                newTasks.Clear();
                Parallel.ForEach(BoardItemInfos,
                                 boardItemInfo =>
                {
                    var serviceAddress = ProjectHelper.GetBoardServiceAddress(boardItemInfo.DeviceNumber, BoardType);
                    var portName       = ProjectHelper.GetBoardPortName(boardItemInfo.DeviceNumber, BoardType);
                    var wcfCommand     = new SendWcfCommand <ITLAutoDevice>(serviceAddress, writeLogMsgAction);
                    _commands.Add(wcfCommand);
                    var task = wcfCommand.SendAsync(async proxy =>
                    {
                        try
                        {
                            var controlInfo = new ControlInfo {
                                ServiceKey = ConfigHelper.PLCServiceKey
                            };
                            var serviceData = new PLCControlServiceData
                            {
                                ControlPLCType = ControlPLCType.QueryDigitalSwitch,
                                DeviceNumber   = boardItemInfo.DeviceNumber,
                                Number         = new[] { boardItemInfo.Number },
                                PortSignName   = portName
                            };
                            controlInfo.Data = serviceData.ToBytes();
                            var result1      = await proxy.ControlDevice(controlInfo);
                            if (!result1.IsError && (result1.Data != null))
                            {
                                var switchItems = result1.Data.ToObject <IEnumerable <SwitchItem> >().ToList();
                                var switchItem  = switchItems.Find(s => (s != null) && (s.SwitchNumber == boardItemInfo.Number) && (s.SwitchStatus.ToInt32().ToBoolean() == !boardItemInfo.IsNo));
                                if (switchItem != null)
                                {
                                    return(true);
                                }
                            }
                            serviceData.ControlPLCType         = ControlPLCType.QueryDiaitalSwitchWithAutoUpload;
                            serviceData.QueryTimeForAutoUpload = ConfigHelper.QuerySwitchTime;
                            controlInfo.Data = serviceData.ToBytes();
                            var result2      = await proxy.ControlDevice(controlInfo);
                            if (!result2.IsError && (result2.Data != null))
                            {
                                var switchItems = result2.Data.ToObject <IEnumerable <SwitchItem> >().ToList();
                                var switchItem  = switchItems.Find(s => (s != null) && (s.SwitchNumber == boardItemInfo.Number) && (s.SwitchStatus.ToInt32().ToBoolean() == !boardItemInfo.IsNo));
                                if (switchItem != null)
                                {
                                    return(true);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            writeLogMsgAction("执行任务时出错,原因为:" + ex.Message);
                        }
                        if (_isBreak)
                        {
                            return(true);
                        }
                        return(false);
                    });
                    newTasks.Add(new Tuple <Task <bool>, BoardItemInfo>(task, boardItemInfo));
                });
                result = await Task.WhenAll(newTasks.Select(s => s.Item1).ToArray());

                _commands.Clear();
            }while (!result.All(s => s) && !_isStop && !_isBreak);
            if (_isStop)
            {
                return(false);
            }
            return(true);
        }