diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..e10e727be5ce2dc48c7d4d86a401a2744e73c8c1 --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/.metadata/ diff --git a/app/src/BluetoothJavaServer/src/edu/kufpg/bluetooth/server/BluetoothManager.java b/app/src/BluetoothJavaServer/src/edu/kufpg/bluetooth/server/BluetoothManager.java new file mode 100644 index 0000000000000000000000000000000000000000..34cf1e378618d17cb99dc05367f7aaad71289a8d --- /dev/null +++ b/app/src/BluetoothJavaServer/src/edu/kufpg/bluetooth/server/BluetoothManager.java @@ -0,0 +1,153 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package BluetoothJavaServer.src.edu.kufpg.bluetooth.server; + +import Vue.FXMLVueController; +import app.App; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import javax.bluetooth.DeviceClass; +import javax.bluetooth.DiscoveryAgent; +import javax.bluetooth.DiscoveryListener; +import javax.bluetooth.LocalDevice; +import javax.bluetooth.RemoteDevice; +import javax.bluetooth.ServiceRecord; +import javax.bluetooth.UUID; +import javax.microedition.io.Connector; +import javax.microedition.io.StreamConnection; + +/** + * + * @author puiss + */ +public class BluetoothManager implements DiscoveryListener { + + private Object lock=new Object(); + private static ArrayList<RemoteDevice> devices; + private final List<ServiceRecord> serviceRecords = new ArrayList<ServiceRecord>(); + private final List<BluetoothConnection> connections = new ArrayList<BluetoothConnection>(); + private BluetoothThread t; + private FXMLVueController observer; + + public BluetoothThread getWorkingThread(){ + return t; + } + + public BluetoothManager(FXMLVueController observer){ + this.devices = new ArrayList<RemoteDevice>(); + this.observer = observer; + } + + @Override + public void deviceDiscovered(RemoteDevice btDevice, DeviceClass arg1) { + String name; + try { + name = btDevice.getFriendlyName(false); + } catch (Exception e) { + name = btDevice.getBluetoothAddress(); + } + + devices.add(btDevice); + System.out.println("device found: " + name); + + } + + @Override + public void inquiryCompleted(int arg0) { + synchronized(lock){ + lock.notify(); + } + } + + + @Override + public void serviceSearchCompleted(int arg0, int arg1) { + synchronized (lock) { + lock.notify(); + } + } + + @Override + public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { + System.out.println("hey ! " + servRecord); + for(ServiceRecord s : servRecord) { + System.out.println("-------"+ s + "-------"); + } + serviceRecords.addAll(Arrays.asList(servRecord)); + } + + + public boolean launch(){ + try{ + UUID serialPortUuid = new UUID(BluetoothServiceUuids.SERIAL_PORT.getHexValue()); + UUID[] desiredServiceUuids = new UUID[] { serialPortUuid }; + LocalDevice localDevice = LocalDevice.getLocalDevice(); + DiscoveryAgent agent = localDevice.getDiscoveryAgent(); + RemoteDevice[] devices = agent.retrieveDevices(DiscoveryAgent.PREKNOWN); + for(RemoteDevice d : devices) { + if(d.getBluetoothAddress().equals("201611073171")) { + System.out.println("Thingz trouve"); + synchronized(lock) { + int result = agent.searchServices(null, desiredServiceUuids, d, this); + if( DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE == result) { + System.out.println(" but it could not be reached"); + continue; + } + lock.wait(); + + + System.out.println("service records found"); + + if(null != serviceRecords && serviceRecords.size() > 0) { + System.out.println("YA DES SERVICES"); + for(ServiceRecord serviceRecord: serviceRecords) { + String connectionUrl = serviceRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); // I have no idea what these args are + StreamConnection connection; + try { + connection = (StreamConnection)Connector.open(connectionUrl); + DataInputStream inputStream = new DataInputStream(connection.openDataInputStream()); + BluetoothConnection bluetoothConnection = new BluetoothConnection(d.getBluetoothAddress(), inputStream); + bluetoothConnection.setOutputStream(new DataOutputStream(connection.openOutputStream())); + connections.add(bluetoothConnection); + System.out.println("Connected to " + d.getBluetoothAddress()); + return true; + } catch (IOException e) { + System.out.println("Could not connect to " + d.getBluetoothAddress() + ": " + e.getMessage()); + e.printStackTrace(); + return false; + } + } + } + } + } + } + } catch(Exception e){ + e.printStackTrace(); + return false; + } + return false; + } + + /** + * + */ + public void startThread() throws Exception { + try { + if(connections.size() > 0) { // we are connected + System.out.println("start"); + this.t = new BluetoothThread(connections.get(0), observer); + t.start(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/app/src/BluetoothJavaServer/src/edu/kufpg/bluetooth/server/BluetoothThread.java b/app/src/BluetoothJavaServer/src/edu/kufpg/bluetooth/server/BluetoothThread.java index 7c125f55434e33af316c5b843374badf6559b3d7..6b9035eccbb0c986c83c003e5427966505bd6eef 100644 --- a/app/src/BluetoothJavaServer/src/edu/kufpg/bluetooth/server/BluetoothThread.java +++ b/app/src/BluetoothJavaServer/src/edu/kufpg/bluetooth/server/BluetoothThread.java @@ -1,45 +1,47 @@ package BluetoothJavaServer.src.edu.kufpg.bluetooth.server; +import Vue.FXMLVueController; import java.io.EOFException; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.util.logging.Level; +import java.util.logging.Logger; import sun.misc.IOUtils; public class BluetoothThread extends Thread { private BluetoothConnection connection; private boolean keepGoing = true; - - public BluetoothThread(BluetoothConnection connection) { + private String buffer; + private FXMLVueController observer; + + public BluetoothThread(BluetoothConnection connection, FXMLVueController observer) { this.connection = connection; + this.observer = observer; } + public void notifyObserver(){ + observer.update(this); + } + + public String getBuffer(){ + return this.buffer; + } + @Override - public void run() { - System.out.println("hey c parti"); - // If there are custom settings to be written to each headset, add them at this point -// try { -// connection.getOutputStreamWriter().write(MindwaveSerialPacket.Codes.DISCONNECT.getHexValue()); -// } catch (IOException e1) { -// // TODO Auto-generated catch block -// e1.printStackTrace(); -// } - + public void run() { while (keepGoing) { - System.out.println("hi"); byte b; try { // Each packet must start with two SYNC bytes in a row - - - System.out.println("New packet found"); - byte[] payload = new byte[100]; + byte[] payload = new byte[16]; connection.getInputStream().read(payload); - String str = new String(payload, StandardCharsets.UTF_8); - System.out.println("payload : " + str); + this.buffer = new String(payload, StandardCharsets.UTF_8); + notifyObserver(); - - connection.getOutputStream().write("STOP".getBytes()); + + //connection.getOutputStream().write("SOUND".getBytes()); + } catch (Exception e) { System.out.println("Could not read stream" + e); @@ -50,7 +52,12 @@ public class BluetoothThread extends Thread { } public void stopThread() { - keepGoing = false; + try { + keepGoing = false; + finalize(); + } catch (Throwable ex) { + Logger.getLogger(BluetoothThread.class.getName()).log(Level.SEVERE, null, ex); + } } @Override diff --git a/app/src/Vue/FXMLVueController.java b/app/src/Vue/FXMLVueController.java index b7f135b0c0c75fd97eb774d025e64fcca23424fd..89a91b438aa4bbdb2942c10ac2ecc0370b1a7ab9 100644 --- a/app/src/Vue/FXMLVueController.java +++ b/app/src/Vue/FXMLVueController.java @@ -5,8 +5,12 @@ */ package Vue; +import BluetoothJavaServer.src.edu.kufpg.bluetooth.server.BluetoothManager; +import BluetoothJavaServer.src.edu.kufpg.bluetooth.server.BluetoothThread; import java.net.URL; import java.util.ResourceBundle; +import java.util.logging.Level; +import java.util.logging.Logger; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; @@ -63,11 +67,24 @@ public class FXMLVueController implements Initializable { @FXML private Label activationMessage; + private boolean isConnected = false; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { + + /** + BluetoothManager bluetoothManager = new BluetoothManager(this); + bluetoothManager.launch(); + + try { + bluetoothManager.startThread(); + } catch (Exception ex) { + Logger.getLogger(FXMLVueController.class.getName()).log(Level.SEVERE, null, ex); + } + **/ + //Put the gridPane Accueil visible accueil.setVisible(true); currentGridPane = accueil; @@ -136,4 +153,9 @@ public class FXMLVueController implements Initializable { private void clickActivationBluetooth(ActionEvent event) { } + public void update(BluetoothThread t){ + // MAJ LA VUE + System.err.println(t.getBuffer()); + } + } diff --git a/app/src/app/.classpath b/app/src/app/.classpath new file mode 100644 index 0000000000000000000000000000000000000000..ac37fb2e4bca5ec7510383d7e55ea7b6b759e05a --- /dev/null +++ b/app/src/app/.classpath @@ -0,0 +1,5 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="output" path="bin"/> +</classpath> diff --git a/app/src/app/.project b/app/src/app/.project new file mode 100644 index 0000000000000000000000000000000000000000..aaec6ee4f9b85fd64d2d3768bd3c0239a21f0c90 --- /dev/null +++ b/app/src/app/.project @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<projectDescription> + <name>app</name> + <comment></comment> + <projects> + </projects> + <buildSpec> + <buildCommand> + <name>org.eclipse.jdt.core.javabuilder</name> + <arguments> + </arguments> + </buildCommand> + </buildSpec> + <natures> + <nature>org.eclipse.jdt.core.javanature</nature> + </natures> +</projectDescription> diff --git a/app/src/app/App.java b/app/src/app/App.java index f297f85eb501fcf5dd3c6decfe9efa4a45a22b86..503619f26ffeef3331337d14c77cbd8f1638f2e9 100644 --- a/app/src/app/App.java +++ b/app/src/app/App.java @@ -44,13 +44,7 @@ import javax.obex.ResponseCodes; * * @author invite */ -public class App extends Application implements DiscoveryListener { - - private static Object lock=new Object(); - private static ArrayList<RemoteDevice> devices; - static DiscoveryListener listener = new App(); - final static List<ServiceRecord> serviceRecords = new ArrayList<ServiceRecord>(); - final static List<BluetoothConnection> connections = new ArrayList<BluetoothConnection>(); +public class App extends Application { @Override public void start(Stage stage) throws Exception{ @@ -66,101 +60,6 @@ public class App extends Application implements DiscoveryListener { * @param args the command line arguments */ public static void main(String[] args) throws IOException { - devices = new ArrayList<RemoteDevice>(); - try{ - UUID serialPortUuid = new UUID(BluetoothServiceUuids.SERIAL_PORT.getHexValue()); - UUID[] desiredServiceUuids = new UUID[] { serialPortUuid }; - LocalDevice localDevice = LocalDevice.getLocalDevice(); - DiscoveryAgent agent = localDevice.getDiscoveryAgent(); - RemoteDevice[] devices = agent.retrieveDevices(DiscoveryAgent.PREKNOWN); - for(RemoteDevice d : devices) { - if(d.getBluetoothAddress().equals("201611073171")) { - System.out.println("Thingz trouve"); - synchronized(lock) { - int result = agent.searchServices(null, desiredServiceUuids, d, listener); - if( DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE == result) { - System.out.println(" but it could not be reached"); - continue; - } - lock.wait(); - - - System.out.println("service records found"); - - if(null != serviceRecords && serviceRecords.size() > 0) { - System.out.println("YA DES SERVICES"); - for(ServiceRecord serviceRecord: serviceRecords) { - String connectionUrl = serviceRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); // I have no idea what these args are - StreamConnection connection; - try { - connection = (StreamConnection)Connector.open(connectionUrl); - DataInputStream inputStream = new DataInputStream(connection.openDataInputStream()); - BluetoothConnection bluetoothConnection = new BluetoothConnection(d.getBluetoothAddress(), inputStream); - bluetoothConnection.setOutputStream(new DataOutputStream(connection.openOutputStream())); - connections.add(bluetoothConnection); - System.out.println("Connected to " + d.getBluetoothAddress()); - } catch (IOException e) { - System.out.println("Could not connect to " + d.getBluetoothAddress() + ": " + e.getMessage()); - e.printStackTrace(); - } - } - } - } - } - } - - if(connections.size() > 0) { // we are connected - System.out.println("start"); - BluetoothThread t = new BluetoothThread(connections.get(0)); - t.start(); - } - } catch (Exception e) { - e.printStackTrace(); - } - - launch(args); - - } - - - - - @Override - public void deviceDiscovered(RemoteDevice btDevice, DeviceClass arg1) { - String name; - try { - name = btDevice.getFriendlyName(false); - } catch (Exception e) { - name = btDevice.getBluetoothAddress(); - } - - devices.add(btDevice); - System.out.println("device found: " + name); - - } - - @Override - public void inquiryCompleted(int arg0) { - synchronized(lock){ - lock.notify(); - } - } - - - @Override - public void serviceSearchCompleted(int arg0, int arg1) { - synchronized (lock) { - lock.notify(); - } - } - - @Override - public void servicesDiscovered(int transID, ServiceRecord[] servRecord) { - System.out.println("hey ! " + servRecord); - for(ServiceRecord s : servRecord) { - System.out.println("-------"+ s + "-------"); - } - serviceRecords.addAll(Arrays.asList(servRecord)); - } - + launch(args); + } } diff --git a/core/thingz.ino.txt b/core/thingz.ino.txt index 50b98d5e6c28b34337d8a2e5d9642a86e3cc809b..36775131cf07ff28b85641d851ba0bb1c137643f 100644 --- a/core/thingz.ino.txt +++ b/core/thingz.ino.txt @@ -6,9 +6,11 @@ #include "Bluetooth.h" #include "Son.h" #include "Potentiometer.h" +#include "MotionSensor.h" #define SPAN 3000 #define HEADER_LIGHT_ON "LIGHT" #define HEADER_SOUND_ON "SOUND" +#define HEADER_PLANT_OK "STOP" #define REMAINING_TIME 300000 // 5MIN #define DATASPAN 10000 // 5MIN #define ALARMSPAN 10000 // 10SEC @@ -40,6 +42,7 @@ Luminosite luminosite; Potentiometer pot; Son buzzer; Bluetooth bluetooth; +MotionSensor motion; int humi; int lumi; @@ -49,6 +52,7 @@ int dataTimeStamp; unsigned long timeStamp; boolean ledHasBeenTurnedOn; boolean alarmHasBeenTurnedOn; +boolean plantInNeed; unsigned long timePushed; unsigned long alarmTimeStamp; unsigned long lightTimeStamp; @@ -63,7 +67,11 @@ void updateSensors(){ void display(){ monEcran1.setContrast(70, false); updateSensors(); - monEcran1.printMsg("CRYING PLANT !",0); + if(plantInNeed){ + monEcran1.printMsg("CRYING PLANT !",0); + } else { + monEcran1.printMsg("PLANT IS OK !",0); + } monEcran1.printMsg(((String)"TEMP : ") + temp,2); monEcran1.printMsg(((String)"HUMI : ") + humi,3); monEcran1.printMsg(((String)"LUMI : ") + lumi,4); @@ -96,9 +104,13 @@ void receiveFromBluetooth() { monEcran1.printMsg(buffer,0); if (buffer.startsWith(HEADER_LIGHT_ON)){ dealWithLight(); + plantInNeed = true; } else if (buffer.startsWith(HEADER_SOUND_ON)){ dealWithSound(); + plantInNeed = true; + } else if(buffer.startsWith(HEADER_PLANT_OK)) { + plantInNeed = false; } } @@ -132,6 +144,7 @@ void setup() timeStamp = 0; ledHasBeenTurnedOn = false; alarmHasBeenTurnedOn = false; + plantInNeed = false; timePushed = 0; alarmTimeStamp = 0; dataTimeStamp = 0; @@ -180,7 +193,12 @@ if(millis() - timePushed < REMAINING_TIME){ ledHasBeenTurnedOn = false; maLed1.eteint(); } - } + } + + if(plantInNeed && motion.detectsStartOfMotion()){ + dealWithSound(); + dealWithLight(); + } }