Search in sources :

Example 1 with SyncHelper

use of com.cpjd.roblu.sync.SyncHelper in project Roblu by wdavies973.

the class Service method loop.

/**
 * This is the main background service looper, this should perform any necessary
 * Roblu Cloud sync operations
 */
public void loop() {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
    StrictMode.setThreadPolicy(policy);
    if (!Utils.hasInternetConnection(getApplicationContext())) {
        Log.d("RBS", "No internet connection detected. Ending loop() early.");
        return;
    }
    /*
         * Create all the utilities we need for this loop
         */
    IO io = new IO(getApplicationContext());
    RSettings settings = io.loadSettings();
    RSyncSettings cloudSettings = io.loadCloudSettings();
    Request r = new Request(settings.getServerIP());
    ObjectMapper mapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    CloudTeamRequest teamRequest = new CloudTeamRequest(r, settings.getCode());
    CloudCheckoutRequest checkoutRequest = new CloudCheckoutRequest(r, settings.getCode());
    boolean result = r.ping();
    if (result)
        Utils.requestServerHealthRefresh(getApplicationContext(), "online");
    else
        Utils.requestServerHealthRefresh(getApplicationContext(), "offline");
    if (!result) {
        Log.d("RBS", "Roblu server is down. Unable to connect.");
        return;
    }
    // Load the active event
    REvent[] events = io.loadEvents();
    REvent activeEvent = null;
    for (int i = 0; events != null && events.length > 0 && i < events.length; i++) {
        if (events[i].isCloudEnabled()) {
            activeEvent = events[i];
            break;
        }
    }
    if (activeEvent != null && activeEvent.getReadOnlyTeamNumber() != -1) {
        teamRequest.setTeamNumber(activeEvent.getReadOnlyTeamNumber());
        teamRequest.setCode("");
        checkoutRequest.setTeamNumber(activeEvent.getReadOnlyTeamNumber());
        checkoutRequest.setTeamCode("");
    }
    /*
         * Check if a purge is requested
         */
    if (cloudSettings.isPurgeRequested() && checkoutRequest.purge()) {
        cloudSettings.setPurgeRequested(false);
        cloudSettings.setTeamSyncID(0);
        cloudSettings.getCheckoutSyncIDs().clear();
        Log.d("RBS", "Event successfully purged from Roblu Cloud.");
        io.saveCloudSettings(cloudSettings);
        Notify.notifyNoAction(getApplicationContext(), "Event purged", "Active event successfully removed from Roblu Cloud.");
        return;
    }
    if (activeEvent == null)
        return;
    // Create the sync helper
    SyncHelper syncHelper = new SyncHelper(getApplicationContext(), activeEvent, SyncHelper.MODES.NETWORK);
    RForm form = io.loadForm(activeEvent.getID());
    /*
         * Check to see if the form was modified and needs to be uploaded
         */
    if (form.isUploadRequired()) {
        try {
            teamRequest.pushForm(mapper.writeValueAsString(form));
            form.setUploadRequired(false);
            io.saveForm(activeEvent.getID(), form);
            Notify.notifyNoAction(getApplicationContext(), "Form uploaded", "Successfully uploaded RForm to the server.");
            Log.d("RBS-Service", "Successfully uploaded RForm to the server.");
        } catch (Exception e) {
            Log.d("RBS-Service", "Failed to complete an upload required request for RForm.");
        }
    }
    /*
         * Check to see if the UI model should be uploaded
         */
    if (settings.getRui().isUploadRequired()) {
        try {
            teamRequest.pushUI(mapper.writeValueAsString(settings.getRui()));
            settings.getRui().setUploadRequired(false);
            io.saveSettings(settings);
            Log.d("RBS-Service", "Successfully uploaded RUI to the server.");
        } catch (Exception e) {
            Log.d("RBS-Service", "Failed to complete an upload required request for RUI.");
        }
    }
    /*
         * Check for cloud team updates
         */
    try {
        CloudTeam t = teamRequest.getTeam(cloudSettings.getTeamSyncID());
        if (t != null) {
            /*
                 * If a different master app overwrites the cloud app with a different event, run this check to prevent conflicts
                 * from happening.
                 */
            if (t.getActiveEventName() != null && !t.getActiveEventName().equals("") && activeEvent.getName() != null && !t.getActiveEventName().equals(activeEvent.getName())) {
                activeEvent.setCloudEnabled(false);
                cloudSettings.getCheckoutSyncIDs().clear();
                io.saveCloudSettings(cloudSettings);
                io.saveEvent(activeEvent);
                return;
            }
            // Merge RForm
            form = mapper.readValue(t.getForm(), RForm.class);
            form.setUploadRequired(false);
            io.saveForm(activeEvent.getID(), form);
            // Merge RUI
            RUI rui = mapper.readValue(t.getUi(), RUI.class);
            rui.setUploadRequired(false);
            settings.setRui(rui);
            // make sure to refresh this
            settings = io.loadSettings();
            io.saveSettings(settings);
            // Update the sync ID
            cloudSettings.setTeamSyncID((int) t.getSyncID());
            io.saveCloudSettings(cloudSettings);
            Log.d("RBS-Service", "Successfully pulled team data from the server.");
        }
    } catch (Exception e) {
        Log.d("RBS-Service", "Failed to pull team data from the server: " + e.getMessage());
    }
    /*
         *
         * Alright, into the belly of the beast.
         * This code will check for completed checkouts on the server and merge them with the local repository.
         * Shall we begin?
         *
         */
    try {
        CloudCheckout[] checkouts = checkoutRequest.pullCompletedCheckouts(syncHelper.packSyncIDs(cloudSettings.getCheckoutSyncIDs()));
        syncHelper.unpackCheckouts(checkouts, cloudSettings);
        io.saveCloudSettings(cloudSettings);
    } catch (Exception e) {
        Log.d("RBS-Service", "An error occurred while fetching completed checkouts. " + e.getMessage());
    }
    /*
         * Next, uploading everything from /pending/
         */
    try {
        Log.d("RBS-Service", "Checking for any checkouts to upload...");
        ArrayList<RCheckout> checkouts = new ArrayList<>(Arrays.asList(io.loadPendingCheckouts()));
        boolean wasSuccess = checkoutRequest.pushCheckouts(syncHelper.packCheckouts(checkouts));
        if (wasSuccess) {
            for (RCheckout checkout : checkouts) {
                io.deletePendingCheckout(checkout.getID());
            }
            Notify.notifyNoAction(getApplicationContext(), "Uploaded new checkouts", "Uploaded " + checkouts.size() + " new checkout(s).");
        }
        Log.d("RBS-Service", "Uploaded " + checkouts.size() + " checkouts.");
    } catch (Exception e) {
        Log.d("RBS-Service", "An error occurred while attempting to push /pending/ checkouts: " + e.getMessage());
    }
    io.saveCloudSettings(cloudSettings);
    Log.d("RBS-Service", "Sleeping Roblu background service for 10 seconds...");
}
Also used : IO(com.cpjd.roblu.io.IO) RUI(com.cpjd.roblu.models.RUI) Request(com.cpjd.http.Request) CloudTeamRequest(com.cpjd.requests.CloudTeamRequest) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest) ArrayList(java.util.ArrayList) StrictMode(android.os.StrictMode) RForm(com.cpjd.roblu.models.RForm) CloudTeamRequest(com.cpjd.requests.CloudTeamRequest) REvent(com.cpjd.roblu.models.REvent) SyncHelper(com.cpjd.roblu.sync.SyncHelper) CloudTeam(com.cpjd.models.CloudTeam) RCheckout(com.cpjd.roblu.models.RCheckout) RSyncSettings(com.cpjd.roblu.models.RSyncSettings) CloudCheckout(com.cpjd.models.CloudCheckout) RSettings(com.cpjd.roblu.models.RSettings) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest)

Example 2 with SyncHelper

use of com.cpjd.roblu.sync.SyncHelper in project Roblu by wdavies973.

the class QrReader method onQRCodeRead.

// Called when a QR is decoded
// "text" : the text encoded in QR
// "points" : points where QR control points are placed in View
@Override
public void onQRCodeRead(final String text, PointF[] points) {
    if (syncedAlready)
        return;
    if (!syncedAlready) {
        syncedAlready = true;
    }
    Log.d("RBS", "QR Read: " + text);
    /*
             * Decompress and import the checkout
             */
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                /*
                     * Import the checkout!
                     */
                RCheckout checkout = new CheckoutEncoder().decodeCheckout(text);
                new SyncHelper(getApplicationContext(), event, SyncHelper.MODES.QR).mergeCheckout(checkout);
                // Flag for upload
                // reload the team after merge
                checkout.setTeam(new IO(getApplicationContext()).loadTeam(event.getID(), checkout.getTeam().getID()));
                new IO(getApplicationContext()).savePendingCheckout(checkout);
                Notify.notifyMerged(getApplicationContext(), event.getID(), checkout);
                // close QR scanner
                finish();
            } catch (Exception e) {
                Log.d("RBS", "Failed to import checkout from QR code: " + e.getMessage());
            }
        }
    }).start();
}
Also used : IO(com.cpjd.roblu.io.IO) SyncHelper(com.cpjd.roblu.sync.SyncHelper) RCheckout(com.cpjd.roblu.models.RCheckout)

Example 3 with SyncHelper

use of com.cpjd.roblu.sync.SyncHelper in project Roblu by wdavies973.

the class InitPacker method doInBackground.

@Override
protected Boolean doInBackground(Void... params) {
    /*
         * Make sure this thread has network permissions
         */
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
    StrictMode.setThreadPolicy(policy);
    Log.d("RBS", "Executing InitPacker task...");
    IO io = ioWeakReference.get();
    RSettings settings = io.loadSettings();
    RSyncSettings cloudSettings = io.loadCloudSettings();
    cloudSettings.setPurgeRequested(false);
    io.saveCloudSettings(cloudSettings);
    io.saveSettings(settings);
    Request r = new Request(settings.getServerIP());
    if (!r.ping()) {
        listener.statusUpdate("It appears as though the server is offline. Try again later.");
        return false;
    }
    /*
         * Load all teams from the event, also make sure that that the teams are verified
         */
    REvent event = io.loadEvent(eventID);
    event.setReadOnlyTeamNumber(-1);
    RForm form = io.loadForm(eventID);
    RTeam[] teams = io.loadTeams(eventID);
    if (event == null || form == null || teams == null || teams.length == 0) {
        Log.d("RBS", "Not enough data to warrant an event upload.");
        listener.statusUpdate("This event doesn't contain any teams or sufficient data to upload to the server. Create some teams!");
        return false;
    }
    // Generate the checkouts
    SyncHelper syncHelper = new SyncHelper(io, event, SyncHelper.MODES.NETWORK);
    ArrayList<RCheckout> checkouts = syncHelper.generateCheckoutsFromEvent(teams, -1);
    // Remove field data
    try {
        for (RCheckout checkout : checkouts) {
            for (RTab tab : checkout.getTeam().getTabs()) {
                for (RMetric metric : tab.getMetrics()) {
                    if (metric instanceof RFieldData) {
                        ((RFieldData) metric).setData(null);
                    }
                }
            }
        }
    } catch (Exception e) {
    // Doesn't matter
    }
    /*
         * Convert into JSON and upload
         */
    ObjectMapper mapper = new ObjectMapper();
    try {
        // serialization all the checkouts and pack them in an json array, this will be processed by the server
        String serializedCheckouts = syncHelper.packCheckouts(checkouts);
        String serializedForm = mapper.writeValueAsString(form);
        String serializedUI = mapper.writeValueAsString(settings.getRui());
        String eventName = event.getName();
        if (eventName == null)
            eventName = "";
        if (event.getKey() == null)
            event.setKey("");
        CloudCheckoutRequest ccr = new CloudCheckoutRequest(r, settings.getCode());
        Log.d("RBS", "Initializing init packer upload...");
        boolean success = ccr.init(settings.getTeamNumber(), eventName, serializedForm, serializedUI, serializedCheckouts, event.getKey());
        /*
             * Disable all other events with cloud syncing enabled
             */
        if (success) {
            REvent[] events = io.loadEvents();
            for (int i = 0; events != null && i < events.length; i++) {
                events[i].setCloudEnabled(events[i].getID() == eventID);
                io.saveEvent(events[i]);
            }
            cloudSettings.getCheckoutSyncIDs().clear();
            /*
                 * Add default sync ids
                 */
            for (RCheckout checkout : checkouts) {
                cloudSettings.getCheckoutSyncIDs().put(checkout.getID(), 0L);
            }
            io.saveCloudSettings(cloudSettings);
            io.saveSettings(settings);
        } else
            listener.statusUpdate("An error occurred. Event was not uploaded.");
        return success;
    } catch (Exception e) {
        Log.d("RBS", "An error occurred in InitPacker: " + e.getMessage());
        listener.statusUpdate("An error occurred. Event was not uploaded.");
        return false;
    } finally {
        /*
             * Set all images to null to return memory to normal
             */
        for (RCheckout checkout : checkouts) {
            for (RTab tab : checkout.getTeam().getTabs()) {
                for (RMetric metric : tab.getMetrics()) {
                    if (metric instanceof RGallery) {
                        ((RGallery) metric).setImages(null);
                    }
                }
            }
        }
    }
}
Also used : RTab(com.cpjd.roblu.models.RTab) RGallery(com.cpjd.roblu.models.metrics.RGallery) RFieldData(com.cpjd.roblu.models.metrics.RFieldData) RMetric(com.cpjd.roblu.models.metrics.RMetric) StrictMode(android.os.StrictMode) RForm(com.cpjd.roblu.models.RForm) REvent(com.cpjd.roblu.models.REvent) SyncHelper(com.cpjd.roblu.sync.SyncHelper) RCheckout(com.cpjd.roblu.models.RCheckout) RSyncSettings(com.cpjd.roblu.models.RSyncSettings) RSettings(com.cpjd.roblu.models.RSettings) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest) RTeam(com.cpjd.roblu.models.RTeam) IO(com.cpjd.roblu.io.IO) Request(com.cpjd.http.Request) CloudCheckoutRequest(com.cpjd.requests.CloudCheckoutRequest)

Example 4 with SyncHelper

use of com.cpjd.roblu.sync.SyncHelper in project Roblu by wdavies973.

the class BTServer method run.

/**
 * Starts the sync task
 */
@Override
public void run() {
    /*
         * Load the active Bluetooth event
         */
    IO io = new IO(bluetooth.getActivity());
    REvent[] events = io.loadEvents();
    if (events == null || events.length == 0) {
        bluetooth.getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(bluetooth.getActivity(), "No events found. Please create an event to enable Bluetooth syncing.", Toast.LENGTH_LONG).show();
            }
        });
        pd.dismiss();
        return;
    }
    for (REvent event : events) {
        if (event.isBluetoothEnabled()) {
            this.event = event;
            break;
        }
    }
    if (this.event == null) {
        bluetooth.getActivity().runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(bluetooth.getActivity(), "No active Bluetooth event found. Please enable Bluetooth in event settings to enable Bluetooth syncing.", Toast.LENGTH_LONG).show();
            }
        });
        pd.dismiss();
        return;
    }
    this.syncHelper = new SyncHelper(bluetooth.getActivity(), this.event, SyncHelper.MODES.BLUETOOTH);
    if (bluetooth.isEnabled()) {
        bluetooth.startServer();
    } else
        bluetooth.enable();
}
Also used : IO(com.cpjd.roblu.io.IO) REvent(com.cpjd.roblu.models.REvent) SyncHelper(com.cpjd.roblu.sync.SyncHelper)

Aggregations

IO (com.cpjd.roblu.io.IO)4 SyncHelper (com.cpjd.roblu.sync.SyncHelper)4 RCheckout (com.cpjd.roblu.models.RCheckout)3 REvent (com.cpjd.roblu.models.REvent)3 StrictMode (android.os.StrictMode)2 Request (com.cpjd.http.Request)2 CloudCheckoutRequest (com.cpjd.requests.CloudCheckoutRequest)2 RForm (com.cpjd.roblu.models.RForm)2 RSettings (com.cpjd.roblu.models.RSettings)2 RSyncSettings (com.cpjd.roblu.models.RSyncSettings)2 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 CloudCheckout (com.cpjd.models.CloudCheckout)1 CloudTeam (com.cpjd.models.CloudTeam)1 CloudTeamRequest (com.cpjd.requests.CloudTeamRequest)1 RTab (com.cpjd.roblu.models.RTab)1 RTeam (com.cpjd.roblu.models.RTeam)1 RUI (com.cpjd.roblu.models.RUI)1 RFieldData (com.cpjd.roblu.models.metrics.RFieldData)1 RGallery (com.cpjd.roblu.models.metrics.RGallery)1 RMetric (com.cpjd.roblu.models.metrics.RMetric)1