Exemplo n.º 1
0
        /// <summary>
        /// Verify a given Hub (protocol + hubId) aganinst a HubModel
        /// </summary>
        /// <param name="protocol"></param>
        /// <param name="hubId"></param>
        /// <param name="hubModel"></param>
        /// <returns></returns>
        public DeploymentModelError[] Verify(IPoweredUpProtocol protocol, byte hubId, DeploymentHubModel hubModel)
        {
            if (protocol is null)
            {
                throw new ArgumentNullException(nameof(protocol));
            }

            if (hubModel is null)
            {
                throw new ArgumentNullException(nameof(hubModel));
            }

            var result = new List <DeploymentModelError>();

            var hubInfo = protocol.Knowledge.Hub(hubId);

            if (hubModel.HubType != null && hubModel.HubType != hubInfo.SystemType)
            {
                result.Add(new DeploymentModelError(1002, hubInfo.HubId, null, $"Hub {hubInfo.HubId} with system type {hubInfo.SystemType} does not match expected {hubModel.HubType}."));
            }

            result.AddRange(
                hubModel.Devices.Select(Device =>
            {
                var PortInfo = protocol.Knowledge.Port(hubId, Device.PortId);

                return(Device, PortInfo);
            })
                .Select(t => t switch
            {
                (var Device, var PortInfo)when !PortInfo.IsDeviceConnected => (Error: 1000, t.Device, t.PortInfo),                                         // no device connected
                (var Device, var PortInfo)when Device.DeviceType != null && PortInfo.IOTypeId != Device.DeviceType => (Error: 1001, t.Device, t.PortInfo), // wrong device connected
                _ => (Error: 0, t.Device, t.PortInfo),
            })
Exemplo n.º 2
0
        public BasicMotor(IPoweredUpProtocol protocol, byte hubId, byte portId)
            : base(protocol, hubId, portId)
        {
            _powerMode = SingleValueMode <sbyte>(ModeIndexPower);

            ObserveForPropertyChanged(_powerMode.Observable, nameof(Power), nameof(PowerPct));
        }
Exemplo n.º 3
0
 public MultiValueMode(IPoweredUpProtocol protocol, PortModeInfo modeInfo, IObservable <PortValueData> modeValueObservable)
     : base(protocol, modeInfo, modeValueObservable)
 {
     Observable = CreateObservable();
     ObserveOnLocalProperty(Observable, v => Raw = v.Raw, v => SI = v.SI, v => Pct = v.Pct);
     ObserveForPropertyChanged(Observable, nameof(Raw), nameof(SI), nameof(Pct));
 }
Exemplo n.º 4
0
        protected AbsoluteMotor(IPoweredUpProtocol protocol, byte hubId, byte portId)
            : base(protocol, hubId, portId)
        {
            _absoluteMode = SingleValueMode <short>(ModeIndexAbsolutePosition);

            ObserveForPropertyChanged(_absoluteMode.Observable, nameof(AbsolutePosition), nameof(AbsolutePositionPct));
        }
        public TechnicMediumHubTemperatureSensor(IPoweredUpProtocol protocol, byte hubId, byte portId)
            : base(protocol, hubId, portId)
        {
            _temperatureMode = SingleValueMode <short>(ModeIndexTemperature);

            ObserveForPropertyChanged(_temperatureMode.Observable, nameof(Temperature), nameof(TemperaturePct));
        }
        public static async Task <PortFeedback> SendPortOutputCommandAsync(this IPoweredUpProtocol self, PortOutputCommandMessage message)
        {
            var portId = message.PortId;

            var response = await self.SendMessageReceiveResultAsync <PortOutputCommandFeedbackMessage>(message, msg => msg.Feedbacks.Any(f => f.PortId == portId));

            return(response.Feedbacks.FirstOrDefault(f => f.PortId == portId).Feedback);
        }
Exemplo n.º 7
0
        public Voltage(IPoweredUpProtocol protocol, byte hubId, byte portId)
            : base(protocol, hubId, portId)
        {
            _voltageLMode = SingleValueMode <short>(ModeIndexVoltageL);
            _voltageSMode = SingleValueMode <short>(ModeIndexVoltageS);

            ObserveForPropertyChanged(_voltageLMode.Observable, nameof(VoltageL), nameof(VoltageLPct));
            ObserveForPropertyChanged(_voltageSMode.Observable, nameof(VoltageS), nameof(VoltageSPct));
        }
Exemplo n.º 8
0
        protected TachoMotor(IPoweredUpProtocol protocol, byte hubId, byte portId)
            : base(protocol, hubId, portId)
        {
            _speedMode    = SingleValueMode <sbyte>(ModeIndexSpeed);
            _positionMode = SingleValueMode <int>(ModeIndexPosition);

            ObserveForPropertyChanged(_speedMode.Observable, nameof(Speed), nameof(SpeedPct));
            ObserveForPropertyChanged(_positionMode.Observable, nameof(Position), nameof(PositionPct));
        }
Exemplo n.º 9
0
        public Current(IPoweredUpProtocol protocol, byte hubId, byte portId)
            : base(protocol, hubId, portId)
        {
            _currentLMode = SingleValueMode <short>(ModeIndexCurrentL);
            _currentSMode = SingleValueMode <short>(ModeIndexCurrentS);

            ObserveForPropertyChanged(_currentLMode.Observable, nameof(CurrentL), nameof(CurrentLPct));
            ObserveForPropertyChanged(_currentSMode.Observable, nameof(CurrentS), nameof(CurrentSPct));
        }
Exemplo n.º 10
0
        public Hub(IPoweredUpProtocol protocol, IDeviceFactory deviceFactory, ILogger <Hub> logger, IServiceProvider serviceProvider, Port[] knownPorts)
        {
            Protocol        = protocol ?? throw new ArgumentNullException(nameof(protocol));
            _deviceFactory  = deviceFactory ?? throw new ArgumentNullException(nameof(deviceFactory));
            ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
            AddKnownPorts(knownPorts ?? throw new ArgumentNullException(nameof(knownPorts)));
            _logger = logger;

            SetupOnHubChange();
            SetupOnPortChangeObservable(Protocol.UpstreamMessages);
            SetupHubAlertObservable(Protocol.UpstreamMessages);
            SetupHubPropertyObservable(Protocol.UpstreamMessages);
        }
Exemplo n.º 11
0
        public Device(IPoweredUpProtocol protocol, byte hubId, byte portId)
        {
            _protocol = protocol ?? throw new ArgumentNullException(nameof(protocol));
            _hubId    = hubId;
            _portId   = portId;

            _portValueObservable = _protocol.UpstreamMessages
                                   .Where(msg => msg.HubId == _hubId)
                                   .SelectMany(msg => msg switch
            {
                PortValueSingleMessage pvsm => pvsm.Data,
                PortValueCombinedModeMessage pvcmm => pvcmm.Data,
                _ => Array.Empty <PortValueData>(),
            })
Exemplo n.º 12
0
 public TechnicMediumHub(IPoweredUpProtocol protocol, IDeviceFactory deviceFactory, ILogger <TechnicMediumHub> logger, IServiceProvider serviceProvider = default)
     : base(protocol, deviceFactory, logger, serviceProvider, new Port[] {
     new Port(0, nameof(A), true),
     new Port(1, nameof(B), true),
     new Port(2, nameof(C), true),
     new Port(3, nameof(D), true),
     new Port(50, string.Empty, false, expectedDevice: DeviceType.RgbLight),
     new Port(59, string.Empty, false, expectedDevice: DeviceType.Current),
     new Port(60, string.Empty, false, expectedDevice: DeviceType.Voltage),
     new Port(61, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubTemperatureSensor),
     new Port(96, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubTemperatureSensor),
     new Port(97, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubAccelerometer),
     new Port(98, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubGyroSensor),
     new Port(99, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubTiltSensor),
     new Port(100, string.Empty, false, expectedDevice: DeviceType.TechnicMediumHubGestSensor),
 })
 {
 }
Exemplo n.º 13
0
        public static Mode Create(IPoweredUpProtocol protocol, byte hubId, byte portId, byte modeIndex, IObservable <PortValueData> modeValueObservable)
        {
            var modeInfo = protocol.Knowledge.PortMode(hubId, portId, modeIndex);

            Mode result = null;

            result = (modeInfo.DatasetType, modeInfo.NumberOfDatasets) switch
            {
                (PortModeInformationDataType.SByte, 1) => new SingleValueMode <sbyte>(protocol, modeInfo, modeValueObservable),
                (PortModeInformationDataType.SByte, _) => new MultiValueMode <sbyte>(protocol, modeInfo, modeValueObservable),

                (PortModeInformationDataType.Int16, 1) => new SingleValueMode <short>(protocol, modeInfo, modeValueObservable),
                (PortModeInformationDataType.Int16, _) => new MultiValueMode <short>(protocol, modeInfo, modeValueObservable),

                (PortModeInformationDataType.Int32, 1) => new SingleValueMode <int>(protocol, modeInfo, modeValueObservable),
                (PortModeInformationDataType.Int32, _) => new MultiValueMode <int>(protocol, modeInfo, modeValueObservable),

                (PortModeInformationDataType.Single, 1) => new SingleValueMode <float>(protocol, modeInfo, modeValueObservable),
                (PortModeInformationDataType.Single, _) => new MultiValueMode <float>(protocol, modeInfo, modeValueObservable),

                _ => throw new NotSupportedException("Mode of unknown data type"),
            };
 public TechnicLargeLinearMotor(IPoweredUpProtocol protocol, byte hubId, byte portId)
     : base(protocol, hubId, portId)
 { }
Exemplo n.º 15
0
 public DynamicDevice(IPoweredUpProtocol protocol, byte hubId, byte portId)
     : base(protocol, hubId, portId)
 {
 }
Exemplo n.º 16
0
 public DevicesList(IPoweredUpProtocol protocol, DiscoverPorts discoverPorts)
 {
     this.protocol      = protocol ?? throw new ArgumentNullException(nameof(protocol));
     this.discoverPorts = discoverPorts ?? throw new ArgumentNullException(nameof(discoverPorts));
 }
Exemplo n.º 17
0
 public IPoweredUpDevice CreateConnected(DeviceType deviceType, IPoweredUpProtocol protocol, byte hubId, byte portId)
 => new DynamicDevice(protocol, hubId, portId);
Exemplo n.º 18
0
 public TraceMessages(IPoweredUpProtocol protocol, ILogger <TraceMessages> logger = default)
 {
     _protocol = protocol ?? throw new ArgumentNullException(nameof(protocol));
     _logger   = logger;
 }
        public static async Task <TResultMessage> SendMessageReceiveResultAsync <TResultMessage>(this IPoweredUpProtocol self, PoweredUpMessage message, Func <TResultMessage, bool> filter = default)
        {
            var awaitable = self.UpstreamMessages
                            .OfType <TResultMessage>()
                            .Where(resultMessage => filter == null || filter(resultMessage))
                            .FirstAsync()
                            .GetAwaiter(); // make sure the subscription is present at the moment the message is sent.

            await self.SendMessageAsync(message);

            var result = await awaitable;

            return(result);
        }
Exemplo n.º 20
0
 public DiscoverPorts(IPoweredUpProtocol protocol, byte hubId = 0, ILogger <DiscoverPorts> logger = default)
 {
     _protocol = protocol ?? throw new System.ArgumentNullException(nameof(protocol));
     _hubId    = hubId;
     _logger   = logger;
 }
Exemplo n.º 21
0
 public RgbLight(IPoweredUpProtocol protocol, byte hubId, byte portId)
     : base(protocol, hubId, portId)
 {
 }
Exemplo n.º 22
0
        public IPoweredUpDevice CreateConnected(DeviceType deviceType, IPoweredUpProtocol protocol, byte hubId, byte portId)
        {
            var type = GetTypeFromDeviceType(deviceType);

            return((type == null) ? new DynamicDevice(protocol, hubId, portId) : (IPoweredUpDevice)ActivatorUtilities.CreateInstance(_serviceProvider, type, protocol, hubId, portId));
        }
Exemplo n.º 23
0
 public DumpStaticPortInfo(IPoweredUpProtocol protocol, DiscoverPorts discoverPorts)
 {
     this.protocol      = protocol ?? throw new ArgumentNullException(nameof(protocol));
     this.discoverPorts = discoverPorts ?? throw new ArgumentNullException(nameof(discoverPorts));
 }
 public TechnicMediumHubGestSensor(IPoweredUpProtocol protocol, byte hubId, byte portId)
     : base(protocol, hubId, portId)
 {
 }