// BE CAREFUL!  RUNS ON A SEPARATE THREAD!
        public NodeDriver ReadConfiguration()
        {
            var driver = NodeDriver.BuildWith(
                new FieldGuid(SnapDriver.TWITTER_DRIVER_ID),    // TypeId
                new FieldString(string.Empty),                  // Address
                new FieldBase64(string.Empty),                  // Configuration
                new FieldString(Resources.Strings.DriverName)); // DriverName

            driver = driver.SetRunning(new FieldBool(Running)); // status only
            if (Running)                                        // Running property is threadsafe
            {
                var devicesMutable = new Collection <NodeDevice>();
                driver = driver.NodeDeviceChildren.Append(
                    new ReadOnlyCollection <NodeDevice>(devicesMutable));
            }
            return(driver);
        }
Example #2
0
        // BE CAREFUL!  RUNS ON A SEPARATE THREAD!
        public NodeDriver ReadConfiguration()
        {
            var driver = NodeDriver.BuildWith(
                new FieldGuid(SnapDriver.GAMECONTROLLERS_DRIVER_ID), // TypeId
                new FieldString(string.Empty),                       // Address
                new FieldBase64(string.Empty),                       // Configuration
                new FieldString(Resources.Strings.DriverName));      // DriverName

            driver = driver.SetRunning(new FieldBool(Running));      // status only
            if (Running && this.directInput != null)                 // Running property is threadsafe
            {
                var devicesMutable  = new Collection <NodeDevice>();
                var joystickDevices = getJoystickCompatibleDevices();
                foreach (var joystickDevice in joystickDevices)
                {
                    var dev = JoystickDevice.StaticBuild(this.directInput, joystickDevice);
                    devicesMutable.Add(dev);
                }
                driver = driver.NodeDeviceChildren.Append(
                    new ReadOnlyCollection <NodeDevice>(devicesMutable));
            }
            return(driver);
        }
Example #3
0
        // BE CAREFUL!  RUNS ON A SEPARATE THREAD!
        public NodeDriver ReadConfiguration()
        {
            var driver = NodeDriver.BuildWith(
                new FieldGuid(SnapDriver.PHIDGETS_DRIVER_ID),   // TypeId
                new FieldString(string.Empty),                  // Address
                new FieldBase64(string.Empty),                  // Configuration
                new FieldString(Resources.Strings.DriverName)); // DriverName

            driver = driver.SetRunning(new FieldBool(Running)); // status only
            if (Running)                                        // Running property is threadsafe
            {
                var devicesMutable = new Collection <NodeDevice>();
                // Phidgets claims to be a threadsafe library,
                // and manager is readonly
                foreach (Phidget phidget in manager.Devices)
                {
                    var dev = new PhidgetsDevice(phidget);
                    devicesMutable.Add(dev.Device);
                }
                driver = driver.NodeDeviceChildren.Append(
                    new ReadOnlyCollection <NodeDevice>(devicesMutable));
            }
            return(driver);
        }
        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);
        }