Пример #1
0
        /**
         * The Device Configuration Response
         *
         * @param status {@link byte} Status
         * @param power {@link short} Power
         * @param pathLossExponent {@link ushort} Path Loss Exponent
         * @param calculationPeriod {@link ushort} Calculation Period
         * @param numberRSSIMeasurements {@link byte} Number RSSI Measurements
         * @param reportingPeriod {@link ushort} Reporting Period
         * @return the Task<CommandResult> command result Task
         */
        public Task <CommandResult> DeviceConfigurationResponse(byte status, short power, ushort pathLossExponent, ushort calculationPeriod, byte numberRSSIMeasurements, ushort reportingPeriod)
        {
            DeviceConfigurationResponse command = new DeviceConfigurationResponse();

            // Set the fields
            command.Status                 = status;
            command.Power                  = power;
            command.PathLossExponent       = pathLossExponent;
            command.CalculationPeriod      = calculationPeriod;
            command.NumberRSSIMeasurements = numberRSSIMeasurements;
            command.ReportingPeriod        = reportingPeriod;

            return(Send(command));
        }
        internal static NodeDeviceConfiguration MergeDeviceConfiguration(
            DeviceConfigurationResponse deviceConfigurationResponse,
            NodeDeviceConfiguration existingDeviceConfiguration = null)
        {
            var driversMutable = new Collection <NodeDriver>();
            var nodeDriver     = NodeDriver.BuildWith(
                new FieldGuid(SnapDriver.ARDUINO_LOCAL_IO_DRIVER_TYPE_ID), // TypeId
                new FieldString(string.Empty),                             // Address
                FieldBase64.Encode(string.Empty),                          // Configuration
                new FieldString(deviceConfigurationResponse.DeviceName));

            var devicesMutable = new Collection <NodeDevice>();

            // Just creating 4 "virtual" devices under the Arduino Local I/O driver
            // so that it looks organized in the Solution Explorer
            var discreteInputsDevice = NodeDevice.BuildWith(
                new FieldIdentifier("DiscreteInputs"),    // Code
                new FieldGuid(DISCRETE_INPUTS_DEVICE_ID), // TypeId
                new FieldString(),                        // Address
                new FieldBase64(string.Empty),            // Configuration
                new FieldDeviceName("Discrete Inputs"));
            var discreteOutputsDevice = NodeDevice.BuildWith(
                new FieldIdentifier("DiscreteOutputs"),    // Code
                new FieldGuid(DISCRETE_OUTPUTS_DEVICE_ID), // TypeId
                new FieldString(),                         // Address
                new FieldBase64(string.Empty),             // Configuration
                new FieldDeviceName("Discrete Outputs"));
            var analogInputsDevice = NodeDevice.BuildWith(
                new FieldIdentifier("AnalogInputs"),    // Code
                new FieldGuid(ANALOG_INPUTS_DEVICE_ID), // TypeId
                new FieldString(),                      // Address
                new FieldBase64(string.Empty),          // Configuration
                new FieldDeviceName("Analog Inputs"));
            var analogOutputsDevice = NodeDevice.BuildWith(
                new FieldIdentifier("AnalogOutputs"),    // Code
                new FieldGuid(ANALOG_OUTPUTS_DEVICE_ID), // TypeId
                new FieldString(),                       // Address
                new FieldBase64(string.Empty),           // Configuration
                new FieldDeviceName("Analog Outputs"));

            var discreteInputsMutable  = new Collection <NodeDiscreteInput>();
            var discreteOutputsMutable = new Collection <NodeDiscreteOutput>();
            var analogInputsMutable    = new Collection <NodeAnalogInput>();
            var analogOutputsMutable   = new Collection <NodeAnalogOutput>();

            foreach (var ioSignal in deviceConfigurationResponse.IOSignals)
            {
                switch (ioSignal.Type)
                {
                case DeviceConfigurationResponse.IOSignalType.DiscreteInput:
                    var newDiscreteInput = NodeDiscreteInput.BuildWith(
                        new FieldIdentifier(ioSignal.Name),
                        new FieldString(ioSignal.Address),
                        new FieldSignalName(ioSignal.Name));
                    if (existingDeviceConfiguration != null)
                    {
                        var existingDiscreteInput = existingDeviceConfiguration.GetChildrenRecursive()
                                                    .Select(x => x.Value as NodeDiscreteInput)
                                                    .Where(x => x != null)
                                                    .Where(x => x.Code.ToString() == ioSignal.Name)
                                                    .SingleOrDefault(x => x.Address.ToString() == ioSignal.Address);
                        if (existingDiscreteInput != null)
                        {
                            newDiscreteInput = newDiscreteInput
                                               .SetSignal(existingDiscreteInput.Signal);
                        }
                    }
                    discreteInputsMutable.Add(newDiscreteInput);
                    break;

                case DeviceConfigurationResponse.IOSignalType.DiscreteOutput:
                    var newDiscreteOutput = NodeDiscreteOutput.BuildWith(
                        new FieldIdentifier(ioSignal.Name),
                        new FieldString(ioSignal.Address),
                        new FieldSignalName(ioSignal.Name));
                    if (existingDeviceConfiguration != null)
                    {
                        var existingDiscreteOutput = existingDeviceConfiguration.GetChildrenRecursive()
                                                     .Select(x => x.Value as NodeDiscreteOutput)
                                                     .Where(x => x != null)
                                                     .Where(x => x.Code.ToString() == ioSignal.Name)
                                                     .SingleOrDefault(x => x.Address.ToString() == ioSignal.Address);
                        if (existingDiscreteOutput != null)
                        {
                            newDiscreteOutput = newDiscreteOutput
                                                .SetSignalIn(existingDiscreteOutput.SignalIn);
                        }
                    }
                    discreteOutputsMutable.Add(newDiscreteOutput);
                    break;

                case DeviceConfigurationResponse.IOSignalType.AnalogInput:
                    var newAnalogInput = NodeAnalogInput.BuildWith(
                        new FieldIdentifier(ioSignal.Name),
                        new FieldString(ioSignal.Address),
                        new FieldSignalName(ioSignal.Name));
                    if (existingDeviceConfiguration != null)
                    {
                        var existingAnalogInput = existingDeviceConfiguration.GetChildrenRecursive()
                                                  .Select(x => x.Value as NodeAnalogInput)
                                                  .Where(x => x != null)
                                                  .Where(x => x.Code.ToString() == ioSignal.Name)
                                                  .SingleOrDefault(x => x.Address.ToString() == ioSignal.Address);
                        if (existingAnalogInput != null)
                        {
                            newAnalogInput = newAnalogInput
                                             .SetSignal(existingAnalogInput.Signal);
                        }
                    }
                    analogInputsMutable.Add(newAnalogInput);
                    break;

                case DeviceConfigurationResponse.IOSignalType.AnalogOutput:
                    var newAnalogOutput = NodeAnalogOutput.BuildWith(
                        new FieldIdentifier(ioSignal.Name),
                        new FieldString(ioSignal.Address),
                        new FieldSignalName(ioSignal.Name));
                    if (existingDeviceConfiguration != null)
                    {
                        var existingAnalogOutput = existingDeviceConfiguration.GetChildrenRecursive()
                                                   .Select(x => x.Value as NodeAnalogOutput)
                                                   .Where(x => x != null)
                                                   .Where(x => x.Code.ToString() == ioSignal.Name)
                                                   .SingleOrDefault(x => x.Address.ToString() == ioSignal.Address);
                        if (existingAnalogOutput != null)
                        {
                            newAnalogOutput = newAnalogOutput
                                              .SetSignalIn(existingAnalogOutput.SignalIn);
                        }
                    }
                    analogOutputsMutable.Add(newAnalogOutput);
                    break;
                }
            }

            discreteInputsDevice = discreteInputsDevice.NodeDiscreteInputChildren.Append(
                new ReadOnlyCollection <NodeDiscreteInput>(discreteInputsMutable));
            discreteOutputsDevice = discreteOutputsDevice.NodeDiscreteOutputChildren.Append(
                new ReadOnlyCollection <NodeDiscreteOutput>(discreteOutputsMutable));
            analogInputsDevice = analogInputsDevice.NodeAnalogInputChildren.Append(
                new ReadOnlyCollection <NodeAnalogInput>(analogInputsMutable));
            analogOutputsDevice = analogOutputsDevice.NodeAnalogOutputChildren.Append(
                new ReadOnlyCollection <NodeAnalogOutput>(analogOutputsMutable));

            devicesMutable.Add(discreteInputsDevice);
            devicesMutable.Add(discreteOutputsDevice);
            devicesMutable.Add(analogInputsDevice);
            devicesMutable.Add(analogOutputsDevice);
            nodeDriver = nodeDriver.NodeDeviceChildren.Append(
                new ReadOnlyCollection <NodeDevice>(devicesMutable));

            driversMutable.Add(nodeDriver);
            var nodeDeviceConfiguration = NodeDeviceConfiguration.BuildWith(
                new ReadOnlyCollection <NodeDriver>(driversMutable));

            return(nodeDeviceConfiguration);
        }