// Always pair this struct with ArduinoVendingMachine/blob/master/src_arduino/entities/product.h private byte[] ToBytes(ProductRail product) { var result = new byte[PRODUCT_SIZE]; var priceDouble = Convert.ToDouble(product.Product.Price); var priceBytes = BitConverter.GetBytes(priceDouble); var nameBytes = product.Product.DisplayName .PadRight(20) .Substring(0, 20) .Select(Convert.ToByte); //TODO: Review int type inconsistency result[0] = (byte)product.ProductId; result[1] = (byte)product.Helix; result[2] = (byte)product.Count; result[3] = priceBytes[0]; result[4] = priceBytes[1]; result[5] = priceBytes[2]; result[6] = priceBytes[3]; Array.Copy( sourceArray: nameBytes.ToArray(), sourceIndex: 0, destinationArray: result, destinationIndex: 7, length: 20); return(result); }
private static async Task PopulateProductRailAsync(VendingMachineDbContext context) { if (context.ProductRails.Any()) { return; } var machine = context.Machines.Find(1); var products = context.Products.ToList(); for (int i = 1; i <= 16; i++) { var productRail = new ProductRail { Machine = machine, Helix = i, Count = 3, Product = products[i - 1] }; await context.ProductRails.AddAsync(productRail); } }