public SignalChooser(INodeWrapper nodeItem, NodeSignalIn initialSignalIn, double maxWidth, TextAlignment textAlignment)
 {
     NodeItem      = nodeItem;
     SignalIn      = initialSignalIn;
     MaxWidth      = maxWidth;
     TextAlignment = textAlignment;
 }
Exemple #2
0
 public SignalInItem(ISolutionItem parent, NodeSignalIn signalIn)
     : base(parent, string.Empty)
 {
     ContextMenu = extensionService.Sort(contextMenu);
     SignalIn    = signalIn;
     runtimeService.SignalChanged += new SignalChangedHandler(runtimeService_SignalChanged);
 }
        protected InstructionLDAbstractCounter(IEditorItem parent, NodeInstruction instruction, FieldInstructionType instructionType)
            : base(parent, instructionType)
        {
            if (instruction == null)
            {
                var newInstruction = NodeInstruction.BuildWith(InstructionType);
                // Output signal: Named boolean - the counter done signal
                newInstruction = newInstruction.NodeSignalChildren.Append(
                    NodeSignal.BuildWith(
                        new FieldSignalName(Resources.Strings.LD_Snap_Ctr_DefaultName),
                        new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                        new FieldBool(false), // not forced
                        FieldConstant.Constants.BOOL.LOW)
                    );
                newInstruction = newInstruction.NodeSignalChildren.Append( // memory of counter value
                    NodeSignal.BuildWith(
                        new FieldSignalName(Resources.Strings.LD_Snap_Ctr_DefaultName + SEPARATOR + Resources.Strings.LD_Snap_Ctr_CountName),
                        new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                        new FieldBool(false), // not forced
                        FieldConstant.Constants.NUMBER.ZERO)
                    );
                newInstruction = newInstruction.NodeSignalChildren.Append( // previous rung-in condition
                    NodeSignal.BuildWith(
                        new FieldSignalName(Resources.Strings.LD_Snap_Ctr_DefaultName + SEPARATOR + Resources.Strings.LD_Snap_Ctr_OneshotStateName),
                        new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                        new FieldBool(false), // not forced
                        FieldConstant.Constants.BOOL.LOW)
                    );
                // Input signal: setpoint
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                        new FieldConstant(FieldDataType.DataTypeEnum.NUMBER, 0)));
                // Input signal: reset
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                        new FieldConstant(FieldDataType.DataTypeEnum.BOOL, false)));
                Instruction = newInstruction;
            }
            else
            {
                if (instruction.InstructionType != InstructionType)
                {
                    throw new InvalidOperationException("Tried to instantiate a Counter but passed a different instruction type.");
                }
                Instruction = instruction;
            }

            // Build the context menu
            if (extensionService != null)
            {
                ContextMenu        = extensionService.SortAndJoin(ldInstructionContextMenu, m_staticMenuItemSeparator, contextMenu);
                ContextMenuEnabled = true;
            }
        }
Exemple #4
0
 public NodeSignalIn SignalDialog(INodeWrapper requester, NodeSignalIn defaultSignalIn)
 {
     if (DisconnectDialog(requester))
     {
         return(signalChooserDialog.ShowDialog(requester, defaultSignalIn));
     }
     else
     {
         return(defaultSignalIn);
     }
 }
Exemple #5
0
        public bool TryGetNumericAddressBySignal(NodeSignalIn signalIn, out Byte?numericAddress)
        {
            var signalId = Guid.Parse(signalIn.SignalId.ToString());

            if (this.m_numericAddressBySignal.ContainsKey(signalId))
            {
                numericAddress = this.m_numericAddressBySignal[signalId];
                return(true);
            }
            numericAddress = null;
            return(false);
        }
        protected InstructionLDMathBase(IEditorItem parent, FieldInstructionType instructionType, NodeInstruction instruction,
                                        string defaultName, bool factory, string instructionName, string firstSignalName, string secondSignalName)
            : base(parent, instructionType)
        {
            if (factory)
            {
                return;
            }

            this.InstructionName  = instructionName;
            this.FirstSignalName  = firstSignalName;
            this.SecondSignalName = secondSignalName;

            if (instruction == null)
            {
                var newInstruction = NodeInstruction.BuildWith(InstructionType);
                // Output signal: Named number - result
                newInstruction = newInstruction.NodeSignalChildren.Append(
                    NodeSignal.BuildWith(
                        new FieldSignalName(defaultName),
                        new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                        new FieldBool(false), // not forced
                        FieldConstant.Constants.NUMBER.ZERO)
                    );
                // Input signal: First
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                        new FieldConstant(FieldDataType.DataTypeEnum.NUMBER, 0)));
                // Input signal: Second
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                        new FieldConstant(FieldDataType.DataTypeEnum.NUMBER, 0)));
                Instruction = newInstruction;
            }
            else
            {
                if (instruction.InstructionType != InstructionType)
                {
                    throw new InvalidOperationException("Tried to instantiate a different instruction type.");
                }
                Instruction = instruction;
            }

            // Build the context menu
            if (extensionService != null)
            {
                ContextMenu        = extensionService.SortAndJoin(ldInstructionContextMenu, m_staticMenuItemSeparator, contextMenu);
                ContextMenuEnabled = true;
            }
        }
Exemple #7
0
        private void readTextBox()
        {
            var    tb           = textBox;
            var    selectedText = tb.SelectedText;
            string unselectedText;

            if (selectedText.Length > 0)
            {
                unselectedText = tb.Text.Replace(selectedText, string.Empty);
            }
            else
            {
                unselectedText = tb.Text;
            }

            if (unselectedText.Length > 0)
            {
                // check to see if the value entered is a valid literal
                object parseResult;
                if (SignalIn.CompatibleTypes.TryParse(unselectedText, out parseResult))
                {
                    tb.Text          = parseResult.ToString();
                    m_editedSignalIn = NodeSignalIn.BuildWith(
                        SignalIn.DataType, SignalIn.CompatibleTypes,
                        new FieldConstant(SignalIn.CompatibleTypes.DataType, parseResult));
                }
                else
                {
                    Tuple <string, NodeSignal> closestSignal = findClosestMatchingSignal(unselectedText);
                    if (closestSignal != null)
                    {
                        var signalName = closestSignal.Item1;
                        var nSignal    = closestSignal.Item2;
                        tb.Text            = signalName;
                        tb.SelectionStart  = tb.Text.ToLower().IndexOf(unselectedText.ToLower()) + unselectedText.Length;
                        tb.SelectionLength = tb.Text.Length - unselectedText.Length;
                        m_editedSignalIn   = NodeSignalIn.BuildWith(
                            SignalIn.DataType, SignalIn.CompatibleTypes,
                            closestSignal.Item2.SignalId);
                    }
                    else
                    {
                        m_editedSignalIn = SignalIn;
                    }
                }
            }
            else
            {
                m_editedSignalIn = SignalIn;
            }
        }
Exemple #8
0
        public byte GetNumericSignalAddress(NodeSignalIn signalIn)
        {
            if (signalIn == null)
            {
                throw new ArgumentNullException("signalIn");
            }
            Byte?result;

            if (!this.TryGetNumericAddressBySignal(signalIn, out result))
            {
                throw new Exception("Signal not found.");
            }
            return(result.Value);
        }
Exemple #9
0
        public Int16 GetBooleanSignalAddress(NodeSignalIn signalIn)
        {
            if (signalIn == null)
            {
                throw new ArgumentNullException("signalIn");
            }
            Int16 result;

            if (!this.TryGetBooleanAddressBySignal(signalIn, out result))
            {
                throw new Exception("Signal not found.");
            }
            return(result);
        }
Exemple #10
0
        public SignalDescriptionDisplay(IInstructionItem instructionNode, NodeSignalIn signalIn, double maxWidth, double maxHeight, TextAlignment textAlignment)
            : base(string.Empty, maxWidth, maxHeight, textAlignment, false)
        {
            if (instructionNode == null)
            {
                throw new ArgumentNullException("instructionNode");
            }

            m_instructionNode = instructionNode;
            SignalIn          = signalIn;
            if (runtimeService != null)
            {
                runtimeService.SignalChanged += new SignalChangedHandler(runtimeService_SignalChanged);
            }
        }
Exemple #11
0
        protected InstructionLDStringContains(IEditorItem parent, NodeInstruction instruction)
            : base(parent, m_InstructionType)
        {
            if (instruction == null)
            {
                var newInstruction = NodeInstruction.BuildWith(InstructionType);
                // Output signal: Named boolean - result of the comparison
                newInstruction = newInstruction.NodeSignalChildren.Append(
                    NodeSignal.BuildWith(
                        new FieldSignalName(Resources.Strings.LD_Snap_StringContains_DefaultName),
                        new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                        new FieldBool(false), // not forced
                        FieldConstant.Constants.BOOL.LOW)
                    );
                // Input signal: string to search
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.STRING),
                        new FieldConstant(FieldDataType.DataTypeEnum.STRING, string.Empty)));
                // Input signal: string to find
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.STRING),
                        new FieldConstant(FieldDataType.DataTypeEnum.STRING, string.Empty)));
                // Input signal: case sensitive
                newInstruction = newInstruction.NodeSignalInChildren.Append(
                    NodeSignalIn.BuildWith(
                        new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                        new FieldConstant(FieldDataType.DataTypeEnum.BOOL, false)));
                Instruction = newInstruction;
            }
            else
            {
                if (instruction.InstructionType != InstructionType)
                {
                    throw new InvalidOperationException("Tried to instantiate a StringContains but passed a different instruction type.");
                }
                Instruction = instruction;
            }

            // Build the context menu
            if (extensionService != null)
            {
                ContextMenu        = extensionService.SortAndJoin(ldInstructionContextMenu, m_staticMenuItemSeparator, contextMenu);
                ContextMenuEnabled = true;
            }
        }
Exemple #12
0
        private void setNumericAddressToValueWithAddInstruction(
            byte address, decimal value,
            SignalTable signalTable, List <CompiledInstruction> compiledInstructions)
        {
            var compiledOperand1Signal = new CompiledNumericSignal(value);
            var operand2SignalIn       = NodeSignalIn.BuildWith(
                new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                new FieldConstant(FieldDataType.DataTypeEnum.NUMBER, 0M));
            var compiledOperand2Signal = numericSignal(operand2SignalIn, signalTable);

            var compiledResultSignal = new CompiledNumericSignal(false, address, signalTable.NumericAddressBits);
            var add = new CompiledInstruction(
                0x78, // add
                7,
                compiledOperand1Signal, compiledOperand2Signal, compiledResultSignal);

            compiledInstructions.Add(add);
            compiledInstructions.Add(new CompiledInstruction(0x03, 3)); // series instruction end (end of rung)
        }
        /// <summary>
        /// Displays the Dialog as modal
        /// </summary>
        /// <returns>Signal ID</returns>
        public NodeSignalIn ShowDialog(INodeWrapper requester, NodeSignalIn originalSignalIn)
        {
            m_dataTypefilter = originalSignalIn.CompatibleTypes.DataType;
            OriginalSignalIn = originalSignalIn;
            setDefaultValues();
            NodeItem = requester;
            Window dlg = new SignalChooserDialogView();

            dlg.Owner       = mainWindowExport.Value;
            dlg.DataContext = this;
            dlg.ShowDialog();
            if (SignalSelected)
            {
                return(NodeSignalIn.BuildWith(OriginalSignalIn.DataType, OriginalSignalIn.CompatibleTypes, SignalId));
            }
            else
            {
                return(NodeSignalIn.BuildWith(OriginalSignalIn.DataType, OriginalSignalIn.CompatibleTypes, Literal));
            }
        }
Exemple #14
0
        public bool TryGetBooleanAddressBySignal(NodeSignalIn signalIn, out Int16 booleanAddress)
        {
            if (signalIn == null)
            {
                throw new ArgumentNullException("signalIn");
            }
            if (signalIn.SignalId == null)
            {
                throw new ArgumentNullException("signalIn.SignalId");
            }
            var signalId = Guid.Parse(signalIn.SignalId.ToString());

            if (this.m_booleanAddressBySignal.ContainsKey(signalId))
            {
                booleanAddress = this.m_booleanAddressBySignal[signalId];
                return(true);
            }
            booleanAddress = -1;
            return(false);
        }
Exemple #15
0
 private CompiledNumericSignal numericSignal(NodeSignalIn signalIn, SignalTable signalTable)
 {
     if (signalIn.SignalId != null)
     {
         var signalAddress  = signalTable.GetNumericSignalAddress(signalIn);
         var compiledSignal = new CompiledNumericSignal(true, signalAddress, signalTable.NumericAddressBits);
         return(compiledSignal);
     }
     else if (signalIn.Literal != null)
     {
         var literal = signalIn.Literal;
         if (literal.DataType != FieldDataType.DataTypeEnum.NUMBER)
         {
             throw new Exception("Signal should be a NUMBER.");
         }
         var literalValue = Convert.ToDecimal(literal.Value);
         return(new CompiledNumericSignal(literalValue));
     }
     else
     {
         throw new Exception("Expecting one of SignalIn.SignalId or SignalIn.Literal to be non-null");
     }
 }
Exemple #16
0
 private CompiledBooleanSignal booleanSignal(NodeSignalIn signalIn, SignalTable signalTable)
 {
     if (signalIn.SignalId != null)
     {
         var signalAddress  = signalTable.GetBooleanSignalAddress(signalIn);
         var compiledSignal = new CompiledBooleanSignal(true, signalAddress, signalTable.BooleanAddressBits);
         return(compiledSignal);
     }
     else if (signalIn.Literal != null)
     {
         var literal = signalIn.Literal;
         if (literal.DataType != FieldDataType.DataTypeEnum.BOOL)
         {
             throw new Exception("Signal should be a BOOL.");
         }
         var literalValue = (bool)(literal.Value);
         return(new CompiledBooleanSignal(literalValue));
     }
     else
     {
         throw new Exception("Expecting one of SignalIn.SignalId or SignalIn.Literal to be non-null");
     }
 }
Exemple #17
0
 protected InstructionLDAbstractContact(IEditorItem parent, FieldInstructionType instructionType, NodeInstruction instruction)
     : base(parent, instructionType)
 {
     if (instruction == null)
     {
         var newInstruction = NodeInstruction.BuildWith(InstructionType);
         // Input signal: (coil that we're a contact off) default to always false
         newInstruction = newInstruction.NodeSignalInChildren.Append(
             NodeSignalIn.BuildWith(
                 new FieldDataType(FieldDataType.DataTypeEnum.BOOL),
                 new FieldConstant(FieldDataType.DataTypeEnum.BOOL, false)));
         Instruction = newInstruction;
     }
     else
     {
         if (instructionType != instruction.InstructionType)
         {
             throw new ArgumentOutOfRangeException("Tried to instantiate a contact of type " +
                                                   instructionType.ToString() + " with an instruction of a different type.");
         }
         Instruction = instruction;
     }
 }
Exemple #18
0
        public CompiledProgram Compile(NodeRuntimeApplication rta, SignalTable signalTable)
        {
            if (rta == null)
            {
                throw new ArgumentNullException("rta");
            }
            if (signalTable == null)
            {
                throw new ArgumentNullException("signalTable");
            }
            var runtimeId = Guid.Parse(rta.RuntimeId.ToString());
            var versionId = Guid.Parse(rta.ID.ToString());

            var logic = rta.Logic;
            var compiledInstructions = new List <CompiledInstruction>();

            var discreteInputs = rta.DeviceConfiguration.GetChildrenRecursive()
                                 .Select(x => x.Value)
                                 .OfType <NodeDiscreteInput>();

            foreach (var discreteInput in discreteInputs)
            {
                if (discreteInput.Forced.BoolValue)
                {
                    var address = Int16.Parse(discreteInput.Address.ToString());
                    this.driveSignalWithCoilToConstantValue(
                        address, discreteInput.ForcedValue.BoolValue,
                        signalTable, compiledInstructions);
                }
            }

            var analogInputs = rta.DeviceConfiguration.GetChildrenRecursive()
                               .Select(x => x.Value)
                               .OfType <NodeAnalogInput>();

            foreach (var analogInput in analogInputs)
            {
                if (analogInput.Forced.BoolValue)
                {
                    if (analogInput.ForcedValue.DataType != FieldDataType.DataTypeEnum.NUMBER)
                    {
                        throw new Exception("Data type of forced value must be NUMBER.");
                    }
                    var address = byte.Parse(analogInput.Address.ToString());
                    this.setNumericAddressToValueWithAddInstruction(
                        address, (decimal)(analogInput.ForcedValue.Value),
                        signalTable, compiledInstructions);
                }
            }

            compiledInstructions.AddRange(compilePageCollection(logic, signalTable));

            // discrete output literals
            var discreteOutputs = rta.DeviceConfiguration.GetChildrenRecursive()
                                  .Select(x => x.Value)
                                  .OfType <NodeDiscreteOutput>();

            foreach (var discreteOutput in discreteOutputs)
            {
                var literal = discreteOutput.SignalIn.Literal;
                var address = Int16.Parse(discreteOutput.Address.ToString());
                if (literal != null)
                {
                    // insert a new rung to set the literal value
                    if (literal.DataType != FieldDataType.DataTypeEnum.BOOL)
                    {
                        throw new Exception("Data type must be BOOL.");
                    }
                    var literalValue = (bool)(literal.Value);

                    this.driveSignalWithCoilToConstantValue(
                        address, literalValue,
                        signalTable, compiledInstructions);
                }
            }

            // boolean jumpers (from internal signals to outputs)
            foreach (var booleanJumper in signalTable.BooleanJumpers)
            {
                var compiledContactSignal = new CompiledBooleanSignal(true, booleanJumper.Item1, signalTable.BooleanAddressBits);
                var contact = new CompiledInstruction(
                    0x01, // NO contact
                    3,
                    compiledContactSignal);
                compiledInstructions.Add(contact);

                var compiledDiscreteOutputSignal = new CompiledBooleanSignal(false, booleanJumper.Item2, signalTable.BooleanAddressBits);
                var coil = new CompiledInstruction(
                    0x00, // coil
                    3,
                    compiledDiscreteOutputSignal);
                compiledInstructions.Add(coil);
                compiledInstructions.Add(new CompiledInstruction(0x03, 3)); // series instruction end (end of rung)
            }

            // boolean forces
            foreach (var discreteOutput in discreteOutputs)
            {
                var address = Int16.Parse(discreteOutput.Address.ToString());
                if (discreteOutput.Forced.BoolValue)
                {
                    this.driveSignalWithCoilToConstantValue(
                        address, discreteOutput.ForcedValue.BoolValue,
                        signalTable, compiledInstructions);
                }
            }

            // analog output literals
            var analogOutputs = rta.DeviceConfiguration.GetChildrenRecursive()
                                .Select(x => x.Value)
                                .OfType <NodeAnalogOutput>();

            foreach (var analogOutput in analogOutputs)
            {
                var literal = analogOutput.SignalIn.Literal;
                var address = Byte.Parse(analogOutput.Address.ToString());
                if (literal != null)
                {
                    // insert a new rung to set the literal value
                    if (literal.DataType != FieldDataType.DataTypeEnum.NUMBER)
                    {
                        throw new Exception("Data type of literal must be NUMBER.");
                    }
                    this.setNumericAddressToValueWithAddInstruction(
                        address, (decimal)(literal.Value),
                        signalTable, compiledInstructions);
                }
            }

            // analog jumpers (from internal signals to outputs)
            foreach (var numericJumper in signalTable.NumericJumpers)
            {
                var sourceAddress      = numericJumper.Item1;
                var destinationAddress = numericJumper.Item2;

                var compiledOperand1Signal = new CompiledNumericSignal(true, sourceAddress, signalTable.NumericAddressBits);
                var operand2SignalIn       = NodeSignalIn.BuildWith(
                    new FieldDataType(FieldDataType.DataTypeEnum.NUMBER),
                    new FieldConstant(FieldDataType.DataTypeEnum.NUMBER, 0M));
                var compiledOperand2Signal = numericSignal(operand2SignalIn, signalTable);

                var compiledResultSignal = new CompiledNumericSignal(false, destinationAddress, signalTable.NumericAddressBits);
                var add = new CompiledInstruction(
                    0x78, // add
                    7,
                    compiledOperand1Signal, compiledOperand2Signal, compiledResultSignal);
                compiledInstructions.Add(add);
                compiledInstructions.Add(new CompiledInstruction(0x03, 3)); // series instruction end (end of rung)
            }

            // analog forces
            foreach (var analogOutput in analogOutputs)
            {
                var address = Byte.Parse(analogOutput.Address.ToString());
                if (analogOutput.Forced.BoolValue)
                {
                    if (analogOutput.ForcedValue.DataType != FieldDataType.DataTypeEnum.NUMBER)
                    {
                        throw new Exception("Data type of forced value must be NUMBER.");
                    }
                    this.setNumericAddressToValueWithAddInstruction(
                        address, (decimal)(analogOutput.ForcedValue.Value),
                        signalTable, compiledInstructions);
                }
            }

            // end of program
            compiledInstructions.Add(new CompiledInstruction(PROGRAM_END, 8));

            var packedInstructions = bitPackInstructions(compiledInstructions);

            return(new CompiledProgram(
                       runtimeId,
                       versionId,
                       signalTable.BooleanAddressBits,
                       signalTable.NumericAddressBits,
                       packedInstructions));
        }
 public static void SetControlFillColorBySignalIn(DependencyObject target, NodeSignalIn value)
 {
     target.SetValue(SignalValueBehavior.ControlFillColorBySignalInProperty, value);
 }