ppyordanov / Dynamic-Noise-and-Pollution-Map

A dynamically generated University of Glasgow noise and pollution campus map via the innovative Smart Citizen Kit developed in Barcelona, Spain by FabLab.
0 stars 0 forks source link

Server RESTful Infrastructure #61

Closed ppyordanov closed 9 years ago

ppyordanov commented 9 years ago

The server's structure needs to be improved to facilitate RESTful communication with the client application (Android).

ppyordanov commented 9 years ago

The most important communication point is transmitting newly generated routes/ data readings mapped to locations (latitude and longitude) from the Android client application to the server. In order to implement this, multiple approaches were considered and tested:

    $.ajax({
        url:"http://127.0.0.1:8080/addRoute",
        //url:"http://178.62.100.239:8080/addRoute",
        type:"GET",
        //crossDomain: true,
        dataType: "jsonp",
        //contentType: "application/javascript",
        data: JSONdata,
        async: false,
        cache: false,
        processData:false,
        success: function(response)
    {
        console.log(response);
        return response;
    }});

The client is currently communicating with the server using an ajax POST request to http://178.62.100.239:8080/addRoute and the dataType is JSON. There are no concerns using this approach as there are no security issues that could potentially arise at this point. The JSON String is directly being deserialized into a list of DataReading models as well as a Route model on each transmission. Both entities are then added to the DB with a "routeId" one-to-many association between Route and DataReading. Here is how the request looks in JavaScript:

    var dataReading ={json:JSON.stringify(context_data)};
    $.ajax({
        type: 'POST',
        //url: "http://127.0.0.1:8080/addRoute",
        url:"http://178.62.100.239:8080/addRoute",
        data: dataReading,
        dataType: "json",
        success: function(response)
        {
            console.log(response);
        }
    });

An important consideration was whether to reduce the models' atomicity in order to improve query performance as well as reduce code complexity. The change would involve making data readings an integral part of the route model, meaning that the latter would have a list of data readings. Currently each data reading is associated to a route via its "routeId" attribute. This seems to be efficient and satisfactory at this stage of development, but essentially merging the two models is a potential optimization that can be implemented in the future.

ppyordanov commented 9 years ago

HomeController.java

The main view, consuming JSON, is addRoute():

    @RequestMapping(value = "/addRoute", method = RequestMethod.POST)
    public @ResponseBody
    String addRoute( @RequestParam("json") String json)
    {
        //save new route
        Route newRoute = new Route();
        routeRepository.save(newRoute);

        //save data reading
        Type listType = new TypeToken<List<DataReading>>() {
        }.getType();
        List<DataReading> data = new Gson().fromJson(json, listType);

        //DataReading dr = new Gson().fromJson(json,DataReading.class);
        LOGGER.info("Route saved: " + newRoute);
        LOGGER.info("ds" + data.size());
        for(DataReading dr: data) {

            dr.setRouteId(newRoute.getId());
            /*
            Map<String, Double> position = dataPopulation.randomPosGen(Constants.MIN_LAT_BOUNDS, Constants.MAX_LAT_BOUNDS, Constants.MIN_LON_BOUNDS, Constants.MAX_LON_BOUNDS);
            dr.setLatitude(position.get("latitude"));
            dr.setLongitude(position.get("longitude"));
            */

            dataReadingRepository.save(dr);
            LOGGER.info("Data reading saved: " + dr);
        }

        return "success";
    }
ppyordanov commented 9 years ago

Update: the web server should be available on the its newly registered domain name:

ppyordanov commented 9 years ago

This task has been completed and can be closed now.