Esse commit está contido em:
vsenger
2013-06-04 13:55:51 -03:00
commit 73497f7c41
51 arquivos alterados com 437 adições e 578 exclusões
+8 -1
Ver Arquivo
@@ -1,3 +1,10 @@
/api/Things4Java/jHomeWebAdmin/target/
/api/Things4Java/Things.Prototipo/target/
/apps/jHomeAutomation/target/
/apps/jHomeAutomation/target/
/api/Things4Java/Things.Gateway.Embedded/target/
/api/Things.Gateway.REST/nbproject/private/
/api/Things4Java/Things.Gateway.REST/nbproject/private/
/api/Things4Java/Things.Gateway.REST/build/
/api/Things4Java/Things.Gateway.REST/dist/
/apps/DNTV/target/
/api/Things4Java/Things.Robot/target/
-103
Ver Arquivo
@@ -1,103 +0,0 @@
#include <Component.h>
#include <Arduino.h>
Component::Component() {}
//very high cost to have each state in a string, this var is for all objects
char readValue[15];
Component::Component(char* name1, int type1, int port1) {
name=name1;
type=type1;
port=port1;
if(type==DIGITAL) {
pinMode(port, OUTPUT);
}
emptyReadValue();
state=0;
}
char* Component::getValue() {
itoa(state,readValue, 10);
return readValue;
}
char* Component::getTypeName() {
static char* typeNames[] = {"DIGITAL", "ANALOG", "PWM", "RELAY", "LIGHT", "TEMPERATURE", "SERIAL", "LIB", "PING"};
return typeNames[type];
}
char* Component::write(char* c1) {
char* r="\0";
if(type==DIGITAL || type==RELAY) {
state = atoi(c1);
digitalWrite(port, state);
return r;
}
else if(type==PWM) {
state = atoi(c1);
analogWrite(port, atoi(c1));
return r;
}
else if(type==SERIAL) {
Serial.print(c1);
return r;
}
}
void Component::emptyReadValue() {
for(int x=0;x<15;x++) {
readValue[x]='\0';
}
}
char* Component::read() {
emptyReadValue();
if(type==ANALOG || type==LIGHT || type==TEMP) {
//in my country we call this code as "gambiarra" #Gambiarrafeelings
//mas na verdade o sensor de temperatura analógico causa!
state = analogRead(port);
delay(5);
state = analogRead(port);
delay(5);
state = analogRead(port);
delay(5);
return getValue();
}
else if(type==DIGITAL || type==RELAY || type==PWM) {
pinMode(port, INPUT);
state = digitalRead(port);
pinMode(port,OUTPUT);
return getValue();
} else if(type==SERIAL) {
int counter=0;
while(Serial.available()>0 && counter<15)
{
char c = Serial.read();
delay(5);
readValue[counter++]=c;
}
return readValue;
} else if(type==LIB) {
//should call function pointer here...
} else if(type==PING) {
//Parallax ping based on digital pulseIn
pinMode(port, OUTPUT);
digitalWrite(port, LOW);
delayMicroseconds(2);
digitalWrite(port, HIGH);
delayMicroseconds(5);
digitalWrite(port, LOW);
pinMode(port, INPUT);
//long duration1 = pulseIn(port, HIGH);
//long cm1 = duration1 / 29 /2;
//Serial.print("Duration: ");
//Serial.println(duration1);
//Serial.print("CM : ");
//Serial.println(cm1);
state = pulseIn(port, HIGH);
return getValue();
}
else return "\0";
}
-37
Ver Arquivo
@@ -1,37 +0,0 @@
#ifndef COMPONENT_H
#define COMPONENT_H
#include "Arduino.h"
#define DIGITAL 0
#define ANALOG 1
#define PWM 2
#define RELAY 3
#define LIGHT 4
#define TEMP 5
#define SERIAL 6
#define LIB 7
#define PING 8
#define ALL 9
class Component {
private:
public:
char* name;
int type;
int port;
int state;
Component(char*, int, int);
Component();
char* getValue();
char* write(char *);
char* read();
char* getTypeName();
void emptyReadValue();
};
#endif
-154
Ver Arquivo
@@ -1,154 +0,0 @@
#include "Device.h"
#include "Arduino.h" // Arduino 1.0
#include "Component.h"
Device::Device() {
componentIndex=0;
serial=true;
network=false;
found=false;
name = "noname1";
}
Device::Device(char* name1) {
componentIndex=0;
serial=true;
network=false;
found=false;
name = name1;
}
char command1[10];
char parameter1[10];
char return1[10];
void Device::add(char* name, int type, int port) {
Component c1 = Component(name, type,port);
components[numberOfComponents++]= c1;
}
void Device::add(char* name, int type, int port, char*(*readFunction)(), void(*writeFunction)(char*)) {
Component c1 = Component(name, type,port);
components[numberOfComponents++]= c1;
}
int Device::findComponent(char* name) {
int n = 0;
found=false;
//Serial.println("searching component...");
for(int x=0;x<numberOfComponents;x++) {
//Serial.print(x);
//Serial.print(" - Name: ");
//Serial.println(name);
if(strcmp(name,components[x].name)==0) {
n = x;
found=true;
break;
}
}
return n;
}
void Device::loop() {
if(serial && Serial.available()) {
serialServer();
}
}
void split(char* command) {
for(int x=0;x<15;x++) {
command1[x]='\0';
parameter1[x]='\0';
}
int cv=0;
int cp=0;
for(int x=0;x<15;x++) {
if(command[x]!='?' && command[x]!='\0') {
command1[cv++]=command[x];
} else {
cp = command[x]=='?' ? x : 0;
break;
}
}
if(cp>0) {
for(int x=cp+1;x<15;x++) {
parameter1[x-cp-1]=command[x];
if(command[x]=='\0') break;
}
}
}
char* Device::execute(char* command) {
split(command);
Component &c = components[findComponent(command1)];
if(!found) {
//Serial.println("component not found");
return "\n";
}
if(parameter1[0]=='\0') {
//Serial.println("reading");
char* r=c.read();
return r;
}
else {
//Serial.println("writing");
return c.write(parameter1);
}
}
void Device::serialServer() {
char command[15];
int counter=0;
while(Serial.available()>0)
{
char c = Serial.read();
delay(1);
command[counter++]=c;
}
if(counter>0) {
command[counter]='\0';
if(strcmp("discovery",command)==0) {
//Serial.println("discovery serial");
discoverySerial();
return;
}
//Serial.println("executing command...");
//Serial.println(command);
char* r = execute(command);
//Serial.println("executed command. returned value:");
//Serial.println(r);
if(r[0]!='\0') {
Serial.print(r);
//Serial.print('\0');
Serial.flush();
}
}
}
void Device::discoverySerial() {
Serial.print(name);
Serial.print("|");
Serial.print(numberOfComponents);
Serial.print("|");
for(int x=0;x<numberOfComponents;x++) {
Serial.print(components[x].name);
Serial.print("|");
Serial.print(components[x].getTypeName());
Serial.print("|");
Serial.print(components[x].port);
Serial.print("|");
Serial.print(components[x].getValue());
Serial.print("|");
}
}
-36
Ver Arquivo
@@ -1,36 +0,0 @@
#ifndef DEVICE_H
#define DEVICE_H
#include "Component.h"
#include "Arduino.h"
#define MAX 15
#define I2C 99
class Device {
private:
bool debug;
bool found;
int componentIndex;
public:
Device();
Device(char*);
int numberOfComponents;
bool serial;
bool network;
Component components[MAX];
void add(char*, int, int);
void add(char*, int, int,char*(*readFunction)(), void(*writeFunction)(char*));
void loop();
String discoveryString();
int findComponent(char*) ;
void serialServer();
char* execute(char *);
char* name;
void printComponents(int type);
void discoverySerial();
};
#endif
+1 -1
Ver Arquivo
@@ -26,7 +26,7 @@
<groupId>org.rxtx</groupId>
<artifactId>rxtx</artifactId>
<version>2.1.7</version>
<scope>provided</scope>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
@@ -32,6 +32,7 @@ public class Things {
try {
Thread.sleep(milis);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
protected Collection<Device> devices;
@@ -43,7 +44,7 @@ public class Things {
for (Device device : devices) {
t += device.getThings().size();
}
String retornao = "jhome-server|" + t + "|";
String retornao = "things-server|" + t + "|";
for (Device device : devices) {
for (String c : device.getThings().keySet()) {
@@ -84,6 +85,12 @@ public class Things {
found.setLastValue(r);
} catch (Exception ex) {
Logger.getLogger(Things.class.getName()).log(Level.SEVERE, null, ex);
try {
found.getDevice().close();
} catch (IOException ex1) {
}
devices.remove(found.getDevice());
devicesTable.remove(found.getDevice().getID());
}
} else {
Logger.getLogger(Things.class.getName()).log(Level.INFO, "Component " + thing + " not found!");
@@ -110,6 +117,43 @@ public class Things {
return r;
}
public synchronized String execute(String deviceName, String thing, String args) {
timeControl();
if (devices == null) {
devices = new ArrayList<Device>();
devicesTable = new HashMap<String, Device>();
}
Device device = null;
if (!devicesTable.containsKey(deviceName)) {
device =
new SerialDevice(deviceName, 115200);
try {
device.open();
this.devices.add(device);
this.devicesTable.put(deviceName, device);
} catch (IOException ex) {
Logger.getLogger(Things.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}else {
device = devicesTable.get(deviceName);
}
String r = null;
try {
device.send(thing + "?" + args);
if (device instanceof SerialDevice) {
Things.delay(40);//era 30
}
r = device.receive();
} catch (Exception ex) {
Logger.getLogger(Things.class.getName()).log(Level.SEVERE, null, ex);
}
//Logger.getLogger(Things.class.getName()).log(Level.INFO, "Component " + thing + " not found!");
return r;
}
public void close() {
if (devices == null || devices.isEmpty()) {
return;
@@ -192,4 +236,8 @@ public class Things {
/*public Collection<Device> discoveryBluetooth(String args) throws Exception {
return this.discoverySerial(args);
}*/
public Thing bluetooth(String device) {
return null;
}
}
@@ -14,7 +14,6 @@ import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
import org.things.Thing;
import org.things.Things;
@@ -22,7 +21,7 @@ import org.things.Things;
*
* @author vsenger
*/
public class SerialDevice implements Device {
public class I2CDevice implements Device {
private static final int DEFAULT_BAUDRATE = 115200;
final static int DISCOVERY_RETRY = 3;
@@ -39,14 +38,14 @@ public class SerialDevice implements Device {
Map<String, Thing> things;
Collection<Thing> thingsList;
public SerialDevice(CommPortIdentifier portId, int baudRate) {
public I2CDevice(CommPortIdentifier portId, int baudRate) {
this.portId = portId;
this.baudRate = baudRate;
things = new Hashtable<String, Thing>();
thingsList = new ArrayList<Thing>();
}
public SerialDevice(String portName, int baudRate) {
public I2CDevice(String portName, int baudRate) {
this.portName = portName;
this.baudRate = baudRate;
things = new Hashtable<String, Thing>();
@@ -60,7 +59,7 @@ public class SerialDevice implements Device {
@Override
public void close() throws IOException {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(I2CDevice.class.getName()).log(Level.INFO,
"Closing device on {0}", serialPort.getName());
//send("X");
connected = false;
@@ -87,12 +86,12 @@ public class SerialDevice implements Device {
CommPortIdentifier.getPortIdentifier(portName);
}
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort =
(SerialPort) portId.open(portId.getName(), baudRate);
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort.setSerialPortParams(baudRate,
@@ -102,11 +101,11 @@ public class SerialDevice implements Device {
serialPort.notifyOnOutputEmpty(true);
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(I2CDevice.class.getName()).log(Level.INFO,
"Connection Stabilished with {0}", serialPort.getName());
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
Logger.getLogger(I2CDevice.class.getName()).log(Level.SEVERE,
"Could not init the device on " + serialPort.getName(), e);
serialPort.close();
}
@@ -124,7 +123,7 @@ public class SerialDevice implements Device {
Things.delay(50);
if (resources != null) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(I2CDevice.class.getName()).log(Level.INFO,
"Things API Compatible Device found! Resource String: {0}", resources);
things = new Hashtable<String, Thing>();
thingsList = new ArrayList<Thing>();
@@ -147,11 +146,11 @@ public class SerialDevice implements Device {
}
break;
} catch (Exception e) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(I2CDevice.class.getName()).log(Level.INFO,
"Wrong resource String. Parse error!", e);
}
} else {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(I2CDevice.class.getName()).log(Level.INFO,
"Empty Resource String - Nor a Thigns API device", resources);
}
}
@@ -161,7 +160,7 @@ public class SerialDevice implements Device {
public void send(String s) throws IOException {
if (outputStream == null) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
Logger.getLogger(I2CDevice.class.getName()).log(Level.SEVERE,
"This device ({0}) is not working because IO objects are null. "
+ "You should restart the device!", this.getName());
} else {
@@ -178,7 +177,7 @@ public class SerialDevice implements Device {
String msg = "This device (" + this.getName()
+ ") is not working because IO objects are null. "
+ "You should restart the device!";
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE, msg);
Logger.getLogger(I2CDevice.class.getName()).log(Level.SEVERE, msg);
throw new IOException(msg);
} else {
int available = inputStream.available();
@@ -248,7 +247,7 @@ public class SerialDevice implements Device {
/*serialPort =
(SerialPort) portId.open(portId.getName(), 115200);
Device device = new SerialDevice(serialPort);*/
Device device = new SerialDevice(portId, DEFAULT_BAUDRATE);
Device device = new I2CDevice(portId, DEFAULT_BAUDRATE);
device.open();
device.discovery();
if (device.connected()) {
@@ -89,7 +89,9 @@ public class InternetDevice implements Device {
byte[] responseBody = null;
try {
HttpClient client = new HttpClient();
client.setConnectionTimeout(200);
HttpMethod method = new GetMethod(url);
client.executeMethod(method);
responseBody = method.getResponseBody();
} catch (Exception e) {
@@ -14,7 +14,6 @@ import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
import org.things.Thing;
import org.things.Things;
@@ -24,7 +23,7 @@ import org.things.Things;
*/
public class SerialDevice implements Device {
private static final int DEFAULT_BAUDRATE = 115200;
private static final int DEFAULT_BAUDRATE = 19200;
final static int DISCOVERY_RETRY = 3;
CommPortIdentifier portId;
String portName;
@@ -87,12 +86,12 @@ public class SerialDevice implements Device {
CommPortIdentifier.getPortIdentifier(portName);
}
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort =
(SerialPort) portId.open(portId.getName(), baudRate);
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort.setSerialPortParams(baudRate,
@@ -156,7 +155,23 @@ public class SerialDevice implements Device {
}
}
}
public void send(char s) throws IOException {
if (outputStream == null) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
"This device ({0}) is not working because IO objects are null. "
+ "You should restart the device!", this.getName());
} else {
outputStream.write(s);
outputStream.flush();
}
}
@Override
public void send(String s) throws IOException {
@@ -181,9 +196,10 @@ public class SerialDevice implements Device {
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE, msg);
throw new IOException(msg);
} else {
int available = inputStream.available();
if (available == 0) {
inputStream.close();
//inputStream.close();
return null;
} else {
byte chunk[] = new byte[available];
@@ -194,7 +210,31 @@ public class SerialDevice implements Device {
}
}
}
public String receive(long timeout) throws IOException {
if (inputStream == null) {
String msg = "This device (" + this.getName()
+ ") is not working because IO objects are null. "
+ "You should restart the device!";
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE, msg);
throw new IOException(msg);
} else {
long time = System.currentTimeMillis();
while(inputStream.available()==0 && System.currentTimeMillis() - time<timeout);
int available = inputStream.available();
if (available == 0) {
//inputStream.close();
return null;
} else {
byte chunk[] = new byte[available];
inputStream.read(chunk, 0, available);
String retorno = new String(chunk);
inputStream.close();
return retorno;
}
}
}
@Override
public String getName() {
return name;
@@ -14,7 +14,6 @@ import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
import org.things.Thing;
import org.things.Things;
@@ -22,7 +21,7 @@ import org.things.Things;
*
* @author vsenger
*/
public class SerialDevice implements Device {
public class ZigBeeDevice implements Device {
private static final int DEFAULT_BAUDRATE = 115200;
final static int DISCOVERY_RETRY = 3;
@@ -39,14 +38,14 @@ public class SerialDevice implements Device {
Map<String, Thing> things;
Collection<Thing> thingsList;
public SerialDevice(CommPortIdentifier portId, int baudRate) {
public ZigBeeDevice(CommPortIdentifier portId, int baudRate) {
this.portId = portId;
this.baudRate = baudRate;
things = new Hashtable<String, Thing>();
thingsList = new ArrayList<Thing>();
}
public SerialDevice(String portName, int baudRate) {
public ZigBeeDevice(String portName, int baudRate) {
this.portName = portName;
this.baudRate = baudRate;
things = new Hashtable<String, Thing>();
@@ -60,7 +59,7 @@ public class SerialDevice implements Device {
@Override
public void close() throws IOException {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.INFO,
"Closing device on {0}", serialPort.getName());
//send("X");
connected = false;
@@ -87,12 +86,12 @@ public class SerialDevice implements Device {
CommPortIdentifier.getPortIdentifier(portName);
}
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort =
(SerialPort) portId.open(portId.getName(), baudRate);
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort.setSerialPortParams(baudRate,
@@ -102,11 +101,11 @@ public class SerialDevice implements Device {
serialPort.notifyOnOutputEmpty(true);
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.INFO,
"Connection Stabilished with {0}", serialPort.getName());
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.SEVERE,
"Could not init the device on " + serialPort.getName(), e);
serialPort.close();
}
@@ -124,7 +123,7 @@ public class SerialDevice implements Device {
Things.delay(50);
if (resources != null) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.INFO,
"Things API Compatible Device found! Resource String: {0}", resources);
things = new Hashtable<String, Thing>();
thingsList = new ArrayList<Thing>();
@@ -147,11 +146,11 @@ public class SerialDevice implements Device {
}
break;
} catch (Exception e) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.INFO,
"Wrong resource String. Parse error!", e);
}
} else {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.INFO,
"Empty Resource String - Nor a Thigns API device", resources);
}
}
@@ -161,7 +160,7 @@ public class SerialDevice implements Device {
public void send(String s) throws IOException {
if (outputStream == null) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.SEVERE,
"This device ({0}) is not working because IO objects are null. "
+ "You should restart the device!", this.getName());
} else {
@@ -178,7 +177,7 @@ public class SerialDevice implements Device {
String msg = "This device (" + this.getName()
+ ") is not working because IO objects are null. "
+ "You should restart the device!";
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE, msg);
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.SEVERE, msg);
throw new IOException(msg);
} else {
int available = inputStream.available();
@@ -248,7 +247,7 @@ public class SerialDevice implements Device {
/*serialPort =
(SerialPort) portId.open(portId.getName(), 115200);
Device device = new SerialDevice(serialPort);*/
Device device = new SerialDevice(portId, DEFAULT_BAUDRATE);
Device device = new ZigBeeDevice(portId, DEFAULT_BAUDRATE);
device.open();
device.discovery();
if (device.connected()) {
Arquivo binário não exibido.
+2 -21
Ver Arquivo
@@ -4,11 +4,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.things</groupId>
<artifactId>gateway</artifactId>
<artifactId>gateway.embedded</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Things.Gateway</name>
<name>Things.Gateway.Embedded</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
@@ -16,27 +16,8 @@
<org.richfaces.bom.version>4.0.0.Final</org.richfaces.bom.version>
<netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.richfaces</groupId>
<artifactId>richfaces-bom</artifactId>
<version>${org.richfaces.bom.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.richfaces.ui</groupId>
<artifactId>richfaces-components-ui</artifactId>
</dependency>
<dependency>
<groupId>org.richfaces.core</groupId>
<artifactId>richfaces-core-impl</artifactId>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
@@ -19,8 +19,8 @@ import javax.servlet.http.HttpServletResponse;
*
* @author vsenger
*/
@WebServlet(name = "Emulator", urlPatterns = {"/ThingsEmulator/*"})
public class Emulator extends HttpServlet {
@WebServlet(name = "Serial", urlPatterns = {"/Serial/*"})
public class Serial extends HttpServlet {
static Collection<Component> components;
@@ -40,22 +40,7 @@ public class Emulator extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
String component =
request.getPathInfo() != null ? request.getPathInfo().substring(
1, request.getPathInfo().length()) : null;
String param = request.getQueryString();
if (component == null || component.equals("") || component.equals("discovery")) {
discovery(out);
} else {
execute(component, param, out);
}
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@@ -1,16 +1,21 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* and open the template in the editor.
*/
package org.things.web;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.things.Device;
import static org.things.Things.*;
/**
@@ -20,80 +25,103 @@ import static org.things.Things.*;
@WebServlet(name = "ThingServlet", urlPatterns = {"/thing/*"})
public class Server extends HttpServlet {
@Override
public void init() {
}
@Override
public void init() {
try {
Logger.getLogger(Server.class.getName()).log(Level.INFO,
"Starting connection with /dev/ttyUSB0");
//ObjectInputStream oos = new ObjectInputStream(new FileInputStream("things.setup"));
//if(oos!=null) {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String command = "";
Enumeration<String> args = request.getParameterNames();
if (args != null) {
if (args.hasMoreElements()) {
command = args.nextElement();
}
//}
Device d = things.discoverySerial("/dev/ttyUSB0", 115200);
//Device d1 = things.discoveryNetworkThings("ip");
// //if (d != null) {
//}
} catch (Exception ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Command: " + command);
System.out.println("URI: " + request.getRequestURI());
System.out.println("URL : " + request.getRequestURL());
String thingName = request.getRequestURI().replace("/things/thing/", "");
try {
String r = null;
if (command != null && command.equals("discovery")) {
response.setContentType("text/plain;charset=UTF-8");
response.getWriter().write(things.getThingsString());
}
if (command != null && !command.equals("")) {
r = things.execute(thingName, command);
} else {
r = things.execute(thingName);
}
if (r != null) {
response.setContentType("text/plain;charset=UTF-8");
response.getWriter().write(r);
}
} catch (Exception e) {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String command = "";
Enumeration<String> args = request.getParameterNames();
if (args != null) {
if (args.hasMoreElements()) {
command = args.nextElement();
}
}
/*System.out.println("Command: " + command);
System.out.println("URI: " + request.getRequestURI());
System.out.println("URL : " + request.getRequestURL());*/
String thingName = request.getRequestURI().replace("/things/thing/", "");
try {
String r = null;
if (thingName != null && thingName.equals("discovery")) {
Device d = things.discoverySerial("/dev/ttyUSB0", 115200);
response.setContentType("text/plain;charset=UTF-8");
response.getWriter().write(things.getThingsString());
}
else if (thingName != null && thingName.equals("close")) {
things.close();
response.setContentType("text/plain;charset=UTF-8");
response.getWriter().write("closed");
}
if (command != null && !command.equals("")) {
r = things.execute(thingName, command);
} else {
r = things.execute(thingName);
}
if (r != null) {
response.setContentType("text/plain;charset=UTF-8");
response.getWriter().write(r);
}
} catch (Exception e) {
}
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
@@ -1,45 +1,58 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* and open the template in the editor.
*/
package org.things.web;
import java.io.IOException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.things.Device;
import static org.things.Things.*;
/**
*
* @author vsenger
*/
@WebServlet(name = "ThingServlet", urlPatterns = {"/thing/*"})
public class Server extends HttpServlet {
@WebServlet(name = "SetupSaveSerlvlet", urlPatterns = {"/SetupSave"})
public class SetupSave extends HttpServlet {
@Override
public void init() {
try {
Logger.getLogger(SetupSave.class.getName()).log(Level.INFO,
"Starting connection with /dev/ttyUSB0");
Device d = things.discoverySerial("/dev/ttyUSB0", 115200);
//Device d1 = things.discoveryNetworkThings("ip");
if (d != null) {
}
} catch (Exception ex) {
Logger.getLogger(SetupSave.class.getName()).log(Level.SEVERE, null, ex);
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String command = "";
Enumeration<String> args = request.getParameterNames();
if (args != null) {
if (args != null) {
if (args.hasMoreElements()) {
command = args.nextElement();
}
}
System.out.println("Command: " + command);
}
/*System.out.println("Command: " + command);
System.out.println("URI: " + request.getRequestURI());
System.out.println("URL : " + request.getRequestURL());
System.out.println("URL : " + request.getRequestURL());*/
String thingName = request.getRequestURI().replace("/things/thing/", "");
try {
String r = null;
if (command != null && command.equals("discovery")) {
if (thingName != null && thingName.equals("discovery")) {
response.setContentType("text/plain;charset=UTF-8");
response.getWriter().write(things.getThingsString());
}
@@ -55,7 +68,6 @@ public class Server extends HttpServlet {
} catch (Exception e) {
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
@@ -96,4 +108,7 @@ public class Server extends HttpServlet {
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
@@ -3,24 +3,4 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<context-param>
<param-name>org.richfaces.skin</param-name>
<param-value>DEFAULT</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.globalcode</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.globalcode</url-pattern>
</servlet-mapping>
</web-app>
@@ -39,7 +39,7 @@ public class Server extends HttpServlet {
String thingName = request.getRequestURI().replace("/things/thing/", "");
try {
String r = null;
if (command != null && command.equals("discovery")) {
if (thingName != null && thingName.equals("discovery")) {
response.setContentType("text/plain;charset=UTF-8");
response.getWriter().write(things.getThingsString());
}
@@ -97,3 +97,14 @@ public class Server extends HttpServlet {
return "Short description";
}// </editor-fold>
}
class t {
public void changeDirection(String ip, String dir) {
things.discoveryNetworkThings(ip);
things.execute(dir);
}
public String readBluetoothLightSensor(String device) throws Exception {
return things.bluetooth(device).execute("ligth");
}
}
Arquivo executável → Arquivo normal
Ver Arquivo
+8 -4
Ver Arquivo
@@ -3,11 +3,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.things</groupId>
<artifactId>things-api</artifactId>
<artifactId>things-robot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Things.API</name>
<name>Things.Robot</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
@@ -23,16 +23,20 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.things</groupId>
<artifactId>things-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--<dependency>
<groupId>org.rxtx</groupId>
<artifactId>rxtx</artifactId>
<version>2.1.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
</dependency> -->
</dependencies>
<build>
@@ -32,6 +32,7 @@ public class Things {
try {
Thread.sleep(milis);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
protected Collection<Device> devices;
@@ -43,7 +44,7 @@ public class Things {
for (Device device : devices) {
t += device.getThings().size();
}
String retornao = "jhome-server|" + t + "|";
String retornao = "things-server|" + t + "|";
for (Device device : devices) {
for (String c : device.getThings().keySet()) {
@@ -84,6 +85,12 @@ public class Things {
found.setLastValue(r);
} catch (Exception ex) {
Logger.getLogger(Things.class.getName()).log(Level.SEVERE, null, ex);
try {
found.getDevice().close();
} catch (IOException ex1) {
}
devices.remove(found.getDevice());
devicesTable.remove(found.getDevice().getID());
}
} else {
Logger.getLogger(Things.class.getName()).log(Level.INFO, "Component " + thing + " not found!");
@@ -110,6 +117,43 @@ public class Things {
return r;
}
public synchronized String execute(String deviceName, String thing, String args) {
timeControl();
if (devices == null) {
devices = new ArrayList<Device>();
devicesTable = new HashMap<String, Device>();
}
Device device = null;
if (!devicesTable.containsKey(deviceName)) {
device =
new SerialDevice(deviceName, 115200);
try {
device.open();
this.devices.add(device);
this.devicesTable.put(deviceName, device);
} catch (IOException ex) {
Logger.getLogger(Things.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}else {
device = devicesTable.get(deviceName);
}
String r = null;
try {
device.send(thing + "?" + args);
if (device instanceof SerialDevice) {
Things.delay(40);//era 30
}
r = device.receive();
} catch (Exception ex) {
Logger.getLogger(Things.class.getName()).log(Level.SEVERE, null, ex);
}
//Logger.getLogger(Things.class.getName()).log(Level.INFO, "Component " + thing + " not found!");
return r;
}
public void close() {
if (devices == null || devices.isEmpty()) {
return;
@@ -192,4 +236,8 @@ public class Things {
/*public Collection<Device> discoveryBluetooth(String args) throws Exception {
return this.discoverySerial(args);
}*/
public Thing bluetooth(String device) {
return null;
}
}
@@ -14,7 +14,6 @@ import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
import org.things.Thing;
import org.things.Things;
@@ -22,7 +21,7 @@ import org.things.Things;
*
* @author vsenger
*/
public class SerialDevice implements Device {
public class I2CDevice implements Device {
private static final int DEFAULT_BAUDRATE = 115200;
final static int DISCOVERY_RETRY = 3;
@@ -39,14 +38,14 @@ public class SerialDevice implements Device {
Map<String, Thing> things;
Collection<Thing> thingsList;
public SerialDevice(CommPortIdentifier portId, int baudRate) {
public I2CDevice(CommPortIdentifier portId, int baudRate) {
this.portId = portId;
this.baudRate = baudRate;
things = new Hashtable<String, Thing>();
thingsList = new ArrayList<Thing>();
}
public SerialDevice(String portName, int baudRate) {
public I2CDevice(String portName, int baudRate) {
this.portName = portName;
this.baudRate = baudRate;
things = new Hashtable<String, Thing>();
@@ -60,7 +59,7 @@ public class SerialDevice implements Device {
@Override
public void close() throws IOException {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(I2CDevice.class.getName()).log(Level.INFO,
"Closing device on {0}", serialPort.getName());
//send("X");
connected = false;
@@ -87,12 +86,12 @@ public class SerialDevice implements Device {
CommPortIdentifier.getPortIdentifier(portName);
}
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort =
(SerialPort) portId.open(portId.getName(), baudRate);
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort.setSerialPortParams(baudRate,
@@ -102,11 +101,11 @@ public class SerialDevice implements Device {
serialPort.notifyOnOutputEmpty(true);
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(I2CDevice.class.getName()).log(Level.INFO,
"Connection Stabilished with {0}", serialPort.getName());
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
Logger.getLogger(I2CDevice.class.getName()).log(Level.SEVERE,
"Could not init the device on " + serialPort.getName(), e);
serialPort.close();
}
@@ -124,7 +123,7 @@ public class SerialDevice implements Device {
Things.delay(50);
if (resources != null) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(I2CDevice.class.getName()).log(Level.INFO,
"Things API Compatible Device found! Resource String: {0}", resources);
things = new Hashtable<String, Thing>();
thingsList = new ArrayList<Thing>();
@@ -147,11 +146,11 @@ public class SerialDevice implements Device {
}
break;
} catch (Exception e) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(I2CDevice.class.getName()).log(Level.INFO,
"Wrong resource String. Parse error!", e);
}
} else {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(I2CDevice.class.getName()).log(Level.INFO,
"Empty Resource String - Nor a Thigns API device", resources);
}
}
@@ -161,7 +160,7 @@ public class SerialDevice implements Device {
public void send(String s) throws IOException {
if (outputStream == null) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
Logger.getLogger(I2CDevice.class.getName()).log(Level.SEVERE,
"This device ({0}) is not working because IO objects are null. "
+ "You should restart the device!", this.getName());
} else {
@@ -178,7 +177,7 @@ public class SerialDevice implements Device {
String msg = "This device (" + this.getName()
+ ") is not working because IO objects are null. "
+ "You should restart the device!";
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE, msg);
Logger.getLogger(I2CDevice.class.getName()).log(Level.SEVERE, msg);
throw new IOException(msg);
} else {
int available = inputStream.available();
@@ -248,7 +247,7 @@ public class SerialDevice implements Device {
/*serialPort =
(SerialPort) portId.open(portId.getName(), 115200);
Device device = new SerialDevice(serialPort);*/
Device device = new SerialDevice(portId, DEFAULT_BAUDRATE);
Device device = new I2CDevice(portId, DEFAULT_BAUDRATE);
device.open();
device.discovery();
if (device.connected()) {
@@ -89,7 +89,9 @@ public class InternetDevice implements Device {
byte[] responseBody = null;
try {
HttpClient client = new HttpClient();
client.setConnectionTimeout(200);
HttpMethod method = new GetMethod(url);
client.executeMethod(method);
responseBody = method.getResponseBody();
} catch (Exception e) {
@@ -14,7 +14,6 @@ import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
import org.things.Thing;
import org.things.Things;
@@ -24,7 +23,7 @@ import org.things.Things;
*/
public class SerialDevice implements Device {
private static final int DEFAULT_BAUDRATE = 115200;
private static final int DEFAULT_BAUDRATE = 19200;
final static int DISCOVERY_RETRY = 3;
CommPortIdentifier portId;
String portName;
@@ -87,12 +86,12 @@ public class SerialDevice implements Device {
CommPortIdentifier.getPortIdentifier(portName);
}
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort =
(SerialPort) portId.open(portId.getName(), baudRate);
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort.setSerialPortParams(baudRate,
@@ -156,7 +155,23 @@ public class SerialDevice implements Device {
}
}
}
public void send(char s) throws IOException {
if (outputStream == null) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
"This device ({0}) is not working because IO objects are null. "
+ "You should restart the device!", this.getName());
} else {
outputStream.write(s);
outputStream.flush();
}
}
@Override
public void send(String s) throws IOException {
@@ -181,9 +196,10 @@ public class SerialDevice implements Device {
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE, msg);
throw new IOException(msg);
} else {
int available = inputStream.available();
if (available == 0) {
inputStream.close();
//inputStream.close();
return null;
} else {
byte chunk[] = new byte[available];
@@ -194,7 +210,31 @@ public class SerialDevice implements Device {
}
}
}
public String receive(long timeout) throws IOException {
if (inputStream == null) {
String msg = "This device (" + this.getName()
+ ") is not working because IO objects are null. "
+ "You should restart the device!";
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE, msg);
throw new IOException(msg);
} else {
long time = System.currentTimeMillis();
while(inputStream.available()==0 && System.currentTimeMillis() - time<timeout);
int available = inputStream.available();
if (available == 0) {
//inputStream.close();
return null;
} else {
byte chunk[] = new byte[available];
inputStream.read(chunk, 0, available);
String retorno = new String(chunk);
inputStream.close();
return retorno;
}
}
}
@Override
public String getName() {
return name;
@@ -14,7 +14,6 @@ import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.IIOException;
import org.things.Thing;
import org.things.Things;
@@ -22,7 +21,7 @@ import org.things.Things;
*
* @author vsenger
*/
public class SerialDevice implements Device {
public class ZigBeeDevice implements Device {
private static final int DEFAULT_BAUDRATE = 115200;
final static int DISCOVERY_RETRY = 3;
@@ -39,14 +38,14 @@ public class SerialDevice implements Device {
Map<String, Thing> things;
Collection<Thing> thingsList;
public SerialDevice(CommPortIdentifier portId, int baudRate) {
public ZigBeeDevice(CommPortIdentifier portId, int baudRate) {
this.portId = portId;
this.baudRate = baudRate;
things = new Hashtable<String, Thing>();
thingsList = new ArrayList<Thing>();
}
public SerialDevice(String portName, int baudRate) {
public ZigBeeDevice(String portName, int baudRate) {
this.portName = portName;
this.baudRate = baudRate;
things = new Hashtable<String, Thing>();
@@ -60,7 +59,7 @@ public class SerialDevice implements Device {
@Override
public void close() throws IOException {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.INFO,
"Closing device on {0}", serialPort.getName());
//send("X");
connected = false;
@@ -87,12 +86,12 @@ public class SerialDevice implements Device {
CommPortIdentifier.getPortIdentifier(portName);
}
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort =
(SerialPort) portId.open(portId.getName(), baudRate);
if(portId==null) {
throw new IIOException("Invalid port " + portName);
throw new IOException("Invalid port " + portName);
}
serialPort.setSerialPortParams(baudRate,
@@ -102,11 +101,11 @@ public class SerialDevice implements Device {
serialPort.notifyOnOutputEmpty(true);
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.INFO,
"Connection Stabilished with {0}", serialPort.getName());
} catch (Exception e) {
e.printStackTrace();
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.SEVERE,
"Could not init the device on " + serialPort.getName(), e);
serialPort.close();
}
@@ -124,7 +123,7 @@ public class SerialDevice implements Device {
Things.delay(50);
if (resources != null) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.INFO,
"Things API Compatible Device found! Resource String: {0}", resources);
things = new Hashtable<String, Thing>();
thingsList = new ArrayList<Thing>();
@@ -147,11 +146,11 @@ public class SerialDevice implements Device {
}
break;
} catch (Exception e) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.INFO,
"Wrong resource String. Parse error!", e);
}
} else {
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.INFO,
"Empty Resource String - Nor a Thigns API device", resources);
}
}
@@ -161,7 +160,7 @@ public class SerialDevice implements Device {
public void send(String s) throws IOException {
if (outputStream == null) {
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.SEVERE,
"This device ({0}) is not working because IO objects are null. "
+ "You should restart the device!", this.getName());
} else {
@@ -178,7 +177,7 @@ public class SerialDevice implements Device {
String msg = "This device (" + this.getName()
+ ") is not working because IO objects are null. "
+ "You should restart the device!";
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE, msg);
Logger.getLogger(ZigBeeDevice.class.getName()).log(Level.SEVERE, msg);
throw new IOException(msg);
} else {
int available = inputStream.available();
@@ -248,7 +247,7 @@ public class SerialDevice implements Device {
/*serialPort =
(SerialPort) portId.open(portId.getName(), 115200);
Device device = new SerialDevice(serialPort);*/
Device device = new SerialDevice(portId, DEFAULT_BAUDRATE);
Device device = new ZigBeeDevice(portId, DEFAULT_BAUDRATE);
device.open();
device.discovery();
if (device.connected()) {
+1 -1
Ver Arquivo
@@ -8,7 +8,7 @@
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>Things.jHome.Automation</name>
<name>DNTV Control</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
+6 -3
Ver Arquivo
@@ -8,11 +8,10 @@
<title>jHome 1.0 Reference Implementation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/reset.css" type="text/css" media="all"/>
<!--<link rel="stylesheet" href="css/reset.css" type="text/css" media="all"/>
<link rel="stylesheet" href="css/layout.css" type="text/css" media="all"/>
<link rel="stylesheet" href="css/style.css" type="text/css" media="all"/>
<link rel="stylesheet" href="css/prettyPhoto.css" type="text/css" media="all"/>
<script type="text/javascript" src="js/jquery-1.5.2.js" ></script>
<script type="text/javascript" src="js/mobilyblocks.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script src="js/jquery.transform-0.9.3.min.js"></script>
@@ -23,8 +22,12 @@
<script type="text/javascript" src="js/jquery.mousewheel.js"></script>
<link rel="stylesheet" media="screen" type="text/css" href="css/colorpicker.css" />
<script type="text/javascript" src="js/colorpicker.js"></script>
<!--<script type="text/javascript" src="js/contact-form.js"></script>-->
<script type="text/javascript" src="js/jquery.prettyPhoto.js"></script>
-->
<script type="text/javascript" src="js/jquery-1.5.2.js" ></script>
<!--<script type="text/javascript" src="js/contact-form.js"></script>-->
<!--[if lt IE 9]>
<script type="text/javascript" src="js/html5.js"></script>
<style type="text/css">
+3 -3
Ver Arquivo
@@ -3,7 +3,7 @@ $(document).ready(function(){
var lampon=false;
var aberto=false;
var readingSensor = true;
var identificador = self.setInterval(atualizar, 10000);
var identificador = self.setInterval(atualizar, 1000);
function atualizar() {
if(readingSensor) {
@@ -18,7 +18,7 @@ $(document).ready(function(){
$.ajax({
type: "POST",
dataType: "text",
url: things + "light1" ,
url: things + "light" ,
success: function(data) {
$('#sensorLight').html(data);
}
@@ -73,7 +73,7 @@ return false;
lampon=!lampon;
$.ajax({
type: "GET",
url: things + "relay1?" + (lampon ? 1 : 0),
url: things + "relay3?" + (lampon ? 1 : 0),
success: function() {
}
Arquivo executável → Arquivo normal
Ver Arquivo

Antes

Largura:  |  Altura:  |  Tamanho: 5.4 KiB

Depois

Largura:  |  Altura:  |  Tamanho: 5.4 KiB

Arquivo executável → Arquivo normal
Ver Arquivo

Antes

Largura:  |  Altura:  |  Tamanho: 1.1 KiB

Depois

Largura:  |  Altura:  |  Tamanho: 1.1 KiB

Arquivo executável → Arquivo normal
Ver Arquivo

Antes

Largura:  |  Altura:  |  Tamanho: 1.1 KiB

Depois

Largura:  |  Altura:  |  Tamanho: 1.1 KiB

Arquivo executável → Arquivo normal
Ver Arquivo

Antes

Largura:  |  Altura:  |  Tamanho: 1.0 KiB

Depois

Largura:  |  Altura:  |  Tamanho: 1.0 KiB

Arquivo executável → Arquivo normal
Ver Arquivo

Antes

Largura:  |  Altura:  |  Tamanho: 1.0 KiB

Depois

Largura:  |  Altura:  |  Tamanho: 1.0 KiB

Arquivo executável → Arquivo normal
Ver Arquivo

Antes

Largura:  |  Altura:  |  Tamanho: 247 B

Depois

Largura:  |  Altura:  |  Tamanho: 247 B

Arquivo executável → Arquivo normal
Ver Arquivo

Antes

Largura:  |  Altura:  |  Tamanho: 1.0 KiB

Depois

Largura:  |  Altura:  |  Tamanho: 1.0 KiB

Arquivo executável → Arquivo normal
Ver Arquivo

Antes

Largura:  |  Altura:  |  Tamanho: 4.7 KiB

Depois

Largura:  |  Altura:  |  Tamanho: 4.7 KiB

Arquivo executável → Arquivo normal
Ver Arquivo

Antes

Largura:  |  Altura:  |  Tamanho: 1.0 KiB

Depois

Largura:  |  Altura:  |  Tamanho: 1.0 KiB

Arquivo executável → Arquivo normal
Ver Arquivo

Antes

Largura:  |  Altura:  |  Tamanho: 1.0 KiB

Depois

Largura:  |  Altura:  |  Tamanho: 1.0 KiB

@@ -128,32 +128,31 @@ public abstract class ThingsForm extends Form {
}
}
public void sendMessage(String message) throws ThingException {
public void sendCommand(String command) throws ThingException {
if (this.thingURL.equals("")) {
if (Things.anything() == null) {
return;
}
Things.send(message);
Things.send(command);
thingURL = Things.anything().getThingURL();
saveThingURL();
} else {
Things.bluetooth(thingURL).send(message);
Things.bluetooth(thingURL).send(command);
}
}
public String sendAndReceiveMessage(String message) throws ThingException {
public String sendReceiveCommand(String command) throws ThingException {
if (this.thingURL.equals("")) {
if (Things.anything() == null) {
return "";
}
Things.send(message);
Things.send(command);
thingURL = Things.anything().getThingURL();
saveThingURL();
Things.delay(50);
return Things.receive();
} else {
Things.bluetooth(thingURL).send(message);
Things.bluetooth(thingURL).send(command);
Things.delay(50);
return Things.receive();
}
@@ -52,7 +52,7 @@ public class RGBForm extends ThingsForm {
}
setProgress(actualValue);
try {
sendMessage(color + "?" + actualValue);
sendCommand(color + "?" + actualValue);
} catch (ThingException ex) {
ex.printStackTrace();
}
@@ -64,7 +64,7 @@ public class RGBForm extends ThingsForm {
}
this.setProgress(actualValue);
try {
sendMessage(color + "?" + actualValue);
sendCommand(color + "?" + actualValue);
} catch (ThingException ex) {
ex.printStackTrace();
}
@@ -33,7 +33,7 @@ public class RelayForm extends ThingsForm {
public void actionPerformed(ActionEvent ae) {
if (on) {
try {
sendMessage(RelayForm.this.getThingCommand() + "0");
sendCommand(RelayForm.this.getThingCommand() + "0");
//Things.bluetooth(deviceURL).send(RelayForm.this.getThingCommand() + "0");
} catch (ThingException ex) {
ThingsMenu.getInstance().updateStatus(ex.getMessage());
@@ -41,7 +41,7 @@ public class RelayForm extends ThingsForm {
}
else {
try {
sendMessage(RelayForm.this.getThingCommand() + "1");
sendCommand(RelayForm.this.getThingCommand() + "1");
//Things.bluetooth(deviceURL).send(RelayForm.this.getThingCommand() + "1");
} catch (ThingException ex) {
ThingsMenu.getInstance().updateStatus(ex.getMessage());
@@ -84,7 +84,7 @@ public class DriveForm extends ThingsForm {
case UP_KEY:
setThingCommand("frente?");
try {
sendMessage(getThingCommand() + speed);
sendCommand(getThingCommand() + speed);
} catch (ThingException ex) {
ex.printStackTrace();
}
@@ -98,7 +98,7 @@ public class DriveForm extends ThingsForm {
case DOWN_KEY:
setThingCommand("re?");
try {
sendMessage(getThingCommand() + speed);
sendCommand(getThingCommand() + speed);
} catch (ThingException ex) {
ex.printStackTrace();
}
@@ -112,7 +112,7 @@ public class DriveForm extends ThingsForm {
case LEFT_KEY:
setThingCommand("ga?");
try {
sendMessage(getThingCommand() + speed);
sendCommand(getThingCommand() + speed);
} catch (ThingException ex) {
ex.printStackTrace();
}
@@ -126,7 +126,7 @@ public class DriveForm extends ThingsForm {
case RIGHT_KEY:
setThingCommand("gh?");
try {
sendMessage(getThingCommand() + speed);
sendCommand(getThingCommand() + speed);
} catch (ThingException ex) {
ex.printStackTrace();
}
@@ -140,7 +140,7 @@ public class DriveForm extends ThingsForm {
case STOP_KEY:
setThingCommand("parar");
try {
sendMessage(getThingCommand());
sendCommand(getThingCommand());
} catch (ThingException ex) {
ex.printStackTrace();
}
@@ -44,7 +44,7 @@ public class ServoForm extends ThingsForm {
}
setProgress(actualAngle);
try {
sendMessage("" + actualAngle);
sendCommand("" + actualAngle);
} catch (ThingException ex) {
ex.printStackTrace();
}
@@ -56,7 +56,7 @@ public class ServoForm extends ThingsForm {
}
this.setProgress(actualAngle);
try {
sendMessage("" + actualAngle);
sendCommand("" + actualAngle);
} catch (ThingException ex) {
ex.printStackTrace();
}
@@ -30,7 +30,7 @@ public class LightSensorForm extends ThingsForm {
public void actionPerformed(ActionEvent ae) {
String rcvd="";
try {
rcvd = sendAndReceiveMessage("light");
rcvd = sendReceiveCommand("light");
} catch (ThingException ex) {
ex.printStackTrace();
}
@@ -31,7 +31,7 @@ public class TemperatureSensorForm extends ThingsForm {
public void actionPerformed(ActionEvent ae) {
String rcvd="";
try {
rcvd = sendAndReceiveMessage("temp");
rcvd = sendReceiveCommand("temp");
} catch (ThingException ex) {
ex.printStackTrace();
}
+3 -3
Ver Arquivo
@@ -3,7 +3,7 @@ $(document).ready(function(){
var lampon=false;
var aberto=false;
var readingSensor = true;
var identificador = self.setInterval(atualizar, 10000);
var identificador = self.setInterval(atualizar, 1000);
function atualizar() {
if(readingSensor) {
@@ -18,7 +18,7 @@ $(document).ready(function(){
$.ajax({
type: "POST",
dataType: "text",
url: things + "light1" ,
url: things + "light" ,
success: function(data) {
$('#sensorLight').html(data);
}
@@ -73,7 +73,7 @@ return false;
lampon=!lampon;
$.ajax({
type: "GET",
url: things + "relay1?" + (lampon ? 1 : 0),
url: things + "relay2?" + (lampon ? 1 : 0),
success: function() {
}