Exemple #1
0
 /// <summary>
 /// 停止
 /// </summary>
 public void Stop()
 {
     MainTask.Dispose();
     PortInstance.Dispose();
     PortInstance = null;
     ProtocolIntance.Dispose();
     ProtocolIntance = null;
 }
Exemple #2
0
 public bool Start(object o)
 {
     if (null != PortInstance)
     {
         return(PortInstance.Start(o));
     }
     MainTask = Task.Factory.StartNew(TaskOn);
     return(false);
 }
Exemple #3
0
        /// <summary>
        /// 一次通讯过程
        /// </summary>
        protected virtual void OneCommunication()
        {
            try
            {
                if (PortInstance != null && ProtocolIntance != null)
                {
                    #region 发送

                    byte[] array = ProtocolIntance.GetCBytesommand();
                    if (array != null)
                    {
                        PortInstance.Send(array);
                    }

                    #endregion  end 发送

                    #region 接收

                    //等1s超时
                    DateTime dtLastSendOn = DateTime.Now;
                    while (DateTime.Now.Subtract(dtLastSendOn).TotalMilliseconds < 1000)
                    {
                        byte[] arrayRec;
                        if ((arrayRec = PortInstance.Recive()) != null)
                        {
                            byte[] arrayBack;
                            if (RegDeata(arrayRec, out arrayBack))
                            {
                                ProtocolIntance.Update(arrayBack);
                            }

                            return;
                        }
                    }

                    #endregion end 接收
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }
        }
        public async Task <IDisposable> Start(PortPool portPool, IPAddress address = null, bool useHttps = false)
        {
            _portInstance = await portPool.NextAsync();

            return(await Start(_portInstance.Port, address, useHttps));
        }
Exemple #5
0
        private static void UpdateSubContainers(DesignPrimitivesWrapper de,
                                                Container rootContainerType,
                                                DesignModel rootDesignModel,
                                                Dictionary <CyPhy.Port, PortInstance> cyphyPorts2PortInstances,
                                                Dictionary <CyPhy.Port, ContainerPort> containerPorts,
                                                List <Connection> connectionsToBeVisited,
                                                Dictionary <CyPhy.SimpleFormula, ContainerCalculation> simpleFormulas,
                                                Dictionary <CyPhy.ValueFlowTarget, AVM.META.Design.ValueType> valueInstances,
                                                Dictionary <CyPhy.Port, StructuralInterfaceInstance> innerStructuralPortInstances,
                                                Dictionary <CyPhy.Port, ContainerStructuralInterface> containerStructuralPortInstances)
        {
            System.Diagnostics.Debug.WriteLine(de.Path);

            // Init list
            if (rootContainerType.ContainerPorts == null)
            {
                rootContainerType.ContainerPorts = new List <ContainerPort>();
            }
            rootContainerType.ComponentInstances = new List <ComponentInstance>();

            // Add Powerflows, JointStrucutres and ValueFlows for later steps
            connectionsToBeVisited.AddRange(de.ValueFlows);


            #region Handle ComponentRefs
            var componentRefs = de.ComponentRefs;
            foreach (var compRef in componentRefs)
            {
                var isComponent = false;
                try
                {
                    var c = compRef.Referred.Component;
                    // line above throws an exception if it is not a component
                    isComponent = true;
                }
                catch
                {
                }

                if (compRef != null && isComponent)
                {
                    CyPhy.Component comp = compRef.Referred.Component;
                    System.Diagnostics.Debug.WriteLine(comp.Path);

                    ComponentInstance compInst = Component2ComponentInstance(comp);
                    rootContainerType.ComponentInstances.AddItem(compInst);
                    compInst.Name = compRef.Name;
                    //compInst.id = GetOrSetID(compRef);
                    //compInst.id = compRef.Guid.ToString();

                    String instanceGUID = compRef.Attributes.InstanceGUID;
                    // Set this value if not set
                    if (String.IsNullOrWhiteSpace(instanceGUID))
                    {
                        compRef.Attributes.InstanceGUID = compRef.Guid.ToString();
                        instanceGUID = compRef.Attributes.InstanceGUID;
                    }
                    compInst.id = compRef.Attributes.InstanceGUID;


                    String s_SrcDesignSpaceContainerID = GetDesignSpaceSourceObjectID(compRef);
                    if (!String.IsNullOrWhiteSpace(s_SrcDesignSpaceContainerID))
                    {
                        compInst.SrcDesignSpaceContainerID = s_SrcDesignSpaceContainerID;
                    }

                    // Update Component's PortInstances
                    compInst.PortInstances = new List <PortInstance>();
                    foreach (CyPhy.Port port in comp.Children.PortCollection)
                    {
#if TRACK_PORT_ID
                        var portInst = new PortInstance {
                            IDinSourceModel = port.ID
                        };
#else
                        var portInst = new PortInstance {
                            IDinSourceModel = port.Attributes.ID
                        };
#endif
                        //var portInst = new PortInstance { IDinSourceModel = port.ID };
                        compInst.PortInstances.AddItem(portInst);

                        if (!cyphyPorts2PortInstances.ContainsKey(port))
                        {
                            cyphyPorts2PortInstances.Add(port, portInst); // Update Ports list
                        }
                    }

                    // Convert parameters
                    foreach (var p in comp.Children.ParameterCollection)
                    {
                        double result = 0;
                        double.TryParse(p.Attributes.Value, out result);
                        var componentNamedValueInstance = new ComponentNamedValueInstance {
                            IDinComponentModel = p.Attributes.ID, Value = result
                        };
                        compInst.NamedValueInstances.AddItem(componentNamedValueInstance);
                        if (valueInstances.ContainsKey(p))
                        {
                            continue;
                        }

                        valueInstances.Add(p, componentNamedValueInstance);
                    }

                    // Convert properties
                    foreach (var p in comp.Children.PropertyCollection)
                    {
                        double result = 0;
                        double.TryParse(p.Attributes.Value, out result);
                        var componentNamedValueInstance = new ComponentNamedValueInstance {
                            IDinComponentModel = p.Attributes.ID, Value = result
                        };
                        compInst.NamedValueInstances.AddItem(componentNamedValueInstance);
                        if (valueInstances.ContainsKey(p))
                        {
                            continue;
                        }

                        valueInstances.Add(p, componentNamedValueInstance);
                    }
                }
            }
            #endregion

            #region Handle Component instances
            List <CyPhy.Component> lcChildComps = de.Components;

            foreach (CyPhy.Component comp in lcChildComps)
            {
                System.Diagnostics.Debug.WriteLine(comp.Path);

                ComponentInstance compInst = Component2ComponentInstance(comp);
                rootContainerType.ComponentInstances.AddItem(compInst);

                // Update Component's PortInstances
                compInst.PortInstances = new List <PortInstance>();
                foreach (CyPhy.Port port in comp.Children.PortCollection)
                {
#if TRACK_PORT_ID
                    var structPort = new StructuralInterfaceInstance {
                        IDinSourceModel = port.ID
                    };
#else
                    var structPort = new StructuralInterfaceInstance {
                        IDinSourceModel = port.Attributes.ID
                    };
#endif
                    if (!innerStructuralPortInstances.ContainsKey(port))
                    {
                        innerStructuralPortInstances.Add(port, structPort);
                        compInst.StructuralInterfaceInstances.Add(structPort);
                    }

#if TRACK_PORT_ID
                    var portInst = new PortInstance {
                        IDinSourceModel = port.ID
                    };
#else
                    var portInst = new PortInstance {
                        IDinSourceModel = port.Attributes.ID
                    };
#endif
                    compInst.PortInstances.AddItem(portInst);

                    if (!cyphyPorts2PortInstances.ContainsKey(port))
                    {
                        cyphyPorts2PortInstances.Add(port, portInst); // Update Ports list
                    }
                }

                // Convert parameters
                foreach (var p in comp.Children.ParameterCollection)
                {
                    double result = 0;
                    double.TryParse(p.Attributes.Value, out result);
                    var componentNamedValueInstance = new ComponentNamedValueInstance {
                        IDinComponentModel = p.Attributes.ID, Value = result
                    };
                    compInst.NamedValueInstances.AddItem(componentNamedValueInstance);
                    if (valueInstances.ContainsKey(p))
                    {
                        continue;
                    }

                    valueInstances.Add(p, componentNamedValueInstance);
                }

                // Convert properties
                foreach (var p in comp.Children.PropertyCollection)
                {
                    double result = 0;
                    double.TryParse(p.Attributes.Value, out result);
                    var componentNamedValueInstance = new ComponentNamedValueInstance {
                        IDinComponentModel = p.Attributes.ID, Value = result
                    };
                    compInst.NamedValueInstances.AddItem(componentNamedValueInstance);
                    if (valueInstances.ContainsKey(p))
                    {
                        continue;
                    }

                    valueInstances.Add(p, componentNamedValueInstance);
                }
            }
            #endregion

            // Recursively add all ContainerTypes from sub-DesignEntities
            rootContainerType.Containers = new List <Container>();
            var ldeDesignEntityChildren = de.ComponentAssemblies.Cast <CyPhy.DesignEntity>().ToList();
            ldeDesignEntityChildren.AddRange(de.DesignContainers);

            foreach (CyPhy.DesignEntity subDE in ldeDesignEntityChildren)
            {
                Container subRootContainerType;
                if (subDE is CyPhy.DesignContainer &&
                    (subDE as CyPhy.DesignContainer).Attributes.ContainerType == CyPhyClasses.DesignContainer.AttributesClass.ContainerType_enum.Compound)
                {
                    subRootContainerType = new Compound();
                }
                else if (subDE is CyPhy.DesignContainer &&
                         (subDE as CyPhy.DesignContainer).Attributes.ContainerType == CyPhyClasses.DesignContainer.AttributesClass.ContainerType_enum.Alternative)
                {
                    subRootContainerType = new Alternative();
                }
                else if (subDE is CyPhy.DesignContainer &&
                         (subDE as CyPhy.DesignContainer).Attributes.ContainerType == CyPhyClasses.DesignContainer.AttributesClass.ContainerType_enum.Optional)
                {
                    subRootContainerType = new Optional();
                }
                else if (subDE is CyPhy.ComponentAssembly)
                {
                    subRootContainerType = new Compound();
                }
                else
                {
                    continue;
                }

                subRootContainerType.Name = subDE.Name;
                subRootContainerType.id   = GetOrSetID(subDE);

                rootContainerType.Containers.AddItem(subRootContainerType);

                UpdateSubContainers(new DesignPrimitivesWrapper(subDE), subRootContainerType, rootDesignModel, cyphyPorts2PortInstances, containerPorts, connectionsToBeVisited, simpleFormulas, valueInstances, innerStructuralPortInstances, containerStructuralPortInstances);
            }

            #region Elements on the current container

            // Convert simple formulas
            foreach (var sf in de.SimpleFormulas)
            {
                var containerCalculation = new ContainerCalculation();
                rootContainerType.ContainerValues.AddItem(containerCalculation);
                simpleFormulas[sf] = containerCalculation;
            }

            // Add container ports (from ports)
            foreach (var port in de.Ports)
            {
                if (containerPorts.ContainsKey(port))
                {
                    continue;
                }

                var pi = new ContainerPort();
                containerPorts.Add(port, pi);
                rootContainerType.ContainerPorts.AddItem(pi);
            }

            // Convert parameters
            foreach (var p in de.Parameters)
            {
                var containerNamedValue = new ContainerNamedValue {
                    IsParameter = true, Value = p.Attributes.Value
                };
                rootContainerType.ContainerValues.AddItem(containerNamedValue);
                if (valueInstances.ContainsKey(p))
                {
                    continue;
                }

                valueInstances.Add(p, containerNamedValue);
            }

            // Convert properties
            foreach (var p in de.Properties)
            {
                var containerNamedValue = new ContainerNamedValue {
                    IsParameter = false, Value = p.Attributes.Value
                };
                rootContainerType.ContainerValues.AddItem(containerNamedValue);
                if (valueInstances.ContainsKey(p))
                {
                    continue;
                }

                valueInstances.Add(p, containerNamedValue);
            }

            #endregion
        }