private async void OnInputTelemetryReceived(object sender, BeerliftMessage message) { if (message == null || message.deviceId != deviceId) { return; } var changedIndexers = ProcessChanges(_lastBeerliftMessage, message); foreach (var changedIndex in changedIndexers) { var bottleHolder = (from x in Bottleholders where x.indexer.ToString() == changedIndex.ToString() select x).First(); var state = message.IsSlotInUse(changedIndex) ? "occupied" : ""; _sqliteService.UpdateBottleHolderState(deviceId, moduleName, changedIndex, state); } Bottleholders = _sqliteService.GetBottleHolders(deviceId, moduleName); // Remember _lastBeerliftMessage = message; await InvokeAsync(() => StateHasChanged()); }
public async Task AddBottle() { _busyService.SetBusy(true); addingBeerBottle = true; try { var emptySlotId = 0; BottleActionText = string.Empty; if (string.IsNullOrEmpty(BottleBrandAndMake)) { BottleActionText = "Enter brand and make"; return; } BottleActionText = "Searching for empty slot..."; var response = await _ioTHubServiceClientService.SendDirectMethod <FindEmptySlotRequest, FindEmptySlotResponse>(deviceId, moduleName, "FindEmptySlot", new FindEmptySlotRequest()); if (response.ResponseStatus == 200) { emptySlotId = response.FindEmptySlotPayload.emptySlot; } if (emptySlotId == 0) { BottleActionText = "No empty slot available "; return; } BottleActionText = $"Found empty slot {emptySlotId}, Place the bottle"; await InvokeAsync(() => StateHasChanged()); var placed = false; var i = 0; while (!placed) { if (i == 4) { // Max 4 attempts. break; } i++; BottleActionText = $"Found empty slot {emptySlotId}, Place the bottle... ({i})"; await InvokeAsync(() => StateHasChanged()); var beerHoldersResponse = await _ioTHubServiceClientService.SendDirectMethod <BottleHoldersRequest, BottleHoldersResponse>(deviceId, moduleName, "BottleHolders", new BottleHoldersRequest()); if (beerHoldersResponse.ResponseStatus == 200) { await _telemetryService.SendTelemetry(beerHoldersResponse.BeerHoldersPayload.BeerLiftMessage); _lastBeerliftMessage = beerHoldersResponse.BeerHoldersPayload.BeerLiftMessage; if (_lastBeerliftMessage.IsSlotInUse(emptySlotId)) { // The correct bottle is placed placed = true; break; } } await MarkPosition(emptySlotId); } if (placed) { BottleActionText = $"Bottle is '{BottleBrandAndMake}' placed"; _sqliteService.PutBottleHolder(deviceId, moduleName, emptySlotId, BottleBrandAndMake, "occupied"); Bottleholders = _sqliteService.GetBottleHolders(deviceId, moduleName); BottleBrandAndMake = string.Empty; await InvokeAsync(() => StateHasChanged()); await Task.Delay(5000); } else { BottleActionText = $"Time out, please try again"; await InvokeAsync(() => StateHasChanged()); await Task.Delay(5000); } } finally { addingBeerBottle = false; _busyService.SetBusy(false); } }