<commit_before>package examples;

import com.devicehive.client.ApiClient;
import com.devicehive.client.api.DeviceApi;
import com.devicehive.client.api.DeviceCommandApi;
import com.devicehive.client.model.DeviceClassUpdate;
import com.devicehive.client.model.DeviceCommandItem;
import com.devicehive.client.model.DeviceCommandWrapper;
import com.devicehive.client.model.DeviceUpdate;
import com.google.common.base.Strings;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

import java.net.MalformedURLException;
import java.util.List;

public class DeviceExample {


    public static void main(String[] args) throws MalformedURLException {
        ApiClient apiClient = new ApiClient(Const.URL, ApiClient.AUTH_API_KEY, Const.MY_API_KEY);
        String timestamp = "";

        if (Strings.isNullOrEmpty(timestamp)) {

            /**FIXME "2015-5-09T14:37:17.404" test data to get list of command
             *ApiInfoApi infoService = apiClient.createService(ApiInfoApi.class);
             *ApiInfo apiInfo = infoService.getApiInfo();
             *TIMESTAMP = apiInfo.getServerTimestamp();
             */

            timestamp = "2015-5-09T14:37:17.404";

            try {
                System.out.println(timestamp);
                timestamp = polling(apiClient, timestamp);
                System.out.println(timestamp);

                DeviceApi deviceService=apiClient.createService(DeviceApi.class);
//                System.out.println("\n"+deviceService.get(Const.ID));
                System.out.println("\n"+deviceService.list(null,null,null,null,null,null,null,null,null,null,20,0));


            } catch (RetrofitError e) {
            e.printStackTrace();
            }
        }
    }

    private static String polling(ApiClient apiClient, String timestamp)  throws RetrofitError{

        DeviceApi deviceService = apiClient.createService(DeviceApi.class);
        DeviceUpdate device = createDevice();
        deviceService.register(device, Const.ID);

        DeviceCommandApi commandService = apiClient.createService(DeviceCommandApi.class);

        List<DeviceCommandItem> deviceCommandItems = commandService.poll(Const.ID, "", timestamp, 30L);
        if (deviceCommandItems.size() > 0) {
            timestamp = deviceCommandItems.get(0).getTimestamp();
            DeviceCommandItem dev = deviceCommandItems.get(0);
            DeviceCommandWrapper deviceCommandWrapper = getWrapper(dev);
            commandService.update(dev.getDeviceGuid(), dev.getId(), deviceCommandWrapper, new Callback<DeviceCommandItem>() {
                @Override
                public void success(DeviceCommandItem deviceCommandItem, Response response) {
                    System.out.println(response.getStatus());

                }

                @Override
                public void failure(RetrofitError error) {
                    System.out.println(error.toString());
                }
            });



        }
        return timestamp;
    }

    private static DeviceCommandWrapper getWrapper(DeviceCommandItem deviceCommandItem) {
        DeviceCommandWrapper deviceCommandWrapper = new DeviceCommandWrapper();
        deviceCommandWrapper.setCommand(deviceCommandItem.getCommand());
        deviceCommandWrapper.setParameters(deviceCommandItem.getParameters());
        return deviceCommandWrapper;
    }

    private static DeviceUpdate createDevice() {
        DeviceUpdate device = new DeviceUpdate();
        device.setName(Const.NAME);
        device.setStatus(Const.STATUS);
        DeviceClassUpdate deviceClass = new DeviceClassUpdate();
        deviceClass.setName(Const.DC_NAME);
        deviceClass.setVersion(Const.DC_VERSION);
        device.setDeviceClass(deviceClass);

        return device;
    }
}
<commit_msg>DeviceExample refactoring<commit_after>package examples;

import com.devicehive.client.ApiClient;
import com.devicehive.client.api.ApiInfoApi;
import com.devicehive.client.api.DeviceApi;
import com.devicehive.client.api.DeviceCommandApi;
import com.devicehive.client.model.*;
import com.google.common.base.Strings;
import retrofit.RetrofitError;

import java.net.MalformedURLException;
import java.util.List;

public class DeviceExample {


    public static void main(String[] args) throws MalformedURLException {
        
        ApiClient apiClient = new ApiClient(Const.URL, ApiClient.AUTH_API_KEY, Const.MY_API_KEY);

        String timestamp = "";

        if (Strings.isNullOrEmpty(timestamp)) {

            ApiInfoApi infoService = apiClient.createService(ApiInfoApi.class);
            ApiInfo apiInfo = infoService.getApiInfo();
            timestamp = apiInfo.getServerTimestamp();

            /**FIXME "2015-5-09T14:37:17.404" test data to get list of command
             timestamp = "2015-5-09T14:37:17.404";
             */

            try {
                List<DeviceCommandItem> deviceCommandItems = polling(apiClient, timestamp);
                if (deviceCommandItems.size() > 0) {
                    DeviceCommandItem newest = deviceCommandItems.get(0);
                    timestamp = newest.getTimestamp();
                    udpateCommand(apiClient, newest);
                }
                System.out.println(timestamp);

            } catch (RetrofitError e) {
                e.printStackTrace();
            }
        }
    }


    //Data polling
    private static List<DeviceCommandItem> polling(ApiClient apiClient, String timestamp) throws RetrofitError {
        
        DeviceApi deviceService = apiClient.createService(DeviceApi.class);
        DeviceUpdate device = createDevice();
        deviceService.register(device, Const.ID);
        DeviceCommandApi commandService = apiClient.createService(DeviceCommandApi.class);
        
        return commandService.poll(Const.ID, "", timestamp, 30L);
    }

    //Command updating
    private static void udpateCommand(ApiClient apiClient, DeviceCommandItem newest) {
        
        Long id = newest.getId();
        String guid = newest.getDeviceGuid();
        DeviceCommandWrapper wrapper = getWrapper(newest);
        DeviceCommandApi commandService = apiClient.createService(DeviceCommandApi.class);
        commandService.update(guid, id, wrapper);
    }

    //Command wrapping
    private static DeviceCommandWrapper getWrapper(DeviceCommandItem deviceCommandItem) {
        DeviceCommandWrapper deviceCommandWrapper = new DeviceCommandWrapper();
        deviceCommandWrapper.setCommand(deviceCommandItem.getCommand());
        deviceCommandWrapper.setParameters(deviceCommandItem.getParameters());
        return deviceCommandWrapper;
    }

    //Device creating
    private static DeviceUpdate createDevice() {
        DeviceUpdate device = new DeviceUpdate();
        device.setName(Const.NAME);
        device.setStatus(Const.STATUS);
        DeviceClassUpdate deviceClass = new DeviceClassUpdate();
        deviceClass.setName(Const.DC_NAME);
        deviceClass.setVersion(Const.DC_VERSION);
        device.setDeviceClass(deviceClass);

        return device;
    }
}

{'fraction_non_alphanumeric': 0.0923728813559322, 'fraction_numerical': 0.010734463276836158, 'mean_word_length': 2.8337845154304278, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1202549', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1773, 'n_words': 336}