Quote:
I am attempting to create my own version of a smart fridge concept design that essentially sends the weighted value of a grocery item on an external USB weighing scale(for my example i'll be using a carton of 6 eggs). It will store the 6 eggs in a MySQL database table named 'fridge' and each time an egg is removed it will add one to a database table 'shopping list'. The shopping list table will communicate with an online grocery API( I am using Tesco's API: https://devportal.tescolabs.com/products/56c73300d73fa303ed060001).
import java.io.Console;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.usb.UsbConfiguration;
import javax.usb.UsbDevice;
import javax.usb.UsbDeviceDescriptor;
import javax.usb.UsbEndpoint;
import javax.usb.UsbException;
import javax.usb.UsbHostManager;
import javax.usb.UsbHub;
import javax.usb.UsbInterface;
import javax.usb.UsbPipe;
import javax.usb.UsbServices;
import javax.usb.event.UsbPipeDataEvent;
import javax.usb.event.UsbPipeErrorEvent;
import javax.usb.event.UsbPipeListener;
public class UsbScale implements UsbPipeListener {
private final UsbDevice device;
private UsbInterface iface;
private UsbPipe pipe;
private byte[] data = new byte[6];
private UsbScale(UsbDevice device) {
this.device = device;
}
public static void main(String[] args) throws UsbException {
UsbScale scale = UsbScale.findScale();
scale.open();
try {
for (boolean i = true; i; i = true) {
scale.syncSubmit();
}
} finally {
scale.close();
}
}
public static UsbScale findScale() throws UsbException {
UsbServices services = UsbHostManager.getUsbServices();
UsbHub rootHub = services.getRootUsbHub();
UsbDevice device = findDevice(rootHub, (short) 0x0922, (short) 0x8003);
if (device == null) {
device = findDevice(rootHub, (short) 0x0922, (short) 0x8005);
}
if (device == null) {
return null;
}
return new UsbScale(device);
}
private static UsbDevice findDevice(UsbHub hub, short vendorId, short productId) {
for (UsbDevice device : (List<UsbDevice>) hub.getAttachedUsbDevices()) {
UsbDeviceDescriptor desc = device.getUsbDeviceDescriptor();
if (desc.idVendor() == vendorId && desc.idProduct() == productId) {
return device;
}
if (device.isUsbHub()) {
device = findDevice((UsbHub) device, vendorId, productId);
if (device != null) {
return device;
}
}
}
return null;
}
private void open() throws UsbException {
UsbConfiguration configuration = device.getActiveUsbConfiguration();
iface = configuration.getUsbInterface((byte) 0);
iface.claim(usbInterface -> true);
final List<UsbEndpoint> endpoints = iface.getUsbEndpoints();
pipe = endpoints.get(0).getUsbPipe();
pipe.addUsbPipeListener(this);
pipe.open();
}
private void syncSubmit() throws UsbException {
pipe.syncSubmit(data);
}
public void close() throws UsbException {
pipe.close();
iface.release();
}
@Override
public void dataEventOccurred(UsbPipeDataEvent upde) {
boolean empty = data[1] == 2;
boolean overweight = data[1] == 6;
boolean negative = data[1] == 5;
boolean grams = data[2] == 2;
int scalingFactor = data[3];
int weight = (data[4] & 0xFF) + (data[5] << 8);
boolean EggsOnScale = false;
int[] EggsWeight = { 440, 460 };
int[] OneEgg = { 60, 75 };
int count = 0;
int oldweight=0;
if (oldweight == weight) {
} else {
System.out.println("Weight Changes");
oldweight=weight;
}
if (weight != oldweight) {
while (count <= 5000) {
count++;
}
while (count < 5)
weight = count++;
System.out.println("My Weight: " + weight);
if(EggsWeight[0] <= weight && weight <= EggsWeight[1]) {
EggsOnScale = true;
System.out.println("6 Eggs on scale");
try {
String myDriver = "com.mysql.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/smartfridge?autoReconnect=true&useSSL=false";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "root", "admin");
String query = " insert into fridge (name, UnitOfSale, ContentsQuantity, department, AverageSellingUnitWeight)"
+ " values (?, ?, ?, ?, ?)";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1, "Eggs");
preparedStmt.setInt(2, 1);
preparedStmt.setInt(3, 6);
preparedStmt.setString(4, "Milk, Butter & Eggs");
preparedStmt.setBoolean(5, EggsOnScale);
preparedStmt.execute();
} catch (Exception e) {
}
} else{
oldweight = weight;
}
}
}
private double scaleWeight(int weight, int scalingFactor) {
return weight * Math.pow(10, scalingFactor);
}
@Override
public void errorEventOccurred(UsbPipeErrorEvent upee) {
Logger.getLogger(UsbScale.class.getName()).log(Level.SEVERE, "Scale Error", upee);
}
}
What I have tried:
Quote:
I have made headway with it so far where I am able to get the weight of the eggs to print out 'Weight Changes'(As shown in my main class pasted below) once the weight of the eggs falls in between the values array I have set. Basically I am stuck on the next part of the project which is writing the values to the database tables everytime the weight changes. I have the database connection and a query that inserts the eggs values in once the weight is the same as the EggsWeight integer but I want the database to update the quantity values consistently in accordance to the weight changing. Does anyone have any guidance or suggestions on how to go about this? Any feedback would be appreciated.