Commit a8d4983b authored by Ferreira's avatar Ferreira

Added the spectra demo source code, new install scripts and readme's as is...

Added the spectra demo source code, new install scripts and readme's as is after SPECTRA project review.


git-svn-id: http://svn.eurecom.fr/openair4G/trunk@5873 818b1a75-f10b-46b9-bf7c-635c3b92a50f
parent 065f943c
########################################
#
# SPECTRA DEMO INSTALLATION AND RUN
#
#
#
# Rui Costa <ferreira@eurecom.fr>
#
# v1.12 (13 Oct 2014)
#
########################################
########################################
1. REQUIREMENTS
To properly run the demo these are the main pieces of software
that are required:
- OPENAIR4G (checkout the main trunk from the EURECOM SVN).
- ODTONE (Open 802.21 protocol from ITAveiro git server).
- Boost C++ Libraries are requested by ODTONE (v1.48 or higher)
- REST Toolkit (Codename "Casablanca")
- Adequate shell/bash start-up scripts for both eNB and UE.
BEFORE attempting any installation YOU MUST check your system for
any required software that these components require, as for
instance, a compatible GCC/G++ compiler, build tools, etc.
NOTES BEFORE INSTALLATION:
N1. Installation SHOULD NOT be done as ROOT. IF and WHEN super
user permissions are required you will be prompted for them.
N2. You MUST have a valid EURECOM SVN-enabled account for the
Openair4G code.
N3. For ODTONE software, you MUST have installed the ITAveiro
SSL/TLS certificates available at their webserver.
N4. You MUST have Internet access in order to properly build and
deploy the software, as some of the software is fetched online.
########################################
2. BUILD & INSTALL
2.1. PREPARATION
Start by creating a folder on a non-root directory, preferably
your home folder. Copy the <spectra_demo_prepare.sh> script into
the newly created folder, henceforth referred to as
<spectra_root_folder>.
2.2. INSTALLATION
There are two ways of processing the installation:
- If you already have performed a checkout of the OpenAir svn.
(Aimed at single installations for OAI developers.)
- If you only have the demonstration source code & scripts.
(To allow easier deployment over several machines, just copy
the demo's source folder, including the prepare and install
scripts to which ever machine you need and let the scripts do
the work for you)
2.2.1 From the OPENAIR SVN
Go to the folder:
<openair_root_folder>/trunk/targets/PROJECTS/SPECTRA/DEMO_SPECTRA
and run the installation script with the node type to install:
# ./spectra_demo_install.sh -oai_svn <node_type>
2.2.2 From the demo's source code and scripts.
Go to the <spectra_root_folder> and run:
# ./spectra_demo_install.sh -full <node_type>
If the previous step has been well performed, you should now have
a local working copy of the OpenAir SVN on your local machine, along
with additional code required for the demo.
2.3. CRM
This component of the demo MUST be compiled and installed separately.
Check <spectra_root_folder>/CRM/ for more details.
2.4. TVWS Sensing Measurements
There is a server and client for simulating the TVWS measurements and
run the cognitive algorithm to activate the required actions on the
LTE link. (this is the goal of the demo)
Copy the client & server located at <spectra_root_folder>/clientSensing/
into the ENB2 startup folder (<openair_root_folder>/targets/PROJECTS/SPECTRA)
2.5. TROUBLESHOOTING
If for some reason the installation does not run as expected, check
for permission-related issues or missing software packages.
Otherwise, try to install the components one by one, as defined in
the installation scripts.
(see <spectra_demo_prepare.sh> and <spectra_demo_install.sh> for
compilation and configuration details).
The preparation script will automatically call the installation
script, but in case it does not, run it manually, it is located at:
<spectra_root_folder>/openair4G/targets/PROJECTS/SPECTRA/
NOTE: During installation we apply a patch to ODTONE, due to the
lack of a definition for LTE links and their attributes.
However, as new versions are released the patch may need to
be adapted. Therefore, be aware of compilation issues
originating from the compilation of the ODTONE core functions.
########################################
3. CONFIGURATION
You MUST make sure that the configuration of your physical and
logical interfaces matches with your Open Air and ODTONE
configuration files. Take all parameters into account while
checking the configuration files and launch scripts, located in:
<spectra_root_folder>/openair4G/targets/PROJECTS/SPECTRA/
<spectra_root_folder>/ODTONE/dist/
3.1 Base Configuration
This is the network configuration on the config files:
UE1
eth0 (192.168.13.2) - oai0 (10.0.0.2) (LTE emulated link)
ENB1
eth0 (192.168.12.122) internet access gw (192.168.12.100)
eth1 (192.168.13.1) - oai0 (10.0.0.1) (LTE emulated link)
eth2 (192.168.14.3) (Internal Relay Connection)
UE2
eth0 (192.168.14.4) (Internal Relay Connection)
eth1 (192.168.15.5) - oai0 (10.0.2.3) (LTE emulated link)
ENB2
eth0 (192.168.15.6) - oai0 (10.0.2.4) (LTE emulated link)
########################################
4. RUNNING THE DEMO
1. We advise you to turn off the network manager service in order
for the startup script configuration to be effective and final.
This can be achieved by executing:
# sudo service network-manager stop
2. (On all machines) Go to OpenAir Launch folder:
<spectra_root_folder>/openair4G/targets/PROJECTS/SPECTRA/
3. Launch the eNB1 on eNB1 machine:
# sudo ./start_enb.bash
Wait for eNB1 to reach stand by mode.
4. Launch the UE1 on UE1 machine:
# sudo ./start_ue.bash
5. Launch the eNB2 on eNB2 machine:
# sudo ./start_enb.bash
Wait for eNB2 to reach stand by mode.
6. Launch the UE2 on UE2 machine:
# sudo ./start_ue.bash
7. Any other modules will be launched automatically
if present. (CRM client, TVWS sensing, MIHF, MIH Users, etc.)
Enjoy the demo!
########################################
CHANGELOG
v0.99 (03/09/2014)
- First <spectra_demo_prepare.sh> script and <README> file.
v1.00 (04/09/2014)
- Added compartmentalization for each sw component
- Added full installation procedure from scratch
- Fetching sw from repositories, compilation & installation
- Revisioned output
v1.01 (05/09/2014)
- Added OpenAir conf files <spectra_demo_src/openair_conf>
- Added MIHF Users <spectra_demo_src/mih_users>
- Reduced printed output
v1.02 (08/09/2014)
- Bug correcting on installation and configuration paths & scripts
- Patched Open Air script <targets/PROJECTS/SPECTRA/build_all.sh>
v1.03 (09/09/2014)
- Patched MIHF to support LTE link based on previous code
- Added <spectra_demo_src/mihf/>
with patch files: <link.hpp> <bin_query.hpp>
- Revisioned <spectra_demo_prepare.sh>
v1.04 (11/09/2014)
- Corrected MIHF patch bugs
- Added patch file <archive.hpp>
- Corrected path and filename bugs on <spectra_demo_prepare.sh>
v1.05 (15/09/2014)
- Corrected MIHF patch bugs
- Added a number of patch files
- Adjusted patch execution on <spectra_demo_prepare.sh>
v1.06 (17/09/2014)
- Working demo 1 eNB 1 UE
- Modified configuration files for UE and eNB
- Substituted overwrite of files into a Git patch for SPECTRA
v1.07 (19/09/2014)
- Expanded demo to 4 machines:
- 2 real: ue1, eNB1
- 2 virtual: eNB2, ue2 (both hosted by eNB1)
v1.08 (23/09/2014)
- Included CRM client and TVWS sensing modules.
v1.09 (24/09/2014)
- Final Demo setup ready.
v1.10 (29/09/2014)
- Updated RRC code for OpenAir.
- Code commit to OpenAir SVN trunk.
v1.11 (01/10/2014)
- Restructure of source code tree and installation files to
include the full demo (2 eNB and 2 ue)
- Splitted prepare and install scripts.
- Added colour coded steps and error stoppage on install script.
v1.12 (13/10/2014)
- Added bug fixes on launch and install scripts
- Added final version to openair svn
- Reworked README file.
This diff is collapsed.
#!/bin/bash
########################################
#
# SPECTRA DEMO PREPARATION
#
#
#
SCRIPT_VERSION="v1.11"
DATE="01 Oct 2014"
AUTHOR="Rui Costa"
EMAIL="ferreira@eurecom.fr"
########################################
OWN_TAG=$0
INSTALL_PATH=$PWD
OPENAIR_PATH=$INSTALL_PATH/openair4G
ODTONE_PATH=$INSTALL_PATH/ODTONE
BOOST_PATH=$INSTALL_PATH/boost_1_49_0
SPECTRA_SRC_PATH=$INSTALL_PATH/spectra_demo_src
SPECTRA_OAI_PATH=$OPENAIR_PATH/targets/PROJECTS/SPECTRA/
########################################################################################### UTILS
function util_echo {
# return value passed in $1 (usually $?)
# return value = 0 - OK - Light green
if [ $1 -eq 0 ] ; then
echo -e "\e[92m\e[1m[$OWN_TAG]\e[0m $2"
# return value != 0 - ERROR
else
# return value -gt 255 - WARNING - Light Yellow
if [ $1 -gt 255 ] ; then
echo -e "\e[93m\e[1m[$OWN_TAG]\e[0m $2"
else
# return value 1 to 255 - ERROR - Light RED
echo -e "\e[91m\e[1m[$OWN_TAG]\e[0m $2"
exit
fi
fi
}
########################################################################################### OPENAIR
function fetch_openair {
mkdir $OPENAIR_PATH
cd $INSTALL_PATH
util_echo $? "-------------------------------------------------"
util_echo $? "OPENAIR EURECOM ..."
sleep 2
svn co http://svn.eurecom.fr/openair/openair4G/trunk $OPENAIR_PATH
util_echo $? "Checked Out openair4G/trunk from EURECOM SVN..."
}
########################################################################################### MAIN
clear
# Starting...
util_echo $? " -------------------------------------------------"
util_echo $? " SPECTRA DEMO "
util_echo $? " Prepararation Script"
util_echo $? " "
util_echo $? " $SCRIPT_VERSION $DATE"
util_echo $? " "
util_echo $? " Author: $AUTHOR <$EMAIL> "
util_echo $? " -------------------------------------------------"
util_echo $? " "
sleep 3
util_echo $? " -------------------------------------------------"
util_echo $? " >> Fetching sources"
util_echo $? " -------------------------------------------------"
fetch_openair
cd $SPECTRA_OAI_PATH/
util_echo $? " >> Finished Prepare Script!!"
util_echo $? " >> Entering Install Script!!"
./spectra_demo_install.sh -oai_svn $1
########################################################################################### EOF
#include <cpprest/http_client.h>
#include <cpprest/json.h>
#include <iostream>
#include <ostream>
#include <sstream>
#include <fstream>
#include "cpprest/basic_types.h"
#include "cpprest/asyncrt_utils.h"
#include "cpprest/uri.h"
#include <string>
#include "CRMClient.hpp"
using namespace std;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace utility;
// namespace odtone {
CRMClient::CRMClient(char* url)
{
m_url = (char*) (malloc (200* sizeof (char)));
m_url = url;
}
CRMClient::CRMClient()
{
}
CRMClient::~CRMClient()
{
}
void CRMClient::SetUrl(char* url)
{
m_url = url;
}
// Retrieves a JSON value from an HTTP request.
pplx::task<void> CRMClient::RequestJSONValueAsync()
{
http_client client (U(m_url));
return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value>
{
if(response.status_code() == status_codes::OK)
{
return response.extract_json();
}
return pplx::task_from_result(json::value());
})
.then([](pplx::task<json::value> previousTask)
{
try
{
ofstream myfile;
myfile.open ("outputGET.txt");
const json::value& v = previousTask.get();
string_t jsonString = v.to_string();
cout << U("Response...") << jsonString <<endl;
//Parsing Begin
cout << U("Start Parsing...") << endl;
for(auto iterArray = v.cbegin(); iterArray != v.cend(); ++iterArray)
{
const json::value &arrayValue = iterArray->second;
for(auto iterInner = arrayValue.cbegin(); iterInner != arrayValue.cend(); ++iterInner)
{
const json::value &propertyValue = iterInner->second;
for(auto iterlast = propertyValue.cbegin(); iterlast != propertyValue.cend(); ++iterlast)
{
const json::value &Name = iterlast->first;
const json::value &Value = iterlast->second;
cout<< U("Parameter: ") << Name.to_string()<<endl;
cout<< U("Value: ") << Value.to_string()<< std::endl;
}
}
cout << std::endl;
}
//parsing End
myfile << jsonString.c_str();
myfile.close();
}
catch (const http_exception& e)
{
wostringstream ss;
ss << e.what() << endl;
wcout << ss.str();
}
});
}
// Stores a JSON value (a Policy) from an HTTP request.
pplx::task<void> CRMClient::StoreJSONValuePolicies(char * param1, char * param2, char * param3, char * param4)
{
http_client client (U(m_url));
json::value::field_map putvalue;
putvalue.push_back(make_pair(json::value("pid"), json::value(param1)));
putvalue.push_back(make_pair(json::value("name"), json::value(param2)));
putvalue.push_back(make_pair(json::value("description"), json::value(param3)));
putvalue.push_back(make_pair(json::value("value"), json::value(param4)));
const string_t& s = "/";
json::value object = json::value::object(putvalue);
return client.request(methods::PUT, s, object).then([](http_response response)-> pplx::task<json::value>
{
if(response.status_code() == status_codes::OK)
{
return response.extract_json();
}
return pplx::task_from_result(json::value());
})
.then([](pplx::task<json::value> previousTask)
{
try
{
const json::value& v = previousTask.get();
string_t jsonString = v.to_string();
cout << U("Response...") << jsonString <<endl;
}
catch (const http_exception& e)
{
wostringstream ss;
ss << e.what() << endl;
wcout << ss.str();
}
});
}
// Stores a JSON value (a Measurement) from an HTTP request.
pplx::task<void> CRMClient::StoreJSONValuemeasurements(char * param1, char * param2, char * param3, char * param4, char * param5, char * param6)
{
http_client client (U(m_url));
json::value::field_map putvalue;
putvalue.push_back(make_pair(json::value("key"), json::value(param1)));
putvalue.push_back(make_pair(json::value("name"), json::value(param2)));
putvalue.push_back(make_pair(json::value("type"), json::value(param3)));
putvalue.push_back(make_pair(json::value("unit"), json::value(param4)));
putvalue.push_back(make_pair(json::value("value"), json::value(param5)));
putvalue.push_back(make_pair(json::value("time"), json::value(param6)));
const string_t& s = "/";
json::value object = json::value::object(putvalue);
return client.request(methods::PUT, s, object).then([](http_response response)-> pplx::task<json::value>
{
if(response.status_code() == status_codes::OK)
{
return response.extract_json();
}
return pplx::task_from_result(json::value());
})
.then([](pplx::task<json::value> previousTask)
{
try
{
const json::value& v = previousTask.get();
string_t jsonString = v.to_string();
cout << U("Response...") << jsonString <<endl;
}
catch (const http_exception& e)
{
wostringstream ss;
ss << e.what() << endl;
wcout << ss.str();
}
});
}
// Stores a JSON value (a Decision) from an HTTP request.
pplx::task<void> CRMClient::StoreJSONValuedecisions(char * param1, char * param2, char * param3, char * param4, char * param5)
{
http_client client (U(m_url));
json::value::field_map putvalue;
putvalue.push_back(make_pair(json::value("did"), json::value(param1)));
putvalue.push_back(make_pair(json::value("name"), json::value(param2)));
putvalue.push_back(make_pair(json::value("description"), json::value(param3)));
putvalue.push_back(make_pair(json::value("value"), json::value(param4)));
putvalue.push_back(make_pair(json::value("time"), json::value(param5)));
const string_t& s = "/";
json::value object = json::value::object(putvalue);
return client.request(methods::PUT, s, object).then([](http_response response)-> pplx::task<json::value>
{
if(response.status_code() == status_codes::OK)
{
return response.extract_json();
}
return pplx::task_from_result(json::value());
})
.then([](pplx::task<json::value> previousTask)
{
try
{
const json::value& v = previousTask.get();
string_t jsonString = v.to_string();
cout << U("Response...") << jsonString <<endl;
}
catch (const http_exception& e)
{
wostringstream ss;
ss << e.what() << endl;
wcout << ss.str();
}
});
}
pplx::task<void> CRMClient::Delete()
{
char* url = m_url;
return pplx::create_task([url]
// return pplx::create_task([m_url]
{
http_client client (U(url));
return client.request(methods::DEL);
}).then([](http_response response)
{
if(response.status_code() == status_codes::OK)
{
auto body = response.extract_string();
std::wcout << L"Deleted: " << body.get().c_str() << std::endl;
}
});
}
// }
\ No newline at end of file
//==============================================================================
// Brief : MIH-User
// Authors : FATMA HRIZI <hrizi@eurecom.fr>
//------------------------------------------------------------------------------
// ODTONE - Open Dot Twenty One
//
// Copyright (C) 2009-2012 Universidade Aveiro
// Copyright (C) 2009-2012 Instituto de Telecomunicações - Pólo Aveiro
//
// This software is distributed under a license. The full license
// agreement can be found in the file LICENSE in this distribution.
// This software may not be copied, modified, sold or distributed
// other than expressed in the named license agreement.
//
// This software is distributed without any warranty.
//==============================================================================
#ifndef ODTONE_CRM_CLIENT_HPP
#define ODTONE_CRM_CLIENT_HPP
#include <cpprest/http_client.h>
#include <cpprest/json.h>
#include <iostream>
#include <ostream>
#include <sstream>
#include "cpprest/basic_types.h"
#include "cpprest/asyncrt_utils.h"
#include "cpprest/uri.h"
#include <string>
// namespace odtone {
class CRMClient {
public:
CRMClient();
~CRMClient();
CRMClient(char* url);
void SetUrl(char* url);
pplx::task<void> RequestJSONValueAsync();
pplx::task<void> StoreJSONValuePolicies(char * param1, char * param2, char * param3, char * param4);
pplx::task<void> StoreJSONValuemeasurements(char * param1, char * param2, char * param3, char * param4, char * param5, char * param6);
pplx::task<void> StoreJSONValuedecisions(char * param1, char * param2, char * param3, char * param4, char * param5);
pplx::task<void> Delete();
private:
char * m_url;
};
// }
#endif
# CRMClient.o: CRMClient.cpp CRMClient.hpp
# g++-4.8 CRMClient.cpp -I /opt/casablanca/Release/include/ -L/usr/local/lib -L/opt/casablanca/Binaries/Release32/ -std=c++11 -lpthread -lboost_system -lboost_thread -lcasablanca -lboost_filesystem
#
#
# install: main.cpp
# g++-4.8 -Wall main.cpp -I /opt/casablanca/Release/include/ -L/usr/local/lib -L/opt/casablanca/Binaries/Release32/ -L. -std=c++11 -lcrmclient -lpthread -lboost_system -lboost_thread -lcasablanca -lboost_filesystem -o CRMClientmain
main: main.cpp
g++-4.8 -Wall main.cpp -I /opt/casablanca/Release/include/ -L/usr/local/lib -L/opt/casablanca/Binaries/Release32/ -L. -std=c++11 -lcrmclient -lpthread -lboost_system -lboost_thread -lcasablanca -lboost_filesystem -o CRMClientmain
lib: CRMClient.cpp CRMClient.hpp
# 64 bits compilation
# g++-4.8 -Wall -fPIC -c CRMClient.cpp -I /opt/casablanca/Release/include/ -L/usr/local/lib -L/opt/casablanca/Binaries/Release32/ -std=c++11 -m64 -lpthread -lboost_system -lboost_thread -lcasablanca -lboost_filesystem
# g++-4.8 -shared -m64 -Wl,-soname,libcrmclient.so.1 -o libcrmclient.so.1.0 CRMClient.o
# ln -sf libcrmclient.so.1.0 libcrmclient.so
# ln -sf libcrmclient.so.1.0 libcrmclient.so.1
# 32 bits compilation
g++-4.8 -Wall -fPIC -c CRMClient.cpp -I /opt/casablanca/Release/include/ -L/usr/local/lib -L/opt/casablanca/Binaries/Release32/ -std=c++11 -lpthread -lboost_system -lboost_thread -lcasablanca -lboost_filesystem
g++-4.8 -shared -Wl,-soname,libcrmclient.so.1 -o libcrmclient.so.1.0 CRMClient.o
ln -sf libcrmclient.so.1.0 libcrmclient.so
ln -sf libcrmclient.so.1.0 libcrmclient.so.1
clean:
rm *.o *.so*
#include "CRMClient.hpp"
int main(int argc,char *argv[])
{
char * policiesurl = (char*) malloc( 100 * sizeof(char) );
strcpy(policiesurl,"http://1.gae-spectra.appspot.com/policies");
// strcpy(policiesurl,"http://localhost:8888/policies");
char * measurementsurl = (char*) malloc( 100* sizeof(char) );
strcpy(measurementsurl,"http://1.gae-spectra.appspot.com/measurements");
// strcpy(measurementsurl,"http://localhost:8888/measurements");
char * decisionsurl = (char*) malloc( 100 * sizeof(char));
strcpy(decisionsurl,"http://1.gae-spectra.appspot.com/decisions");
int c;
CRMClient client (policiesurl);
client.RequestJSONValueAsync().wait();
return 0;
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="con" path="com.google.appengine.eclipse.core.GAE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/org.restlet.jar"/>
<classpathentry kind="lib" path="lib/org.restlet.ext.servlet.jar"/>
<classpathentry kind="lib" path="lib/org.restlet.ext.wadl.jar"/>
<classpathentry kind="lib" path="lib/org.restlet.ext.xml.jar"/>
<classpathentry kind="lib" path="lib/org.restlet.ext.json.jar"/>
<classpathentry kind="lib" path="lib/org.json.jar"/>
<classpathentry kind="lib" path="lib/org.springframework.context-3.1.1.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/org.springframework.web-3.1.1.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/org.springframework.web.servlet-3.1.1.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/org.springframework.beans-3.1.1.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/json_simple-1.1.jar"/>
<classpathentry kind="lib" path="lib/gcm-server.jar"/>
<classpathentry kind="output" path="war/WEB-INF/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>rest-gae-server</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.google.gdt.eclipse.core.webAppProjectValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.google.appengine.eclipse.core.gaeProjectChangeNotifier</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.google.appengine.eclipse.core.projectValidator</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.google.appengine.eclipse.core.enhancerbuilder</name>
<arguments></arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>com.google.appengine.eclipse.core.gaeNature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>
#Thu Sep 19 14:41:16 CEST 2013
eclipse.preferences.version=1
filesCopiedToWebInfLib=appengine-api-1.0-sdk-1.7.4.jar|appengine-api-labs.jar|appengine-endpoints.jar|appengine-jsr107cache-1.7.4.jar|asm-4.0.jar|datanucleus-api-jdo-3.1.1.jar|datanucleus-api-jpa-3.1.1.jar|datanucleus-appengine-2.1.1.jar|datanucleus-core-3.1.1.jar|geronimo-jpa_2.0_spec-1.0.jar|jdo-api-3.0.1.jar|jsr107cache-1.1.jar|jta-1.1.jar
gaeDatanucleusVersion=v2
gaeDeployDialogSettings=true
gaeIsEclipseDefaultInstPath=true
googleCloudSqlEnabled=false
localDevMySqlEnabled=true
ormEnhancementInclusions=src/
#Wed Dec 12 09:47:01 CET 2012
eclipse.preferences.version=1
jarsExcludedFromWebInfLib=
warSrcDir=war
warSrcDirIsOutput=true
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
<persistence-manager-factory name="transactions-optional">
<property name="javax.jdo.PersistenceManagerFactoryClass" value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory"/>
<property name="javax.jdo.option.ConnectionURL" value="appengine"/>
<property name="javax.jdo.option.NontransactionalRead" value="true"/>
<property name="javax.jdo.option.NontransactionalWrite" value="true"/>
<property name="javax.jdo.option.RetainValues" value="true"/>
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
<property name="datanucleus.appengine.singletonPMFForName" value="true"/>
</persistence-manager-factory>
</jdoconfig>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="transactions-optional">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
</properties>
</persistence-unit>
</persistence>
package fr.eurecom.pushing;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mortbay.log.Log;
import org.restlet.resource.Get;
import fr.eurecom.senml.entity.RegisteredDeviceType;
import fr.eurecom.senml.persistence.JDOStorage;
/**
* Exposes the following URLs:
* - /pushing/register, POST, to register a device
* - /pushing/unregister, POST, to unregister a device
* - /pushing/list, GET, to retrieve the list of registered devices
* - /pushing/test, GET, test a notification to the registered devices
*
*/
public class DeviceManager extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger("DeviceManager");
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
System.out.println("OK doPost");
System.out.println(request.getParameter("reqType") + " " + request.getParameter("deviceType"));
try {
if (request.getParameter("reqType").equals("reg")) {
// Registration of a device. Android or iOS?
if (request.getParameter("deviceType").equals("android")) {
registerAndroidDevice(request, response);
} else if (request.getParameter("deviceType").equals("ios")) {
registerAppleDevice(request, response);
}
} else if (request.getParameter("reqType").equals("unreg")) {
System.out.println("OK UNREGISTERING!");
if (request.getParameter("deviceType").equals("android")) {
System.out.println("UNREGISTERING ANDROID!");
unregisterAndroidDevice(request, response);
} else if (request.getParameter("deviceType").equals("ios")) {
unregisterAppleDevice(request, response);
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response.setStatus(500);
}
}
private void unregisterAppleDevice(HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
response.setStatus(403);
}
private void unregisterAndroidDevice(HttpServletRequest request,
HttpServletResponse response) {
String key = request.getParameter("registrationID");
// TODO Auto-generated method stub
List<RegisteredDeviceType> devices = JDOStorage.getInstance().getAll(RegisteredDeviceType.class);
Iterator<RegisteredDeviceType> iter = devices.iterator();
while (iter.hasNext()) {
RegisteredDeviceType device = iter.next();
System.out.print("Analyzing " + device.getDeviceRegistrationKey());
if (device.getDeviceRegistrationKey().equals(key)) {
JDOStorage.getInstance().delete(device);
System.out.println("OK");
return;
} else {
System.out.println("NO!");
}
}
}
private void registerAppleDevice(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
response.setStatus(403);
}
private void registerAndroidDevice(HttpServletRequest request, HttpServletResponse response) throws JSONException, IOException {
Date n = new Date();
boolean isadmin = (request.getParameter("isadmin") != null);
RegisteredDeviceType device = new RegisteredDeviceType(
UUID.randomUUID().toString(),
request.getParameter("deviceType") + ": " + request.getParameter("deviceDet"),
request.getParameter("devicekey"), n.getTime());
if (isadmin) {
System.out.println("ADMIN");
device.setIsadmin(isadmin);
}
JDOStorage.getInstance().write(device);
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
response.setStatus(200);
writer.write("Registered");
}
/**
* Handle HTTP GET method / Json
*
* @return a JSON Zone Representation
*/
@Get
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
PrintWriter writer = response.getWriter();
if (request.getPathInfo().equals("/test")) {
GCMPushingServer GCM = GCMPushingServer.getInstance();
try {
GCM.sendNotification("notification test ");
writer.write("Sent a notification test to the following devices:");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (request.getPathInfo().equals("/alarm")) {
GCMPushingServer GCM = GCMPushingServer.getInstance();
try {
GCM.sendNotification("ALARM for sensor: " + request.getParameter("sensor"));
writer.write("OK");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
writer.write("NO");
}
return;
}
List<RegisteredDeviceType> devices = JDOStorage.getInstance().getAll(RegisteredDeviceType.class);
if (devices.isEmpty()) {
// No devices registered
response.setStatus(200);
writer.write("The list of devices is empty.");
return;
}
response.setContentType("text/html");
JSONArray jsonDevices = new JSONArray();
Iterator<RegisteredDeviceType> iter = devices.iterator();
writer.write("<table border=1><tr style=\"font-weight: bold;\"><th>Device Logo</th><th>Device Type</th><th>registered on</th><th>Registration Key</th></tr>");
// Get the list of devices and build the json
try {
while (iter.hasNext()) {
RegisteredDeviceType current = iter.next();
JSONObject obj = new JSONObject(current);
jsonDevices.put(obj);
String deviceLogo = obj.getString("deviceType").split(":")[0].toLowerCase();
writer.write("<tr><td><img src=\"/images/" + deviceLogo + ".jpg\"/></td><td>"
+ obj.getString("deviceType") + "</td><td>"
+ obj.getString("time") + "</td><td>"
+ obj.getString("deviceRegistrationKey") + "</td></tr>");
}
writer.write("</table>");
} catch (Exception e) {
response.setStatus(500);
log.log(Level.WARNING, "", e);
}
}
}
package fr.eurecom.pushing;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.MulticastResult;
import com.google.android.gcm.server.Sender;
import fr.eurecom.senml.entity.RegisteredDeviceType;
import fr.eurecom.senml.entity.SensorAdmin;
import fr.eurecom.senml.persistence.JDOStorage;
public class GCMPushingServer implements IPushingServer {
private List<String> REG_IDS, ADMIN_REG_IDS;
// HARD-CODED, move on a different configuration file!
private String API_KEY = "AIzaSyDO3RS9FZPTp0d2BNn-0zKUuiiOI45-ED8";
private boolean initialized = false;
private static final GCMPushingServer instance = new GCMPushingServer();
protected GCMPushingServer() {
}
public static GCMPushingServer getInstance() {
if (!instance.isInitialized()) {
instance.initialize();
}
return instance;
}
public boolean isInitialized() {
return initialized;
}
public void initialize() {
REG_IDS = ADMIN_REG_IDS = new ArrayList<String>();
initialized = true;
}
public void refreshDevicesList() {
REG_IDS = new ArrayList<String>();
List<RegisteredDeviceType> list = JDOStorage.getInstance().getAll(
RegisteredDeviceType.class);
Iterator<RegisteredDeviceType> iter = list.iterator();
while (iter.hasNext()) {
RegisteredDeviceType current = iter.next();
if (isAndroidDevice(current)) {
if (current.isIsadmin()) {
if (ADMIN_REG_IDS.indexOf(current) == -1) {
System.out.println("Refreshing the list of admins...adding "
+ current.getDeviceRegistrationKey());
ADMIN_REG_IDS.add(current.getDeviceRegistrationKey());
}
} else {
if (REG_IDS.indexOf(current) == -1) {
System.out.println("Refreshing the list...adding "
+ current.getDeviceRegistrationKey());
REG_IDS.add(current.getDeviceRegistrationKey());
}
}
}
}
}
/**
* Return true if the device is of type Android
*
* @param device
* @return
*/
private boolean isAndroidDevice(RegisteredDeviceType device) {
System.out.print("isAndroidDevice? ");
if (device.getDeviceType().toLowerCase().indexOf("android") > -1) {
System.out.println("Yes");
} else {
System.out.println("No");
}
return (device.getDeviceType().toLowerCase().indexOf("android") > -1);
}
@Override
public void sendNotification(String data) throws JSONException {
// Refresh the devices list and put it on the JSON object
refreshDevicesList();
System.out.println("Calling gcmnotify with data = " + data);
GCMnotify(data);
// TODO: Check that is part of monitored sensors
GCMnotify(data, true);
}
private void GCMnotify(String data) {
System.out.println("Sending data: " + data);
// Instance of com.android.gcm.server.Sender, that does the
// transmission of a Message to the Google Cloud Messaging service.
Sender sender = new Sender(API_KEY);
// This Message object will h old the data that is being transmitted
// to the Android client devices. For this demo, it is a simple text
// string, but could certainly be a JSON object.
Message message = new Message.Builder()
// If multiple messages are sent using the same .collapseKey()
// the android target device, if it was offline during earlier
// message
// transmissions, will only receive the latest message for that
// key when
// it goes back on-line.
// .collapseKey(collapseKey)
.timeToLive(30).delayWhileIdle(true)
.addData("type", "zoneupdated")
.addData("notificationmessage", "WL-BOX, zone updated: " + data.replace("ZoneUpdated", ""))
.addData("alertstatus", "info")
.addData("message", data).build();
if (REG_IDS.size() == 0) {
// Noone to notify
return;
}
try {
// use this for multicast messages. The second parameter
// of sender.send() will need to be an array of register ids.
MulticastResult result = sender.send(message, REG_IDS, 1);
if (result.getResults() != null) {
int canonicalRegId = result.getCanonicalIds();
if (canonicalRegId != 0) {
}
} else {
int error = result.getFailure();
System.out.println("Broadcast failure: " + error);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// TODO: Only one constructor, most of the code is the same...
private void GCMnotify(String data, boolean isadmin) {
System.out.println("ADMIN! Sending data: " + data);
// Instance of com.android.gcm.server.Sender, that does the
// transmission of a Message to the Google Cloud Messaging service.
Sender sender = new Sender(API_KEY);
// This Message object will h old the data that is being transmitted
// to the Android client devices. For this demo, it is a simple text
// string, but could certainly be a JSON object.
Message message = new Message.Builder()
// If multiple messages are sent using the same .collapseKey()
// the android target device, if it was offline during earlier
// message
// transmissions, will only receive the latest message for that
// key when
// it goes back on-line.
// .collapseKey(collapseKey)
.timeToLive(30).delayWhileIdle(true)
.addData("type", "alert")
.addData("notificationmessage", "WL-BOX admin, SENSOR ALERT!")
.addData("alertstatus", "alert")
.addData("message", data).build();
if (ADMIN_REG_IDS.size() == 0) {
// Noone to notify
System.out.println("Admin IDS empty");
return;
}
try {
// use this for multicast messages. The second parameter
// of sender.send() will need to be an array of register ids.
MulticastResult result = sender.send(message, ADMIN_REG_IDS, 1);
if (result.getResults() != null) {
System.out.println("!) null");
int canonicalRegId = result.getCanonicalIds();
if (canonicalRegId != 0) {
}
} else {
int error = result.getFailure();
System.out.println("Broadcast failure: " + error);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package fr.eurecom.pushing;
import org.json.JSONException;
public interface IPushingServer {
public void sendNotification(String data) throws JSONException;
}
package fr.eurecom.restlet.resources.common;
/**
* This class implements the Contact entity.
*
*/
public class Contact {
// private static final long serialVersionUID = 7390103290165670089L;
/**
* Contact's identifier.
*/
private final int id;
/**
* Contact's first name
*/
private String firstName = "N/A";
/**
* Contact's last name
*/
private String lastName = "N/A";
/**
* Contact's email address
*/
private String mail = "N/A";
/**
* Contact's phone number
*/
private String phone = "N/A";
/**
* Constructs a contact with the specified id.
*
* @param id
*/
public Contact(int id) {
this.id = id;
}
/**
* Constructs a contact with the specified values.
*
* @param id
* Contact's identifier.
* @param firstname
* Contact's first name.
* @param lastname
* Contact's last name.
* @param mail
* Contact's email address.
* @param phone
* Contact's Phone number.
*/
public Contact(int id, String firstname, String lastname, String mail,
String phone) {
this.id = id;
this.firstName = firstname;
this.lastName = lastname;
this.mail = mail;
this.phone = phone;
}
/**
*
* @return the contact id.
*/
public int getId() {
return id;
}
/**
* Set an email address
*
* @param mail
* email address.
*/
public void setMail(String mail) {
this.mail = mail;
}
/**
* Retrieve the email address.
*
* @return the mail address if set, "N/A" otherwise.
*/
public String getMail() {
return mail;
}
/**
* Retrieve the first name.
*
* @return the first name if set, "N/A" otherwise.
*/
public String getFirstName() {
return firstName;
}
/**
* Set a first name.
*
* @param firstname
* to set.
*/
public void setFirstName(String firstname) {
this.firstName = firstname;
}
/**
* Retrieve the last name.
*
* @return the last name set, "N/A" otherwise.
*/
public String getLastName() {
return lastName;
}
/**
* Set a last name.
*
* @param lastname
* to set, "N/A" otherwise.
*/
public void setLastName(String lastname) {
this.lastName = lastname;
}
/**
* Retrieve the phone number.
*
* @return the phone set, "N/A" otherwise.
*/
public String getPhone() {
return phone;
}
/**
* Set the phone number.
*
* @param phone
* number to set.
*/
public void setPhone(String phone) {
this.phone = phone;
}
}
package fr.eurecom.restlet.resources.common;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.restlet.data.Status;
import org.restlet.ext.wadl.WadlServerResource;
import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import fr.eurecom.restlet.resources.persistence.Storage;
public abstract class ContactBaseResource extends WadlServerResource {
private final static String CONTACTS_UPDATED = "contactsupdated";
private static final Logger log = Logger.getLogger("ContactBaseResource");
public enum MEMBERS {
CONTACT, ID, FIRSTNAME, LASTNAME, PHONE, MAIL
}
protected static final String CONTACTS = "contacts";
protected static final String CONTACT = "contact";
protected static final String ID = "id";
protected static final String FIRST_NAME = "firstname";
protected static final String LAST_NAME = "lastname";
protected static final String PHONE = "phone";
protected static final String MAIL = "mail";
protected static final String URL = "url";
protected static final String REQUEST_QUERY_SORT = "sort";
// HACK, this class is also used to support subscribers for senml objects. Move the subscribers channel API Stuffs to a common place.
protected static final Map<String, Token> subscribers = new HashMap<String, Token>();
final static class Token {
public String token;
public long timestamp;
public Token(String token) {
this.token = token;
this.timestamp = System.currentTimeMillis();
}
}
/**
* Retrieve the contacts from the persistence layer.
*
* @return the contact's list.
*/
protected List<Contact> getContacts() {
// Dispatcher d = (Dispatcher)getApplication();
return Storage.getInstance().getContacts(MEMBERS.ID, 0, 999);
}
/**
* Retrieve the contacts sorted by Last name from the persistence layer.
*
* @return a sorted contacts list.
*/
@SuppressWarnings("unchecked")
protected List<Contact> getSortedContacts(String sort) {
try {
MEMBERS mSorted = sort == null || sort.trim().isEmpty() ? MEMBERS.LASTNAME
: MEMBERS.valueOf(sort.trim().toUpperCase());
return Storage.getInstance().getContacts(mSorted, 0, 999);
} catch (Exception e) {
e.printStackTrace();
return Collections.EMPTY_LIST;
}
}
/**
* Retrieve the contact which matches this id from the persistence layer.
*
* @param id
* @return contact or null.
*/
protected Contact getContact(int id) {
return Storage.getInstance().getContact(id);
}
/**
* Delete a contact from the persistence layer.
*
* @param id
* @return Status.SUCCESS_NO_CONTENT if deleted,
* Status.CLIENT_ERROR_NOT_FOUND otherwise.
*/
protected Status removeContact(int id) {
if (Storage.getInstance().removeContact(id)) {
sendUpdateSubscribers(CONTACTS_UPDATED);
return Status.SUCCESS_NO_CONTENT;
}
else {
return Status.CLIENT_ERROR_NOT_FOUND;
}
}
/**
* Save a new contact to the persistence layer. (restricted to 50 contacts
* currently).
*
* @param contact
* @return Status.SUCCESS_CREATED if saved,
* Status.SERVER_ERROR_INSUFFICIENT_STORAGE otherwise.
*/
protected Status addContact(Contact contact) {
if (Storage.getInstance().saveContact(contact)) {
sendUpdateSubscribers(CONTACTS_UPDATED);
return Status.SUCCESS_CREATED;
}
return Status.SERVER_ERROR_INSUFFICIENT_STORAGE;
}
/**
* Update a contact to the persistence layer.
*
* @param contact
* @return Status.SUCCESS_OK if updated, Status.CLIENT_ERROR_NOT_FOUND
* otherwise.
*/
protected Status updateContact(Contact contact) {
if (Storage.getInstance().saveContact(contact)) {
sendUpdateSubscribers(CONTACTS_UPDATED);
return Status.SUCCESS_NO_CONTENT;
} else {
return Status.CLIENT_ERROR_NOT_FOUND;
}
}
public static void addSubscriber(String clientID, String token) {
subscribers.put(clientID, new Token(token));
}
public static void deleteSubscriber(String clientID) {
subscribers.remove(clientID);
}
public static String getToken(String clientID) {
Token t = subscribers.get(clientID);
if (t != null && (System.currentTimeMillis() - t.timestamp) < 7200000) {
return t.token;
}
else {
return null;
}
}
public static void sendUpdateSubscribers(String message) {
ChannelService service = ChannelServiceFactory.getChannelService();
try {
service.sendMessage(new ChannelMessage("I235", message));
log.info("Notified I235" + message);
}
catch (Exception e) {
log.log(Level.WARNING, "Error Notifying client I235", e);
System.out.println("Error notifying client I235");
}
/* for (String clientID : subscribers.keySet()) {
System.out.println("Will notify " + clientID + " " + message);
service.sendMessage(new ChannelMessage(clientID, message));
}
*/
}
}
package fr.eurecom.restlet.resources.common;
import java.util.Map;
import org.json.JSONObject;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.ext.wadl.MethodInfo;
import org.restlet.ext.wadl.ParameterInfo;
import org.restlet.ext.wadl.ParameterStyle;
import org.restlet.ext.wadl.RepresentationInfo;
import org.restlet.ext.wadl.RequestInfo;
import org.restlet.ext.wadl.ResponseInfo;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ResourceException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
*
* This class represents the Contact resource
*/
public class ContactResource extends ContactBaseResource {
/**
* underlying contact.
*/
private Contact contact = null;
/**
* Resource contact identifier.
*/
private int identifier = -1;
@Override
public Representation describe() {
System.out.println("describe");
setName("Contact Resource");
setDescription("Manage the current Contact");
return super.describe();
}
@Override
public void describeDelete(MethodInfo mInfo) {
mInfo.setIdentifier("ContactDelete");
mInfo.setDocumentation("To Delete the current contact.");
ResponseInfo response = new ResponseInfo(
"Contact deleted. No contents returned.");
response.getStatuses().add(Status.SUCCESS_NO_CONTENT);
mInfo.getResponses().add(response);
response = new ResponseInfo("Contact not found");
response.getStatuses().add(Status.CLIENT_ERROR_NOT_FOUND);
mInfo.getResponses().add(response);
}
@Override
public void describeGet(MethodInfo mInfo) {
mInfo.setIdentifier("ContactDetail");
mInfo.setDocumentation("To retrieve details of the current contact.");
ResponseInfo response = new ResponseInfo("Current contact details");
response.getStatuses().add(Status.SUCCESS_OK);
RepresentationInfo repInfo = new RepresentationInfo(
MediaType.APPLICATION_XML);
repInfo.setXmlElement(CONTACT);
repInfo.setDocumentation("XML representation of the current contact.");
response.getRepresentations().add(repInfo);
repInfo = new RepresentationInfo(MediaType.APPLICATION_JSON);
repInfo.setDocumentation("JSON representation of the current contact");
repInfo.setXmlElement(CONTACT);
response.getRepresentations().add(repInfo);
mInfo.getResponses().add(response);
response = new ResponseInfo("Contact not found");
repInfo = new RepresentationInfo(MediaType.TEXT_HTML);
repInfo.setIdentifier("contactError");
response.getStatuses().add(Status.CLIENT_ERROR_NOT_FOUND);
response.getRepresentations().add(repInfo);
mInfo.getResponses().add(response);
}
@Override
public void describePut(MethodInfo mInfo) {
mInfo.setIdentifier("ContactUpdate");
mInfo.setDocumentation("To update details of the current contact");
RequestInfo requestInfo = new RequestInfo();
// requestInfo.getParameters().add(
// new ParameterInfo(ID, true, "Number", ParameterStyle.QUERY,
// "Contact identifier"));
requestInfo.getParameters().add(
new ParameterInfo(FIRST_NAME, false, "String",
ParameterStyle.QUERY, "Contact First Name"));
requestInfo.getParameters().add(
new ParameterInfo(LAST_NAME, false, "String",
ParameterStyle.QUERY, "Contact Last Name"));
requestInfo.getParameters().add(
new ParameterInfo(PHONE, false, "String", ParameterStyle.QUERY,
"Contact Phone number"));
requestInfo.getParameters().add(
new ParameterInfo(MAIL, false, "String", ParameterStyle.QUERY,
"Contact email address"));
mInfo.setRequest(requestInfo);
ResponseInfo responseInfo = new ResponseInfo("Contact not Found");
responseInfo.getStatuses().add(Status.CLIENT_ERROR_NOT_FOUND);
mInfo.getResponses().add(responseInfo);
responseInfo = new ResponseInfo(
"Contact updated, no contents returned.");
responseInfo.getStatuses().add(Status.SUCCESS_NO_CONTENT);
mInfo.getResponses().add(responseInfo);
}
@Override
protected void doInit() throws ResourceException {
String sIdentifier = (String) getRequest().getAttributes().get("id");
if (sIdentifier != null) {
identifier = Integer.parseInt(sIdentifier);
contact = getContact(identifier);
}
setExisting(contact != null);
}
/**
* Handle HTTP GET method / xml
*
* @return a xml resource representation
*/
@Get("xml")
public Representation toXML() {
try {
DomRepresentation representation = new DomRepresentation(
MediaType.TEXT_XML);
Document d = representation.getDocument();
Element eltItem = d.createElement(CONTACT);
d.appendChild(eltItem);
Element el = d.createElement(ID);
el.appendChild(d.createTextNode("" + contact.getId()));
eltItem.appendChild(el);
el = d.createElement(FIRST_NAME);
el.appendChild(d.createTextNode(contact.getFirstName()));
eltItem.appendChild(el);
el = d.createElement(LAST_NAME);
el.appendChild(d.createTextNode(contact.getLastName()));
eltItem.appendChild(el);
el = d.createElement(PHONE);
el.appendChild(d.createTextNode(contact.getPhone()));
eltItem.appendChild(el);
el = d.createElement(MAIL);
el.appendChild(d.createTextNode(contact.getMail()));
eltItem.appendChild(el);
d.normalizeDocument();
return representation;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* Handle HTTP GET method / json
*
* @return a json resource representation.
*/
@Get("json")
public Representation toJSON() {
JSONObject jObject = new JSONObject();
try {
jObject.put(ID, "" + contact.getId());
jObject.put(FIRST_NAME, contact.getFirstName());
jObject.put(LAST_NAME, contact.getLastName());
jObject.put(PHONE, contact.getPhone());
jObject.put(MAIL, contact.getMail());
return new JsonRepresentation(jObject);
} catch (Exception e) {
return null;
}
}
/**
* Handle HTTP PUT method. Update an existing contact.
*
* @param entity
*/
@Put
public void updateContact(Representation entity) {
Map<String, String> map = new Form(entity).getValuesMap();
for (String k : map.keySet()) {
MEMBERS m = MEMBERS.valueOf(k.toUpperCase());
switch (m) {
case FIRSTNAME:
contact.setFirstName(map.get(k));
break;
case LASTNAME:
contact.setLastName(map.get(k));
break;
case PHONE:
contact.setPhone(map.get(k));
break;
case MAIL:
contact.setMail(map.get(k));
break;
}
}
Status updateStatus = super.updateContact(contact);
setStatus(updateStatus);
}
/**
* Handle HTTP DELETE method. Delete an existing contact.
*/
@Delete
public void deleteContact() {
Status deleteStatus = super.removeContact(identifier);
setStatus(deleteStatus);
}
}
package fr.eurecom.restlet.resources.common;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONObject;
import org.restlet.data.Form;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.ext.wadl.MethodInfo;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import com.google.appengine.api.channel.ChannelServiceFactory;
public class ContactsSubResource extends ContactBaseResource {
private static final Logger log = Logger.getLogger("ContactSubResource");
/**
* {@inheritDoc}
*/
@Override
public void describePost(MethodInfo mInfo) {
}
/**
* Handle HTTP GET Subscribe method
*
* @param entity - must contains the client id channel.
* @return a JSON Representation of the token.
*/
@Get
public Representation subscribe(Representation entity) {
Reference ref = getRequest().getResourceRef();
Form formQuery = ref.getQueryAsForm();
JSONObject json = new JSONObject();
try {
String clientID = formQuery.getFirstValue("clientID", "").trim();
if (clientID.isEmpty()) {
log.warning("client ID not found");
setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED,
"Must provide a clientID field.");
return null;
}
String token = getToken(clientID);
if (token == null) {
token = ChannelServiceFactory.getChannelService().createChannel(clientID);
log.info("Create a new token [" + token + "]");
addSubscriber(clientID, token);
}
else {
log.info("Token already existing for client " + clientID);
}
json.append("token", token);
return new JsonRepresentation(json);
} catch (Exception e) {
e.printStackTrace();
setStatus(Status.SERVER_ERROR_INTERNAL);
return null;
}
}
}
\ No newline at end of file
package fr.eurecom.restlet.resources.common;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.ext.wadl.MethodInfo;
import org.restlet.ext.wadl.RepresentationInfo;
import org.restlet.ext.wadl.ResponseInfo;
import org.restlet.ext.wadl.WadlServerResource;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Delete;
import org.restlet.resource.Put;
import org.restlet.resource.Post;
import fr.eurecom.senml.entity.*;
import fr.eurecom.senml.persistence.JDOStorage;
public class SensorInResource extends WadlServerResource {
private static final Logger log = Logger.getLogger("SensorInResource");
private static final MediaType SENML_JSON = MediaType.valueOf("application/senml+json");
/**
* {@inheritDoc}
*/
@Override
public Representation describe() {
setName("Sensor-In Resource");
setDescription("Acquisition sensors receiver");
return super.describe();
}
@Post
@Put
public void updateSensor(Representation entity) {
JSONObject jObject = null;
String text = null;
try {
MediaType mt = entity.getMediaType();
text = entity.getText();
if (mt.isCompatible(MediaType.APPLICATION_JSON) || mt.isCompatible(SENML_JSON) ) {
jObject = new JSONObject(text);
}
else {
setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE, "Content is not JSON compatible");
log.info("This entry cause a 406 response / reason not json");
return;
}
String bn = jObject.has(ISensor.BASE_NAME) ? jObject.getString(ISensor.BASE_NAME) : null;
long bt = jObject.has(ISensor.BASE_TIME) ? jObject.getLong(ISensor.BASE_TIME) : Long.MIN_VALUE;
String bu = jObject.has(ISensor.BASE_UNIT) ? jObject.getString(ISensor.BASE_UNIT) : null;
if (! jObject.has(ISensor.MEASUREMENT) || bn == null) {
setStatus(Status.CLIENT_ERROR_BAD_REQUEST, "Parameter Base Name[bn] or measure[e] missing.");
log.info("This entry cause a 400 response. reason bn - e missing ||" + text + "||");
return;
}
JSONArray measures = jObject.getJSONArray(ISensor.MEASUREMENT);
// TODO: Remove this hack: zone key is static. For first tests with IQSIM and demo.
IZone zone = JDOStorage.getInstance().getById("Aix", ZoneAdmin.class);
SensorAdmin sa = new SensorAdmin(bn, "Generic Sensor", zone);
boolean parsingOK = true;
String reasonPhrase = null;
for (int i = 0; i < measures.length(); i++) {
JSONObject m = measures.getJSONObject(i);
Measure.ParamTypeValue tv = getUnitType(m);
if (tv == null) {
parsingOK = false;
reasonPhrase = "Measure miss type value [v|sv|bv]";
break;
}
Units unit = getUnit(m, bu);
if (unit == null) {
parsingOK = false;
reasonPhrase = "parameter unit [bu|u] invalid (missing or unknown)";
break;
}
String name = m.has(Measure.PARAM_NAME) ? m.getString(Measure.PARAM_NAME) : null;
if ( name == null || name.trim().isEmpty()) {
parsingOK = false;
reasonPhrase = "parameter name [n] missing or empty. Not supported";
break;
}
String type = m.has(Measure.PARAM_TYPE) ? m.getString(Measure.PARAM_TYPE) : null;
if ( type == null || type.trim().isEmpty()) {
parsingOK = false;
reasonPhrase = "parameter type [n] missing or empty. Not supported";
break;
}
sa.addMeasure(name.trim(), unit, tv,
m.getString(tv.getSenML()),
m.has(Measure.PARAM_TIME) ? m.getLong(Measure.PARAM_TIME) :
bt == Long.MIN_VALUE ? 0 : bt, type.trim());
}
if (!parsingOK) {
setStatus(Status.CLIENT_ERROR_BAD_REQUEST, reasonPhrase);
log.info("This entry cause a 400 response / reason " + reasonPhrase + " ||" + text + "||");
}
else {
setStatus(Status.SUCCESS_CREATED);
}
}
catch (Exception e) {
log.warning("Unexpected error receiving this entry : ||" + text + "||");
log.log(Level.WARNING, "Unexpected Error", e);
setStatus(Status.SERVER_ERROR_INTERNAL, "Unexpected Error");
}
return;
}
private static Measure.ParamTypeValue getUnitType(JSONObject m) {
if (m.has(Measure.PARAM_VALUE)) {
return Measure.ParamTypeValue.FLOAT;
}
else if (m.has(Measure.PARAM_BOOLEAN_VALUE)) {
return Measure.ParamTypeValue.BOOL;
}
else if (m.has(Measure.PARAM_STRING_VALUE)) {
return Measure.ParamTypeValue.STRING;
}
else if (m.has(Measure.PARAM_SUM_VALUE)) {
return Measure.ParamTypeValue.SUM;
}
return null;
}
private static Units getUnit(JSONObject m, String bu) throws JSONException {
String u = m.has(Measure.PARAM_UNIT) ? m.getString(Measure.PARAM_UNIT) : bu != null ? bu : null;
return u == null ? null : Units.getUnit(u);
}
}
\ No newline at end of file
package fr.eurecom.restlet.resources.common;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.data.MediaType;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.data.Tag;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.ext.wadl.MethodInfo;
import org.restlet.ext.wadl.RepresentationInfo;
import org.restlet.ext.wadl.ResponseInfo;
import org.restlet.ext.wadl.WadlServerResource;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import fr.eurecom.restlet.resources.common.ZonesResource.ZoneFormat;
import fr.eurecom.senml.entity.ISensor;
import fr.eurecom.senml.entity.IZone;
import fr.eurecom.senml.entity.PostValAdmin;
import fr.eurecom.senml.entity.ZoneAdmin;
import fr.eurecom.senml.persistence.JDOStorage;
public class SensorPost extends WadlServerResource {
private static final Logger log = Logger.getLogger("SensorPost");
/**
* {@inheritDoc}
*/
@Override
public Representation describe() {
setName("Sensor Post");
setDescription("Post Something, is a test!");
return super.describe();
}
@Post("json")
public Representation doPost(String entity) {
String senmlValue = (String) getRequest().getAttributes().get("senml");
//IZone zone = JDOStorage.getInstance().getById(id, ZoneAdmin.class);
JSONObject jzone = new JSONObject();
try {
jzone.put("senml", senmlValue);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date n = new Date();
PostValAdmin sa = new PostValAdmin();
sa.addPost("testing post " + String.valueOf(n),
entity,
"NOW".equals(n) ? 0 : System.currentTimeMillis());
Representation out = new JsonRepresentation(jzone);
return out;
}
/**
* {@inheritDoc}
*/
@Override
public void describeGet(MethodInfo mInfo) {
mInfo.setIdentifier("Sensor Posting");
mInfo.setDocumentation("To Retrieve a zone description");
ResponseInfo response = new ResponseInfo("Current list of zones");
response.getStatuses().add(Status.SUCCESS_OK);
RepresentationInfo repInfo = new RepresentationInfo(
MediaType.APPLICATION_JSON);
repInfo.setDocumentation("JSON array of of the current list zone");
response.getRepresentations().add(repInfo);
mInfo.getResponses().add(response);
}
/**
* Handle HTTP GET method / Json
*
* @return a JSON Zone Representation
*/
@Get("json")
public Representation toJSON() {
final String baseURL = buildBaseURL(getRequest().getResourceRef());
String id = (String) getRequest().getAttributes().get("id");
IZone zone = JDOStorage.getInstance().getById(id, ZoneAdmin.class);
JSONObject jzone = new JSONObject();
try {
jzone.put(ZoneFormat.NAME.getRestAtr(), zone.getName());
JSONArray subZones = new JSONArray();
for (IZone sub : zone.getSubZones()) {
JSONObject z = new JSONObject();
z.put(ZoneFormat.URI.getRestAtr(),
String.format("%s/%s", baseURL, sub.getKey()));
z.put(ZoneFormat.NAME.getRestAtr(), sub.getName());
z.put(ZoneFormat.HAS_SENSORS.getRestAtr(), sub.getSensors()
.size() > 0);
z.put(ZoneFormat.HAS_SUBZONES.getRestAtr(), sub.getSubZones()
.size() > 0);
subZones.put(z);
}
if (subZones.length() > 0) {
jzone.put(ZoneFormat.SUBZONE.getRestAtr(), subZones);
}
JSONArray sensors = new JSONArray();
for (ISensor sensor : zone.getSensors()) {
sensors.put(sensor.toJSONSenML());
}
if (sensors.length() > 0) {
jzone.put(ZoneFormat.SENSORS_SENML.getRestAtr(), sensors);
}
/* String md5 = md5(jzone.toString());
Tag tag = md5 == null ? null : new Tag(md5, false);
if (!isModified(tag)) {
setStatus(Status.REDIRECTION_NOT_MODIFIED);
return null;
}
*/
Representation json = new JsonRepresentation(jzone);
// json.setTag(tag);
return json;
} catch (Exception e) {
setStatus(Status.SERVER_ERROR_INTERNAL);
log.log(Level.WARNING, "", e);
return null;
}
}
/**
* HTTP GET method / xml accept
*
* @return a XML Zone Representation
*/
@Get("xml")
public Representation toXML() {
long before = System.currentTimeMillis();
String id = (String) getRequest().getAttributes().get("id");
IZone zone = JDOStorage.getInstance().getById(id, ZoneAdmin.class);
final String baseURL = buildBaseURL(getRequest().getResourceRef());
try {
DomRepresentation xml = new DomRepresentation(MediaType.TEXT_XML);
Document d = xml.getDocument();
Element currentZone = d.createElement(ZoneFormat.ZONE.getRestAtr());
currentZone.setAttribute(ZoneFormat.NAME.getRestAtr(),
zone.getName());
d.appendChild(currentZone);
for (IZone subzone : zone.getSubZones()) {
Element sub = d.createElement(ZoneFormat.SUBZONE.getRestAtr());
sub.setAttribute(ZoneFormat.NAME.getRestAtr(),
subzone.getName());
sub.setAttribute(ZoneFormat.URI.getRestAtr(),
String.format("%s/%s", baseURL, subzone.getKey()));
sub.setAttribute(ZoneFormat.HAS_SENSORS.getRestAtr(), ""
+ (subzone.getSensors().size() > 0));
sub.setAttribute(ZoneFormat.HAS_SUBZONES.getRestAtr(), ""
+ (subzone.getSubZones().size() > 0));
currentZone.appendChild(sub);
}
for (ISensor sensor : zone.getSensors()) {
Node senml = sensor.toXMLSenML(d);
currentZone.appendChild(senml);
}
/* String md5 = md5(xml.getText());
Tag tag = md5 == null ? null : new Tag(md5, false);
if (!isModified(tag)) {
setStatus(Status.REDIRECTION_NOT_MODIFIED);
return null;
}
*/
// xml.setTag(tag);
return xml;
} catch (Exception e) {
log.log(Level.WARNING, "", e);
setStatus(Status.SERVER_ERROR_INTERNAL);
return null;
}
}
/*
* Alternative method to add an header to the response. Series<Header>
* responseHeaders = (Series<Header>)
* getResponse().getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS); if
* (responseHeaders == null) { responseHeaders = new
* Series<Header>(Header.class); getResponse().getAttributes().put(
* HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders);
*
* } responseHeaders.add(new Header("ETag", md5));
*/
/**
* Build a String baseURL = to current URI/Reference less the last path
* segment.
*
* @param reference
* the current reference object
* @return String base URI.
*/
private String buildBaseURL(Reference reference) {
Reference ref = new Reference(reference);
List<String> segments = ref.getSegments();
if (segments.size() > 0) {
segments.remove(segments.size() - 1);
ref.setSegments(segments);
}
return ref.getHierarchicalPart();
}
private String md5(String value) {
try {
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(value.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1, digest);
String hashtext = bigInt.toString(16);
// Now we need to zero pad it if you actually want the full 32
// chars.
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private boolean isModified(Tag currentTag) {
boolean modified = true;
if (currentTag == null) {
return modified;
}
if (getRequest().getConditions() != null
&& getRequest().getConditions().getNoneMatch() != null) {
for (Tag noMatch : getRequest().getConditions().getNoneMatch()) {
if (currentTag.equals(noMatch)) {
modified = false;
break;
}
}
}
return modified;
}
}
package fr.eurecom.restlet.resources.common;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.restlet.data.MediaType;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.data.Tag;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.ext.wadl.MethodInfo;
import org.restlet.ext.wadl.RepresentationInfo;
import org.restlet.ext.wadl.ResponseInfo;
import org.restlet.ext.wadl.WadlServerResource;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import fr.eurecom.restlet.resources.common.ZonesResource.ZoneFormat;
import fr.eurecom.senml.entity.ISensor;
import fr.eurecom.senml.entity.IZone;
import fr.eurecom.senml.entity.ZoneAdmin;
import fr.eurecom.senml.persistence.JDOStorage;
public class ZoneResource extends WadlServerResource {
// Map<String, Tag> etags = new HashMap<String, Tag>();
private static final Logger log = Logger.getLogger("ZoneResource");
/**
* {@inheritDoc}
*/
@Override
public Representation describe() {
setName("Zone Resource");
setDescription("Retrieve a zone description");
return super.describe();
}
/**
* {@inheritDoc}
*/
@Override
public void describeGet(MethodInfo mInfo) {
mInfo.setIdentifier("Zone description");
mInfo.setDocumentation("To Retrieve a zone description");
ResponseInfo response = new ResponseInfo("Current list of zones");
response.getStatuses().add(Status.SUCCESS_OK);
RepresentationInfo repInfo = new RepresentationInfo(
MediaType.APPLICATION_JSON);
repInfo.setDocumentation("JSON array of of the current list zone");
response.getRepresentations().add(repInfo);
mInfo.getResponses().add(response);
}
/**
* Handle HTTP GET method / Json
*
* @return a JSON Zone Representation
*/
@Get("json")
public Representation toJSON() {
final String baseURL = buildBaseURL(getRequest().getResourceRef());
String id = (String) getRequest().getAttributes().get("id");
IZone zone = JDOStorage.getInstance().getById(id, ZoneAdmin.class);
JSONObject jzone = new JSONObject();
try {
jzone.put(ZoneFormat.NAME.getRestAtr(), zone.getName());
JSONArray subZones = new JSONArray();
for (IZone sub : zone.getSubZones()) {
JSONObject z = new JSONObject();
z.put(ZoneFormat.URI.getRestAtr(),
String.format("%s/%s", baseURL, sub.getKey()));
z.put(ZoneFormat.NAME.getRestAtr(), sub.getName());
z.put(ZoneFormat.ZONE_TYPE.getRestAtr(), sub.getType());
z.put(ZoneFormat.HAS_SENSORS.getRestAtr(), sub.getSensors()
.size() > 0);
z.put(ZoneFormat.HAS_SUBZONES.getRestAtr(), sub.getSubZones()
.size() > 0);
subZones.put(z);
}
if (subZones.length() > 0) {
jzone.put(ZoneFormat.SUBZONE.getRestAtr(), subZones);
}
JSONArray sensors = new JSONArray();
for (ISensor sensor : zone.getSensors()) {
sensors.put(sensor.toJSONSenML());
}
if (sensors.length() > 0) {
jzone.put(ZoneFormat.SENSORS_SENML.getRestAtr(), sensors);
}
/* String md5 = md5(jzone.toString());
Tag tag = md5 == null ? null : new Tag(md5, false);
if (!isModified(tag)) {
setStatus(Status.REDIRECTION_NOT_MODIFIED);
return null;
}
*/
Representation json = new JsonRepresentation(jzone);
// json.setTag(tag);
return json;
} catch (Exception e) {
setStatus(Status.SERVER_ERROR_INTERNAL);
log.log(Level.WARNING, "", e);
return null;
}
}
/**
* HTTP GET method / xml accept
*
* @return a XML Zone Representation
*/
@Get("xml")
public Representation toXML() {
long before = System.currentTimeMillis();
String id = (String) getRequest().getAttributes().get("id");
IZone zone = JDOStorage.getInstance().getById(id, ZoneAdmin.class);
final String baseURL = buildBaseURL(getRequest().getResourceRef());
try {
DomRepresentation xml = new DomRepresentation(MediaType.TEXT_XML);
Document d = xml.getDocument();
Element currentZone = d.createElement(ZoneFormat.ZONE.getRestAtr());
currentZone.setAttribute(ZoneFormat.NAME.getRestAtr(),
zone.getName());
currentZone.setAttribute(ZoneFormat.ZONE_TYPE.getRestAtr(), zone.getType());
d.appendChild(currentZone);
for (IZone subzone : zone.getSubZones()) {
Element sub = d.createElement(ZoneFormat.SUBZONE.getRestAtr());
sub.setAttribute(ZoneFormat.NAME.getRestAtr(),
subzone.getName());
sub.setAttribute(ZoneFormat.URI.getRestAtr(),
String.format("%s/%s", baseURL, subzone.getKey()));
sub.setAttribute(ZoneFormat.HAS_SENSORS.getRestAtr(), ""
+ (subzone.getSensors().size() > 0));
sub.setAttribute(ZoneFormat.HAS_SUBZONES.getRestAtr(), ""
+ (subzone.getSubZones().size() > 0));
currentZone.appendChild(sub);
}
for (ISensor sensor : zone.getSensors()) {
Node senml = sensor.toXMLSenML(d);
currentZone.appendChild(senml);
}
/* String md5 = md5(xml.getText());
Tag tag = md5 == null ? null : new Tag(md5, false);
if (!isModified(tag)) {
setStatus(Status.REDIRECTION_NOT_MODIFIED);
return null;
}
*/
// xml.setTag(tag);
return xml;
} catch (Exception e) {
log.log(Level.WARNING, "", e);
setStatus(Status.SERVER_ERROR_INTERNAL);
return null;
}
}
/*
* Alternative method to add an header to the response. Series<Header>
* responseHeaders = (Series<Header>)
* getResponse().getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS); if
* (responseHeaders == null) { responseHeaders = new
* Series<Header>(Header.class); getResponse().getAttributes().put(
* HeaderConstants.ATTRIBUTE_HEADERS, responseHeaders);
*
* } responseHeaders.add(new Header("ETag", md5));
*/
/**
* Build a String baseURL = to current URI/Reference less the last path
* segment.
*
* @param reference
* the current reference object
* @return String base URI.
*/
private String buildBaseURL(Reference reference) {
Reference ref = new Reference(reference);
List<String> segments = ref.getSegments();
if (segments.size() > 0) {
segments.remove(segments.size() - 1);
ref.setSegments(segments);
}
return ref.getHierarchicalPart();
}
private String md5(String value) {
try {
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(value.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1, digest);
String hashtext = bigInt.toString(16);
// Now we need to zero pad it if you actually want the full 32
// chars.
while (hashtext.length() < 32) {
hashtext = "0" + hashtext;
}
return hashtext;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private boolean isModified(Tag currentTag) {
boolean modified = true;
if (currentTag == null) {
return modified;
}
if (getRequest().getConditions() != null
&& getRequest().getConditions().getNoneMatch() != null) {
for (Tag noMatch : getRequest().getConditions().getNoneMatch()) {
if (currentTag.equals(noMatch)) {
modified = false;
break;
}
}
}
return modified;
}
}
\ No newline at end of file
package fr.eurecom.restlet.resources.common;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.mortbay.log.Log;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Reference;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.ext.wadl.MethodInfo;
import org.restlet.ext.wadl.RepresentationInfo;
import org.restlet.ext.wadl.ResponseInfo;
import org.restlet.ext.wadl.WadlServerResource;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import fr.eurecom.senml.entity.IZone;
import fr.eurecom.senml.entity.ZoneAdmin;
import fr.eurecom.senml.persistence.JDOStorage;
public class ZonesResource extends WadlServerResource {
public static enum ZoneFormat {
ID("id", "zone identifier"), NAME("name", "zone name"), SUBZONE(
"subzone", "subzone description"),
// SENSORS("sensors","sensors lsit uri"),
PARENT_ZONE("parent", "name zone parent"), ZONE("zone", "Zone"), URI(
"uri", "URI"), HAS_SUBZONES("has_subzones", "Has subzones"), HAS_SENSORS(
"has_sensors", "Has_sensors"), SENSORS_SENML("senml",
"sensors over senml"),ZONE_TYPE("type", "type of the zone");
private final String restAttr;
private final String desc;
ZoneFormat(String restAttr, String desc) {
this.restAttr = restAttr;
this.desc = desc;
}
public String getRestAtr() {
return restAttr;
}
public String getDescription() {
return desc;
}
};
/**
* {@inheritDoc}
*/
@Override
public Representation describe() {
setName("Zones Resource");
setDescription("Retrieve zones list");
return super.describe();
}
/**
* {@inheritDoc}
*/
@Override
public void describeGet(MethodInfo mInfo) {
mInfo.setIdentifier("Zones List");
Log.info("oooooo");
mInfo.setDocumentation("To Retrieve a list of zones");
ResponseInfo response = new ResponseInfo("Current list of zones");
response.getStatuses().add(Status.SUCCESS_OK);
RepresentationInfo repInfo = new RepresentationInfo(
MediaType.APPLICATION_JSON);
repInfo.setDocumentation("JSON array of of the current list zone");
response.getRepresentations().add(repInfo);
mInfo.getResponses().add(response);
}
// HACK: Version Restlet-gae 2.1-rc5.
// There is an error routing the GET request when
// the Request Header Content-Type = application/x-www-form-urlencoded and
// Header Accept = "application/json" and
// URI have a query : http://localhost:8888/contacts?sort=firstname.
// In this case the router ignores the Accept header and route the request
// to @Get("xml") systematically because the query is badly interpreted.
// To workaround this problem, at the init we reformat the Entity with an
// empty Representation.
@Override
public void init(Context context, Request request, Response response) {
// System.out.println("init" + request + " " + response);
// System.out.println("Content-Type: " +
// request.getEntity().getMediaType());
if (request.getMethod().compareTo(Method.GET) == 0) {
Representation entity = request.getEntity();
System.out.println("Entity " + entity + " Content-Type: "
+ request.getEntity().getMediaType());
request.setEntity(new StringRepresentation(""));
}
super.init(context, request, response);
}
/**
* Handle HTTP GET method / Json
*
* @return a JSON list of root zones by default.
*/
@Get("json")
public Representation toJSON() {
// System.out.println("Request Original Ref " + getOriginalRef());
// System.out.println("Request Entity " + getRequest().getEntityAsText()
// +
// " entity mediaType " + getRequest().getEntity().getMediaType());
try {
List<IZone> zones = JDOStorage.getInstance()
.getAll(ZoneAdmin.class);
JSONArray jzones = new JSONArray();
Reference ref = getRequest().getResourceRef();
final String baseURL = ref.getHierarchicalPart();
for (IZone zone : zones) {
if (zone.getParentZone() == null) {
JSONObject jzone = new JSONObject();
jzone.put(ZoneFormat.URI.getRestAtr(),
String.format("%s/%s", baseURL, zone.getKey()));
jzone.put(ZoneFormat.NAME.getRestAtr(), zone.getName());
jzone.put(ZoneFormat.ZONE_TYPE.getRestAtr(), zone.getType());
jzone.put(ZoneFormat.HAS_SENSORS.getRestAtr(), zone
.getSensors().size() > 0);
jzone.put(ZoneFormat.HAS_SUBZONES.getRestAtr(), zone
.getSubZones().size() > 0);
jzones.put(jzone);
}
}
JSONObject zoneHead = new JSONObject();
zoneHead.put(ZoneFormat.SUBZONE.getRestAtr(), jzones);
return new JsonRepresentation(zoneHead);
} catch (Exception e) {
setStatus(Status.SERVER_ERROR_INTERNAL);
return null;
}
}
@Get("xml")
public Representation toXML() {
try {
List<IZone> zones = JDOStorage.getInstance()
.getAll(ZoneAdmin.class);
Reference ref = getRequest().getResourceRef();
final String baseURL = ref.getHierarchicalPart();
DomRepresentation xml = new DomRepresentation(MediaType.TEXT_XML);
Document d = xml.getDocument();
Element root = d.createElement(ZoneFormat.ZONE.getRestAtr());
d.appendChild(root);
for (IZone zone : zones) {
if (zone.getParentZone() == null) {
Element subzone = d.createElement(ZoneFormat.SUBZONE
.getRestAtr());
subzone.setAttribute(ZoneFormat.NAME.getRestAtr(),
zone.getName());
subzone.setAttribute(ZoneFormat.URI.getRestAtr(),
String.format("%s/%s", baseURL, zone.getKey()));
subzone.setAttribute(ZoneFormat.HAS_SENSORS.getRestAtr(),
"" + (zone.getSensors().size() > 0));
subzone.setAttribute(ZoneFormat.HAS_SUBZONES.getRestAtr(),
"" + (zone.getSubZones().size() > 0));
root.appendChild(subzone);
}
}
return xml;
} catch (Exception e) {
e.printStackTrace();
setStatus(Status.SERVER_ERROR_INTERNAL);
return null;
}
}
}
\ No newline at end of file
package fr.eurecom.restlet.resources.gae;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.ext.wadl.ApplicationInfo;
import org.restlet.ext.wadl.DocumentationInfo;
import org.restlet.ext.wadl.WadlApplication;
import org.restlet.resource.Directory;
import org.restlet.routing.Router;
import fr.eurecom.restlet.resources.common.ContactResource;
import fr.eurecom.restlet.resources.common.ContactsResource;
import fr.eurecom.restlet.resources.common.ContactsSubResource;
public class Dispatcher extends WadlApplication {
@Override
public Restlet createInboundRoot() {
Router router = new Router(getContext());
router.attachDefault(new Directory(getContext(), "war:///"));
// web xml mapping = /*
// router.attach("/contacts", ContactsResource.class);
// router.attach("/contacts/{id}", ContactResource.class);
// web xml mapping = /contacts/*
router.attach("", ContactsResource.class);
router.attach("/subscribe", ContactsSubResource.class);
router.attach("/{id}", ContactResource.class);
return router;
}
@Override
public ApplicationInfo getApplicationInfo(Request request, Response response) {
ApplicationInfo info = super.getApplicationInfo(request, response);
DocumentationInfo docInfo = new DocumentationInfo(
"emulator-box-services application documentation");
docInfo.setTitle("Emulator Box Services application infos.");
info.setDocumentation(docInfo);
return info;
}
}
package fr.eurecom.restlet.resources.gae;
import java.util.logging.Logger;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.Restlet;
import org.restlet.ext.wadl.ApplicationInfo;
import org.restlet.ext.wadl.DocumentationInfo;
import org.restlet.ext.wadl.WadlApplication;
import org.restlet.resource.Directory;
import org.restlet.routing.Router;
import fr.eurecom.restlet.resources.common.SensorPost;
import fr.eurecom.restlet.resources.common.ZonesResource;
import fr.eurecom.restlet.resources.common.ZoneResource;
import fr.eurecom.restlet.resources.common.SensorInResource;
public class DispatcherSenml extends WadlApplication {
private static final Logger log = Logger.getLogger("DispatcherSenml");
@Override
public Restlet createInboundRoot() {
log.info("SenML dispatcher attaches resources");
Router router = new Router(getContext());
router.attachDefault(new Directory(getContext(), "war:///"));
// web xml mapping = /*
router.attach("/zones", ZonesResource.class);
router.attach("/zones/{id}", ZoneResource.class);
router.attach("/sensorsin", SensorInResource.class);
router.attach("/posttest", SensorPost.class);
return router;
}
@Override
public ApplicationInfo getApplicationInfo(Request request, Response response) {
ApplicationInfo info = super.getApplicationInfo(request, response);
DocumentationInfo docInfo = new DocumentationInfo(
"emulator-box-services application documentation");
docInfo.setTitle("Emulator Box Services application infos.");
info.setDocumentation(docInfo);
return info;
}
}
package fr.eurecom.restlet.resources.gae;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* This class serves for test only. Can be deleted.
* @author ouest
*
*/
@SuppressWarnings("serial")
public class ResourcesServletGAE extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
}
package fr.eurecom.restlet.resources.persistence;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
public final class PMF {
private static final PersistenceManagerFactory pmfInstance = JDOHelper
.getPersistenceManagerFactory("transactions-optional");
private PMF() {
}
public static PersistenceManagerFactory get() {
return pmfInstance;
}
}
// PersistenceManager pm = PMF.get().getPersistenceManager();
package fr.eurecom.restlet.resources.persistence;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import fr.eurecom.restlet.resources.common.Contact;
import fr.eurecom.restlet.resources.common.ContactBaseResource.MEMBERS;
/**
* This class is a naive implementation of contact's datastore, it should be
* replaced by a NOSQL JDO storage.
*/
// TODO: Replace this class by NOSQL JDO implementation.
public class Storage {
/**
* Array of contacts. Restricted to 50 contacts.
*/
private final Contact[] backend = new Contact[50];
/**
* Storage instance.
*/
private static Storage instance = null;
private static final String TEMPLATE_SORT = "%s-%s";
/**
* Constructs a storage. Initialize a bunch of contacts.
*/
private Storage() {
backend[0] = new Contact(0, "Bonnie", "Parker", "bonnie@gangsters.org",
"+1-555-329-4589");
backend[1] = new Contact(1, "Clyde", "Barrow", "clyde@gangsters.org",
"+1-555-329-4589");
backend[2] = new Contact(2, "John", "Dillinger", "john@gangsters.org",
"+1-555-355-3232");
backend[3] = new Contact(3, "Herman", "Lamm", "herman@gangsters.org",
"+1-555-258-2365");
backend[4] = new Contact(4, "Willie", "Sutton", "will@gangsters.org",
"+1-555-258-2365");
for (int i = 5; i < 50; i++) {
backend[i] = null;
}
}
/**
* Retrieve the singleton storage.
*
* @return a Storage instance.
*/
public static Storage getInstance() {
if (instance == null) {
instance = new Storage();
}
return instance;
}
/**
* Retrieve a list of sorted contacts, starting with the given offset.
*
* @param sort
* - Sort parameter.
* @param minLimit
* - reserved for a future usage
* @param maxLimit
* - reserved for a future usage
* @return a sorted list of the contacts.
*/
@SuppressWarnings("unchecked")
public List<Contact> getContacts(MEMBERS sort, int minLimit, int maxLimit) {
switch (sort) {
case ID:
return getSortedById();
case FIRSTNAME:
return getSortedByFirstName();
case LASTNAME:
return getSortedByLastName();
case PHONE:
return getSortedByPhone();
case MAIL:
return getSortedByEmail();
}
return Collections.EMPTY_LIST;
}
/**
* Save a contact, the Contact's id must be comprise between 0 and 49.
*
* @param contact
* to save.
* @return true if correctly saved, false otherwise.
*/
public boolean saveContact(Contact contact) {
if (checkIndex(contact.getId())) {
backend[contact.getId()] = contact;
return true;
}
return false;
}
/**
* Delete a contact. Same as {@link #removeContact(int)}
* {@code
* removeContact(conctact.getId();
* }
*
* @param contact
* to delete.
* @return true if deleted, false otherwise.
*/
public boolean removeContact(Contact contact) {
return removeContact(contact.getId());
}
/**
* Delete a contact
*
* @param identifier
* - Contact's identifier
* @return true if deleted, false otherwise
* @see Storage.removeContact(Contact)
*/
public boolean removeContact(int identifier) {
if (checkIndex(identifier)) {
backend[identifier] = null;
return true;
}
return false;
}
/**
* Retrieve a contact from an identifier.
*
* @param identifier
* - Contact's identifier
* @return Contact.
*/
public Contact getContact(int identifier) {
if (checkIndex(identifier)) {
return backend[identifier];
}
return null;
}
private boolean checkIndex(int index) {
return index > -1 && index < 50;
}
/**
* Sort contacts by email field.
*
* @return a sorted list of contacts.
*/
private List<Contact> getSortedByEmail() {
Map<String, Contact> sorted = new TreeMap<String, Contact>(
String.CASE_INSENSITIVE_ORDER);
for (int i = 0; i < 50; i++) {
if (backend[i] != null) {
sorted.put(unicKey(backend[i].getMail(), i), backend[i]);
}
}
return new ArrayList<Contact>(sorted.values());
}
/**
* Sort contacts by phone number
*
* @return a sorted list of contacts.
*/
private List<Contact> getSortedByPhone() {
Map<String, Contact> sorted = new TreeMap<String, Contact>(
String.CASE_INSENSITIVE_ORDER);
for (int i = 0; i < 50; i++) {
if (backend[i] != null) {
sorted.put(unicKey(toNumeric(backend[i].getPhone()), i),
backend[i]);
}
}
return new ArrayList<Contact>(sorted.values());
}
/**
* Sort contacts by last name
*
* @return a sorted list of contacts.
*/
private List<Contact> getSortedByLastName() {
Map<String, Contact> sorted = new TreeMap<String, Contact>(
String.CASE_INSENSITIVE_ORDER);
for (int i = 0; i < 50; i++) {
if (backend[i] != null) {
sorted.put(unicKey(backend[i].getLastName(), i), backend[i]);
}
}
return new ArrayList<Contact>(sorted.values());
}
/**
* Sort contacts by first name
*
* @return a sorted list of contacts.
*/
private List<Contact> getSortedByFirstName() {
Map<String, Contact> sorted = new TreeMap<String, Contact>(
String.CASE_INSENSITIVE_ORDER);
for (int i = 0; i < 50; i++) {
if (backend[i] != null) {
sorted.put(unicKey(backend[i].getFirstName(), i), backend[i]);
}
}
return new ArrayList<Contact>(sorted.values());
}
/**
* Sort contacts by identifier.
*
* @return a sorted list of contacts.
*/
private List<Contact> getSortedById() {
return Arrays.asList(backend);
}
private static String toNumeric(String phone) {
char[] chars = phone.toCharArray();
StringBuilder builder = new StringBuilder();
for (char c : chars) {
if (Character.isDigit(c)) {
builder.append(c);
}
}
return builder.toString();
}
/**
* Format a unic key (necessary when at least 2 contacts have a same
* property value like firstname/lastname..)
*
* @param key
* @param id
* @return
*/
private static String unicKey(String key, int id) {
return String.format(TEMPLATE_SORT, key, id);
}
}
package fr.eurecom.senml.entity;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.google.appengine.api.datastore.Key;
@PersistenceCapable
public class ContactTest {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String firstName;
@Persistent
private String lastName;
@Persistent
private String email;
public ContactTest(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getirstName() {return firstName;}
public String getLastName() {return lastName;}
public String getEmail() {return email;}
public Key getKey() {return key;}
}
package fr.eurecom.senml.entity;
public interface IPostVal {
public final static String BASE_NAME = "bn";
public final static String STATUS = "status";
}
package fr.eurecom.senml.entity;
import java.util.List;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
public interface ISensor {
public final static String BASE_NAME = "bn";
public final static String BASE_TIME = "bt";
public final static String BASE_UNIT = "bu";
public final static String VERSION = "ver";
public final static String MEASUREMENT = "e";
public String getUUID();
public IZone getZone();
public String getTitle();
public List <Measure> getMeasures();
// public List<String> getMeasuresKey();
// public List<String> getMeasuresPrint();
public JSONObject toJSONSenML();
public Document toXMLSenML();
public Node toXMLSenML(Document rootDocument);
public String getBaseUnit();
public String zoneType();
}
package fr.eurecom.senml.entity;
import fr.eurecom.senml.entity.Measure.ParamTypeValue;
public interface ISensorAdmin extends ISensor {
public boolean linkZone(IZone zone);
public boolean linkZone(String zoneKey);
public void unlinkZone();
public Measure addMeasure(String measureName,
Units measureUnit,
ParamTypeValue measureTypeValue,
String measureValue, long timeMeasure, String type);
public void deleteMeasure(String measureKey);
public void deleteMeasure(Measure measure);
public void setTitle(String title);
// void tag(Tag tag);
// void untag(Tag tag);
public void destroy();
public Measure addMeasure(String measureName, Units measureUnit,
ParamTypeValue measureTypeValue, String measureValue,
long timeMeasure, String type, String sum);
}
package fr.eurecom.senml.entity;
import java.util.List;
public interface IZone extends Comparable<IZone> {
// public boolean hasSensors();
// public boolean hasSubZones();
public List<IZone> getSubZones();
public List<ISensor> getSensors();
public String getName();
public String getKey();
public IZone getParentZone();
String getType();
}
package fr.eurecom.senml.entity;
public interface IZoneAdmin extends IZone {
public boolean linkParentZone(String parentZoneKey);
public boolean linkParentZone(IZone parentZone);
public void unlinkParentZone();
// public boolean addSensor(String sensorKey);
// public boolean removeSensor(String sensorKey);
public void destroy();
}
package fr.eurecom.senml.entity;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.Map;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import fr.eurecom.senml.persistence.JDOStorage;
/**
* Immutable object. Represents the SenML Measurement entry.
*
* @author dalmassi
*
*/
@PersistenceCapable(detachable = "true")
public class Measure {
public enum ParamTypeValue {
BOOL("bv"), FLOAT("v"), STRING("sv"), SUM("s");
private String encoded;
private final static Map<String, ParamTypeValue> mTypeValue = new HashMap<String, ParamTypeValue>();
ParamTypeValue(String encoded) {
this.encoded = encoded;
}
public String getSenML() {
return encoded;
}
public static ParamTypeValue getParamTypeValue(String senmlEncoded) {
if (mTypeValue == null) {
initMapValue();
}
return mTypeValue.get(senmlEncoded);
}
private static synchronized void initMapValue() {
if (mTypeValue.size() > 0) {
return;
}
for (ParamTypeValue v : values()) {
mTypeValue.put(v.encoded, v);
}
}
};
@NotPersistent
private static final DateFormat df = new SimpleDateFormat(
"yyyy.MM.dd G 'at' HH:mm:ss z");
@NotPersistent
private static final Logger log = Logger
.getLogger("sensors.entity.Measure");
@NotPersistent
public static final String PARAM_UNIT = "u";
@NotPersistent
public static final String PARAM_NAME = "n";
@NotPersistent
public static final String PARAM_TIME = "t";
@NotPersistent
public static final String PARAM_VALUE = "v";
@NotPersistent
public static final String PARAM_STRING_VALUE = "sv";
@NotPersistent
public static final String PARAM_BOOLEAN_VALUE = "bv";
@NotPersistent
public static final String PARAM_SUM_VALUE = "s";
@NotPersistent
public static final String PARAM_UPDATE_TIME = "ut";
@NotPersistent
public static final String PARAM_TYPE = "type";
@PrimaryKey
@Persistent
Key key;
@Persistent
private String name;
@Persistent
private String unitCS;
@Persistent
private String unitPrint;
@Persistent
private String unitKindOfQty;
@Persistent
private String value;
@Persistent
private String paramTypeValueEncoded;
private long time;
@Persistent
private String sensorKey;
private String baseunitCS;
private String baseunitPrint;
private String baseunitKindOfQty;
private String type;
private String sum;
public Measure(String sensorKey, String name, Units unit,
ParamTypeValue typeValue, String value, long time, String type) {
this.sensorKey = sensorKey;
this.name = name == null || name.isEmpty() ? generateName() : name;
this.unitCS = unit == null ? null : unit.getCS();
this.unitPrint = unit == null ? null : unit.getPrint();
this.unitKindOfQty = unit == null ? null : unit.getKindofQty();
this.paramTypeValueEncoded = typeValue.getSenML();
this.value = value;
this.time = time;
this.key = KeyFactory.createKey(Measure.class.getSimpleName(),
sensorKey + name);
this.type = type;
}
public Measure(String sensorKey, String name, Units unit,
ParamTypeValue typeValue, String value, long time, String type, String sum) {
this.sensorKey = sensorKey;
this.name = name == null || name.isEmpty() ? generateName() : name;
this.unitCS = unit == null ? null : unit.getCS();
this.unitPrint = unit == null ? null : unit.getPrint();
this.unitKindOfQty = unit == null ? null : unit.getKindofQty();
this.paramTypeValueEncoded = typeValue.getSenML();
this.value = value;
this.time = time;
this.sum = sum;
this.key = KeyFactory.createKey(Measure.class.getSimpleName(),
sensorKey + name);
this.type = type;
}
public void destroy() {
JDOStorage.getInstance().delete(this);
}
public String getKey() {
return key == null ? null : KeyFactory.keyToString(key);
}
public String getType() {
return type;
}
public JSONObject toJSONSenML() {
try {
JSONObject entrySenML = new JSONObject();
if (name != null) {
entrySenML.put(PARAM_NAME, name);
}
if (sum != null) {
entrySenML.put(PARAM_SUM_VALUE, sum);
}
entrySenML.put(PARAM_TYPE, type);
entrySenML.put(PARAM_TIME, time);
if (this.unitPrint != null) {
entrySenML.put(PARAM_UNIT, unitCS);
}
if (paramTypeValueEncoded != null) {
entrySenML.put(paramTypeValueEncoded, value);
}
return entrySenML;
} catch (Exception e) {
e.printStackTrace();
log.warning("Error Encoding a measure");
return null;
}
}
public Node toXMLSenML(Document d) {
Element measure = d.createElement("e");
measure.setAttribute(PARAM_NAME, name);
measure.setAttribute(PARAM_TYPE, type);
measure.setAttribute(PARAM_TIME, "" + time);
if (this.unitPrint != null) {
measure.setAttribute(PARAM_UNIT, unitCS);
}
if (this.sum != null) {
measure.setAttribute(PARAM_SUM_VALUE, sum);
}
if (paramTypeValueEncoded != null) {
measure.setAttribute(paramTypeValueEncoded, value);
}
return measure;
}
public String toString() {
StringBuilder builder = new StringBuilder();
if (this.unitCS != null) {
builder.append("key[" + getKey()).append("] name[" + name)
.append("] unitCS[" + unitCS)
.append("] unitPrint[" + unitPrint)
.append(this.sum == null ? "" : "] sum[" + sum)
.append("] unitKindOfQty[" + unitKindOfQty)
.append("] paramTypeEncoded[" + paramTypeValueEncoded)
.append("] value[" + value).append("] time[" + time)
.append("] sensorKey[" + sensorKey).append("]");
} else {
// Check if we have to initialize the base unit
baseUnitInit();
builder.append("key[" + getKey())
.append("] name[" + name)
.append("] unitCS (base unit) [" + baseunitCS)
.append("] unitPrint (base unit) [" + baseunitPrint)
.append(this.sum == null ? "" : "] sum[" + sum)
.append("] unitKindOfQty (base unit) [" + baseunitKindOfQty)
.append("] paramTypeEncoded[" + paramTypeValueEncoded)
.append("] value[" + value).append("] time[" + time)
.append("] sensorKey[" + sensorKey).append("]");
}
return builder.toString();
}
public String toPrettyString() {
StringBuilder builder = new StringBuilder();
if (this.unitCS != null) {
builder.append(name).append(": ").append("(kind: ")
.append(unitKindOfQty).append(") ").append(value)
.append(this.sum == null ? "" : " sum: " + sum)
.append(" ").append(unitPrint).append(" @ ")
.append(formatDate(time));
} else {
// Check if we have to initialize the base unit
baseUnitInit();
builder.append(name).append(": ").append("(kind (baseunit): ")
.append(baseunitKindOfQty).append(") ").append(value)
.append(" ").append(baseunitPrint).append("(baseunit) @ ")
.append(formatDate(time));
}
return builder.toString();
}
private void baseUnitInit() {
System.out.println("unit is null, parent sensor is " + sensorKey);
// Retrieve info about base unit from the parent sensor
Sensor parent = JDOStorage.getInstance().getById(sensorKey,
Sensor.class);
System.out.println("sensor is " + parent.getTitle());
String bu = parent.getBaseUnit();
System.out.println("sensor bu is " + bu);
Units baseUnits = Units.getUnit(bu);
this.baseunitCS = baseUnits.getCS();
this.baseunitKindOfQty = baseUnits.getKindofQty();
this.baseunitPrint = baseUnits.getPrint();
System.out.println("base unit is: " + this.baseunitCS);
}
private String formatDate(long ms) {
if (ms == 0)
return "now";
if (ms < 0)
return ("now - " + ms);
return df.format(new Date(ms));
}
public String getSensorKey() {
return sensorKey;
}
private String generateName() {
return UUID.randomUUID().toString();
}
}
package fr.eurecom.senml.entity;
import java.util.UUID;
import javax.jdo.annotations.Persistent;
import fr.eurecom.senml.persistence.JDOStorage;
public class PostValAdmin implements IPostVal {
@Persistent
boolean active = true;
public PostValType addPost(String name, String val, long timeMeasure) {
PostValType m = new PostValType(UUID.randomUUID().toString(), name, val, timeMeasure);
JDOStorage.getInstance().write(m);
//JDOStorage.getInstance().deleteAll(PostValType.class);
return m;
//return null;
}
}
package fr.eurecom.senml.entity;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.Map;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import fr.eurecom.senml.persistence.JDOStorage;
/**
* Immutable object. Represents the SenML Measurement entry.
*
*
*/
@PersistenceCapable(detachable="true")
public class PostValType {
public enum ParamTypeValue {
STR("sv");
private String encoded;
private final static Map<String, ParamTypeValue> mTypeValue = new HashMap<String, ParamTypeValue>();
ParamTypeValue(String encoded) {
this.encoded = encoded;
}
public String getSenML() {
return encoded;
}
public static ParamTypeValue getParamTypeValue(String senmlEncoded) {
if (mTypeValue == null) {
initMapValue();
}
return mTypeValue.get(senmlEncoded);
}
private static synchronized void initMapValue() {
if (mTypeValue.size() > 0) {
return;
}
for (ParamTypeValue v : values()) {
mTypeValue.put(v.encoded, v);
}
}
};
@NotPersistent
private static final DateFormat df = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
@NotPersistent
private static final Logger log = Logger
.getLogger("sensors.entity.PostValType");
@NotPersistent
public static final String PARAM_STRING_VALUE = "sv";
@NotPersistent
public static final String PARAM_NAME = "n";
@PrimaryKey
@Persistent
Key key;
@Persistent
private String name;
@Persistent
private String value;
@Persistent
private long time;
public String getName() {
return name;
}
public String getValue() {
return String.valueOf(value);
}
public String getTime() {
return formatDate(time);
}
public PostValType(String sensorKey, String name,
String val, long time) {
this.name = name == null || name.isEmpty() ? generateName() : name;
this.value = val;
this.time = time;
this.key = KeyFactory.createKey(PostValType.class.getSimpleName(), sensorKey + name +"" + time);
}
public void destroy() {
JDOStorage.getInstance().delete(this);
}
public String getKey() {
return key == null ? null : KeyFactory.keyToString(key);
}
public JSONObject toJSONSenML() {
try {
JSONObject entrySenML = new JSONObject();
if (name != null) {
entrySenML.put(PARAM_NAME, name);
}
entrySenML.put(PARAM_STRING_VALUE, value);
return entrySenML;
} catch (Exception e) {
e.printStackTrace();
log.warning("Error Encoding a measure");
return null;
}
}
public Node toXMLSenML(Document d) {
Element measure = d.createElement("e");
measure.setAttribute(PARAM_NAME, name);
measure.setAttribute(PARAM_STRING_VALUE, String.valueOf(value));
return measure;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("key[" + getKey())
.append("] name[" + name)
.append("] value[" + value)
.append("] time[" + formatDate(time))
.append("]");
return builder.toString();
}
public String toPrettyString() {
StringBuilder builder = new StringBuilder();
builder.append(name)
.append(": ")
.append(") ")
.append(value)
.append(" @ ")
.append(formatDate(time));
return builder.toString();
}
private String formatDate(long ms) {
if (ms == 0) return "now";
if (ms < 0) return ("now - " + ms);
return df.format(new Date(ms));
}
private String generateName() {
return UUID.randomUUID().toString();
}
}
package fr.eurecom.senml.entity;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.Map;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import fr.eurecom.senml.persistence.JDOStorage;
/**
* Immutable object. Represents the registered device.
*/
@PersistenceCapable(detachable="true")
public class RegisteredDeviceType {
public enum ParamTypeValue {
STR("sv");
private String encoded;
private final static Map<String, ParamTypeValue> mTypeValue = new HashMap<String, ParamTypeValue>();
ParamTypeValue(String encoded) {
this.encoded = encoded;
}
public String getSenML() {
return encoded;
}
public static ParamTypeValue getParamTypeValue(String senmlEncoded) {
if (mTypeValue == null) {
initMapValue();
}
return mTypeValue.get(senmlEncoded);
}
private static synchronized void initMapValue() {
if (mTypeValue.size() > 0) {
return;
}
for (ParamTypeValue v : values()) {
mTypeValue.put(v.encoded, v);
}
}
};
@NotPersistent
private static final DateFormat df = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
@NotPersistent
private static final Logger log = Logger
.getLogger("sensors.entity.RegisteredDeviceType");
@NotPersistent
public static final String PARAM_STRING_VALUE = "sv";
@NotPersistent
public static final String PARAM_NAME = "n";
@PrimaryKey
@Persistent
Key key;
@Persistent
private String deviceType;
@Persistent
private String registrationKey;
@Persistent
private long time;
private boolean isadmin = false;
public String getDeviceType() {
return deviceType;
}
public String getValue() {
return String.valueOf(registrationKey);
}
public String getTime() {
return formatDate(time);
}
public RegisteredDeviceType(String deviceKey, String deviceType,
String val, long time) {
this.deviceType =
deviceType == null || deviceType.isEmpty() ? generateName() : deviceType;
this.registrationKey = val;
this.time = time;
this.key = KeyFactory.createKey(
RegisteredDeviceType.class.getSimpleName(),
deviceKey + deviceType +"" + time);
}
public void destroy() {
JDOStorage.getInstance().delete(this);
}
public String getKey() {
return key == null ? null : KeyFactory.keyToString(key);
}
public String getDeviceRegistrationKey() {
return this.registrationKey;
}
public JSONObject toJSONSenML() {
try {
JSONObject entrySenML = new JSONObject();
if (deviceType != null) {
entrySenML.put(PARAM_NAME, deviceType);
}
entrySenML.put(PARAM_STRING_VALUE, registrationKey);
return entrySenML;
} catch (Exception e) {
e.printStackTrace();
log.warning("Error Encoding a measure");
return null;
}
}
public Node toXMLSenML(Document d) {
Element measure = d.createElement("e");
measure.setAttribute(PARAM_NAME, deviceType);
measure.setAttribute(PARAM_STRING_VALUE, String.valueOf(registrationKey));
return measure;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("key[" + getKey())
.append("] name[" + deviceType)
.append("] value[" + registrationKey)
.append("] time[" + formatDate(time))
.append("]");
return builder.toString();
}
public String toPrettyString() {
StringBuilder builder = new StringBuilder();
builder.append(deviceType)
.append(": ")
.append(") ")
.append(registrationKey)
.append(" @ ")
.append(formatDate(time));
return builder.toString();
}
private String formatDate(long ms) {
if (ms == 0) return "now";
if (ms < 0) return ("now - " + ms);
return df.format(new Date(ms));
}
private String generateName() {
return UUID.randomUUID().toString();
}
public boolean isIsadmin() {
return isadmin;
}
public void setIsadmin(boolean isadmin) {
this.isadmin = isadmin;
}
}
package fr.eurecom.senml.entity;
import java.util.Iterator;
import java.util.List;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.json.JSONObject;
import org.json.JSONArray;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import fr.eurecom.senml.persistence.JDOStorage;
import fr.eurecom.senml.persistence.JDOStorageZone;
@PersistenceCapable(detachable="true")
public class Sensor implements ISensor {
@Persistent
@PrimaryKey
protected String uuid;
@Persistent
protected String title = null;
@Persistent
protected String zoneKey = null;
@Persistent
protected String baseUnit = null;
@Persistent
protected String type = null;
// Not supported. GAE load only lazy objects (empty objects) and does not support joins.
// @Persistent(defaultFetchGroup="true")
// protected Set<Measure> measures;
protected Sensor(String uuid, String title, String zoneKey) {
this.uuid = uuid;
this.title = title.trim();
this.zoneKey = zoneKey;
}
public Sensor(String uuid, String title, String zoneKey, String baseUnit, String type) {
this.uuid = uuid;
this.title = title.trim();
this.zoneKey = zoneKey;
this.baseUnit = baseUnit;
this.type = type;
}
@Override
public String getUUID() {
return uuid;
}
@Override
public String zoneType() {
return type;
}
@Override
public String getTitle() {
return title;
}
@Override
public IZone getZone() {
if (zoneKey != null) {
return JDOStorage.getInstance().getById(zoneKey, ZoneAdmin.class);
}
return null;
}
@Override
public String getBaseUnit() {
return baseUnit;
}
@Override
public JSONObject toJSONSenML() {
JSONObject sensorSenML = new JSONObject();
try {
sensorSenML.put("bn", getBaseName());
sensorSenML.put("type", zoneType());
if (getBaseUnit() != null) {
sensorSenML.put("bu", getBaseUnit());
}
JSONArray mArray = new JSONArray();
for (Measure m : getMeasures()) {
mArray.put(m.toJSONSenML());
}
if (mArray.length() > 0) {
sensorSenML.put("e", mArray);
}
}
catch (Exception e) {
e.printStackTrace();
}
// System.out.println(sensorSenML.toString());
return sensorSenML;
}
@Override
public Document toXMLSenML() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document d = builder.newDocument();
d.appendChild(toXMLSenML(d));
return d;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public Element toXMLSenML(Document documentRoot) {
try {
Element senml = documentRoot.createElement("senml");
senml.setAttribute("bn", getBaseName());
senml.setAttribute("type", zoneType());
if (getBaseUnit() != null) {
senml.setAttribute("bu", getBaseUnit());
}
for (Measure m : getMeasures()) {
senml.appendChild(m.toXMLSenML(documentRoot));
}
return senml;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Title : " + title);
builder.append("\"bn\" :").append(" (" + type).append(") \"uuid:" + uuid + "\" Measures ");
builder.append("\r\n");
for (Measure m : JDOStorageZone.getInstance().getMeasures(this)) {
builder.append("\t").append(m.toString());
builder.append("\r\n");
}
return builder.toString();
}
/* @Override
public List<String> getMeasuresPrint() {
List<String> mString = new ArrayList<String>();
for (String mKey : measuresKey) {
Measure m = JDOStorage.getInstance().getById(mKey, Measure.class);
mString.add(m.toPrettyString());
}
return mString;
}
*/
/* @Override
public List<String> getMeasuresKey() {
return new ArrayList<String>(measuresKey);
}
*/
@Override
public List<Measure> getMeasures() {
return JDOStorageZone.getInstance().getMeasures(this);
}
/* HACK: "Generic Sensor" when the sensor has been created receiving a senml file (sensorin resource).
else sensor has been entered by the jsp file the uuid is generated by this program.
this is a poor workaround to support both. in a future the jsp should disappears.
*/
private final String getBaseName() {
return uuid;
//return "Generic Sensor".equals(title) ? uuid : String.format("urn:%s:uuid:%s", title.toLowerCase(), uuid);
}
public static Sensor getByUUID(String uuid) {
List<Sensor> sensors = JDOStorage.getInstance().getAll(Sensor.class);
Iterator<Sensor> iter = sensors.iterator();
while (iter.hasNext()) {
Sensor current = iter.next();
if (current.getUUID().equals(uuid)) {
return current;
}
}
return null;
}
}
package fr.eurecom.senml.entity;
import java.util.UUID;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import org.json.JSONException;
import fr.eurecom.pushing.GCMPushingServer;
import fr.eurecom.senml.entity.Measure.ParamTypeValue;
import fr.eurecom.senml.persistence.JDOStorage;
@PersistenceCapable
@Inheritance(customStrategy = "complete-table")
public class SensorAdmin extends Sensor implements ISensorAdmin {
@NotPersistent
private final static String ZONE_UPDATED = "ZoneUpdated";
@Persistent
boolean active = true;
public SensorAdmin(String title, IZone zone, String baseUnit, String typeSensor) {
super(UUID.randomUUID().toString(), title, zone != null ? zone.getKey()
: null, baseUnit, typeSensor);
JDOStorage.getInstance().write(this);
}
public SensorAdmin(String bn, String title, IZone zone) {
super(bn, title, zone != null ? zone.getKey() : null);
JDOStorage.getInstance().write(this);
}
@Override
public void setTitle(String title) {
this.title = title;
JDOStorage.getInstance().write(this);
}
@Override
public void destroy() {
try {
JDOStorage.getInstance().delete(getMeasures());
} catch (Exception e) {
e.printStackTrace();
}
JDOStorage.getInstance().delete(this);
}
@Override
public boolean linkZone(IZone zone) {
return zone == null ? false : linkZone(zone.getKey());
}
@Override
public boolean linkZone(String zoneKey) {
if (active && checkKey(zoneKey)) {
this.zoneKey = zoneKey;
JDOStorage.getInstance().write(this);
return active;
}
return false;
}
@Override
public void unlinkZone() {
if (zoneKey != null) {
this.zoneKey = null;
JDOStorage.getInstance().write(this);
}
}
@Override
public void deleteMeasure(String measureKey) {
if (measureKey != null) {
JDOStorage.getInstance()
.delete(JDOStorage.getInstance().getById(measureKey,
Measure.class));
}
fr.eurecom.restlet.resources.common.ContactBaseResource
.sendUpdateSubscribers(ZONE_UPDATED + getZone().getName());
}
@Override
public void deleteMeasure(Measure measure) {
JDOStorage.getInstance().delete(measure);
fr.eurecom.restlet.resources.common.ContactBaseResource
.sendUpdateSubscribers(ZONE_UPDATED + getZone().getName());
}
private boolean checkKey(String key) {
return key != null && !key.trim().isEmpty();
}
@Override
public Measure addMeasure(String measureName, Units measureUnit,
ParamTypeValue measureTypeValue, String measureValue,
long timeMeasure, String type) {
Measure m = new Measure(getUUID(), measureName, measureUnit,
measureTypeValue, measureValue, timeMeasure, type);
JDOStorage.getInstance().write(m);
// fr.eurecom.restlet.resources.common.ContactBaseResource.sendUpdateSubscribers(ZONE_UPDATED
// + getZone().getName());
GCMPushingServer GCM = GCMPushingServer.getInstance();
try {
GCM.sendNotification(ZONE_UPDATED + getZone().getName());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return m;
}
@Override
public Measure addMeasure(String measureName, Units measureUnit,
ParamTypeValue measureTypeValue, String measureValue,
long timeMeasure, String type, String sum) {
Measure m = new Measure(getUUID(), measureName, measureUnit,
measureTypeValue, measureValue, timeMeasure, type, sum);
JDOStorage.getInstance().write(m);
// fr.eurecom.restlet.resources.common.ContactBaseResource.sendUpdateSubscribers(ZONE_UPDATED
// + getZone().getName());
GCMPushingServer GCM = GCMPushingServer.getInstance();
try {
GCM.sendNotification(ZONE_UPDATED + getZone().getName());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return m;
}
}
package fr.eurecom.senml.entity;
import java.util.Collections;
import java.util.List;
import java.util.SortedSet;
import java.util.logging.Logger;
import javax.jdo.annotations.Index;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import fr.eurecom.senml.persistence.JDOStorage;
import fr.eurecom.senml.persistence.JDOStorageZone;
/**
* Not thread-safe.
*
* @author dalmassi
*
*/
@PersistenceCapable
@Inheritance(customStrategy = "complete-table")
// @Index(name="index_parentKey", members={"parentKeyString"})
public class ZoneAdmin extends Zone implements IZoneAdmin {
@NotPersistent
private static final Logger log = Logger.getLogger("Zone");
@Persistent
private boolean isActive = true;
public ZoneAdmin(String name, ZoneAdmin parent, String type) {
super(KeyFactory.createKey(ZoneAdmin.class.getSimpleName(), name),
name, parent == null ? null : parent.getKey(), type);
JDOStorage.getInstance().write(this);
}
@Override
public void destroy() {
isActive = false;
parentKey = null;
List<ISensorAdmin> sensors = JDOStorageZone.getInstance().getSensors(
this);
for (ISensorAdmin s : sensors) {
s.destroy();
}
JDOStorage.getInstance().delete(this);
// TODO: Possible Race condition here. The delete is not effective when
// method JDOStorage delete returns.
log.fine("Removed from database " + this.name);
}
@Override
public boolean linkParentZone(String parentZoneKey) {
if (isActive && checkKey(parentZoneKey)) {
this.parentKey = parentZoneKey;
JDOStorage.getInstance().write(this);
return isActive;
}
return false;
}
@Override
public boolean linkParentZone(IZone parentZone) {
return parentZone == null ? false : linkParentZone(parentZone.getKey());
}
@Override
public void unlinkParentZone() {
if (parentKey != null) {
parentKey = null;
JDOStorage.getInstance().write(this);
}
}
private boolean checkKey(String key) {
return key != null && !key.trim().isEmpty();
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment