package kr.ac.kaist.resl.lilliput.rest;
import kr.ac.kaist.resl.lilliput.util.SpaceEventThread;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import com.mongodb.MongoException;
public class SpaceEventOn extends ServerResource{
@Get("json")
public JsonRepresentation toJson() throws JSONException {
try {
String EPCLocation = (String)getRequest().getAttributes().get("uid");
//Save Space to Ontology if it is not saved
String server_address =null;
int port = 5683;
int period = 3;
if( EPCLocation.equals("urn:epc:id:sgln:00000000001.1.1"))
{
server_address = "iotsensor1.iot.kr";
}
else if(EPCLocation.equals("urn:epc:id:sgln:00000000001.1.2") )
{
server_address = "iotsensor2.iot.kr";
}
else
{
return null;
}
SpaceEventThread spaceEventThread = new SpaceEventThread(EPCLocation, server_address, port, period);
spaceEventThread.start();
JSONObject returnObj = new JSONObject();
returnObj.put("message", EPCLocation + "'s sensor is on");
JsonRepresentation representation = new JsonRepresentation(returnObj);
return representation;
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject err = new JSONObject();
err.put("message", "error occured");
JsonRepresentation representation = new JsonRepresentation(err);
return representation;
}
}