public async Task ExecuteAsync(TextReader reader, byte systemType, ushort deviceType, byte hubId, byte portId, Version hw, Version sw) { var knowledge = _protocol.Knowledge; // override cached data using var disposable = _protocol.UpstreamRawMessages .Subscribe(tuple => KnowledgeManager.ApplyStaticProtocolKnowledge(tuple.message, knowledge)); await _protocol.ConnectAsync((SystemType)systemType); // registering to bluetooth notification if (systemType != 0) { Console.Error.WriteLine("Command Line provided Device Type, hubId, portId and versions hw/sw"); await _mock.MockCharacteristic.AttachIO((DeviceType)deviceType, hubId, portId, hw, sw); } var foundSystemType = false; var foundAttachedIO = false; var line = await reader.ReadLineAsync(); while (line is not null) { if (line.Substring(6, 8) == "01-0B-06") // property msg - systemtype - update { foundSystemType = true; } if (line.Substring(6, 2) == "04" && line.Substring(12, 2) == "01") // attached io msg (04) - port - attach event { foundAttachedIO = true; } await _mock.MockCharacteristic.WriteUpstreamAsync(line); line = await reader.ReadLineAsync(); } if (systemType == 0 && (foundSystemType == false || foundAttachedIO == false)) { Console.Error.WriteLine("#############################"); Console.Error.WriteLine("SystemType and/or attached IO message not found in data or command line arguments"); Console.Error.WriteLine("#############################"); } DevicesList.PrettyPrintKnowledge(System.Console.Out, _protocol.Knowledge); }
public void PortValueCombinedModeEncoder_Decode(string dataAsString, byte expectedPortId, byte[] expectedModes, PortModeInformationDataType[] expectedDataType, int[] expectedData) { var knowledge = new ProtocolKnowledge(); var serviceProvider = new ServiceCollection() .AddPoweredUp() .BuildServiceProvider(); KnowledgeManager.ApplyDynamicProtocolKnowledge(new HubAttachedIOForAttachedDeviceMessage() { HubId = 0, IOTypeId = DeviceType.TechnicLargeLinearMotor, MessageType = MessageType.HubAttachedIO, Event = HubAttachedIOEvent.AttachedIO, PortId = 0x00, HardwareRevision = new Version("0.0.0.1"), SoftwareRevision = new Version("0.0.0.1") }, knowledge, serviceProvider.GetService <IDeviceFactory>()); KnowledgeManager.ApplyDynamicProtocolKnowledge(new PortInputFormatSetupCombinedModeForSetModeDataSetMessage() { PortId = 0, SubCommand = PortInputFormatSetupCombinedSubCommand.SetModeAndDataSetCombination, CombinationIndex = 0, // should refer 0b0000_0000_0000_1110 => SPEED POS APOS ModeDataSets = new PortInputFormatSetupCombinedModeModeDataSet[] { new PortInputFormatSetupCombinedModeModeDataSet() { Mode = 0x01, DataSet = 0, }, new PortInputFormatSetupCombinedModeModeDataSet() { Mode = 0x02, DataSet = 0, }, new PortInputFormatSetupCombinedModeModeDataSet() { Mode = 0x03, DataSet = 0, }, } }, knowledge, serviceProvider.GetService <IDeviceFactory>()); // arrange var data = BytesStringUtil.StringToData(dataAsString); // act var message = MessageEncoder.Decode(data, knowledge) as PortValueCombinedModeMessage; // assert Assert.Equal(expectedPortId, message.PortId); Assert.Collection(message.Data, Enumerable.Range(0, expectedModes.Length).Select <int, Action <PortValueData> >(pos => portValueData => { Assert.Equal(expectedPortId, portValueData.PortId); Assert.Equal(expectedDataType[pos], portValueData.DataType); Assert.Equal(expectedDataType[pos], portValueData.DataType); if (portValueData is PortValueData <sbyte> actual) { Assert.Equal(expectedData[pos], actual.InputValues[0]); } if (portValueData is PortValueData <short> actual2) { Assert.Equal(expectedData[pos], actual2.InputValues[0]); } if (portValueData is PortValueData <int> actual3) { Assert.Equal(expectedData[pos], actual3.InputValues[0]); } }).ToArray()); }