Skip to content

Commit

Permalink
Goto & sync
Browse files Browse the repository at this point in the history
  • Loading branch information
farom57 committed May 24, 2015
1 parent 40f46d4 commit 4c446e3
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 108 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/farom.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

193 changes: 179 additions & 14 deletions app/src/main/java/farom/iparcos/SearchActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package farom.iparcos;


import android.app.Activity;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
Expand All @@ -21,20 +21,46 @@
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import farom.iparcos.catalog.Catalog;
import farom.iparcos.catalog.CatalogEntry;
import farom.iparcos.catalog.Coordinates;
import laazotea.indi.Constants;
import laazotea.indi.client.INDIDevice;
import laazotea.indi.client.INDIDeviceListener;
import laazotea.indi.client.INDINumberElement;
import laazotea.indi.client.INDINumberProperty;
import laazotea.indi.client.INDIProperty;
import laazotea.indi.client.INDIPropertyListener;
import laazotea.indi.client.INDIServerConnection;
import laazotea.indi.client.INDIServerConnectionListener;
import laazotea.indi.client.INDISwitchElement;
import laazotea.indi.client.INDISwitchProperty;



/**
* Allow the user to search for an astronomical object and display the result.
*/
public class SearchActivity extends ListActivity implements MenuItem.OnActionExpandListener, SearchView.OnQueryTextListener, AdapterView.OnItemClickListener {
public class SearchActivity extends ListActivity implements MenuItem.OnActionExpandListener, SearchView.OnQueryTextListener, AdapterView.OnItemClickListener,INDIServerConnectionListener, INDIPropertyListener,
INDIDeviceListener {

ArrayAdapter<CatalogEntry> adapter;
private ArrayList<CatalogEntry> entries;
private Catalog catalog;

// INDI properties
private INDINumberProperty telescopeCoordP = null;
private INDINumberElement telescopeCoordRA = null;
private INDINumberElement telescopeCoordDE = null;
private INDISwitchProperty telescopeOnCoordSetP = null;
private INDISwitchElement telescopeOnCoordSetSync = null;
private INDISwitchElement telescopeOnCoordSetSlew = null;


/**
* Called at the activity creation. Disable opening animation and load default content.
* @param savedInstanceState
Expand All @@ -44,6 +70,7 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(0, 0);

// list setup
entries = new ArrayList<CatalogEntry>();
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, entries) {
@Override
Expand All @@ -59,7 +86,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
};
setListAdapter(adapter);


// List loading
final Context act = this;
new Thread(new Runnable(){
@Override
Expand All @@ -70,6 +97,25 @@ public void run() {
}
}).start(); // TODO : faire plus propre avec Cursor et Loader

// Set up INDI connection
ConnectionActivity.getInstance().registerPermanentConnectionListener(this);

// Enumerate existing properties
INDIServerConnection connection = ConnectionActivity.getConnection();
if (connection != null) {
List<INDIDevice> list = connection.getDevicesAsList();
if (list != null) {
for (Iterator<INDIDevice> it = list.iterator(); it.hasNext();) {
INDIDevice device = it.next();
device.addINDIDeviceListener(this);
List<INDIProperty> properties = device.getPropertiesAsList();
for (Iterator<INDIProperty> it2 = properties.iterator(); it2.hasNext();) {
this.newProperty(device, it2.next());
}
}
}
}

getListView().setOnItemClickListener(this);
}

Expand Down Expand Up @@ -180,21 +226,53 @@ public boolean onQueryTextSubmit(String query) {
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
final Context ctx = view.getContext();
final Coordinates coord = entries.get(position).getCoordinates();
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setMessage(entries.get(position).createDescription(this))
.setTitle(entries.get(position).getName());
builder.setPositiveButton(R.string.GOTO,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
builder.setNeutralButton(R.string.sync,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Only display buttons if the telescope is ready
if(telescopeCoordP!=null && telescopeOnCoordSetP!=null){
builder.setPositiveButton(R.string.GOTO,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
telescopeOnCoordSetSlew.setDesiredValue(Constants.SwitchStatus.ON);
telescopeOnCoordSetSync.setDesiredValue(Constants.SwitchStatus.OFF);
telescopeOnCoordSetP.sendChangesToDriver();
telescopeCoordRA.setDesiredValue(coord.getRaStr());
telescopeCoordDE.setDesiredValue(coord.getDeStr());
telescopeCoordP.sendChangesToDriver();
Toast toast = Toast.makeText(ctx, ctx.getString(R.string.slew_ok), Toast.LENGTH_LONG);
toast.show();
} catch (Exception e) {
Toast toast = Toast.makeText(ctx, ctx.getString(R.string.sync_slew_error), Toast.LENGTH_LONG);
toast.show();
}

}
});
builder.setNeutralButton(R.string.sync,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
telescopeOnCoordSetSlew.setDesiredValue(Constants.SwitchStatus.ON);
telescopeOnCoordSetSync.setDesiredValue(Constants.SwitchStatus.OFF);
telescopeOnCoordSetP.sendChangesToDriver();
telescopeCoordRA.setDesiredValue(coord.getRaStr());
telescopeCoordDE.setDesiredValue(coord.getDeStr());
telescopeCoordP.sendChangesToDriver();
Toast toast = Toast.makeText(ctx, ctx.getString(R.string.sync_ok), Toast.LENGTH_LONG);
toast.show();
} catch (Exception e) {
Toast toast = Toast.makeText(ctx, ctx.getString(R.string.sync_slew_error), Toast.LENGTH_LONG);
toast.show();
}
}
});
}

}
});
builder.setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Expand All @@ -204,6 +282,93 @@ public void onClick(DialogInterface dialog, int which) {
AlertDialog dialog = builder.create();
dialog.show();
}

@Override
public void newProperty(INDIDevice device, INDIProperty property) {
// Look for properties

if (property.getName().equals("ON_COORD_SET")) {
telescopeOnCoordSetSlew = (INDISwitchElement) property.getElement("TRACK");
if(telescopeOnCoordSetSlew==null){telescopeOnCoordSetSync = (INDISwitchElement) property.getElement("SLEW");}
telescopeOnCoordSetSync = (INDISwitchElement) property.getElement("SYNC");

if (telescopeOnCoordSetSlew != null && telescopeOnCoordSetSync != null) {
property.addINDIPropertyListener(this);
telescopeOnCoordSetP = (INDISwitchProperty) property;
Log.i("SearchActivity", "New Property (" + property.getName() + ") added to device " + device.getName());
}else{
Log.w("SearchActivity", "Bad property: " + property.getName() + ", device: " + device.getName());
}
}

if (property.getName().equals("EQUATORIAL_COORD")) {
telescopeCoordRA = (INDINumberElement) property.getElement("RA");
telescopeCoordDE = (INDINumberElement) property.getElement("DE");

if (telescopeCoordDE!= null && telescopeCoordRA != null) {
property.addINDIPropertyListener(this);
telescopeCoordP = (INDINumberProperty) property;
Log.i("SearchActivity", "New Property (" + property.getName() + ") added to device " + device.getName());
}else{
Log.w("SearchActivity", "Bad property: " + property.getName() + ", device: " + device.getName());
}
}
}

@Override
public void removeProperty(INDIDevice device, INDIProperty property) {
if (property.getName().equals("ON_COORD_SET")) {
telescopeCoordP = null;
telescopeCoordRA = null;
telescopeCoordDE = null;
}
if (property.getName().equals("EQUATORIAL_COORD")) {
telescopeOnCoordSetP = null;
telescopeOnCoordSetSlew = null;
telescopeOnCoordSetSync = null;
}
Log.d("SearchActivity", "Removed property (" + property.getName() + ") to device " + device.getName());
}

@Override
public void messageChanged(INDIDevice device) {

}

@Override
public void propertyChanged(INDIProperty property) {

}

@Override
public void newDevice(INDIServerConnection connection, INDIDevice device) {
// We just simply listen to this Device
Log.i("SearchActivity", getString(R.string.new_device) + device.getName());
device.addINDIDeviceListener(this);
}

@Override
public void removeDevice(INDIServerConnection connection, INDIDevice device) {
// We just remove ourselves as a listener of the removed device
Log.i("SearchActivity", getString(R.string.device_removed) + device.getName());
device.removeINDIDeviceListener(this);
}

@Override
public void connectionLost(INDIServerConnection connection) {

telescopeCoordP = null;
telescopeCoordRA = null;
telescopeCoordDE = null;
telescopeOnCoordSetP = null;
telescopeOnCoordSetSlew = null;
telescopeOnCoordSetSync = null;
}

@Override
public void newMessage(INDIServerConnection connection, Date timestamp, String message) {

}
}


31 changes: 15 additions & 16 deletions app/src/main/java/farom/iparcos/catalog/CatalogEntry.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
package farom.iparcos.catalog;

import android.content.Context;
import android.content.res.Resources;
import android.text.Spannable;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
* An astronomical object
*/
public abstract class CatalogEntry implements Comparable<CatalogEntry> {

protected Coordinates coord;
protected String name;


/**
* Return the object coordinates
* @return
* @return coordinates
*/
public abstract Coordinates getCoordinates();
public Coordinates getCoordinates(){ return coord; }


/**
* Return the object name
* @return
* @return name
*/
public abstract String getName();
public String getName(){
return name;
}


/**
* Create the description rich-text string
* @return
*
* @param ctx Context (to access resource strings)
* @return description Spannable
*/
public abstract Spannable createDescription(Context ctx);

/**
* Create the summary rich-text string (1 line)
* @return
*
* @param ctx Context (to access resource strings)
* @return summary Spannable
*/
public abstract Spannable createSummary(Context ctx);

Expand All @@ -60,7 +59,7 @@ public abstract class CatalogEntry implements Comparable<CatalogEntry> {
*/
@Override
public int compareTo(CatalogEntry another) {
return getName().compareToIgnoreCase(another.getName());
return this.getName().compareToIgnoreCase(another.getName());
}
}

Expand Down
Loading

0 comments on commit 4c446e3

Please sign in to comment.