Search in sources :

Example 1 with Leg

use of org.hwyl.sexytopo.model.survey.Leg in project sexytopo by richsmith.

the class DistoXPoller method slurpAllData.

public List<Leg> slurpAllData(BluetoothSocket socket) {
    Log.device(context.getString(R.string.device_log_waiting));
    List<Leg> legs = new ArrayList<>();
    DataInputStream inStream = null;
    DataOutputStream outStream = null;
    try {
        inStream = new DataInputStream(socket.getInputStream());
        outStream = new DataOutputStream(socket.getOutputStream());
        while (isAlive) {
            byte[] packet = new byte[8];
            inStream.readFully(packet, 0, 8);
            Log.d("Received data: " + DistoXProtocol.describeDataPacket(packet));
            byte type = (byte) (packet[0] & 0x3f);
            byte[] acknowledgePacket = DistoXProtocol.createAcknowledgementPacket(packet);
            outStream.write(acknowledgePacket, 0, acknowledgePacket.length);
            Log.d("Sent Ack: " + DistoXProtocol.describeAcknowledgementPacket(acknowledgePacket));
            if ((packet[0] & 0x03) == 0) {
                // Think this means the acknowledgment has been accepted
                break;
            } else if (type != 0x01) {
                Log.device("Unexpected data type");
                continue;
            }
            if (arePacketsTheSame(packet, previousPacket)) {
                continue;
            } else {
                Log.device(context.getString(R.string.device_log_received));
                Leg leg = DistoXProtocol.parseDataPacket(packet);
                legs.add(leg);
                previousPacket = packet;
                continue;
            }
        // pauseForDistoXToCatchUp();
        }
    } catch (IOException e) {
        if (e.getMessage().toLowerCase().contains("bt socket closed")) {
            Log.d("Connection closed");
            disconnect();
        } else {
            Log.device("Communication error: " + e.getMessage());
            disconnect();
        }
    } catch (Exception e) {
        Log.device("General error: " + e);
        disconnect();
    } finally {
        try {
            inStream.close();
            outStream.close();
        } catch (Exception e) {
        // ignore any errors; they are expected if the socket has been closed
        }
        // whatever happens, return the precious data :)
        return legs;
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) IOException(java.io.IOException) Leg(org.hwyl.sexytopo.model.survey.Leg)

Example 2 with Leg

use of org.hwyl.sexytopo.model.survey.Leg in project sexytopo by richsmith.

the class DistoXProtocol method parseDataPacket.

public static Leg parseDataPacket(byte[] dataPacket) {
    int d0 = (int) (dataPacket[ADMIN] & 0x40);
    int d1 = (int) (dataPacket[DISTANCE_LOW_BYTE] & 0xff);
    if (d1 < 0)
        d1 += 256;
    int d2 = (int) (dataPacket[DISTANCE_HIGH_BYTE] & 0xff);
    if (d2 < 0)
        d2 += 256;
    // double d =  (((int)mBuffer[0]) & 0x40) * 1024.0 + (mBuffer[1] & 0xff) * 1.0 + (mBuffer[2] & 0xff) * 256.0;
    // in mm
    double distance = (d0 * 1024 + d2 * 256 + d1 * 1) / 1000.0;
    int b3 = (int) (dataPacket[AZIMUTH_LOW_BYTE] & 0xff);
    if (b3 < 0)
        b3 += 256;
    int b4 = (int) (dataPacket[AZIMUTH_HIGH_BYTE] & 0xff);
    if (b4 < 0)
        b4 += 256;
    // double b = (mBuffer[3] & 0xff) + (mBuffer[4] & 0xff) * 256.0;
    double b = b3 + b4 * 256.0;
    double azimuth = b * 180.0 / 32768.0;
    int c5 = (int) (dataPacket[INCLINATION_LOW_BYTE] & 0xff);
    if (c5 < 0)
        c5 += 256;
    int c6 = (int) (dataPacket[INCLINATION_HIGH_BYTE] & 0xff);
    if (c6 < 0)
        c6 += 256;
    // double c = (mBuffer[5] & 0xff) + (mBuffer[6] & 0xff) * 256.0;
    double c = c5 + c6 * 256.0;
    // 90/0x4000;
    double inclination = c * 90.0 / 16384.0;
    if (c >= 32768) {
        inclination = (65536 - c) * (-90.0) / 16384.0;
    }
    int r7 = (int) (dataPacket[7]);
    if (r7 < 0)
        r7 += 256;
    // double r = (mBuffer[7] & 0xff);
    double r = r7;
    double roll = r * 180.0 / 128.0;
    Leg leg = new Leg(distance, azimuth, inclination);
    return leg;
}
Also used : Leg(org.hwyl.sexytopo.model.survey.Leg)

Example 3 with Leg

use of org.hwyl.sexytopo.model.survey.Leg in project sexytopo by richsmith.

the class TableActivity method onMenuItemClick.

@Override
public boolean onMenuItemClick(MenuItem menuItem) {
    final TableCol col = fieldToTableCol.get(cellBeingClicked);
    final GraphToListTranslator.SurveyListEntry surveyEntry = fieldToSurveyEntry.get(cellBeingClicked);
    switch(menuItem.getItemId()) {
        case R.id.setActiveStation:
            Station newActive = (Station) (GraphToListTranslator.createMap(surveyEntry).get(col));
            getSurvey().setActiveStation(newActive);
            syncTableWithSurvey();
            return true;
        case R.id.renameStation:
            Station toRename = (Station) (GraphToListTranslator.createMap(surveyEntry).get(col));
            if (toRename == Survey.NULL_STATION) {
                showSimpleToast("Can't rename a splay end");
            } else {
                ManualEntry.renameStation(this, getSurvey(), toRename);
            }
            return true;
        case R.id.editLeg:
            Leg toEdit = surveyEntry.getLeg();
            ManualEntry.editLeg(this, getSurvey(), toEdit);
            return true;
        case R.id.upgradeRow:
            Leg toUpgrade = surveyEntry.getLeg();
            SurveyUpdater.upgradeSplayToConnectedLeg(getSurvey(), toUpgrade);
            syncTableWithSurvey();
            return true;
        case R.id.deleteRow:
            int legsToBeDeleted = SurveyStats.calcNumberSubLegs(surveyEntry.getFrom());
            int stationsToBeDeleted = SurveyStats.calcNumberSubStations(surveyEntry.getFrom());
            String detailMessage = "This will delete\n" + TextTools.pluralise(legsToBeDeleted, "leg") + " and " + TextTools.pluralise(stationsToBeDeleted, "station");
            String message = (legsToBeDeleted > 1 || stationsToBeDeleted > 1) ? detailMessage : getString(R.string.delete_row_question);
            new AlertDialog.Builder(this).setMessage(message).setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SurveyUpdater.deleteLeg(getSurvey(), surveyEntry.getLeg());
                    syncTableWithSurvey();
                }
            }).setNegativeButton(R.string.cancel, null).show();
            return true;
        default:
            return false;
    }
}
Also used : Station(org.hwyl.sexytopo.model.survey.Station) GraphToListTranslator(org.hwyl.sexytopo.control.util.GraphToListTranslator) DialogInterface(android.content.DialogInterface) TableCol(org.hwyl.sexytopo.model.table.TableCol) Leg(org.hwyl.sexytopo.model.survey.Leg)

Example 4 with Leg

use of org.hwyl.sexytopo.model.survey.Leg in project sexytopo by richsmith.

the class TableActivity method onLongClick.

@Override
public boolean onLongClick(View view) {
    TextView textView = (TextView) view;
    cellBeingClicked = textView;
    TableCol col = fieldToTableCol.get(textView);
    if (col == TableCol.FROM || col == TableCol.TO) {
        showPopup(view, R.menu.table_station_selected, this);
    } else {
        final GraphToListTranslator.SurveyListEntry surveyEntry = fieldToSurveyEntry.get(cellBeingClicked);
        Leg leg = surveyEntry.getLeg();
        if (leg.hasDestination()) {
            showPopup(view, R.menu.table_full_leg_selected, this);
        } else {
            showPopup(view, R.menu.table_splay_selected, this);
        }
    }
    return true;
}
Also used : GraphToListTranslator(org.hwyl.sexytopo.control.util.GraphToListTranslator) TableCol(org.hwyl.sexytopo.model.table.TableCol) TextView(android.widget.TextView) Leg(org.hwyl.sexytopo.model.survey.Leg)

Example 5 with Leg

use of org.hwyl.sexytopo.model.survey.Leg in project sexytopo by richsmith.

the class GraphView method drawLegs.

private void drawLegs(Canvas canvas, Space<Coord2D> space, int alpha) {
    Map<Leg, Line<Coord2D>> legMap = space.getLegMap();
    for (Leg leg : legMap.keySet()) {
        if (!getDisplayPreference(GraphActivity.DisplayPreference.SHOW_SPLAYS) && !leg.hasDestination()) {
            continue;
        }
        Line<Coord2D> line = legMap.get(leg);
        Coord2D start = surveyCoordsToViewCoords(line.getStart());
        Coord2D end = surveyCoordsToViewCoords(line.getEnd());
        if (PreferenceAccess.getBoolean(getContext(), "pref_key_highlight_latest_leg", true) && survey.getMostRecentLeg() == leg) {
            legPaint.setColor(LATEST_LEG_COLOUR.intValue);
        } else if (!leg.hasDestination()) {
            legPaint.setColor(SPLAY_COLOUR.intValue);
        } else {
            legPaint.setColor(LEG_COLOUR.intValue);
        }
        legPaint.setAlpha(alpha);
        if (projectionType.isLegInPlane(leg)) {
            legPaint.setStyle(Paint.Style.STROKE);
            canvas.drawLine((float) (start.getX()), (float) (start.getY()), (float) (end.getX()), (float) (end.getY()), legPaint);
        } else {
            legPaint.setPathEffect(new DashPathEffect(new float[] { 3, 2 }, 0));
            Path path = new Path();
            path.moveTo((float) (start.getX()), (float) (start.getY()));
            path.lineTo((float) (end.getX()), (float) (end.getY()));
            canvas.drawPath(path, legPaint);
        }
    }
}
Also used : Line(org.hwyl.sexytopo.model.graph.Line) Path(android.graphics.Path) DashPathEffect(android.graphics.DashPathEffect) Coord2D(org.hwyl.sexytopo.model.graph.Coord2D) Leg(org.hwyl.sexytopo.model.survey.Leg)

Aggregations

Leg (org.hwyl.sexytopo.model.survey.Leg)58 Station (org.hwyl.sexytopo.model.survey.Station)18 Test (org.junit.Test)17 Survey (org.hwyl.sexytopo.model.survey.Survey)12 Coord3D (org.hwyl.sexytopo.model.graph.Coord3D)8 AlertDialog (android.app.AlertDialog)5 Coord2D (org.hwyl.sexytopo.model.graph.Coord2D)5 Line (org.hwyl.sexytopo.model.graph.Line)5 Dialog (android.app.Dialog)4 ArrayList (java.util.ArrayList)4 Space (org.hwyl.sexytopo.model.graph.Space)4 TextView (android.widget.TextView)3 GraphToListTranslator (org.hwyl.sexytopo.control.util.GraphToListTranslator)3 TableCol (org.hwyl.sexytopo.model.table.TableCol)3 DialogInterface (android.content.DialogInterface)2 DashPathEffect (android.graphics.DashPathEffect)1 Path (android.graphics.Path)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 DataInputStream (java.io.DataInputStream)1