コード例 #1
0
 public PeripheralTreeEntry(IPeripheral peripheral, IPeripheral parent, Type type, IRegistrationPoint registrationPoint, string name, int level)
 {
     this.type = type;
     Name = name;
     RegistrationPoint = registrationPoint;
     Peripheral = peripheral;
     Parent = parent;
     Level = level;
 }
コード例 #2
0
ファイル: PrimeCellIDHelper.cs プロジェクト: rte-se/emul8
 public PrimeCellIDHelper(int peripheralSize, byte[] data, IPeripheral parent)
 {
     this.peripheralSize = peripheralSize;
     this.data = data.ToArray(); // to obtain a copy
     this.parent = parent;
     if(data.Length != 8)
     {
         throw new RecoverableException("You have to provide full peripheral id and prime cell id (8 bytes).");
     }
 }
コード例 #3
0
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price,
                                    double overallPerformance, string connectionType)
        {
            CheckIfComputerIdExist(computerId);
            IComputer computer = _computers[computerId];

            if (_peripherals.ContainsKey(id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }

            IPeripheral peripheral = _factory.CreatePeripheral(id, peripheralType, manufacturer, model, price,
                                                               overallPerformance, connectionType);

            computer.AddPeripheral(peripheral);
            _peripherals.Add(id, peripheral);

            return(string.Format(SuccessMessages.AddedPeripheral, peripheralType, id, computerId));
        }
コード例 #4
0
        private static IPeripheral PeripheralFactory(
            int id,
            string peripheralType, string manufacturer,
            string model, decimal price, double overallPerformance,
            string connectionType)
        {
            Enum.TryParse(peripheralType, out PeripheralType type);

            IPeripheral peripheral = type switch
            {
                PeripheralType.Headset => new Headset(id, manufacturer, model, price, overallPerformance, connectionType),
                PeripheralType.Keyboard => new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType),
                PeripheralType.Monitor => new Monitor(id, manufacturer, model, price, overallPerformance, connectionType),
                PeripheralType.Mouse => new Mouse(id, manufacturer, model, price, overallPerformance, connectionType),
                _ => null
            };

            return(peripheral);
        }
コード例 #5
0
        async Task Setup()
        {
            this.peripheral = await this.manager
                              .ScanUntilPeripheralFound(Constants.PeripheralName)
                              .Timeout(Constants.DeviceScanTimeout)
                              .ToTask();

            await this.peripheral
            .ConnectWait()
            .Timeout(Constants.ConnectTimeout)     // android can take some time :P
            .ToTask();

            this.characteristics = await this.peripheral
                                   .GetCharacteristicsForService(Constants.ScratchServiceUuid)
                                   .Take(5)
                                   .Timeout(Constants.OperationTimeout)
                                   .ToArray()
                                   .ToTask();
        }
コード例 #6
0
 private void FindPaths(string nameSoFar, IPeripheral peripheralToFind, MultiTreeNode <IPeripheral, IRegistrationPoint> currentNode, List <string> paths)
 {
     foreach (var child in currentNode.Children)
     {
         var    currentPeripheral = child.Value;
         string localName;
         if (!TryGetLocalName(currentPeripheral, out localName))
         {
             continue;
         }
         var name = Subname(nameSoFar, localName);
         if (currentPeripheral == peripheralToFind)
         {
             paths.Add(name);
             return; // shouldn't be attached to itself
         }
         FindPaths(name, peripheralToFind, child, paths);
     }
 }
コード例 #7
0
        void ConnectPrinter(IPeripheral selectedPeripheral)
        {
            if (!selectedPeripheral.IsConnected())
            {
                selectedPeripheral.Connect();
            }

            _perifDisposable = selectedPeripheral.WhenAnyCharacteristicDiscovered().Subscribe((characteristic) =>
            {
                //System.Diagnostics.Debug.WriteLine(characteristic.Description); //this is not suppported at this momment, and no neccesary I guess
                if (characteristic.CanWrite() && !characteristic.CanRead() && !characteristic.CanNotify())
                {
                    IsReadyToPrint       = true;
                    _savedCharacteristic = characteristic;
                    System.Diagnostics.Debug.WriteLine($"Writing {characteristic.Uuid} - {characteristic.CanRead()} - {characteristic.CanIndicate()} - {characteristic.CanNotify()}");
                    _perifDisposable.Dispose();
                }
            });
        }
コード例 #8
0
ファイル: Controller.cs プロジェクト: stefandenchev/Softuni
        public string RemovePeripheral(string peripheralType, int computerId)
        {
            var computer = this.computers.FirstOrDefault(x => x.Id == computerId);

            if (computer == null)
            {
                throw new ArgumentException(ExceptionMessages.NotExistingComputerId);
            }

            computer.RemovePeripheral(peripheralType);
            IPeripheral peripheral = this.peripherals.FirstOrDefault(x => x.GetType().Name == peripheralType);

            if (peripheral != null)
            {
                this.peripherals.Remove(peripheral);
            }

            return(String.Format(SuccessMessages.RemovedPeripheral, peripheralType, peripheral.Id));
        }
コード例 #9
0
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            IComputer computer = computers.FirstOrDefault(c => c.Id == computerId);

            if (computer == null)
            {
                throw new ArgumentException(String.Format(ExceptionMessages.NotExistingComputerId));
            }

            if (peripherals.Any(p => p.Id == id))
            {
                throw new ArgumentException(String.Format(ExceptionMessages.ExistingPeripheralId));
            }

            IPeripheral peripheral = null;

            if (peripheralType == "Headset")
            {
                peripheral = new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Keyboard")
            {
                peripheral = new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Monitor")
            {
                peripheral = new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Mouse")
            {
                peripheral = new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else
            {
                throw new ArgumentException(String.Format(ExceptionMessages.InvalidPeripheralType));
            }

            computer.AddPeripheral(peripheral);
            peripherals.Add(peripheral);

            return(String.Format(SuccessMessages.AddedPeripheral, peripheralType, id, computerId));
        }
コード例 #10
0
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            CheckIfComputerExists(computerId);

            if (peripherals.Any(x => x.Id == id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }

            IPeripheral peripheral = null;

            switch (peripheralType)
            {
            case "Headset":
                peripheral = new Headset
                                 (id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case "Keyboard":
                peripheral = new Keyboard
                                 (id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case "Monitor":
                peripheral = new Monitor
                                 (id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case "Mouse":
                peripheral = new Mouse
                                 (id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            default: throw new ArgumentException(ExceptionMessages.InvalidPeripheralType);
            }

            peripherals.Add(peripheral);

            computers.FirstOrDefault(x => x.Id == computerId).AddPeripheral(peripheral);

            return(string.Format(SuccessMessages.AddedPeripheral, peripheralType, id, computerId));
        }
コード例 #11
0
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            if (!computers.Any(x => x.Id == computerId))
            {
                throw new ArgumentException("Computer with this id does not exist.");
            }

            IComputer computer = computers.FirstOrDefault(x => x.Id == computerId);

            if (peripherals.Any(x => x.Id == id))
            {
                throw new ArgumentException("Peripheral with this id already exists.");
            }

            IPeripheral peripheral = null;

            if (peripheralType == "Headset")
            {
                peripheral = new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Keyboard")
            {
                peripheral = new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Monitor")
            {
                peripheral = new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Mouse")
            {
                peripheral = new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else
            {
                throw new ArgumentException("Peripheral type is invalid.");
            }

            computer.AddPeripheral(peripheral);
            peripherals.Add(peripheral);

            return($"Peripheral {peripheralType} with id {id} added successfully in computer with id {computerId}.");
        }
コード例 #12
0
ファイル: Controller.cs プロジェクト: stefandenchev/Softuni
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            var computer = this.computers.FirstOrDefault(x => x.Id == computerId);

            if (computer == null)
            {
                throw new ArgumentException(ExceptionMessages.NotExistingComputerId);
            }

            IPeripheral peripheral = null;

            if (peripheralType == PeripheralType.Headset.ToString())
            {
                peripheral = new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == PeripheralType.Keyboard.ToString())
            {
                peripheral = new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == PeripheralType.Mouse.ToString())
            {
                peripheral = new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == PeripheralType.Monitor.ToString())
            {
                peripheral = new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else
            {
                throw new ArgumentException(ExceptionMessages.InvalidPeripheralType);
            }

            if (this.peripherals.Any(x => x.Id == id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }

            computer.AddPeripheral(peripheral);
            this.peripherals.Add(peripheral);

            return(string.Format(SuccessMessages.AddedPeripheral, peripheralType, id, computerId));
        }
コード例 #13
0
        private IPeripheral CreatePeripheral(int id,
                                             string peripheralType, string manufacturer,
                                             string model, decimal price,
                                             double overallPerformance, string connectionType)
        {
            Type type = GetDefaultType(peripheralType);

            if (type == null)
            {
                throw new ArgumentException
                          (ExceptionMessages.InvalidPeripheralType);
            }

            IPeripheral peripheral = null;

            if (peripheralType
                == PeripheralType.Headset.ToString())
            {
                peripheral = new Headset(id, manufacturer,
                                         model, price, overallPerformance, connectionType);
            }
            if (peripheralType
                == PeripheralType.Keyboard.ToString())
            {
                peripheral = new Keyboard(id, manufacturer,
                                          model, price, overallPerformance, connectionType);
            }
            if (peripheralType
                == PeripheralType.Monitor.ToString())
            {
                peripheral = new Monitor(id, manufacturer,
                                         model, price, overallPerformance, connectionType);
            }
            if (peripheralType
                == PeripheralType.Mouse.ToString())
            {
                peripheral = new Mouse(id, manufacturer,
                                       model, price, overallPerformance, connectionType);
            }

            return(peripheral);
        }
コード例 #14
0
        private async Task ReadFromDevice(IPeripheral device)
        {
            StopSearch();
            var ServiceId        = Guid.Parse("0000180A-0000-1000-8000-00805F9B34FB");
            var ManufacturerName = Guid.Parse("00002A29-0000-1000-8000-00805F9B34FB");

            var connectedDevice = await device.ConnectWait();

            var service = await device.GetKnownService(ServiceId);

            var characteristic = await service.GetKnownCharacteristics(new Guid[] { ManufacturerName });

            if (characteristic.CanRead())
            {
                var data = await characteristic.Read();

                var readValue = Encoding.UTF8.GetString(data.Data);
                Debug.WriteLine($"> Read characteristic is: {readValue}");
            }
        }
コード例 #15
0
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            this.ValidateComputerWithIdExists(computerId);

            if (this.peripherals.Any(x => x.Id == id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }

            IPeripheral peripheral = CreateValidPeripheral(id, peripheralType, manufacturer, model, price, overallPerformance, connectionType);

            IComputer computer = this.computers.First(x => x.Id == computerId);

            computer.AddPeripheral(peripheral);
            this.peripherals.Add(peripheral);

            string result = string.Format(SuccessMessages.AddedPeripheral, peripheralType, peripheral.Id, computer.Id);

            return(result);
        }
コード例 #16
0
        public string AddPeripheral(int computerId, int id,
                                    string peripheralType, string manufacturer,
                                    string model, decimal price, double overallPerformance,
                                    string connectionType)
        {
            CheckIfPeripheralAlreadyExist(id);
            IPeripheral peripheral = CreatePeripheral(id, peripheralType,
                                                      manufacturer, model, price,
                                                      overallPerformance, connectionType);

            CheckIfComputerExist(computerId);
            IComputer computer = GetComputerById(computerId);

            computer.AddPeripheral(peripheral);
            peripherals.Add(peripheral);

            return(string.Format(SuccessMessages
                                 .AddedPeripheral, peripheral.GetType().Name,
                                 peripheral.Id, computerId));
        }
コード例 #17
0
        public static IObservable <DeviceInfo> ReadDeviceInformation(this IPeripheral peripheral)
        => peripheral
        .GetKnownService(StandardUuids.DeviceInformationServiceUuid, true)
        .SelectMany(x => x.GetCharacteristics())
        .SelectMany(x => x.Select(y => y.Read()))
        .Concat()
        .ToList()
        .Select(data =>
        {
            var dev = new DeviceInfo();
            foreach (var item in data)
            {
                switch (item.Characteristic.Uuid[3])
                {
                case '4':
                    dev.ModelNumber = BitConverter.ToString(item.Data);
                    break;

                case '5':
                    dev.SerialNumber = BitConverter.ToString(item.Data);
                    break;

                case '6':
                    dev.FirmwareRevision = BitConverter.ToString(item.Data);
                    break;

                case '7':
                    dev.HardwareRevision = BitConverter.ToString(item.Data);
                    break;

                case '8':
                    dev.SoftwareRevision = BitConverter.ToString(item.Data);
                    break;

                case '9':
                    dev.ManufacturerName = BitConverter.ToString(item.Data);
                    break;
                }
            }
            return(dev);
        });
コード例 #18
0
        async Task <IGattCharacteristic> SetupCharacteristic(CancellationToken cancelToken)
        {
            var suuid = Guid.Parse(this.ServiceUuid);
            var cuuid = Guid.Parse(this.CharacteristicUuid);

            this.Info = "Searching for device..";

            this.peripheral = await this.centralManager
                              .ScanUntilPeripheralFound(this.DeviceName.Trim())
                              .ToTask(cancelToken);

            this.Info = "Device Found - Connecting";

            await this.peripheral
            .ConnectWait()
            .ToTask(cancelToken);

            this.Info = "Connected - Requesting MTU Change";

            if (this.peripheral is ICanRequestMtu mtu)
            {
                this.MTU = await mtu
                           .RequestMtu(512)
                           .ToTask(cancelToken);
            }
            else
            {
                this.MTU = this.peripheral.MtuSize;
            }

            this.Info = "Searching for characteristic";
            var characteristic = await this.peripheral
                                 .GetKnownCharacteristics(
                suuid,
                cuuid
                )
                                 .ToTask(cancelToken);

            this.Info = "Characteristic Found";
            return(characteristic);
        }
コード例 #19
0
ファイル: Controller.cs プロジェクト: kaloyanTry/OOP2021
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            if (peripherals.Any(p => p.Id == id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }

            if (!IsValidPeriferial(peripheralType))
            {
                throw new ArgumentException(ExceptionMessages.InvalidPeripheralType);
            }

            EnsureExistence(computerId);

            IPeripheral peripheral = null;

            switch (peripheralType)
            {
            case "Headset":
                peripheral = new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case "Keyboard":
                peripheral = new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case "Monitor":
                peripheral = new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case "Mouse":
                peripheral = new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
                break;
            }


            this.computers.First(c => c.Id == computerId).AddPeripheral(peripheral);
            this.peripherals.Add(peripheral);

            return(string.Format(SuccessMessages.AddedPeripheral, peripheralType, id, computerId));
        }
コード例 #20
0
        void InitializeGPIO(IPeripheral device, string deviceName, IGPIOReceiver receiver, IList <object> irqEntry, PropertyInfo defaultConnector)
        {
            if (!(irqEntry[0] is string && irqEntry[1] is int))
            {
                throw new ArgumentException();
            }
            //May throw AmbiguousMatchException - then use BindingFlags.DeclaredOnly or sth
            var connector = device.GetType().GetProperty(irqEntry[0] as string);

            if (connector == null)
            {
                throw new ArgumentException();
            }
            var gpio = connector.GetValue(device, null) as GPIO;

            if (gpio == null)
            {
                FailDevice(deviceName);
            }
            gpio.Connect(receiver, (int)irqEntry[1]);
        }
コード例 #21
0
        public bool TryGetMachineForPeripheral(IPeripheral p, out Machine machine)
        {
            if (peripheralToMachineCache.TryGetValue(p, out machine))
            {
                return(true);
            }

            foreach (var candidate in Machines)
            {
                var candidateAsMachine = candidate;
                if (candidateAsMachine != null && candidateAsMachine.IsRegistered(p))
                {
                    machine = candidateAsMachine;
                    peripheralToMachineCache.Add(p, machine);
                    return(true);
                }
            }

            machine = null;
            return(false);
        }
コード例 #22
0
        public string AddPeripheral(int computerId, int id, string peripheralType,
                                    string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            PeripheralType pType      = new PeripheralType();
            IPeripheral    peripheral = null;

            if (DoesComputerExist(computerId))
            {
                throw new ArgumentException(COMPUTER_DOESNT_EXIST);
            }
            else if (computers.Where(x => x.Id == computerId).Any(x => x.Peripherals.Any(x => x.Id == id)))
            {
                throw new ArgumentException("Peripheral with this id already exists.");
            }
            else if (!Enum.TryParse <PeripheralType>(peripheralType, out pType))
            {
                throw new ArgumentException("Peripheral type is invalid.");
            }
            else if (pType == PeripheralType.Headset)
            {
                peripheral = new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (pType == PeripheralType.Keyboard)
            {
                peripheral = new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (pType == PeripheralType.Monitor)
            {
                peripheral = new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (pType == PeripheralType.Mouse)
            {
                peripheral = new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
            }

            peripherals.Add(peripheral);
            computers.Where(i => i.Id == computerId).FirstOrDefault().AddPeripheral(peripheral);

            return($"Peripheral {peripheral.GetType().Name} with id { peripheral.Id} added successfully in computer with id {computerId}.");
        }
コード例 #23
0
ファイル: Controller.cs プロジェクト: ivaKozarova/CSharpOOP
        public string AddPeripheral(int computerId, int id, string peripheralType,
                                    string manufacturer, string model, decimal price, double overallPerformance,
                                    string connectionType)
        {
            CheckDoesComputerExist(computerId);
            var computer = this.computers.FirstOrDefault(c => c.Id == computerId);

            if (this.peripherals.Any(p => p.Id == id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }
            IPeripheral peripheral = CreatePeripheral(peripheralType, id, manufacturer, model, price,
                                                      overallPerformance, connectionType);

            if (peripheral == null)
            {
                throw new ArgumentException(ExceptionMessages.InvalidPeripheralType);
            }
            computer.AddPeripheral(peripheral);
            this.peripherals.Add(peripheral);
            return(String.Format(SuccessMessages.AddedPeripheral, peripheralType, id, computerId));
        }
コード例 #24
0
        public string AddPeripheral(int computerId, int id, string peripheralTypeName, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            var currentComputer = this.computers.FirstOrDefault(x => x.Id == computerId);

            NonExistingComputeId(currentComputer);

            if (!Enum.TryParse(peripheralTypeName, out PeripheralType peripheralType))
            {
                throw new ArgumentException(ExceptionMessages.InvalidPeripheralType);
            }
            if (peripherals.Any(x => x.Id == id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }
            IPeripheral peripheral = null;

            switch (peripheralType)
            {
            case PeripheralType.Headset:
                peripheral = new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case PeripheralType.Keyboard:
                peripheral = new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case PeripheralType.Monitor:
                peripheral = new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case PeripheralType.Mouse:
                peripheral = new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
                break;
            }
            peripherals.Add(peripheral);
            currentComputer.AddPeripheral(peripheral);

            return(string.Format(SuccessMessages.AddedPeripheral, peripheralTypeName, id, computerId));
        }
コード例 #25
0
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            IComputer currentComputer = computers.Where(x => x.Id == computerId).FirstOrDefault();

            if (currentComputer == null)
            {
                throw new ArgumentException(ExceptionMessages.NotExistingComputerId);
            }
            IPeripheral currentPeripheral = null;

            if (peripherals.Any(x => x.Id == id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }
            if (peripheralType == "Headset")
            {
                currentPeripheral = new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Keyboard")
            {
                currentPeripheral = new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Monitor")
            {
                currentPeripheral = new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Mouse")
            {
                currentPeripheral = new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else
            {
                throw new ArgumentException(ExceptionMessages.InvalidPeripheralType);
            }
            currentComputer.AddPeripheral(currentPeripheral);
            peripherals.Add(currentPeripheral);
            //public const string AddedComponent = "Component {0} with id {1} added successfully in computer with id {2}.";
            return(string.Format(SuccessMessages.AddedPeripheral, currentPeripheral.GetType().Name, currentPeripheral.Id, currentComputer.Id));
        }
コード例 #26
0
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            CheckIfComputerExists(computerId);
            if (periherals.Any(x => x.Id == id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }

            IPeripheral peripheral = peripheralType switch
            {
                nameof(Headset) => new Headset(id, manufacturer, model, price, overallPerformance, connectionType),
                nameof(Keyboard) => new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType),
                nameof(Monitor) => new Monitor(id, manufacturer, model, price, overallPerformance, connectionType),
                nameof(Mouse) => new Mouse(id, manufacturer, model, price, overallPerformance, connectionType),
                _ => throw new ArgumentException(ExceptionMessages.InvalidPeripheralType)
            };
            IComputer computer = computers.FirstOrDefault(x => x.Id == computerId);

            computer.AddPeripheral(peripheral);
            periherals.Add(peripheral);
            return(string.Format(SuccessMessages.AddedPeripheral, peripheralType, id, computerId));
        }
コード例 #27
0
        //[source,dest] or [dest] with non-null defaultConnector
        void InitializeGPIO(IPeripheral device, string deviceName, IGPIOReceiver receiver, IList <int> irqEntry, PropertyInfo defaultConnector)
        {
            var periByNumber = device as INumberedGPIOOutput;

            if (irqEntry.Count == 2 && periByNumber != null)
            {
                periByNumber.Connections[irqEntry[0]].Connect(receiver, irqEntry[1]);
            }
            else if (irqEntry.Count == 1 && defaultConnector != null)
            {
                var gpioField = defaultConnector.GetValue(device, null) as GPIO;
                if (gpioField == null)
                {
                    FailDevice(deviceName);
                }
                gpioField.Connect(receiver, irqEntry[0]);
            }
            else
            {
                throw new ArgumentException();
            }
        }
コード例 #28
0
        public override void OnNavigatedTo(INavigationParameters parameters)
        {
            base.OnNavigatedTo(parameters);

            this.peripheral = parameters.GetValue <IPeripheral>("Peripheral");
            this.Name       = this.peripheral.Name;
            this.Uuid       = this.peripheral.Uuid;

            this.IsMtuVisible     = this.peripheral.IsMtuRequestsAvailable();
            this.IsPairingVisible = this.peripheral.IsPairingRequestsAvailable();

            this.PairingText = this.peripheral.TryGetPairingStatus() == PairingState.Paired
                ? "Peripheral Paired"
                : "Pair Peripheral";

            //this.peripheral
            //    .WhenConnected()
            //    .Select(x => x.ReadRssiContinuously(TimeSpan.FromSeconds(3)))
            //    .Switch()
            //    .SubOnMainThread(x => this.Rssi = x)
            //    .DisposedBy(this.DeactivateWith);

            this.peripheral
            .WhenStatusChanged()
            .Do(x =>
            {
                if (x == ConnectionState.Connected)
                {
                    this.GattCharacteristics.Clear();
                }
            })
            .Select(x => x switch
            {
                ConnectionState.Connecting => "Cancel Connection",
                ConnectionState.Connected => "Disconnect",
                ConnectionState.Disconnected => "Connect",
                ConnectionState.Disconnecting => "Disconnecting..."
            })
コード例 #29
0
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            IsIdExist(computerId);

            if (peripheralType == "Headset")
            {
                peripheral = new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Keyboard")
            {
                peripheral = new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Monitor")
            {
                peripheral = new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else if (peripheralType == "Mouse")
            {
                peripheral = new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
            }
            else
            {
                throw new ArgumentException(ExceptionMessages.InvalidPeripheralType);
            }

            computers.First(x => x.Id == computerId).AddPeripheral(peripheral);

            if (peripherals.Any(x => x.Id == id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }
            else
            {
                peripherals.Add(peripheral);
            }

            return(string.Format(SuccessMessages.AddedPeripheral, peripheralType, id, computerId));
        }
コード例 #30
0
        public string AddPeripheral(int computerId, int id, string peripheralType, string manufacturer, string model, decimal price, double overallPerformance, string connectionType)
        {
            IComputer comp = IsExist(computerId);

            if (comp.Peripherals.Any(x => x.Id == id))
            {
                throw new ArgumentException(ExceptionMessages.ExistingPeripheralId);
            }

            if (!Enum.TryParse(peripheralType, out PeripheralType))
            {
                throw new ArgumentException(ExceptionMessages.InvalidPeripheralType);
            }

            IPeripheral peripheral = null;

            switch (PeripheralType)
            {
            case PeripheralType.Headset:
                peripheral = new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case PeripheralType.Keyboard:
                peripheral = new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case PeripheralType.Monitor:
                peripheral = new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
                break;

            case PeripheralType.Mouse:
                peripheral = new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
                break;
            }
            comp.AddPeripheral(peripheral);
            peripherals.Add(peripheral);
            return(string.Format(SuccessMessages.AddedPeripheral, peripheral.GetType().Name, id, computerId));
        }
コード例 #31
0
        /// <summary>
        /// Waits for connection to actually happen
        /// </summary>
        /// <param name="peripheral"></param>
        /// <returns></returns>
        public static IObservable <IPeripheral> ConnectWait(this IPeripheral peripheral)
        => Observable.Create <IPeripheral>(ob =>
        {
            var sub1 = peripheral
                       .WhenConnected()
                       .Take(1)
                       .Subscribe(_ => ob.Respond(peripheral));

            var sub2 = peripheral
                       .WhenConnectionFailed()
                       .Subscribe(ob.OnError);

            peripheral.ConnectIf();
            return(() =>
            {
                sub1.Dispose();
                sub2.Dispose();
                if (peripheral.Status != ConnectionState.Connected)
                {
                    peripheral.CancelConnection();
                }
            });
        });
コード例 #32
0
        //public override void OnAppearing()
        //{
        //    this.bleManager
        //        .Scan()
        //        .Subscribe(x =>
        //        {

        //        })
        //        .DisposeWith(this.DeactivateWith);
        //}


        //public override void OnDisappearing()
        //{
        //    Console.WriteLine("ONDISAPPEARING CALLED");
        //}

        async Task PairingTest()
        {
            this.Append("Scanning..");
            this.peripheral = await bleManager
                              .ScanForUniquePeripherals(new ScanConfig { ServiceUuids = { "FFF0" } })
                              .Take(1)
                              .ToTask(this.cancelSrc.Token);

            this.Append("Device Found");
            //await this.peripheral.WithConnectIf().ToTask(this.cancelSrc.Token);
            this.Append("Device Connected - trying to pair");

            var result = await this.peripheral.TryPairingRequest().ToTask(this.cancelSrc.Token);

            if (result == null)
            {
                this.Append("Pairing Not Supported");
            }
            else
            {
                this.Append("Pairing Result: " + result.Value);
            }
        }
コード例 #33
0
ファイル: PeripheralCollection.cs プロジェクト: emul8/emul8
 public void Remove(IPeripheral peripheral)
 {
     lock(sync)
     {
         // list is scanned first
         blocks = blocks.Where(x => x.Peripheral.Peripheral != peripheral).ToArray();
         // then dictionary
         var toRemove = shortBlocks.Where(x => x.Value.Peripheral.Peripheral == peripheral).Select(x => x.Key).ToArray();
         foreach(var keyToRemove in toRemove)
         {
             shortBlocks.Remove(keyToRemove);
         }
         InvalidateLastBlock();
     }
 }
コード例 #34
0
ファイル: MonitorCommands.cs プロジェクト: emul8/emul8
        private bool TryFindPeripheralByName(string name, out IPeripheral peripheral, out string longestMatch)
        {
            longestMatch = string.Empty;

            if(currentMachine == null)
            {
                peripheral = null;
                return false;
            }

            string longestMatching;
            string currentMatch;
            string longestPrefix = string.Empty;
            var ret = currentMachine.TryGetByName(name, out peripheral, out longestMatching);
            longestMatch = longestMatching;

            if(!ret)
            {
                foreach(var prefix in usings)
                {
                    ret = currentMachine.TryGetByName(prefix + name, out peripheral, out currentMatch);
                    if(longestMatching.Split('.').Length < currentMatch.Split('.').Length - prefix.Split('.').Length)
                    {
                        longestMatching = currentMatch;
                        longestPrefix = prefix;
                    }
                    if(ret)
                    {
                        break;
                    }
                }
            }
            longestMatch = longestPrefix + longestMatching;
            return ret;
        }
コード例 #35
0
ファイル: DevicesConfig.cs プロジェクト: rte-se/emul8
 private bool IsShortNotation(IPeripheral peripheral, PropertyInfo defaultConnector, IList<dynamic> entry)
 {
     return (peripheral is INumberedGPIOOutput && entry.Count == 2 && entry.All(x => x is int))
     || (defaultConnector != null && entry.Count == 1 && entry[0] is int)
     || (entry.Count == 2 && entry[0] is String && entry[1] is int);
 }
コード例 #36
0
ファイル: DevicesConfig.cs プロジェクト: rte-se/emul8
        void InitializeGPIO(IPeripheral device, IGPIOReceiver receiver, IList<object> irqEntry, PropertyInfo defaultConnector)
        {
            if(!(irqEntry[0] is string && irqEntry[1] is int))
            {
                throw new ArgumentException();
            }
            //May throw AmbiguousMatchException - then use BindingFlags.DeclaredOnly or sth
            var connector = device.GetType().GetProperty(irqEntry[0] as string);
            if(connector == null)
            {
                throw new ArgumentException();
            }
            var gpio = connector.GetValue(device, null) as GPIO;
            if(gpio == null)
            {
                connector.SetValue(device, new GPIO(), null);
                gpio = connector.GetValue(device, null) as GPIO;
            }
            gpio.Connect(receiver, (int)irqEntry[1]);

        }
コード例 #37
0
ファイル: DevicesConfig.cs プロジェクト: rte-se/emul8
 //[source,dest] or [dest] with non-null defaultConnector
 void InitializeGPIO(IPeripheral device, IGPIOReceiver receiver, IList<int> irqEntry, PropertyInfo defaultConnector)
 {
     var periByNumber = device as INumberedGPIOOutput;
     if(irqEntry.Count == 2 && periByNumber != null)
     {
         periByNumber.Connections[irqEntry[0]].Connect(receiver, irqEntry[1]);
     }
     else if(irqEntry.Count == 1 && defaultConnector != null)
     {
         var gpioField = defaultConnector.GetValue(device, null) as GPIO;
         if(gpioField == null)
         {
             defaultConnector.SetValue(device, new GPIO(), null);
             gpioField = defaultConnector.GetValue(device, null) as GPIO;
         }
         gpioField.Connect(receiver, irqEntry[0]);
     }
     else
     {
         throw new ArgumentException();
     }
 }
コード例 #38
0
 public PeripheralsChangedEventArgs(IPeripheral peripheral, PeripheralChangeType operation)
 {
     Peripheral = peripheral;
     Operation = operation;
 }
コード例 #39
0
ファイル: RegisterCollection.cs プロジェクト: rte-se/emul8
 /// <summary>
 /// Initializes a new instance of the <see cref="Emul8.Core.Structure.Registers.DoubleWordRegisterCollection"/> class.
 /// </summary>
 /// <param name="parent">Parent peripheral (for logging purposes).</param>
 /// <param name="registersMap">Map of register offsets and registers.</param>
 public DoubleWordRegisterCollection(IPeripheral parent, IDictionary<long, DoubleWordRegister> registersMap)
 {
     this.parent = parent;
     this.registers = new Dictionary<long, DoubleWordRegister>(registersMap);
 }
コード例 #40
0
ファイル: CC2538RF.cs プロジェクト: emul8/emul8
 private static DoubleWordRegister[] CreateRegistersGroup(int size, IPeripheral parent, int position, int width,
     FieldMode mode = FieldMode.Read | FieldMode.Write, Action<int, uint> writeCallback = null, Func<int, uint> valueProviderCallback = null, string name = null)
 {
     var result = new DoubleWordRegister[size];
     for(var i = 0; i < size; i++)
     {
         var j = i;
         result[i] = new DoubleWordRegister(parent)
             .WithValueField(position, width, mode, name: name + j,
                 valueProviderCallback: valueProviderCallback == null ? (Func<uint, uint>)null : _ => valueProviderCallback(j),
                 writeCallback: writeCallback == null ? (Action<uint, uint>)null : (_, @new) => { writeCallback(j, @new); });
     }
     return result;
 }