Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
serialization fix for products
Browse files Browse the repository at this point in the history
graphical fixes
added script with some products
  • Loading branch information
MartinoMensio committed Jun 24, 2017
1 parent 1f6eb1c commit bc48002
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 61 deletions.
67 changes: 51 additions & 16 deletions ServerApplicationWPF/ServerApplicationWPF/AddProduct.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,57 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ServerApplicationWPF"
mc:Ignorable="d"
Title="AddProduct" Height="300" Width="300">
<Grid>
<Label x:Name="NameLabel" Content="Product name:" HorizontalAlignment="Left" Margin="10,36,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="NameValue" HorizontalAlignment="Left" Height="23" Margin="10,62,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" />
<Label x:Name="PriceLabel" Content="Price:" HorizontalAlignment="Left" Margin="135,36,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="PriceValue" HorizontalAlignment="Left" Height="23" Margin="135,62,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" PreviewTextInput="PriceNumberValidationTextBox"/>
<Label x:Name="BarcodeLabel" Content="Barcode:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<Label x:Name="BarcodeValue" Content="" HorizontalAlignment="Left" Margin="74,10,0,0" VerticalAlignment="Top"/>
<Label x:Name="PointsLabel" Content="Points:" HorizontalAlignment="Left" Margin="10,85,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="PointsValue" HorizontalAlignment="Left" Height="23" Margin="10,111,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" PreviewTextInput="IntNumberValidationTextBox" />
<Label x:Name="StoreQtyLabel" Content="Store quantity:" HorizontalAlignment="Left" Margin="135,85,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="StoreQtyValue" HorizontalAlignment="Left" Height="23" Margin="135,111,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" PreviewTextInput="IntNumberValidationTextBox" />
<Label x:Name="WarehouseQtyLabel" Content="Warehouse quantity:" HorizontalAlignment="Left" Margin="10,134,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="WarehouseQtyValue" HorizontalAlignment="Left" Height="23" Margin="10,160,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" PreviewTextInput="IntNumberValidationTextBox" />
<Button x:Name="CancelButton" Content="Cancel" HorizontalAlignment="Left" Margin="127,239,0,0" VerticalAlignment="Top" Width="75" Click="CancelButton_Click"/>
<Button x:Name="ConfirmButton" Content="Confirm" HorizontalAlignment="Left" Margin="207,239,0,0" VerticalAlignment="Top" Width="75" Click="ConfirmButton_Click"/>
Title="AddProduct" Height="209" Width="727" ResizeMode="NoResize">
<Grid Background="WhiteSmoke">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition />
<RowDefinition Height="50"/>
</Grid.RowDefinitions>

<Label Grid.Row="0" x:Name="BarcodeLabel" Content="Barcode:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<Label Grid.Row="0" x:Name="BarcodeValue" Content="barcode placeholder" HorizontalAlignment="Left" Margin="74,10,0,0" VerticalAlignment="Top"/>

<Button Grid.Row="2" x:Name="CancelButton" Content="Cancel" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="90" Click="CancelButton_Click" Margin="0,0,100,0" Background="LightGray" Height="50"/>
<Button Grid.Row="2" x:Name="ConfirmButton" Content="Confirm" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="90" Click="ConfirmButton_Click" Background="White" Height="50" />
<UniformGrid Grid.Row="1" Columns="2">
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="130" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" x:Name="NameLabel" Content="Product name:" />
<TextBox Grid.Column="1" Grid.Row="0" x:Name="NameValue" TextWrapping="Wrap" Text="TextBox" />
<Label Grid.Column="0" Grid.Row="1" x:Name="PointsLabel" Content="Points:" HorizontalAlignment="Left"/>
<TextBox Grid.Column="1" Grid.Row="1" x:Name="PointsValue" TextWrapping="Wrap" Text="TextBox" PreviewTextInput="IntNumberValidationTextBox" />
<Label Grid.Column="0" Grid.Row="2" x:Name="WarehouseQtyLabel" Content="Warehouse quantity:"/>
<TextBox Grid.Column="1" Grid.Row="2" x:Name="WarehouseQtyValue" TextWrapping="Wrap" Text="TextBox" PreviewTextInput="IntNumberValidationTextBox" />
</Grid>
<Grid Grid.Column="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" x:Name="PriceLabel" Content="Price:" />
<TextBox Grid.Column="1" Grid.Row="0" x:Name="PriceValue" TextWrapping="Wrap" Text="TextBox" PreviewTextInput="PriceNumberValidationTextBox"/>
<Label Grid.Column="0" Grid.Row="1" x:Name="StoreQtyLabel" Content="Store quantity:"/>
<TextBox Grid.Column="1" Grid.Row="1" x:Name="StoreQtyValue" TextWrapping="Wrap" Text="TextBox" PreviewTextInput="IntNumberValidationTextBox" />
</Grid>

</UniformGrid>

</Grid>
</Window>
10 changes: 6 additions & 4 deletions ServerApplicationWPF/ServerApplicationWPF/AddProduct.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public partial class AddProduct : Window
public AddProduct(Product product, DataManager dataManager)
{
InitializeComponent();
Style = (Style)FindResource(typeof(Window));
this.product = product;
this.dataManager = dataManager;
BarcodeValue.Content = product.Barcode;
NameValue.Text = product.Product_name;
PriceValue.Text = product.Price.ToString("N2");
PriceValue.Text = product.Price;
PointsValue.Text = product.Points.ToString();
StoreQtyValue.Text = product.StoreQty.ToString();
WarehouseQtyValue.Text = product.WarehouseQty.ToString();
Expand All @@ -56,12 +57,13 @@ private void CancelButton_Click(object sender, RoutedEventArgs e)
private void ConfirmButton_Click(object sender, RoutedEventArgs e)
{
product.Product_name = NameValue.Text;
product.Price = double.Parse(PriceValue.Text);
product.Points = int.Parse(PointsValue.Text);
product.Price = PriceValue.Text;
product.Points = PointsValue.Text;
double price = double.Parse(product.Price);
product.StoreQty = int.Parse(StoreQtyValue.Text);
product.WarehouseQty = int.Parse(WarehouseQtyValue.Text);

if (product.Price <= 0)
if (price <= 0)
{
MessageBox.Show("Price must be greater than 0");
}
Expand Down
4 changes: 3 additions & 1 deletion ServerApplicationWPF/ServerApplicationWPF/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
xmlns:local="clr-namespace:ServerApplicationWPF"
StartupUri="MainWindow.xaml">
<Application.Resources>

<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Segoe UI semilight" />
</Style>
</Application.Resources>
</Application>
10 changes: 4 additions & 6 deletions ServerApplicationWPF/ServerApplicationWPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
xmlns:local="clr-namespace:ServerApplicationWPF"
mc:Ignorable="d"
Title="Server Application" Height="400" Width="720" Closed="Window_Closed" Icon="Resources/shop-cash-register-icon-87069.png" MinHeight="300" MinWidth="500">
<Grid Background="#FF2BA842">
<Grid Background="WhiteSmoke">
<ScrollViewer Name="MyScrollViewer" Margin="0,250,0,0">
<TextBlock x:Name="Log" TextWrapping="Wrap" Background="#FFAEAEAE"/>
<TextBlock x:Name="Log" TextWrapping="Wrap" Background="LightGray" FontFamily="Consolas"/>
</ScrollViewer>
<Image x:Name="ImageDisplay" HorizontalAlignment="Left" VerticalAlignment="Top" Width="320" Height="198" />
<TextBox x:Name="barcode_txt" HorizontalAlignment="Right" Height="24" Margin="0,9,92,0" TextWrapping="Wrap" Text="Search barcode" VerticalAlignment="Top" Width="120"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Right" Margin="0,10,10,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
<TextBlock x:Name="db_output" HorizontalAlignment="Right" Margin="0,38,92,0" TextWrapping="Wrap" Text="Product name" VerticalAlignment="Top" Background="#FFBFBFBF" Height="21" Width="119"/>
<CheckBox x:Name="AllowRemoteProduct" Content="Allow remote product search" HorizontalAlignment="Left" Margin="242,9,0,0" VerticalAlignment="Top" Checked="checkbox_checked" Unchecked="checkbox_unchecked"/>
<CheckBox x:Name="AllowRemoteProduct" Content="Enable adding new products" HorizontalAlignment="Center" Margin="0,9,0,0" VerticalAlignment="Top" Checked="checkbox_checked" Unchecked="checkbox_unchecked" HorizontalContentAlignment="Center"/>
<Label x:Name="label" Content="LOG:" HorizontalAlignment="Left" Margin="0,224,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
40 changes: 14 additions & 26 deletions ServerApplicationWPF/ServerApplicationWPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public MainWindow()


InitializeComponent();
Style = (Style)FindResource(typeof(Window));
reader = new BarcodeReader();
networkDriver = new NetworkDriver(requestProcessing, messageProcessing);
onlineProductManager = new OnlineProductManager(dbConnect, barcodesToBeSearchedOnline);
Expand All @@ -82,23 +83,17 @@ private NetworkResponse requestProcessing(NetworkRequest request)
Bitmap image2 = new Bitmap(image);
// show the image
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new ImageConsumer(displayImage), image2);
// do the barcode scan
ArrayList barcodes = new ArrayList();
//BarcodeScanner.FullScanPage(ref barcodes, image, 100);

var result = reader.Decode(image);

//string result = codeScanner.ScanPage(image);
int rotation = 0;
while (result == null && rotation < 36)
int rotations = 0;
Result result = null;
// attempt recognition of codes multiple times rotating the images
do
{
//image = rotateImage90(image);
var image_tmp = RotateImg(image, rotation * 10, System.Drawing.Color.Transparent);
var image_tmp = RotateImg(image, rotations * 10, System.Drawing.Color.Transparent);
result = reader.Decode(image_tmp);
rotation++;
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new ImageConsumer(displayImage), image_tmp);
rotations++;
}
//messageProcessing("Scan done. Found " + barcodes.Count + "barcodes");
while (result == null && rotations < 36);

NetworkResponse response;
if (result != null)
{
Expand Down Expand Up @@ -148,8 +143,7 @@ private NetworkResponse requestProcessing(NetworkRequest request)
}
else
{
messageProcessing("No barcodes found");
//response = new NetworkResponse(NetworkResponse.ResponseType.ImageProcessingResult, Encoding.UTF8.GetBytes("{\"ID\":\"10\",\"Name\":\"Martino\",\"Surname\":\"Mensio\",\"Email\":\"[email protected]\"}"));
messageProcessing("No codes found");
response = new NetworkResponse(NetworkResponse.ResponseType.ImageProcessingError, Utils.StringToBytes("Error"));
}
return response;
Expand All @@ -158,10 +152,11 @@ private NetworkResponse requestProcessing(NetworkRequest request)
{
try
{
string req = UDPNetwork.Utils.BytesToString(request.Payload);
string req = Utils.BytesToString(request.Payload);
JObject receipt = JObject.Parse(req);
// get customerId
String userId = receipt["UserID"].ToString();
messageProcessing("received a receipt with userId: " + userId);
JArray list = receipt["List"] as JArray;
IList<JToken> products = list.Children().ToList();
Receipt receiptObj = new Receipt(userId);
Expand All @@ -183,22 +178,20 @@ private NetworkResponse requestProcessing(NetworkRequest request)
catch (Exception e)
{
// some exception
Console.WriteLine("Exception catched reading a receipt: " + e.Message);
messageProcessing("Exception catched processing the receipt: " + e.Message);
return new NetworkResponse(NetworkResponse.ResponseType.ReceiptStorageError, Utils.StringToBytes("Error"));
}
}
else
{
// some errors
Console.WriteLine("Unknown request type");
messageProcessing("Unknown request type");
return new NetworkResponse(NetworkResponse.ResponseType.ReceiptStorageError, Utils.StringToBytes("Error"));
}
}

private void displayImage(Bitmap image)
{
//addMessageToLog("Trying to display image");
//ImageDisplay.Source = (ImageSource)new ImageSourceConverter().ConvertFrom(image);
ImageDisplay.Source = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(image.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}

Expand All @@ -220,11 +213,6 @@ private void Window_Closed(object sender, EventArgs e)
//Application.Current.Shutdown(); not necessary because other thread is background
}

private void button_Click(object sender, RoutedEventArgs e)
{
barcodesToBeSearchedOnline.Add(barcode_txt.Text);
}

private Bitmap rotateImage90(Bitmap b)
{
Bitmap returnBitmap = new Bitmap(b.Height, b.Width);
Expand Down
13 changes: 6 additions & 7 deletions ServerApplicationWPF/ServerApplicationWPF/Model/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class Product
public string ID { get; private set; }
public string Barcode { get; private set; }
public string Product_name { get; set; }
public double Price { get; set; }
public int Points { get; set; }
public string Price { get; set; }
public string Points { get; set; }
public int StoreQty { get; set; }
public int WarehouseQty { get; set; }
public String Type { get; private set; }
Expand All @@ -23,18 +23,17 @@ public Product(string id, string barcode, string name, double price, int points,
ID = id;
Barcode = barcode;
Product_name = name;
Price = price;
Points = points;
Price = price.ToString("N2");
Points = points.ToString();
StoreQty = storeQty;
this.WarehouseQty = warehouseQty;
Type = "product";
}

public override string ToString()
{
//return JsonConvert.SerializeObject(this);
// TODO remove this crap, use properly json
return "{\"Type\":\"product\",\"ID\":\"" + ID + "\",\"Product_name\":\"" + Product_name + "\",\"Price\":\"" + Price + "\",\"Points\":\"" + Points + "\"}";
return JsonConvert.SerializeObject(this);
//return "{\"Type\":\"product\",\"ID\":\"" + ID + "\",\"Product_name\":\"" + Product_name + "\",\"Price\":\"" + Price + "\",\"Points\":\"" + Points + "\"}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Utils
public static string NACK = "nack";
public static int RECEIVE_TIMEOUT = 1000; // timeout between net request and response/ack or between token generation and usage
public static int FRAGMENT_TIMEOUT = 40; // 10 ms for retransimssion
public static int MAX_RETRY = 50;
public static int MAX_RETRY = 100;


public static byte[] StringToBytes(string s)
Expand Down
6 changes: 6 additions & 0 deletions some_products.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
INSERT INTO `product` (`Barcode`, `Name`, `Price`, `StoreQty`, `WarehouseQty`, `Points`) VALUES
(8000340479919, 'SKIPPER kiwi mela', 1.5, 10, 40, 15),
(90370762, 'coca cola', 1, 10, 40, 10),
(5410041000900, 'TUC crackers', 1.2, 8, 10, 7),
(80007951, 'Acqua san Benedetto', 0.25, 3, 20, 5),
(8010333001850, 'Acqua Martina frizzante', 0.50, 7, 20, 5);

0 comments on commit bc48002

Please sign in to comment.