private IDevice CreateDeviceInternal(ISerialNumber serialNumber, EProtocolVersion protocolVersion, bool connected)
        {
            IDevice ret = this[serialNumber];

            if (ret == null)
            {
                var connectionState = EConnectionState.Disconnected;
                if (connected)
                {
                    connectionState = EConnectionState.Connected;
                }
                var connection = new RemoteConnection(serialNumber.rawSerial, connectionState);

                var protocol = Protocol.FindProtocolFromVersion(protocolVersion);

                var definition = deviceFactory.GetDeviceDefinition(serialNumber);

                ret = definition.CreateDevice(ion, serialNumber, connection, protocol);

                ret.onDeviceEvent += OnDeviceEvent;
            }

            if (ret == null)
            {
                var msg = BuildErrorHeader(serialNumber, protocolVersion) +
                          ": Please ensure that the serial number is resgistered in ION.Core.Devices.Devices.xml";
                Log.C(this, msg);
                throw new Exception(msg);
            }
            //ret.isConnected = connected;
            return(ret);
        }
Exemple #2
0
        // Implemented for IConnectionManager
        public IConnection CreateConnection(string address, EProtocolVersion protocolVersion)
        {
            var device = bm.Adapter.GetRemoteDevice(address);

            if (device == null)
            {
                var msg = "Cannot create connection: Failed to find device for address";
                Log.E(this, msg);
                throw new Exception(msg);
            }

            switch (protocolVersion)
            {
            case EProtocolVersion.Classic: {
                return(new ClassicConnection(device));
            }                             // EProtocolVersion.Classic

            case EProtocolVersion.V1: {
                return(new LeConnection(this, device));
            }                             // EProtocolVersion.V1

            case EProtocolVersion.V4: {
                return(new RigadoConnection(this, device));
            }                             // EProtocolVersion.V4

            default: {
                throw new Exception("Cannot create connection for protocol version: " + protocolVersion);
            }
            }
        }
        // Overridden from IDeviceManager
        public IDevice CreateDevice(ISerialNumber serialNumber, EProtocolVersion protocolVersion, bool connected)
        {
            var ret = CreateDeviceInternal(serialNumber, protocolVersion, connected);

            // The register proved superfluous. Consider merging this functions with the internal one.
            // Register(ret);
            return(ret);
        }
        /// <summary>
        /// Queries the protocol that matches the given version. If not protocol is
        /// found, we will return null.
        /// </summary>
        /// <returns>The protocol from version.</returns>
        /// <param name="version">Version.</param>
        public static IGaugeProtocol FindProtocolFromVersion(EProtocolVersion version)
        {
            // Could be made more efficient if the protocol count keeps increasing.
            foreach (IGaugeProtocol protocol in PROTOCOLS)
            {
                if (protocol.version == version)
                {
                    return(protocol);
                }
            }

            return(null);
        }
        // Implemented from IConnectionFactory
        public IConnection CreateConnection(string address, EProtocolVersion protocolVersion)
        {
            lock (centralManager) {
                var uuid = CBUUID.FromString(address);
                if (connectionLookup.ContainsKey(uuid))
                {
                    return(connectionLookup[uuid]);
                }

                var peripheral = centralManager.RetrievePeripheralsWithIdentifiers(new NSUuid(address))[0];
                if (peripheral == null)
                {
                    throw new Exception("Cannot create connection to " + address + ": the address is not valid");
                }

                if (!peripheral.Name.IsValidSerialNumber())
                {
                    throw new Exception("Cannot create connection: peripheral does not have a valid serial number.");
                }

                var serialNumber = peripheral.Name.ParseSerialNumber();

                IConnection ret = null;

                // TODO [email protected]: This is awful. Make an actual parser to create real devices.
                if (serialNumber.rawSerial.StartsWith("S", StringComparison.Ordinal))
                {
                    ret = new IosRigadoConnection(this, peripheral);
                }
                else
                {
                    ret = new IosLeConnection(this, peripheral);
                }

                connectionLookup[uuid] = ret;

                return(ret);
            }
        }
 /// <summary>
 /// Builds a string that can be used as an error header for the logger.
 /// </summary>
 /// <returns>The error header.</returns>
 /// <param name="serialNumber">Serial number.</param>
 /// <param name="protocol">Protocol.</param>
 private string BuildErrorHeader(ISerialNumber serialNumber, EProtocolVersion protocol)
 {
     return("Failed to create device from serial number: " + serialNumber + ", Protocol: " + protocol);
 }
        /// <summary>
        /// The delegate that is called when a device is found by the device manager's scan mode.
        /// </summary>
        /// <param name="device">Device.</param>
        private void OnDeviceFound(IConnectionManager cm, ISerialNumber serialNumber, string address, byte[] packet, EProtocolVersion protocol)
        {
            var device = this[serialNumber];

            if (device == null)
            {
                device = CreateDeviceInternal(serialNumber, EProtocolVersion.V4, false);
                __foundDevices[serialNumber] = device;
            }

            if (device.protocol is IGaugeProtocol)
            {
                var gp = device.protocol as IGaugeProtocol;
                if (gp.supportsBroadcasting)
                {
                    device.HandlePacket(packet);
                }
            }
            device.connection.lastSeen = DateTime.Now;

            NotifyOfDeviceEvent(DeviceEvent.EType.Found, device);
        }
 public IDevice CreateDevice(ISerialNumber serialNumber, string connectionAddress, EProtocolVersion protocol)
 {
     return(null);
 }
 /// <summary>
 /// Creates a new GaugePacket.
 /// </summary>
 /// <param name="version"></param>
 /// <param name="battery"></param>
 /// <param name="readings"></param>
 public GaugePacket(EProtocolVersion version, int battery, GaugeReading[] readings) : this()
 {
     this.version       = version;
     this.battery       = battery;
     this.gaugeReadings = readings;
 }
        /// <summary>
        /// The delegate that is called when a device is found by the device manager's scan mode.
        /// </summary>
        private void OnDeviceFound(IConnectionManager cm, ISerialNumber serialNumber, string address, byte[] packet, EProtocolVersion protocol)
        {
            lock (locker) {
                var device = this[serialNumber];

                if (device == null)
                {
                    device = CreateDevice(serialNumber, address, protocol);
                    MarkDeviceAsFound(device, true);
                }

                device.connection.lastPacket = packet;
                device.connection.lastSeen   = DateTime.Now;
                NotifyOfDeviceEvent(DeviceEvent.EType.Found, device);
            }
        }
        // Overridden from IDeviceManager
        public IDevice CreateDevice(ISerialNumber serialNumber, string connectionAddress, EProtocolVersion protocolVersion)
        {
            lock (locker) {
                IDevice ret = this[serialNumber];

                if (ret == null)
                {
                    var connection = connectionManager.CreateConnection(connectionAddress, protocolVersion);

                    var protocol = Protocol.FindProtocolFromVersion(protocolVersion);
                    if (protocol == null)
                    {
                        protocol = Protocol.FindProtocolFromVersion(EProtocolVersion.V1);
                    }
                    var definition = deviceFactory.GetDeviceDefinition(serialNumber);
                    ret = definition.CreateDevice(ion, serialNumber, connection, protocol);
                    ret.onDeviceEvent += OnDeviceEvent;
                }

                if (ret == null)
                {
                    var msg = BuildErrorHeader(serialNumber, protocolVersion) +
                              ": Please ensure that the serial number is resgistered in ION.Core.Devices.Devices.xml";
                    Log.C(this, msg);
                    throw new Exception(msg);
                }

                return(ret);
            }
        }
 /// <summary>
 /// Notifes the OnDeviceFound callback of a newly found device.
 /// </summary>
 /// <param name="serialNumber">Serial number.</param>
 /// <param name="address">Address.</param>
 /// <param name="packet">Packet.</param>
 /// <param name="version">Version.</param>
 private void NotifyDeviceFound(ISerialNumber serialNumber, string address, byte[] packet, EProtocolVersion version)
 {
     if (onDeviceFound != null)
     {
         onDeviceFound(this, serialNumber, address, packet, version);
     }
 }
Exemple #13
0
        // Implemented from IDeviceManager
        public IDevice CreateDevice(ISerialNumber serialNumber, string connectionAddress, EProtocolVersion protocol)
        {
            var p = Protocol.FindProtocolFromVersion(protocol);

            return(deviceFactory.GetDeviceDefinition(serialNumber).CreateDevice(ion, serialNumber, new RemoteConnection(serialNumber.ToString()), p));
        }
 // Implemented for IConnectionHelper
 public IConnection CreateConnection(string address, EProtocolVersion protocolVersion)
 {
     return(null);
 }