devoxx updates
@@ -1,3 +0,0 @@
|
||||
# cache for current jar dependecy. DO NOT EDIT.
|
||||
# format is <lastModified> <length> <SHA-1> <path>
|
||||
# Encoding is UTF-8
|
||||
|
Antes Largura: | Altura: | Tamanho: 1.6 KiB |
|
Antes Largura: | Altura: | Tamanho: 1.9 KiB |
|
Antes Largura: | Altura: | Tamanho: 2.0 KiB |
|
Antes Largura: | Altura: | Tamanho: 2.4 KiB |
|
Antes Largura: | Altura: | Tamanho: 2.9 KiB |
|
Antes Largura: | Altura: | Tamanho: 7.7 KiB |
|
Antes Largura: | Altura: | Tamanho: 19 KiB |
|
Antes Largura: | Altura: | Tamanho: 14 KiB |
|
Antes Largura: | Altura: | Tamanho: 765 B |
|
Antes Largura: | Altura: | Tamanho: 830 B |
|
Antes Largura: | Altura: | Tamanho: 1019 B |
|
Antes Largura: | Altura: | Tamanho: 1.2 KiB |
|
Antes Largura: | Altura: | Tamanho: 1.4 KiB |
|
Antes Largura: | Altura: | Tamanho: 2.8 KiB |
|
Antes Largura: | Altura: | Tamanho: 19 KiB |
|
Antes Largura: | Altura: | Tamanho: 14 KiB |
|
Antes Largura: | Altura: | Tamanho: 1.0 KiB |
|
Antes Largura: | Altura: | Tamanho: 1.3 KiB |
|
Antes Largura: | Altura: | Tamanho: 1.4 KiB |
|
Antes Largura: | Altura: | Tamanho: 1.6 KiB |
|
Antes Largura: | Altura: | Tamanho: 1.8 KiB |
|
Antes Largura: | Altura: | Tamanho: 4.2 KiB |
|
Antes Largura: | Altura: | Tamanho: 19 KiB |
|
Antes Largura: | Altura: | Tamanho: 14 KiB |
|
Antes Largura: | Altura: | Tamanho: 11 KiB |
@@ -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("|");
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Ethernet.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
static uint8_t mac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
|
||||
static uint8_t ip[4] = {192,168,1,15};
|
||||
static uint16_t port = 80;
|
||||
Ethernet::Ethernet(Device* device1) {
|
||||
device=device1;
|
||||
}
|
||||
void Ethernet::startNetwork(int ipn[]) {
|
||||
ip[0]=ipn[0];
|
||||
ip[1]=ipn[1];
|
||||
ip[2]=ipn[2];
|
||||
ip[3]=ipn[3];
|
||||
ethernet.setup(mac, ip, port);
|
||||
}
|
||||
|
||||
void Ethernet::discoveryNetwork() {
|
||||
ethernet.print(device->name);
|
||||
ethernet.print("|");
|
||||
|
||||
ethernet.print(device->numberOfComponents);
|
||||
ethernet.print("|");
|
||||
for(int x=0;x<device->numberOfComponents;x++) {
|
||||
ethernet.print(device->components[x].name);
|
||||
ethernet.print("|");
|
||||
ethernet.print(device->components[x].getTypeName());
|
||||
ethernet.print("|");
|
||||
ethernet.print(device->components[x].port);
|
||||
ethernet.print("|");
|
||||
|
||||
ethernet.print(device->components[x].getValue());
|
||||
ethernet.print("|");
|
||||
}
|
||||
}
|
||||
void Ethernet::loop() {
|
||||
char* param;
|
||||
if (param = ethernet.serviceRequest())
|
||||
{
|
||||
|
||||
if(param[0]=='\0') {
|
||||
discoveryNetwork();
|
||||
}
|
||||
else if(strcmp("discovery", param)==0) {
|
||||
discoveryNetwork();
|
||||
}
|
||||
else {
|
||||
ethernet.print(device->execute(param));
|
||||
}
|
||||
ethernet.respond();
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
#ifndef ETHERNET_H
|
||||
#define ETHERNET_H
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Arduino.h"
|
||||
#include "Device.h"
|
||||
|
||||
class Ethernet {
|
||||
private:
|
||||
|
||||
public:
|
||||
Device* device;
|
||||
Ethernet(Device* device);
|
||||
ETHER_28J60 ethernet;
|
||||
void startNetwork(int ipn[]);
|
||||
void loop();
|
||||
void discoveryNetwork();
|
||||
};
|
||||
#endif
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <Component.h>
|
||||
#include <Arduino.h>
|
||||
#include <IRremote.h>
|
||||
|
||||
Component::Component() {}
|
||||
//very high cost to have each state in a string, this var is for all objects
|
||||
char readValue[15];
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <Component.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <IRremote.h>
|
||||
Component::Component() {}
|
||||
//very high cost to have each state in a string, this var is for all objects
|
||||
char readValue[15];
|
||||
@@ -12,7 +12,19 @@ Component::Component(char* name1, int type1, int port1) {
|
||||
if(type==DIGITAL) {
|
||||
pinMode(port, OUTPUT);
|
||||
}
|
||||
emptyReadValue();
|
||||
state=0;
|
||||
}
|
||||
Component::Component(char* name1, int type1, int port1, customRead cr, customWrite cw) {
|
||||
name=name1;
|
||||
type=type1;
|
||||
port=port1;
|
||||
meuCustomRead =cr;
|
||||
meuCustomWrite =cw;
|
||||
|
||||
if(type==DIGITAL) {
|
||||
pinMode(port, OUTPUT);
|
||||
}
|
||||
emptyReadValue();
|
||||
state=0;
|
||||
}
|
||||
@@ -22,12 +34,15 @@ char* Component::getValue() {
|
||||
}
|
||||
|
||||
char* Component::getTypeName() {
|
||||
static char* typeNames[] = {"DIGITAL", "ANALOG", "PWM", "RELAY", "LIGHT", "TEMPERATURE", "SERIAL", "LIB", "PING"};
|
||||
static char* typeNames[] = {"DIGITAL", "ANALOG", "PWM", "RELAY", "LIGHT", "TEMPERATURE", "SERIAL", "PING", "ALL", "CUSTOM"};
|
||||
return typeNames[type];
|
||||
}
|
||||
|
||||
char* Component::write(char* c1) {
|
||||
char* r="\0";
|
||||
Serial.println("DEntro do write... ");
|
||||
Serial.println(c1);
|
||||
|
||||
if(type==DIGITAL || type==RELAY) {
|
||||
state = atoi(c1);
|
||||
digitalWrite(port, state);
|
||||
@@ -42,6 +57,9 @@ char* Component::write(char* c1) {
|
||||
Serial.print(c1);
|
||||
return r;
|
||||
}
|
||||
else if(type==CUSTOM) {
|
||||
return meuCustomWrite(c1);
|
||||
}
|
||||
}
|
||||
|
||||
void Component::emptyReadValue() {
|
||||
@@ -53,7 +71,7 @@ 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!
|
||||
//mas na verdade o sensor de temperatura analógico causa no ADC do ARduino!
|
||||
state = analogRead(port);
|
||||
delay(5);
|
||||
state = analogRead(port);
|
||||
@@ -69,7 +87,7 @@ char* Component::read() {
|
||||
return getValue();
|
||||
} else if(type==SERIAL) {
|
||||
|
||||
int counter=0;
|
||||
int counter=0;
|
||||
while(Serial.available()>0 && counter<15)
|
||||
{
|
||||
char c = Serial.read();
|
||||
@@ -77,8 +95,9 @@ char* Component::read() {
|
||||
readValue[counter++]=c;
|
||||
}
|
||||
return readValue;
|
||||
} else if(type==LIB) {
|
||||
//should call function pointer here...
|
||||
} else if(type==CUSTOM) {
|
||||
//should call function pointer here..
|
||||
return meuCustomRead();
|
||||
|
||||
} else if(type==PING) {
|
||||
//Parallax ping based on digital pulseIn
|
||||
@@ -10,13 +10,16 @@
|
||||
#define TEMP 5
|
||||
#define SERIAL 6
|
||||
|
||||
#define LIB 7
|
||||
#define PING 8
|
||||
|
||||
#define ALL 9
|
||||
#define PING 7
|
||||
|
||||
#define ALL 8
|
||||
#define CUSTOM 9
|
||||
class Component {
|
||||
private:
|
||||
typedef char* (*customRead)();
|
||||
typedef char* (*customWrite)(char*);
|
||||
customRead meuCustomRead;
|
||||
customWrite meuCustomWrite;
|
||||
public:
|
||||
char* name;
|
||||
int type;
|
||||
@@ -24,6 +27,8 @@ class Component {
|
||||
int state;
|
||||
|
||||
Component(char*, int, int);
|
||||
Component(char*, int, int, customRead, customWrite);
|
||||
|
||||
Component();
|
||||
char* getValue();
|
||||
char* write(char *);
|
||||
@@ -17,17 +17,17 @@ Device::Device(char* name1) {
|
||||
found=false;
|
||||
name = name1;
|
||||
}
|
||||
char command1[10];
|
||||
char parameter1[10];
|
||||
char return1[10];
|
||||
char command1[15];
|
||||
char parameter1[15];
|
||||
char return1[15];
|
||||
|
||||
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);
|
||||
void Device::add(char* name, int type, int port, char*(*readFunction)(), char*(*writeFunction)(char*)) {
|
||||
Component c1 = Component(name, type,port, readFunction, writeFunction);
|
||||
components[numberOfComponents++]= c1;
|
||||
}
|
||||
|
||||
@@ -83,19 +83,23 @@ void split(char* command) {
|
||||
|
||||
|
||||
char* Device::execute(char* command) {
|
||||
Serial.println("antes do split");
|
||||
Serial.println(command);
|
||||
split(command);
|
||||
Serial.println("depois do split");
|
||||
|
||||
Component &c = components[findComponent(command1)];
|
||||
if(!found) {
|
||||
//Serial.println("component not found");
|
||||
Serial.println("component not found");
|
||||
return "\n";
|
||||
}
|
||||
if(parameter1[0]=='\0') {
|
||||
//Serial.println("reading");
|
||||
Serial.println("reading");
|
||||
char* r=c.read();
|
||||
return r;
|
||||
}
|
||||
else {
|
||||
//Serial.println("writing");
|
||||
Serial.println("writing");
|
||||
return c.write(parameter1);
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public:
|
||||
bool network;
|
||||
Component components[MAX];
|
||||
void add(char*, int, int);
|
||||
void add(char*, int, int,char*(*readFunction)(), void(*writeFunction)(char*));
|
||||
void add(char*, int, int,char*(*readFunction)(), char*(*writeFunction)(char*));
|
||||
void loop();
|
||||
String discoveryString();
|
||||
int findComponent(char*) ;
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <Mode.h>
|
||||
#include "HardwareSerial.h"
|
||||
Mode::Mode() {
|
||||
hasSetup=0;
|
||||
}
|
||||
|
||||
Mode::Mode(ISR mode) {
|
||||
modeFunction=mode;
|
||||
hasSetup=0;
|
||||
}
|
||||
Mode::Mode(ISR mode, ISR setup) {
|
||||
modeFunction=mode;
|
||||
setupFunction=setup;
|
||||
hasSetup=1;
|
||||
}
|
||||
|
||||
void Mode::setup(){
|
||||
if(hasSetup) setupFunction();
|
||||
}
|
||||
void Mode ::execute(){
|
||||
modeFunction();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
class Mode {
|
||||
|
||||
private:
|
||||
typedef void (*ISR)();
|
||||
ISR setupFunction;
|
||||
ISR modeFunction;
|
||||
int hasSetup;
|
||||
public:
|
||||
Modo();
|
||||
Modo(ISR);
|
||||
Modo(ISR, ISR);
|
||||
void execute();
|
||||
void setup();
|
||||
|
||||
};
|
||||
|
||||
@@ -72,7 +72,8 @@ void Thing::loop() {
|
||||
modeChanged1=0;
|
||||
modes[mode].execute();
|
||||
if(serial && Serial.available()) {
|
||||
//debug Serial.println("chamando serial server");
|
||||
//debug
|
||||
//Serial.println("chamando serial server");
|
||||
serialServer();
|
||||
}
|
||||
}
|
||||
@@ -105,26 +106,33 @@ void split(char* command) {
|
||||
|
||||
|
||||
char* Thing::execute(char* command) {
|
||||
//debug Serial.println("antes do split");
|
||||
//debug Serial.println(command);
|
||||
//debug
|
||||
//Serial.println("antes do split");
|
||||
//debug
|
||||
//Serial.println(command);
|
||||
split(command);
|
||||
//debug Serial.println("depois do split");
|
||||
|
||||
Component &c = components[findComponent(command1)];
|
||||
if(!found) {
|
||||
//debug Serial.println("component not found");
|
||||
//debug
|
||||
//Serial.println("component not found");
|
||||
return "\n";
|
||||
}
|
||||
if(parameter1[0]=='\0') {
|
||||
return c.read();
|
||||
//debug Serial.println("reading");
|
||||
//debug
|
||||
//Serial.println("reading");
|
||||
//char* r=c.read();
|
||||
//Serial.print("read ");
|
||||
//Serial.println(r);
|
||||
|
||||
}
|
||||
else {
|
||||
//debug Serial.println("writing");
|
||||
//debug
|
||||
//Serial.println("writing");
|
||||
//Serial.println(parameter1);
|
||||
|
||||
return c.write(parameter1);
|
||||
}
|
||||
|
||||
@@ -152,8 +160,10 @@ void Thing::serialServer() {
|
||||
discoverySerial();
|
||||
return;
|
||||
}
|
||||
//debug Serial.println("executing command...");
|
||||
//debug Serial.println(command);
|
||||
//debug
|
||||
//Serial.println("executing command...");
|
||||
//debug
|
||||
//Serial.println(command);
|
||||
|
||||
char* r = execute(command);
|
||||
//debug Serial.println("executed command. returned value:");
|
||||
@@ -226,6 +236,7 @@ void changeModeViaButton() {
|
||||
|
||||
|
||||
void Thing::start() {
|
||||
|
||||
attachInterrupt(0, changeModeViaButton, LOW);
|
||||
pinMode(pins.speaker, OUTPUT);
|
||||
beep(1);
|
||||
@@ -0,0 +1,249 @@
|
||||
#include "Thing.h"
|
||||
#include "Arduino.h" // Arduino 1.0
|
||||
#include "Component.h"
|
||||
|
||||
volatile long lastDebounce=0;
|
||||
volatile long debounceDelay=50;
|
||||
volatile long ntime=0;
|
||||
char command1[15];
|
||||
char parameter1[15];
|
||||
char return1[15];
|
||||
|
||||
Thing thing;
|
||||
|
||||
Thing::Thing() {
|
||||
componentIndex=0;
|
||||
serial=true;
|
||||
network=false;
|
||||
found=false;
|
||||
name = "t1";
|
||||
pins.speaker=4;
|
||||
}
|
||||
|
||||
Thing::Thing(char* name1) {
|
||||
componentIndex=0;
|
||||
serial=true;
|
||||
network=false;
|
||||
found=false;
|
||||
name = name1;
|
||||
pins.speaker=4;
|
||||
}
|
||||
|
||||
void Thing::addComponent(char* name, int type, int port) {
|
||||
Component c1 = Component(name, type,port);
|
||||
components[numberOfComponents++]= c1;
|
||||
}
|
||||
|
||||
void Thing::addComponent(char* name, int type, int port, char*(*readFunction)(), char*(*writeFunction)(char*)) {
|
||||
Component c1 = Component(name, type,port, readFunction, writeFunction);
|
||||
components[numberOfComponents++]= c1;
|
||||
}
|
||||
void Thing::addMode(int modeNumber, void(*mode1)()) {
|
||||
Mode newMode(mode1);
|
||||
modes[modeNumber]=newMode;
|
||||
modeCounter++;
|
||||
}
|
||||
|
||||
int Thing::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 Thing::loop() {
|
||||
if(lastMode!=mode) {
|
||||
lastMode = mode;
|
||||
beep(mode+1);
|
||||
//modoMudou=1;
|
||||
modes[mode].setup();
|
||||
}
|
||||
modeChanged1=0;
|
||||
modes[mode].execute();
|
||||
if(serial && Serial.available()) {
|
||||
//debug
|
||||
Serial.println("chamando serial server");
|
||||
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* Thing::execute(char* command) {
|
||||
//debug
|
||||
Serial.println("antes do split");
|
||||
//debug
|
||||
Serial.println(command);
|
||||
split(command);
|
||||
//debug Serial.println("depois do split");
|
||||
|
||||
Component &c = components[findComponent(command1)];
|
||||
if(!found) {
|
||||
//debug
|
||||
Serial.println("component not found");
|
||||
return "\n";
|
||||
}
|
||||
if(parameter1[0]=='\0') {
|
||||
return c.read();
|
||||
//debug
|
||||
Serial.println("reading");
|
||||
//char* r=c.read();
|
||||
//Serial.print("read ");
|
||||
//Serial.println(r);
|
||||
|
||||
}
|
||||
else {
|
||||
//debug
|
||||
Serial.println("writing");
|
||||
Serial.println(parameter1);
|
||||
|
||||
return c.write(parameter1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Thing::serialServer() {
|
||||
char command[15];
|
||||
int counter=0;
|
||||
char c;
|
||||
while(Serial.available()>0)
|
||||
{
|
||||
command[counter++]=Serial.read();
|
||||
delay(2);
|
||||
}
|
||||
|
||||
if(counter>0) {
|
||||
command[counter]='\0';
|
||||
|
||||
if(strcmp("discovery",command)==0) {
|
||||
//Serial.println("discovery serial");
|
||||
discoverySerial();
|
||||
return;
|
||||
}
|
||||
//debug
|
||||
Serial.println("executing command...");
|
||||
//debug
|
||||
Serial.println(command);
|
||||
|
||||
char* r = execute(command);
|
||||
//debug Serial.println("executed command. returned value:");
|
||||
//debug Serial.println(r);
|
||||
if(r[0]!='\0') {
|
||||
Serial.print(r);
|
||||
Serial.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Thing::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("|");
|
||||
}
|
||||
}
|
||||
byte Thing::modeChanged() { return modeChanged1;}
|
||||
|
||||
void Thing::wait(long milis) {
|
||||
modeChanged1=0;
|
||||
if(mode!=lastMode) {
|
||||
modeChanged1=1;
|
||||
return;
|
||||
}
|
||||
if(milis<10) delay(milis);
|
||||
else {
|
||||
for(long x=0;x<milis/10;x++) {
|
||||
delay(10);
|
||||
//checarMudancaModo();
|
||||
if(mode!=lastMode) {
|
||||
modeChanged1=1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
delay(milis%10);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Thing::beep(int ntime) {
|
||||
for(int x=0;x<ntime;x++) {
|
||||
digitalWrite(pins.speaker, HIGH);
|
||||
delay(100);
|
||||
digitalWrite(pins.speaker, LOW);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
}
|
||||
void changeModeViaButton() {
|
||||
|
||||
if(millis()-lastDebounce>debounceDelay && ntime++>=100) {
|
||||
thing.lastMode=thing.mode;
|
||||
thing.mode = thing.mode==thing.modeCounter-1 ? 0 : thing.mode + 1;
|
||||
ntime=0;
|
||||
thing.modeChanged1=1;
|
||||
lastDebounce=millis();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Thing::start() {
|
||||
|
||||
attachInterrupt(0, changeModeViaButton, LOW);
|
||||
pinMode(pins.speaker, OUTPUT);
|
||||
beep(1);
|
||||
}
|
||||
|
||||
void Thing::start(int intNumber) {
|
||||
if(intNumber>-1) attachInterrupt(intNumber, changeModeViaButton, LOW);
|
||||
pinMode(pins.speaker, OUTPUT);
|
||||
beep(1);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
//#ifndef THING_H
|
||||
//#define THING_H
|
||||
#include "Component.h"
|
||||
#include "Mode.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
#define MAX_COMPONENTS 12
|
||||
#define MAX_MODES 2
|
||||
#define I2C 99
|
||||
|
||||
struct PinMapping {
|
||||
byte motor1A;
|
||||
byte motor1B;
|
||||
byte motor1PWM;
|
||||
byte motor2A;
|
||||
byte motor2B;
|
||||
byte motor2PWM;
|
||||
byte servo;
|
||||
byte red;
|
||||
byte green;
|
||||
byte blue;
|
||||
|
||||
byte speaker;
|
||||
};
|
||||
|
||||
|
||||
|
||||
class Thing {
|
||||
|
||||
private:
|
||||
bool debug;
|
||||
bool found;
|
||||
int componentIndex;
|
||||
|
||||
public:
|
||||
Thing();
|
||||
Thing(char*);
|
||||
|
||||
bool serial;
|
||||
bool network;
|
||||
|
||||
int mode;
|
||||
int lastMode;
|
||||
int modeCounter;
|
||||
byte modeChanged1;
|
||||
char* name;
|
||||
Mode modes[MAX_MODES];
|
||||
|
||||
int numberOfComponents;
|
||||
Component components[MAX_COMPONENTS];
|
||||
|
||||
PinMapping pins;
|
||||
void start();
|
||||
void start(int noInt);
|
||||
|
||||
void beep(int);
|
||||
void addComponent(char*, int, int);
|
||||
void addComponent(char*, int, int,char*(*readFunction)(), char*(*writeFunction)(char*));
|
||||
void loop();
|
||||
void addMode(int modeNumber, void(*modeFunction)());
|
||||
byte modeChanged();
|
||||
|
||||
String discoveryString();
|
||||
int findComponent(char*) ;
|
||||
void serialServer();
|
||||
char* execute(char *);
|
||||
void printComponents(int type);
|
||||
void discoverySerial();
|
||||
void wait(long milis);
|
||||
};
|
||||
|
||||
extern Thing thing;
|
||||
//#endif
|
||||
|
||||
@@ -6,7 +6,8 @@ byte initSensor = 0;
|
||||
char retorno[6];
|
||||
|
||||
char* temperature_out() {
|
||||
if(!initSensor) InitDHT();
|
||||
//if(!initSensor)
|
||||
InitDHT();
|
||||
ReadDHT();
|
||||
if(!bGlobalErr) {
|
||||
itoa(dht_dat[2],retorno,10);
|
||||
@@ -24,7 +25,8 @@ char* temperature_out() {
|
||||
}
|
||||
|
||||
char* humidity() {
|
||||
if(!initSensor) InitDHT();
|
||||
//if(!initSensor)
|
||||
InitDHT();
|
||||
ReadDHT();
|
||||
if(!bGlobalErr) {
|
||||
itoa(dht_dat[0],retorno,10);
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <Servo.h>
|
||||
|
||||
Servo servo11;
|
||||
Servo servo12;
|
||||
Servo servo2;
|
||||
Servo servo3;
|
||||
|
||||
|
||||
char* servo_read() {
|
||||
return "0";
|
||||
}
|
||||
|
||||
char* servo_write_head(char* args) {
|
||||
servo11.attach(5);
|
||||
int pos = atoi(args);
|
||||
servo11.write(pos);
|
||||
return args;
|
||||
}
|
||||
char* servo_write_arm(char* args) {
|
||||
servo2.attach(11);
|
||||
int pos = atoi(args);
|
||||
servo2.write(pos);
|
||||
return args;
|
||||
}
|
||||
|
||||
@@ -4,18 +4,27 @@ void setup() {
|
||||
thing.addComponent("red", PWM, 3);
|
||||
thing.addComponent("green", PWM, 6);
|
||||
thing.addComponent("blue", PWM, 9);
|
||||
thing.addComponent("speaker", DIGITAL, 4);
|
||||
|
||||
thing.addComponent("light", ANALOG, 3);
|
||||
thing.addComponent("temp_in", ANALOG, 2);
|
||||
thing.addComponent("alcohol", ANALOG, 0);
|
||||
thing.addComponent("temp_out", CUSTOM, 1, temperature_out, sample_write);
|
||||
thing.addComponent("humidity", CUSTOM, 1, humidity, sample_write);
|
||||
thing.addComponent("servo", CUSTOM, 5, servo_read, servo_write);
|
||||
thing.addComponent("servoarm", CUSTOM, 6, servo_read, servo_write_arm);
|
||||
|
||||
thing.addMode(0, automation);
|
||||
thing.start();
|
||||
thing.start(-1); //this mean that we dont want multiple behavior / loops here... any other number
|
||||
Serial.begin(115200);
|
||||
}
|
||||
|
||||
char* all() {
|
||||
|
||||
//Serial.println(temp_out);
|
||||
// Serial.println(humidity);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
thing.loop();
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
#include "Device.h"
|
||||
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Ethernet.h"
|
||||
|
||||
Device homeDevice=Device("central-device");
|
||||
Ethernet ethernet=Ethernet(&homeDevice);
|
||||
|
||||
void setup() {
|
||||
|
||||
|
||||
homeDevice.add("speaker", DIGITAL, 4);
|
||||
homeDevice.add("relay1", DIGITAL, 7);
|
||||
homeDevice.add("relay2", DIGITAL, 8);
|
||||
homeDevice.add("light", ANALOG, 3);
|
||||
homeDevice.add("temperature", ANALOG, 2);
|
||||
|
||||
homeDevice.add("pwm-aux1", PWM, 3);
|
||||
homeDevice.add("pwm-aux2", PWM, 5);
|
||||
//Starting booth communication!
|
||||
Serial.begin(115200);
|
||||
int ip[]={192,168,1,15};
|
||||
ethernet.startNetwork(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
homeDevice.loop();
|
||||
ethernet.loop();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
#include "Device.h"
|
||||
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Ethernet.h"
|
||||
|
||||
Device homeDevice=Device("central-device");
|
||||
Ethernet ethernet=Ethernet(&homeDevice);
|
||||
|
||||
void setup() {
|
||||
homeDevice.add("buzz", PWM, 6);
|
||||
homeDevice.add("fan", PWM, 9);
|
||||
homeDevice.add("pwm-aux1", PWM, 3);
|
||||
homeDevice.add("pwm-aux2", PWM, 5);
|
||||
|
||||
|
||||
homeDevice.add("speaker", DIGITAL, 4);
|
||||
homeDevice.add("relay1", DIGITAL, 7);
|
||||
homeDevice.add("relay2", DIGITAL, 8);
|
||||
homeDevice.add("relay3", DIGITAL, 15);
|
||||
homeDevice.add("relay4", DIGITAL, 14);
|
||||
homeDevice.add("light", ANALOG, 3);
|
||||
homeDevice.add("temp", ANALOG, 2);
|
||||
//Starting booth communication!
|
||||
Serial.begin(115200);
|
||||
int ip[]={192,168,1,15};
|
||||
ethernet.startNetwork(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
homeDevice.loop();
|
||||
ethernet.loop();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
#include "Device.h"
|
||||
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Ethernet.h"
|
||||
|
||||
Device homeDevice=Device("central-device");
|
||||
Ethernet ethernet=Ethernet(&homeDevice);
|
||||
|
||||
void setup() {
|
||||
homeDevice.add("buzz", PWM, 6);
|
||||
homeDevice.add("fan", PWM, 9);
|
||||
homeDevice.add("pwm-aux1", PWM, 3);
|
||||
homeDevice.add("pwm-aux2", PWM, 5);
|
||||
|
||||
|
||||
homeDevice.add("speaker", DIGITAL, 4);
|
||||
homeDevice.add("relay1", DIGITAL, 7);
|
||||
homeDevice.add("relay2", DIGITAL, 8);
|
||||
homeDevice.add("relay3", DIGITAL, 15);
|
||||
homeDevice.add("relay4", DIGITAL, 14);
|
||||
homeDevice.add("light", ANALOG, 3);
|
||||
homeDevice.add("temp", ANALOG, 2);
|
||||
//Starting booth communication!
|
||||
Serial.begin(115200);
|
||||
int ip[]={192,168,1,15};
|
||||
ethernet.startNetwork(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
homeDevice.loop();
|
||||
ethernet.loop();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
#include "Device.h"
|
||||
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Ethernet.h"
|
||||
|
||||
Device homeDevice=Device("central-device");
|
||||
Ethernet ethernet=Ethernet(&homeDevice);
|
||||
|
||||
void setup() {
|
||||
homeDevice.add("buzz", PWM, 6);
|
||||
homeDevice.add("red", PWM, 9);
|
||||
homeDevice.add("green", PWM, 5);
|
||||
homeDevice.add("blue", PWM, 3);
|
||||
|
||||
homeDevice.add("porta-esquerda", DIGITAL, 10);
|
||||
homeDevice.add("portao-direita", DIGITAL, 11);
|
||||
|
||||
homeDevice.add("speaker", DIGITAL, 4);
|
||||
homeDevice.add("ventilador", DIGITAL, 7);
|
||||
homeDevice.add("luz1", DIGITAL, 8);
|
||||
homeDevice.add("luz2", DIGITAL, 15);
|
||||
homeDevice.add("tv-xbox", DIGITAL, 14);
|
||||
homeDevice.add("light", ANALOG, 3);
|
||||
homeDevice.add("temp", ANALOG, 2);
|
||||
//Starting booth communication!
|
||||
Serial.begin(115200);
|
||||
int ip[]={192,168,1,3};
|
||||
ethernet.startNetwork(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
homeDevice.loop();
|
||||
ethernet.loop();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
#include <Servo.h>
|
||||
|
||||
Servo servo;
|
||||
|
||||
|
||||
char* servo_read() {
|
||||
return "0";
|
||||
}
|
||||
|
||||
char* servo_write(char* args) {
|
||||
servo.attach(5);
|
||||
int pos = atoi(args);
|
||||
servo.write(pos);
|
||||
return "0";
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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,54 +0,0 @@
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Ethernet.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
static uint8_t mac[6] = {0x54,0x55,0x58,0x10,0x00,0x24};
|
||||
static uint8_t ip[4] = {192,168,1,15};
|
||||
static uint16_t port = 80;
|
||||
Ethernet::Ethernet(Device* device1) {
|
||||
device=device1;
|
||||
}
|
||||
void Ethernet::startNetwork(int ipn[]) {
|
||||
ip[0]=ipn[0];
|
||||
ip[1]=ipn[1];
|
||||
ip[2]=ipn[2];
|
||||
ip[3]=ipn[3];
|
||||
ethernet.setup(mac, ip, port);
|
||||
}
|
||||
|
||||
void Ethernet::discoveryNetwork() {
|
||||
ethernet.print(device->name);
|
||||
ethernet.print("|");
|
||||
|
||||
ethernet.print(device->numberOfComponents);
|
||||
ethernet.print("|");
|
||||
for(int x=0;x<device->numberOfComponents;x++) {
|
||||
ethernet.print(device->components[x].name);
|
||||
ethernet.print("|");
|
||||
ethernet.print(device->components[x].getTypeName());
|
||||
ethernet.print("|");
|
||||
ethernet.print(device->components[x].port);
|
||||
ethernet.print("|");
|
||||
|
||||
ethernet.print(device->components[x].getValue());
|
||||
ethernet.print("|");
|
||||
}
|
||||
}
|
||||
void Ethernet::loop() {
|
||||
char* param;
|
||||
if (param = ethernet.serviceRequest())
|
||||
{
|
||||
|
||||
if(param[0]=='\0') {
|
||||
discoveryNetwork();
|
||||
}
|
||||
else if(strcmp("discovery", param)==0) {
|
||||
discoveryNetwork();
|
||||
}
|
||||
else {
|
||||
ethernet.print(device->execute(param));
|
||||
}
|
||||
ethernet.respond();
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
#ifndef ETHERNET_H
|
||||
#define ETHERNET_H
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Arduino.h"
|
||||
#include "Device.h"
|
||||
|
||||
class Ethernet {
|
||||
private:
|
||||
|
||||
public:
|
||||
Device* device;
|
||||
Ethernet(Device* device);
|
||||
ETHER_28J60 ethernet;
|
||||
void startNetwork(int ipn[]);
|
||||
void loop();
|
||||
void discoveryNetwork();
|
||||
};
|
||||
#endif
|
||||
@@ -1,32 +0,0 @@
|
||||
#include "Device.h"
|
||||
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Ethernet.h"
|
||||
|
||||
Device homeDevice=Device("central-device");
|
||||
Ethernet ethernet=Ethernet(&homeDevice);
|
||||
|
||||
void setup() {
|
||||
|
||||
|
||||
homeDevice.add("speaker", DIGITAL, 4);
|
||||
homeDevice.add("relay1", DIGITAL, 7);
|
||||
homeDevice.add("relay2", DIGITAL, 8);
|
||||
homeDevice.add("light", ANALOG, 3);
|
||||
homeDevice.add("temperature", ANALOG, 2);
|
||||
|
||||
homeDevice.add("pwm-aux1", PWM, 3);
|
||||
homeDevice.add("pwm-aux2", PWM, 5);
|
||||
//Starting booth communication!
|
||||
Serial.begin(115200);
|
||||
int ip[]={192,168,1,15};
|
||||
ethernet.startNetwork(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
homeDevice.loop();
|
||||
ethernet.loop();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
#include "Device.h"
|
||||
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Ethernet.h"
|
||||
|
||||
Device homeDevice=Device("central-device");
|
||||
Ethernet ethernet=Ethernet(&homeDevice);
|
||||
|
||||
void setup() {
|
||||
homeDevice.add("buzz", PWM, 6);
|
||||
homeDevice.add("fan", PWM, 9);
|
||||
homeDevice.add("pwm-aux1", PWM, 3);
|
||||
homeDevice.add("pwm-aux2", PWM, 5);
|
||||
|
||||
|
||||
homeDevice.add("speaker", DIGITAL, 4);
|
||||
homeDevice.add("relay1", DIGITAL, 7);
|
||||
homeDevice.add("relay2", DIGITAL, 8);
|
||||
homeDevice.add("relay3", DIGITAL, 15);
|
||||
homeDevice.add("relay4", DIGITAL, 14);
|
||||
homeDevice.add("light", ANALOG, 3);
|
||||
homeDevice.add("temp", ANALOG, 2);
|
||||
//Starting booth communication!
|
||||
Serial.begin(115200);
|
||||
int ip[]={192,168,1,15};
|
||||
ethernet.startNetwork(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
homeDevice.loop();
|
||||
ethernet.loop();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
#include "Device.h"
|
||||
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Ethernet.h"
|
||||
|
||||
Device homeDevice=Device("central-device");
|
||||
Ethernet ethernet=Ethernet(&homeDevice);
|
||||
|
||||
void setup() {
|
||||
homeDevice.add("buzz", PWM, 6);
|
||||
homeDevice.add("fan", PWM, 9);
|
||||
homeDevice.add("pwm-aux1", PWM, 3);
|
||||
homeDevice.add("pwm-aux2", PWM, 5);
|
||||
|
||||
|
||||
homeDevice.add("speaker", DIGITAL, 4);
|
||||
homeDevice.add("relay1", DIGITAL, 7);
|
||||
homeDevice.add("relay2", DIGITAL, 8);
|
||||
homeDevice.add("relay3", DIGITAL, 15);
|
||||
homeDevice.add("relay4", DIGITAL, 14);
|
||||
homeDevice.add("light", ANALOG, 3);
|
||||
homeDevice.add("temp", ANALOG, 2);
|
||||
//Starting booth communication!
|
||||
Serial.begin(115200);
|
||||
int ip[]={192,168,1,15};
|
||||
ethernet.startNetwork(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
homeDevice.loop();
|
||||
ethernet.loop();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
#include "Device.h"
|
||||
|
||||
#include "etherShield.h"
|
||||
#include "ETHER_28J60.h"
|
||||
#include "Ethernet.h"
|
||||
|
||||
Device homeDevice=Device("central-device");
|
||||
Ethernet ethernet=Ethernet(&homeDevice);
|
||||
|
||||
void setup() {
|
||||
homeDevice.add("buzz", PWM, 6);
|
||||
homeDevice.add("red", PWM, 9);
|
||||
homeDevice.add("green", PWM, 5);
|
||||
homeDevice.add("blue", PWM, 3);
|
||||
|
||||
homeDevice.add("porta-esquerda", DIGITAL, 10);
|
||||
homeDevice.add("portao-direita", DIGITAL, 11);
|
||||
|
||||
homeDevice.add("speaker", DIGITAL, 4);
|
||||
homeDevice.add("ventilador", DIGITAL, 7);
|
||||
homeDevice.add("luz1", DIGITAL, 8);
|
||||
homeDevice.add("luz2", DIGITAL, 15);
|
||||
homeDevice.add("tv-xbox", DIGITAL, 14);
|
||||
homeDevice.add("light", ANALOG, 3);
|
||||
homeDevice.add("temp", ANALOG, 2);
|
||||
//Starting booth communication!
|
||||
Serial.begin(115200);
|
||||
int ip[]={192,168,1,3};
|
||||
ethernet.startNetwork(ip);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
homeDevice.loop();
|
||||
ethernet.loop();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.things</groupId>
|
||||
<artifactId>things-automation</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>ejb</packaging>
|
||||
|
||||
<name>Things.Automation</name>
|
||||
|
||||
<properties>
|
||||
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
<version>6.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.things</groupId>
|
||||
<artifactId>things-api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
<compilerArguments>
|
||||
<endorseddirs>${endorsed.dir}</endorseddirs>
|
||||
</compilerArguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-ejb-plugin</artifactId>
|
||||
<version>2.3</version>
|
||||
<configuration>
|
||||
<ejbVersion>3.1</ejbVersion>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>validate</phase>
|
||||
<goals>
|
||||
<goal>copy</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${endorsed.dir}</outputDirectory>
|
||||
<silent>true</silent>
|
||||
<artifactItems>
|
||||
<artifactItem>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-endorsed-api</artifactId>
|
||||
<version>6.0</version>
|
||||
<type>jar</type>
|
||||
</artifactItem>
|
||||
</artifactItems>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-release-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>Releases</id>
|
||||
<url>https://globalcode.toolscloud.net/nexus/content/repositories/releases</url>
|
||||
</repository>
|
||||
|
||||
<snapshotRepository>
|
||||
<id>Snapshots</id>
|
||||
<url>https://globalcode.toolscloud.net/nexus/content/repositories/snapshots</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
<scm>
|
||||
<developerConnection>
|
||||
scm:svn:https://unidavi.toolscloud.net/repos/unilabor/trunk/
|
||||
</developerConnection>
|
||||
</scm>
|
||||
</project>
|
||||
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package br.com.globalcode.jhome;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import javax.ejb.Remote;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@Remote
|
||||
@Local
|
||||
public interface RGB {
|
||||
public void changeColor(int red, int blue, int green) ;
|
||||
public void fade(String color, int start, int end, int time);
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package br.com.globalcode.jhome;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import javax.ejb.Remote;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@Local
|
||||
public interface Relay {
|
||||
public void turnOn(String name) ;
|
||||
public void turnOff(String name) ;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package br.com.globalcode.jhome;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import javax.ejb.Remote;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@Local
|
||||
public interface RelayScheduler {
|
||||
public void schedule(long when, long period, String relayName);
|
||||
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package br.com.globalcode.jhome;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
|
||||
public interface Sensor {
|
||||
public void registerSensorBean(SensorDrivenBean o);
|
||||
public void stop();
|
||||
public void startUpdater(long interval);
|
||||
|
||||
public void startUpdater();
|
||||
|
||||
public int getDistance();
|
||||
|
||||
public void setDistance(int distance);
|
||||
|
||||
public int getLight();
|
||||
|
||||
public void setLight(int light);
|
||||
|
||||
public int getTemperature();
|
||||
|
||||
public void setTemperature(int temperature);
|
||||
public int getTemperature1();
|
||||
|
||||
public void setTemperature1(int temperature1);
|
||||
public void setHeartBeat(int hb);
|
||||
public int getHeartBeat();
|
||||
public void setSoundNote(char c);
|
||||
public char getSoundNote();
|
||||
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package br.com.globalcode.jhome;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
public interface SensorDrivenBean {
|
||||
public void notifyChange(String sensor) ;
|
||||
/*public String getSensorName();
|
||||
public void setSensorName(String name);
|
||||
public int getValue();
|
||||
public void setValue(int x);
|
||||
public String getCompare();
|
||||
public void setCompare(String z);*/
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package br.com.globalcode.jhome;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
|
||||
public interface Updater {
|
||||
public void startUpdater(long interval);
|
||||
public void stop();
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
Manifest-Version: 1.0
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
Manifest-Version: 1.0
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
#Generated by Maven
|
||||
#Fri Sep 28 19:11:00 BRT 2012
|
||||
version=1.0-SNAPSHOT
|
||||
groupId=org.things
|
||||
artifactId=things-automation
|
||||
@@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scene Scope="Project" version="2">
|
||||
<Scope Scope="Faces Configuration Only"/>
|
||||
<Scope Scope="Project">
|
||||
<Node id="discovery.xhtml" x="118" y="55" zoom="false"/>
|
||||
<Node id="control.xhtml" x="131" y="249" zoom="true"/>
|
||||
<Node id="index.xhtml" x="534" y="48" zoom="true"/>
|
||||
<Node id="template.xhtml" x="150" y="450" zoom="true"/>
|
||||
<Node id="download.xhtml" x="150" y="300" zoom="true"/>
|
||||
<Node id="contact.xhtml" x="400" y="150" zoom="true"/>
|
||||
<Node id="teste.xhtml" x="650" y="150" zoom="true"/>
|
||||
<Node id="template-app.xhtml" x="650" y="300" zoom="true"/>
|
||||
<Node id="setup.xhtml" x="132" y="145" zoom="false"/>
|
||||
</Scope>
|
||||
<Scope Scope="All Faces Configurations"/>
|
||||
</Scene>
|
||||
@@ -1,18 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project-shared-configuration>
|
||||
<!--
|
||||
This file contains additional configuration written by modules in the NetBeans IDE.
|
||||
The configuration is intended to be shared among all the users of project and
|
||||
therefore it is assumed to be part of version control checkout.
|
||||
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
|
||||
-->
|
||||
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
|
||||
<!--
|
||||
Properties that influence various parts of the IDE, especially code formatting and the like.
|
||||
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
|
||||
That way multiple projects can share the same settings (useful for formatting rules for example).
|
||||
Any value defined here will override the pom.xml file value but is only applicable to the current project.
|
||||
-->
|
||||
<netbeans.compile.on.save>app</netbeans.compile.on.save>
|
||||
</properties>
|
||||
</project-shared-configuration>
|
||||
@@ -1,35 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<actions>
|
||||
<action>
|
||||
<actionName>run</actionName>
|
||||
<goals>
|
||||
<goal>package</goal>
|
||||
</goals>
|
||||
<properties>
|
||||
<netbeans.deploy>true</netbeans.deploy>
|
||||
<netbeans.deploy.clientUrlPart>index.globalcode</netbeans.deploy.clientUrlPart>
|
||||
</properties>
|
||||
</action>
|
||||
<action>
|
||||
<actionName>debug</actionName>
|
||||
<goals>
|
||||
<goal>package</goal>
|
||||
</goals>
|
||||
<properties>
|
||||
<netbeans.deploy>true</netbeans.deploy>
|
||||
<netbeans.deploy.clientUrlPart>index.globalcode</netbeans.deploy.clientUrlPart>
|
||||
<netbeans.deploy.debugmode>true</netbeans.deploy.debugmode>
|
||||
</properties>
|
||||
</action>
|
||||
<action>
|
||||
<actionName>profile</actionName>
|
||||
<goals>
|
||||
<goal>package</goal>
|
||||
</goals>
|
||||
<properties>
|
||||
<netbeans.deploy>true</netbeans.deploy>
|
||||
<netbeans.deploy.clientUrlPart>index.globalcode</netbeans.deploy.clientUrlPart>
|
||||
<netbeans.deploy.profilemode>true</netbeans.deploy.profilemode>
|
||||
</properties>
|
||||
</action>
|
||||
</actions>
|
||||
@@ -1,165 +0,0 @@
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>br.com.jhome</groupId>
|
||||
<artifactId>jHomeWebAdmin</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<name>jHomeWebAdmin</name>
|
||||
|
||||
<properties>
|
||||
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<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>
|
||||
<version>6.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.easymock</groupId>
|
||||
<artifactId>easymock</artifactId>
|
||||
<version>3.1</version>
|
||||
<type>jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey</groupId>
|
||||
<artifactId>jersey-json</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey</groupId>
|
||||
<artifactId>jersey-server</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey</groupId>
|
||||
<artifactId>jersey-client</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey.contribs</groupId>
|
||||
<artifactId>jersey-multipart</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
|
||||
<artifactId>oauth-client</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey.contribs.jersey-oauth</groupId>
|
||||
<artifactId>oauth-signature</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
<!-- dependencias temporárias -->
|
||||
<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>
|
||||
<groupId>org.twitter4j</groupId>
|
||||
<artifactId>twitter4j-core</artifactId>
|
||||
<version>2.2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.twitter4j</groupId>
|
||||
<artifactId>twitter4j-async</artifactId>
|
||||
<version>2.2.3</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
<compilerArguments>
|
||||
<endorseddirs>${endorsed.dir}</endorseddirs>
|
||||
</compilerArguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.1.1</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>validate</phase>
|
||||
<goals>
|
||||
<goal>copy</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>${endorsed.dir}</outputDirectory>
|
||||
<silent>true</silent>
|
||||
<artifactItems>
|
||||
<artifactItem>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-endorsed-api</artifactId>
|
||||
<version>6.0</version>
|
||||
<type>jar</type>
|
||||
</artifactItem>
|
||||
</artifactItems>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -1,25 +0,0 @@
|
||||
package org.things;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import org.things.Thing;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
public interface Device {
|
||||
public String getName();
|
||||
public String getResourceString();
|
||||
public String getDescription();
|
||||
public String getID();
|
||||
public Map<String, Thing> getThings();
|
||||
public Collection<Thing> getThingsList();
|
||||
public void send(String s) throws IOException;
|
||||
public String receive() throws IOException;
|
||||
public void close() throws IOException;
|
||||
public void open() throws IOException;
|
||||
public void discovery() throws Exception;
|
||||
public boolean connected();
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.things.device.SerialDevice;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
public class Thing {
|
||||
|
||||
private Device device;
|
||||
String identifier, name;
|
||||
String lastValue;
|
||||
String port, type;
|
||||
String description;
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Device getDevice() {
|
||||
return device;
|
||||
}
|
||||
|
||||
public void setDevice(Device device) {
|
||||
this.device = device;
|
||||
}
|
||||
|
||||
public String execute() throws Exception {
|
||||
try {
|
||||
//System.out.println("Sending " + identifier);
|
||||
device.send(identifier);
|
||||
if(device instanceof SerialDevice) Things.delay(40);
|
||||
//magic number, just don't change it. :) kkkk
|
||||
//DeviceUtil.delay((identifier.length()+2)*10);
|
||||
String r = device.receive();
|
||||
//System.out.println("Retorno: " + r);
|
||||
lastValue = r == null ? "" : r;
|
||||
return r;
|
||||
|
||||
} catch (Exception e) {
|
||||
//Error with this component, probably device was unplugged
|
||||
device.close();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public String execute(String args) throws Exception {
|
||||
device.send(identifier + "?" + args);
|
||||
//delay(20);
|
||||
if(device instanceof SerialDevice) Things.delay(40);//era 30
|
||||
|
||||
String r = device.receive();
|
||||
lastValue = r == null ? "" : r;
|
||||
return r;
|
||||
}
|
||||
|
||||
public Thing(Device device, String identifier, String name, String port, String type, String lastValue) {
|
||||
this.device = device;
|
||||
|
||||
Logger.getLogger(Thing.class.getName()).log(Level.INFO,
|
||||
"Creating Component " + name + " with ID " + identifier);
|
||||
|
||||
this.identifier = identifier;
|
||||
this.name = name;
|
||||
this.port = port;
|
||||
this.type = type;
|
||||
this.lastValue = lastValue;
|
||||
}
|
||||
|
||||
public void setIdentifier(String identifier) {
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public void setLastValue(String lastValue) {
|
||||
this.lastValue = lastValue;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getLastValue() {
|
||||
return lastValue;
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
public String getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String t) {
|
||||
this.type = t;
|
||||
}
|
||||
|
||||
public void setPort(String p) {
|
||||
this.port = p;
|
||||
}
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Named;
|
||||
import org.things.device.InternetDevice;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@Named
|
||||
public class Things {
|
||||
public Things() {
|
||||
}
|
||||
//para uso em Java SE
|
||||
public static Things things=new Things();
|
||||
|
||||
public static void delay(long milis) {
|
||||
try {
|
||||
Thread.sleep(milis);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
|
||||
protected Collection<Device> devices;
|
||||
|
||||
|
||||
public String getThingsString() {
|
||||
int t = 0;
|
||||
for (Device device : devices) {
|
||||
t += device.getThings().size();
|
||||
}
|
||||
String retornao = "jhome-server|" + t + "|";
|
||||
for (Device device : devices) {
|
||||
|
||||
for(String c : device.getThings().keySet()) {
|
||||
Thing component = device.getThings().get(c);
|
||||
retornao += component.getName() + "|" +
|
||||
component.getType() + "|" +
|
||||
component.getPort() + "|" +
|
||||
component.getLastValue() + "|";
|
||||
}
|
||||
}
|
||||
return retornao;
|
||||
}
|
||||
|
||||
public Thing find(String id) {
|
||||
Thing thing = null;
|
||||
for (Device device : devices) {
|
||||
if (device.getThings().containsKey(id)) {
|
||||
thing = device.getThings().get(id);
|
||||
}
|
||||
}
|
||||
return thing;
|
||||
}
|
||||
|
||||
public String execute(String thing) {
|
||||
String r = null;
|
||||
Thing found = find(thing);
|
||||
if (found != null) {
|
||||
try {
|
||||
r = found.execute();
|
||||
found.setLastValue(r);
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(Things.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
} else {
|
||||
Logger.getLogger(Things.class.getName()).log(Level.INFO, "Component " + thing + " not found!");
|
||||
//Auto rediscovery on..
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
public String execute(String thing, String args) {
|
||||
String r = null;
|
||||
Thing found = find(thing);
|
||||
if (found != null) {
|
||||
try {
|
||||
r = found.execute(args);
|
||||
found.setLastValue(r);
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(Things.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
} else {
|
||||
Logger.getLogger(Things.class.getName()).log(Level.INFO, "Component " + thing + " not found!");
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
for (Device device : devices) {
|
||||
try {
|
||||
device.close();
|
||||
} catch (IOException e) {
|
||||
Logger.getLogger(Things.class.getName()).log(Level.INFO, "Exception while closing " + device.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<Device> discoveryNetworkThings(String args) {
|
||||
if (devices == null) {
|
||||
devices = new ArrayList<Device>();
|
||||
}
|
||||
|
||||
Collection<Device> devicesFound = new ArrayList<Device>();
|
||||
Device device = null;
|
||||
try {
|
||||
device = new InternetDevice(args);
|
||||
device.discovery();
|
||||
|
||||
if (device.connected()) {
|
||||
this.devices.add(device);
|
||||
} else {
|
||||
Logger.getLogger(Things.class.getName()).log(Level.INFO,
|
||||
"Network Device is not jHome compatible " + device.getID());
|
||||
device.close();
|
||||
}
|
||||
devicesFound.add(device);
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(Things.class.getName()).log(Level.WARNING,
|
||||
"Network Device Error " + device.getID());
|
||||
}
|
||||
return devicesFound;
|
||||
}
|
||||
|
||||
public Collection<Device> getDevices() {
|
||||
return devices;
|
||||
}
|
||||
|
||||
/*public Collection<Device> discoveryBluetooth(String args) throws Exception {
|
||||
return this.discoverySerial(args);
|
||||
}*/
|
||||
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
package org.things.device;
|
||||
|
||||
import org.things.Device;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.apache.commons.httpclient.HttpClient;
|
||||
import org.apache.commons.httpclient.HttpMethod;
|
||||
import org.apache.commons.httpclient.methods.GetMethod;
|
||||
import org.things.Thing;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
public class InternetDevice implements Device {
|
||||
|
||||
final static int DISCOVERY_RETRY = 3;
|
||||
String resources;
|
||||
String name;
|
||||
String description;
|
||||
boolean connected;
|
||||
Map<String, Thing> components;
|
||||
Collection<Thing> componentsList;
|
||||
String lastValue;
|
||||
String ip;
|
||||
public InternetDevice(String ip) {
|
||||
this.ip=ip;
|
||||
}
|
||||
@Override
|
||||
public boolean connected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
}
|
||||
|
||||
public void open() throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void discovery() throws Exception {
|
||||
//Those times are totally dependent with the kind of communication...
|
||||
|
||||
for (int x = 0; x < DISCOVERY_RETRY; x++) {
|
||||
System.out.println("try no. " + x);
|
||||
resources = new String(readFromURL("http://" + getID() + "/discovery"));
|
||||
System.out.println("Server " + "http://" + getID() + "/discovery");
|
||||
if (resources != null) {
|
||||
Logger.getLogger(InternetDevice.class.getName()).log(Level.INFO,
|
||||
"jHome Compatible device found! Resource String: {0}", resources);
|
||||
components = new Hashtable<String, Thing>();
|
||||
componentsList = new ArrayList<Thing>();
|
||||
connected = true;
|
||||
System.out.println("Resources " + resources);
|
||||
try {
|
||||
StringTokenizer tokenizer = new StringTokenizer(resources, "|");
|
||||
this.name = tokenizer.nextToken();
|
||||
int numberOfComponents = Integer.parseInt(tokenizer.nextToken());
|
||||
for (int y = 0; y < numberOfComponents; y++) {
|
||||
String name = tokenizer.nextToken();
|
||||
String type = tokenizer.nextToken();
|
||||
String port = tokenizer.nextToken();
|
||||
String value = tokenizer.nextToken();
|
||||
Thing component = new Thing(this,name,name, port, type, value);
|
||||
this.components.put(name, component);
|
||||
this.componentsList.add(component);
|
||||
//this.components.add(new Co);
|
||||
}
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(InternetDevice.class.getName()).log(Level.INFO,
|
||||
"Wrong resource String. Parse error!", e);
|
||||
}
|
||||
} else {
|
||||
Logger.getLogger(InternetDevice.class.getName()).log(Level.INFO,
|
||||
"Empty Resource String - Nor a jHome device", resources);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] readFromURL(String url) {
|
||||
byte[] responseBody = null;
|
||||
try {
|
||||
HttpClient client = new HttpClient();
|
||||
HttpMethod method = new GetMethod(url);
|
||||
client.executeMethod(method);
|
||||
responseBody = method.getResponseBody();
|
||||
} catch (Exception e) {
|
||||
//let's
|
||||
e.printStackTrace();
|
||||
}
|
||||
return responseBody;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(String s) throws IOException {
|
||||
byte b[] =readFromURL("http://" + this.ip + "/" + s);
|
||||
if(b!=null && b.length>0) lastValue = new String(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String receive() throws IOException {
|
||||
return lastValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getResourceString() {
|
||||
return resources;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Thing> getThings() {
|
||||
return this.components;
|
||||
}
|
||||
@Override
|
||||
public Collection<Thing> getThingsList() {
|
||||
return componentsList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
@@ -1,284 +0,0 @@
|
||||
package org.things.device;
|
||||
|
||||
import org.things.Device;
|
||||
import gnu.io.CommPortIdentifier;
|
||||
import gnu.io.SerialPort;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.things.Thing;
|
||||
import org.things.Things;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
public class SerialDevice implements Device {
|
||||
|
||||
private static final int DEFAULT_BAUDRATE = 115200;
|
||||
final static int DISCOVERY_RETRY = 3;
|
||||
CommPortIdentifier portId;
|
||||
String portName;
|
||||
int baudRate;
|
||||
SerialPort serialPort;
|
||||
OutputStream outputStream;
|
||||
InputStream inputStream;
|
||||
String resources;
|
||||
String name;
|
||||
String description;
|
||||
boolean connected;
|
||||
Map<String, Thing> things;
|
||||
Collection<Thing> thingsList;
|
||||
|
||||
public SerialDevice(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) {
|
||||
this.portName = portName;
|
||||
this.baudRate = baudRate;
|
||||
things = new Hashtable<String, Thing>();
|
||||
thingsList = new ArrayList<Thing>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean connected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
|
||||
"Closing device on {0}", serialPort.getName());
|
||||
//send("X");
|
||||
connected = false;
|
||||
try {
|
||||
inputStream.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
try {
|
||||
outputStream.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
try {
|
||||
if (serialPort != null) {
|
||||
serialPort.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open() throws IOException {
|
||||
try {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if (portName != null) {
|
||||
portId =
|
||||
CommPortIdentifier.getPortIdentifier(portName);
|
||||
serialPort =
|
||||
(SerialPort) portId.open(portName, baudRate);
|
||||
} else {
|
||||
serialPort =
|
||||
(SerialPort) portId.open(portId.getName(), baudRate);
|
||||
|
||||
}
|
||||
if(serialPort==null) {
|
||||
throw new IOException("port not found!");
|
||||
}
|
||||
serialPort.setSerialPortParams(baudRate,
|
||||
SerialPort.DATABITS_8,
|
||||
SerialPort.STOPBITS_1,
|
||||
SerialPort.PARITY_NONE);
|
||||
serialPort.notifyOnOutputEmpty(true);
|
||||
outputStream = serialPort.getOutputStream();
|
||||
inputStream = serialPort.getInputStream();
|
||||
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
|
||||
"Connection Stabilished with {0}", serialPort.getName());
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(SerialDevice.class.getName()).log(Level.SEVERE,
|
||||
"Could not init the device on " + serialPort.getName(), e);
|
||||
serialPort.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void discovery() throws Exception {
|
||||
//Those times are totally dependent with the kind of communication...
|
||||
for (int x = 0; x < DISCOVERY_RETRY; x++) {
|
||||
System.out.println("Delaying 2500 - try no." + x);
|
||||
Things.delay(2500);
|
||||
send("discovery");
|
||||
resources = receive();
|
||||
Things.delay(50);
|
||||
|
||||
if (resources != null) {
|
||||
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
|
||||
"Things API Compatible Device found! Resource String: {0}", resources);
|
||||
things = new Hashtable<String, Thing>();
|
||||
thingsList = new ArrayList<Thing>();
|
||||
|
||||
connected = true;
|
||||
try {
|
||||
StringTokenizer tokenizer = new StringTokenizer(resources, "|");
|
||||
this.name = tokenizer.nextToken();
|
||||
int numberOfComponents = Integer.parseInt(tokenizer.nextToken());
|
||||
for (int y = 0; y < numberOfComponents; y++) {
|
||||
String name = tokenizer.nextToken();
|
||||
String type = tokenizer.nextToken();
|
||||
String port = tokenizer.nextToken();
|
||||
String value = tokenizer.nextToken();
|
||||
|
||||
Thing component = new Thing(this, name, name, port, type, value);
|
||||
this.things.put(name, component);
|
||||
this.thingsList.add(component);
|
||||
|
||||
}
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
|
||||
"Wrong resource String. Parse error!", e);
|
||||
}
|
||||
} else {
|
||||
Logger.getLogger(SerialDevice.class.getName()).log(Level.INFO,
|
||||
"Empty Resource String - Nor a jHome device", resources);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(String 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.getBytes());
|
||||
outputStream.flush();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String receive() 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 {
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getResourceString() {
|
||||
return resources;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Thing> getThings() {
|
||||
return this.things;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getID() {
|
||||
return this.portId.getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Thing> getThingsList() {
|
||||
return thingsList;
|
||||
}
|
||||
|
||||
public Collection<Device> scanPorts() {
|
||||
|
||||
Collection<Device> devicesFound = new ArrayList<Device>();
|
||||
|
||||
Enumeration portList;
|
||||
CommPortIdentifier portId;
|
||||
|
||||
portList = CommPortIdentifier.getPortIdentifiers();
|
||||
Logger.getLogger(Things.class.getName()).log(Level.INFO, "Starting jHome 1.0");
|
||||
SerialPort serialPort = null;
|
||||
while (portList.hasMoreElements()) {
|
||||
portId = (CommPortIdentifier) portList.nextElement();
|
||||
Logger.getLogger(Things.class.getName()).log(Level.INFO,
|
||||
"Scaning port: " + portId.getName());
|
||||
|
||||
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
|
||||
Logger.getLogger(Things.class.getName()).log(Level.INFO,
|
||||
"Serial Device Port found " + portId.getName()
|
||||
+ ". Trying to discovery this device.");
|
||||
try {
|
||||
/*serialPort =
|
||||
(SerialPort) portId.open(portId.getName(), 115200);
|
||||
Device device = new SerialDevice(serialPort);*/
|
||||
Device device = new SerialDevice(portId, DEFAULT_BAUDRATE);
|
||||
device.open();
|
||||
device.discovery();
|
||||
if (device.connected()) {
|
||||
devicesFound.add(device);
|
||||
} else {
|
||||
Logger.getLogger(Things.class.getName()).log(Level.INFO,
|
||||
"Serial Device is not Things API Compatible" + portId.getName());
|
||||
device.close();
|
||||
}
|
||||
devicesFound.add(device);
|
||||
} catch (Exception e) {
|
||||
Logger.getLogger(Things.class.getName()).log(Level.SEVERE,
|
||||
"Couldn't connect to" + portId.getName());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return devicesFound;
|
||||
}
|
||||
|
||||
public String getPortName() {
|
||||
return portName;
|
||||
}
|
||||
|
||||
public void setPortName(String portName) {
|
||||
this.portName = portName;
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things.web;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import javax.faces.bean.ManagedBean;
|
||||
import javax.faces.bean.SessionScoped;
|
||||
import javax.inject.Inject;
|
||||
import org.things.Thing;
|
||||
import org.things.Things;
|
||||
import org.things.Device;
|
||||
import org.things.device.SerialDevice;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@ManagedBean
|
||||
@SessionScoped
|
||||
public class AdminController {
|
||||
|
||||
@Inject
|
||||
private Things things;
|
||||
private String discoveryIP = "192.168.1.15";
|
||||
private String discoveryDeviceType;
|
||||
private String discoverySerial = "/dev/ttyUSB0";
|
||||
private Device lastDeviceFound;
|
||||
private Thing componentToEdit;
|
||||
|
||||
public Thing getComponentToEdit() {
|
||||
return componentToEdit;
|
||||
}
|
||||
|
||||
public void setComponentToEdit(Thing componentToEdit) {
|
||||
this.componentToEdit = componentToEdit;
|
||||
}
|
||||
|
||||
public Things getDeviceManager() {
|
||||
return things;
|
||||
}
|
||||
|
||||
public void setDeviceManager(Things deviceManager) {
|
||||
this.things = deviceManager;
|
||||
}
|
||||
|
||||
public String getDiscoveryDeviceType() {
|
||||
return discoveryDeviceType;
|
||||
}
|
||||
|
||||
public void setDiscoveryDeviceType(String discoveryDeviceType) {
|
||||
this.discoveryDeviceType = discoveryDeviceType;
|
||||
}
|
||||
|
||||
public String getDiscoveryIP() {
|
||||
return discoveryIP;
|
||||
}
|
||||
|
||||
public void setDiscoveryIP(String discoveryIP) {
|
||||
this.discoveryIP = discoveryIP;
|
||||
}
|
||||
|
||||
public String getDiscoverySerial() {
|
||||
return discoverySerial;
|
||||
}
|
||||
|
||||
public void setDiscoverySerial(String discoverySerial) {
|
||||
this.discoverySerial = discoverySerial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance of AdminController
|
||||
*/
|
||||
public AdminController() {
|
||||
}
|
||||
|
||||
public Device getLastDeviceFound() {
|
||||
return lastDeviceFound;
|
||||
}
|
||||
|
||||
public void setLastDeviceFound(Device lastDeviceFound) {
|
||||
this.lastDeviceFound = lastDeviceFound;
|
||||
}
|
||||
|
||||
public String discoverySerial() {
|
||||
String navegacao = null;
|
||||
SerialDevice device =
|
||||
new SerialDevice(this.discoverySerial, 115200);
|
||||
try {
|
||||
|
||||
|
||||
device.open();
|
||||
this.lastDeviceFound = device;
|
||||
navegacao = "setup";
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(AdminController.class.getName()).log(Level.SEVERE, null, ex);
|
||||
navegacao = "discovery";
|
||||
}
|
||||
return navegacao;
|
||||
|
||||
}
|
||||
|
||||
public String discoveryNetwork() {
|
||||
try {
|
||||
Collection<Device> devices = things.discoveryNetworkThings(this.getDiscoveryIP());
|
||||
if (devices.size() > 0) {
|
||||
this.lastDeviceFound = devices.iterator().next();
|
||||
return "setup";
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(AdminController.class
|
||||
.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return "discovery";
|
||||
}
|
||||
|
||||
public void updateComponent() {
|
||||
try {
|
||||
componentToEdit.setLastValue(componentToEdit.execute());
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(AdminController.class
|
||||
.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void editComponent(int index) {
|
||||
componentToEdit = (Thing) lastDeviceFound.getThingsList().toArray()[index];
|
||||
}
|
||||
}
|
||||
@@ -1,132 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things.web;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Random;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@WebServlet(name = "Emulator", urlPatterns = {"/Emulator/*"})
|
||||
public class Emulator extends HttpServlet {
|
||||
static Collection<Component> components = new ArrayList<Component>();
|
||||
@Override
|
||||
public void init() {
|
||||
components.add(new Component("relay-1","digital", 0, 1));
|
||||
components.add(new Component("relay-2","digital", 0, 1));
|
||||
components.add(new Component("light","analog", 0, 1));
|
||||
components.add(new Component("temperature","analog", 0, 1));
|
||||
components.add(new Component("red","pwm", 0, 1));
|
||||
components.add(new Component("green","pwm", 0, 1));
|
||||
components.add(new Component("blue","pwm", 0, 1));
|
||||
}
|
||||
|
||||
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.">
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a short description of the servlet.
|
||||
* @return a String containing servlet description
|
||||
*/
|
||||
@Override
|
||||
public String getServletInfo() {
|
||||
return "Short description";
|
||||
}// </editor-fold>
|
||||
|
||||
private void discovery(PrintWriter out) {
|
||||
out.print("emulator-device|" + components.size() + "|");
|
||||
for(Component c : components) {
|
||||
out.print(c.name + "|" + c.type + "|" + c.port + "|" + c.value + "|");
|
||||
}
|
||||
}
|
||||
|
||||
private void execute(String componentName, String value, PrintWriter out) {
|
||||
System.out.println("Component: " + componentName);
|
||||
System.out.println("Value: " + value);
|
||||
Component c = findComponent(componentName);
|
||||
if(c==null) return;
|
||||
if(value!=null) c.value = Integer.parseInt(value);
|
||||
else c.value = new Random().nextInt()/10000;
|
||||
|
||||
out.print(value);
|
||||
}
|
||||
Component findComponent(String name) {
|
||||
Component r=null;
|
||||
for(Component c : components) {
|
||||
if(c.name.equals(name)) r = c;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
class Component
|
||||
{
|
||||
String name;
|
||||
String type;
|
||||
int value;
|
||||
int port;
|
||||
|
||||
public Component(String name, String type, int value, int port) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.value = value;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
package org.things.web.rest;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@XmlRootElement(name = "sensor")
|
||||
public class SensorHelper {
|
||||
String heartBeat;
|
||||
|
||||
public String getHeartBeat() {
|
||||
return heartBeat;
|
||||
}
|
||||
|
||||
public void setHeartBeat(String heartBeat) {
|
||||
this.heartBeat = heartBeat;
|
||||
}
|
||||
String temperature;
|
||||
String temperature1;
|
||||
String light;
|
||||
String distance;
|
||||
String note;
|
||||
String temperatureCelsius;
|
||||
String temperatureCelsius1;
|
||||
public String getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(String distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public String getLight() {
|
||||
return light;
|
||||
}
|
||||
|
||||
public void setLight(String light) {
|
||||
this.light = light;
|
||||
}
|
||||
public String getTemperature1() {
|
||||
return temperature1;
|
||||
}
|
||||
|
||||
public void setTemperature1(String temperature) {
|
||||
this.temperature1 = temperature;
|
||||
}
|
||||
public String getTemperature() {
|
||||
return temperature;
|
||||
}
|
||||
|
||||
public void setTemperature(String temperature) {
|
||||
this.temperature = temperature;
|
||||
}
|
||||
public void setTemperatureCelsius(String temperature) {
|
||||
this.temperature = temperature;
|
||||
}
|
||||
|
||||
public String getTemperatureCelsius() {
|
||||
float f = Float.parseFloat(temperature);
|
||||
double t = f * 0.00488;
|
||||
t*=100;
|
||||
DecimalFormat dc = new DecimalFormat("##.00");
|
||||
return dc.format(t);
|
||||
}
|
||||
|
||||
public void setTemperatureCelsius1(String temperature1) {
|
||||
this.temperature1 = temperature1;
|
||||
}
|
||||
|
||||
public String getNote() {
|
||||
return note + " " + getHumanNote(note.charAt(0));
|
||||
}
|
||||
public String getHumanNote(char c) {
|
||||
if(c=='E') return "MI";
|
||||
else if(c=='G') return "Sol";
|
||||
else if(c=='e') return "mi";
|
||||
else if(c=='A') return "Lá";
|
||||
else if(c=='D') return "Ré";
|
||||
else if(c=='B') return "Si";
|
||||
else return "";
|
||||
|
||||
}
|
||||
public void setNote(String note) {
|
||||
this.note = note;
|
||||
}
|
||||
|
||||
public String getTemperatureCelsius1() {
|
||||
float f = Float.parseFloat(temperature1);
|
||||
double t = f * 0.00488;
|
||||
t*=100;
|
||||
DecimalFormat dc = new DecimalFormat("##.00");
|
||||
return dc.format(t);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things.web.rest;
|
||||
|
||||
import org.things.x.component.Sensor;
|
||||
import javax.ejb.EJB;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Produces;
|
||||
|
||||
/**
|
||||
* REST Web Service
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@Path("sensor")
|
||||
@Stateless
|
||||
public class SensorResource {
|
||||
@EJB
|
||||
private Sensor environment;
|
||||
@Context
|
||||
private UriInfo context;
|
||||
|
||||
/** Creates a new instance of SensorResource */
|
||||
public SensorResource() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves representation of an instance of br.com.globalcode.javahome.rest.SensorResource
|
||||
* @return an instance of java.lang.String
|
||||
*/
|
||||
@GET
|
||||
@Produces("application/json")
|
||||
public SensorHelper getJson() {
|
||||
//TODO return proper representation object
|
||||
SensorHelper s = new SensorHelper();
|
||||
|
||||
s.setTemperature1("" + environment.getTemperature1());
|
||||
s.setTemperature("" + environment.getTemperature());
|
||||
s.setLight("" + environment.getLight());
|
||||
s.setDistance("" + environment.getDistance());
|
||||
s.setHeartBeat("" + environment.getHeartBeat());
|
||||
s.setNote("" + environment.getSoundNote());
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT method for updating or creating an instance of SensorResource
|
||||
* @param content representation for the resource
|
||||
* @return an HTTP response with content of the updated or created resource.
|
||||
*/
|
||||
@PUT
|
||||
@Consumes("application/json")
|
||||
public void putJson(String content) {
|
||||
}
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things.web.servlet;
|
||||
|
||||
import org.things.x.component.RelayScheduler;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Calendar;
|
||||
import javax.ejb.EJB;
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@WebServlet(name = "Agendador", urlPatterns = {"/Agendador"})
|
||||
public class Agendador extends HttpServlet {
|
||||
|
||||
@EJB
|
||||
private RelayScheduler scheduler;
|
||||
|
||||
/**
|
||||
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
|
||||
* @param request servlet request
|
||||
* @param response servlet response
|
||||
* @throws ServletException if a servlet-specific error occurs
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
response.setContentType("text/html;charset=UTF-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
try {
|
||||
|
||||
int tipo =1;
|
||||
if(request.getParameter("tipoValor")!=null) {
|
||||
tipo = Integer.parseInt(request.getParameter("tipoValor"));
|
||||
}
|
||||
int emQuantoTempo = Integer.parseInt(request.getParameter("when"));
|
||||
int porQuantoTempo = Integer.parseInt(request.getParameter("howLong"));
|
||||
String relayName = request.getParameter("relayName");
|
||||
Calendar cal = Calendar.getInstance();
|
||||
cal.add(tipo == 2 ? Calendar.MINUTE : Calendar.SECOND, emQuantoTempo);
|
||||
request.setAttribute("horarioAgendado", cal.getTime());
|
||||
emQuantoTempo *= 1000;
|
||||
porQuantoTempo *= 1000;
|
||||
if (tipo == 2) {
|
||||
emQuantoTempo *= 60;
|
||||
porQuantoTempo *= 60;
|
||||
}
|
||||
scheduler.schedule(emQuantoTempo, porQuantoTempo, relayName);
|
||||
RequestDispatcher dispatcher = request.getRequestDispatcher("index.html");
|
||||
dispatcher.forward(request, response);
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
// <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>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>
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things.web.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.ejb.EJB;
|
||||
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.Things;
|
||||
import org.things.x.component.RGB;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@WebServlet(name = "FadeRGB", urlPatterns = {"/FadeRGB"})
|
||||
public class FadeRGB extends HttpServlet {
|
||||
|
||||
private static boolean on;
|
||||
@EJB
|
||||
Things manager;
|
||||
@EJB
|
||||
private RGB rgb;
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
String color = request.getParameter("color");
|
||||
String start = request.getParameter("start");
|
||||
String end = request.getParameter("end");
|
||||
String time = request.getParameter("time");
|
||||
try {
|
||||
rgb.fade(color, Integer.parseInt(start), Integer.parseInt(end), Integer.parseInt(time));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things.web.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import javax.ejb.EJB;
|
||||
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.Things;
|
||||
import org.things.x.component.Sensor;
|
||||
import org.things.x.component.SensorDrivenBean;
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@WebServlet(name = "HeartDrivenBean",
|
||||
urlPatterns = {"/HeartDrivenBean"},
|
||||
loadOnStartup = 1)
|
||||
public class HeartDrivenBean extends HttpServlet
|
||||
implements SensorDrivenBean {
|
||||
|
||||
@EJB(name = "deviceManagerBean")
|
||||
private Things deviceManager;
|
||||
@EJB
|
||||
private Sensor environment;
|
||||
|
||||
@Override
|
||||
public void notifyChange(String sensor) {
|
||||
if (sensor.equals("heart") && environment.getHeartBeat() > 110) {
|
||||
System.out.println("VAI MORRERRR!!!!!");
|
||||
deviceManager.execute("buzz", "40");
|
||||
deviceManager.execute("relay1", "1");
|
||||
org.things.Things.delay(500);
|
||||
deviceManager.execute("relay1", "0");
|
||||
deviceManager.execute("buzz", "0");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
environment.registerSensorBean(this);
|
||||
}
|
||||
|
||||
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
}
|
||||
|
||||
// <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>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>
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things.web.servlet;
|
||||
|
||||
import org.things.x.component.Relay;
|
||||
import org.things.x.component.Relay;
|
||||
import java.io.IOException;
|
||||
import javax.ejb.EJB;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@WebServlet(name = "Light", urlPatterns = {"/Light"})
|
||||
public class Light extends HttpServlet {
|
||||
|
||||
private static boolean on;
|
||||
@EJB
|
||||
Relay light;
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
if (on) {
|
||||
light.turnOff("relay1");
|
||||
on = false;
|
||||
} else {
|
||||
light.turnOn("relay1");
|
||||
on = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things.web.servlet;
|
||||
|
||||
import org.things.x.component.RGB;
|
||||
import org.things.x.component.RGB;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import javax.ejb.EJB;
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@WebServlet(name = "RGBController", urlPatterns = {"/RGBController"})
|
||||
public class RGBController extends HttpServlet {
|
||||
|
||||
@EJB
|
||||
private RGB rgb;
|
||||
|
||||
|
||||
protected void processRequest(HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
int red = Integer.parseInt(request.getParameter("red"));
|
||||
int green = Integer.parseInt(request.getParameter("green"));
|
||||
int blue = Integer.parseInt(request.getParameter("blue"));
|
||||
rgb.changeColor(red, blue, green);
|
||||
}
|
||||
|
||||
public int map(int val) {
|
||||
return (int) (((float) (val / 255f)) * 9);
|
||||
}
|
||||
|
||||
// <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>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>
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things.web.servlet;
|
||||
|
||||
import org.things.x.component.Sensor;
|
||||
import org.things.x.component.Sensor;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import javax.ejb.EJB;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@WebServlet(name = "SensorServlet", urlPatterns = {"/SensorServlet"})
|
||||
public class SensorServlet extends HttpServlet {
|
||||
@EJB
|
||||
private Sensor environment;
|
||||
/**
|
||||
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
|
||||
* @param request servlet request
|
||||
* @param response servlet response
|
||||
* @throws ServletException if a servlet-specific error occurs
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
response.setContentType("text/html;charset=UTF-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
try {
|
||||
out.println("<html>");
|
||||
out.println("<head>");
|
||||
out.println("<title>Servlet SensorServlet</title>");
|
||||
out.println("</head>");
|
||||
out.println("<body>");
|
||||
out.println("<h1>Servlet SensorServlet at " + request.getContextPath () + "</h1>");
|
||||
out.println("<p>" + environment.getTemperature() + "</p>");
|
||||
out.println("<p>Bluetooth " + environment.getTemperature1() + "</p>");
|
||||
out.println("<p>" + environment.getLight() + "</p>");
|
||||
out.println("</body>");
|
||||
out.println("</html>");
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
// <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>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>
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.things.web.servlet;
|
||||
|
||||
import org.things.x.component.Sensor;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import javax.ejb.EJB;
|
||||
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.Things;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author vsenger
|
||||
*/
|
||||
@WebServlet(name = "Startup", urlPatterns = {"/Startup"})
|
||||
public class Startup extends HttpServlet {
|
||||
|
||||
@EJB
|
||||
private Sensor enviroment;
|
||||
@EJB
|
||||
private Things deviceManager;
|
||||
/**
|
||||
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
|
||||
* @param request servlet request
|
||||
* @param response servlet response
|
||||
* @throws ServletException if a servlet-specific error occurs
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
response.setContentType("text/html;charset=UTF-8");
|
||||
PrintWriter out = response.getWriter();
|
||||
try {
|
||||
//deviceManager.discovery();
|
||||
if(request.getParameter("action")==null) {
|
||||
enviroment.startUpdater();
|
||||
return;
|
||||
}
|
||||
else {
|
||||
String action = request.getParameter("action");
|
||||
if(action.equals("on")) enviroment.startUpdater();
|
||||
else enviroment.stop();
|
||||
}
|
||||
//RequestDispatcher dispatcher = request.getRequestDispatcher("index.html");
|
||||
//dispatcher.forward(request, response);
|
||||
} finally {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
System.out.println("Initializing jHome 1.0");
|
||||
enviroment.startUpdater();
|
||||
}
|
||||
// <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>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>
|
||||
}
|
||||