working on sockets on/off bug

parent 900acc08
......@@ -3,12 +3,6 @@ apply plugin: 'com.android.application'
android {
signingConfigs {
release_config {
/*
storeFile file('F:/Users/Fernando/GoogleDrive/tmp/keystore.jks')
keyAlias 'default_key'
keyPassword 'danoia'
storePassword 'danoia'
*/
}
}
compileSdkVersion 21
......
......@@ -249,6 +249,7 @@ public class RestapiWrapper {
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(HttpAsyncTaskParams params) {
Log.i(this.getClass().getCanonicalName(),"PICTO END RESTAPI POSTEXE " +params.url+":"+params.url_params.toString());
try {
//if (params.e)
if(params.result!=null) {
......@@ -269,7 +270,7 @@ public class RestapiWrapper {
} catch (JSONException e) {
params.listener.error(e);
}
params.url_params.clear();
if (params.url_params!=null) params.url_params.clear();
}
}
}
\ No newline at end of file
......@@ -19,7 +19,9 @@ import com.yottacode.pictogram.tools.PCBcontext;
import java.sql.SQLDataException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
......@@ -36,8 +38,9 @@ public class Room {
protected SailsSocketsIO socket=null;
protected boolean inRoom=false;
protected Hashtable<String, Emitter.Listener> listeners;
public Room( ) {
listeners=new Hashtable<>();
reconnect();
}
......@@ -87,6 +90,7 @@ public class Room {
public void call(Object... args) {
Log.i(this.getClass().getName(), "Reconnect successful");
subscribe();
listen_again();
inRoom=true;
}
}, new Emitter.Listener() {
......@@ -101,8 +105,13 @@ public class Room {
}
public boolean inRoom() {return this.inRoom;}
public void listen(String msg, Emitter.Listener listener) {
this.socket.registerMessage(msg, listener);
this.listeners.put(msg, listener);
}
private void listen_again() {
for (String msg: this.listeners.keySet())
this.socket.registerMessage(msg, this.listeners.get(msg));
}
void subscribe() {
try {
SubscribeAction action = new SubscribeAction();
......
......@@ -38,7 +38,6 @@ public class Device extends SQLiteOpenHelper {
Context context;
final static class PARAMS {
static String keyword="key";
static String token="token";
static String deviceID="deviceID";
static String serial="serial";
static String stu_id="last__stu_id";
......@@ -56,7 +55,7 @@ public class Device extends SQLiteOpenHelper {
super(context, DeviceHelper.getDBName(context), factory, version);
if (DeviceHelper.force_create(context)) {
Log.i(this.getClass().getCanonicalName(),"Forcing create new database");
Log.i(this.getClass().getCanonicalName(),"Forcing create new Database "+DeviceHelper.getDBName(context));
context.deleteDatabase(DeviceHelper.getDBName(context));
Log.i(this.getClass().getCanonicalName(), "Database dropped");
}
......
......@@ -202,17 +202,17 @@ public class PCBDBHelper extends SQLiteOpenHelper {
SQLiteDatabase db = this.getWritableDatabase();
int id_stu = this.getCurrentUser().get_id_stu();
int seconds1 = Calendar.getInstance().get(Calendar.SECOND), inserts=0;
int deleted=db.delete("collection", "id_stu=?", new String[] {String.valueOf(id_stu)});
db.delete("collection", "id_stu=?", new String[] {String.valueOf(id_stu)});
ContentValues values=new ContentValues(5);
values.put("id_stu", id_stu);
for (Picto picto : vocabulary) {
values.put("id_picto", picto.get_id());
values.put("id_picto", picto.get_id());
values.put("url", picto.get_url());
values.put("translation",picto.get_translation());
values.put("attributes",picto.get_json_attrs());
db.insert("collection_detail", null, values);
}
int seconds2 = Calendar.getInstance().get(Calendar.SECOND);
Log.i(this.getClass().getName(), " Local student vocabulary updated, id:" + id_stu + ", cats: "+vocabulary.size() +" time:"+(seconds2-seconds1));
db.close();
......@@ -263,7 +263,10 @@ public class PCBDBHelper extends SQLiteOpenHelper {
public void modifyPicto(int picto_id, String attrs) {
int id_stu = this.getCurrentUser().get_id_stu();
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("UPDATE collection SET attributes='" + attrs + "' WHERE id_stu=" + id_stu + " AND id_picto=" + picto_id);
ContentValues values = new ContentValues(1);
values.put("attributes",attrs);
int updates=db.update("collection",values, "id_stu=? AND id_picto=?", new String[] {Integer.toString(id_stu), Integer.toString(picto_id)});
Log.i(this.getClass().getCanonicalName(),"Modify "+updates+" read only db? "+db.isReadOnly()+" Picto attributes: "+picto_id+" attributes="+attrs);
db.close();
}
......
......@@ -326,6 +326,7 @@ public class Picto extends Img {
e.printStackTrace();
Log.e(this.getClass().getCanonicalName(),e.getMessage());
}
PCBcontext.getActionLog().log(new VocabularyAction(VocabularyAction.ALTERATTRS,this));
return is_enabled();
}
......@@ -344,13 +345,16 @@ public class Picto extends Img {
if (modified)
try {
this.attributes.put(JSON_ATTTRS.PCB_STATUS_MODIFICATION, true);
PCBcontext.getPcbdb().modifyPicto(this.get_id(), this.get_json_attrs());
PCBcontext.getActionLog().log(new VocabularyAction(VocabularyAction.ALTERATTRS, this));
} catch (JSONException e) {
e.printStackTrace();
Log.e(this.getClass().getCanonicalName(), e.getMessage());
}
else
else {
this.attributes.remove(JSON_ATTTRS.PCB_STATUS_MODIFICATION);
PCBcontext.getPcbdb().modifyPicto(this.get_id(), this.get_json_attrs());
}
}
}
\ No newline at end of file
......@@ -333,6 +333,7 @@ public class Vocabulary implements Iterable<Picto> {
e.printStackTrace();
Log.e(Vocabulary.class.getCanonicalName(), e.getMessage());
}
PCBcontext.getActionLog().log(new VocabularyAction(VocabularyAction.ADD, picto));
return picto;
}
......
......@@ -23,7 +23,7 @@ public class VocabularyIterator implements Iterator<Picto> {
this.keys = pictos.keys();
if (pictos.size()>1) {
this.pictos=pictos;
this.category = pictos.keys().nextElement();
this.category = this.keys.nextElement();
}
else {
......
......@@ -55,7 +55,7 @@ public class NetService implements Runnable {
}
});
Log.i(this.getClass().getName(), "Checking Pictogram server access...");
Log.d(this.getClass().getName(), this.updated ? "Pictogram server access ok" : "Pictogram server access failed, Internet connection available?");
Log.i(this.getClass().getName(), this.updated ? "Pictogram server access ok" : "Pictogram server access failed, Internet connection available?");
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.scheduleWithFixedDelay(this, 0, delay, TimeUnit.SECONDS);
}
......@@ -87,12 +87,13 @@ public class NetService implements Runnable {
if (result == null) {
updated = false;
} else if (!updated) {
Log.i(this.getClass().getName(), "PCB reconnect");
PCBcontext.getRoom().reconnect();
PCBcontext.getVocabulary().synchronize();
PCBcontext.getActionLog().batch();
updated = true;
}
Log.i(this.getClass().getName(), "PCB status checked. Updated? " + updated);
Log.i(this.getClass().getName(), "PCB status checked. Updated? " + updated+ " in room?"+PCBcontext.getRoom().inRoom());
}
@Override
......
......@@ -34,44 +34,6 @@ public class PictoUploader {
this.picto=picto;
}
/**
* if the status of a given picto was modifed from the PCB it is uploaded to the server
*
*/
public void uploadState( ){
Hashtable<String, String> params = new Hashtable<String, String>(1);
params.put("attributes", picto.get_json_attrs());
params.put("id_stu", Integer.toString(PCBcontext.getPcbdb().getCurrentUser().get_id_stu()));
params.put("id_pic", Integer.toString(this.picto.get_id()));
picto.status_modified(false);
Log.i(this.getClass().getCanonicalName(), "Uploading " + params.toString());
PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu()
+ "/picto"
, params, "put", new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
}
@Override
public void result(JSONObject result) {
Log.i(this.getClass().getCanonicalName(), "Uploaded state result: " + result.toString());
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(), "Error: " + e.getLocalizedMessage());
picto.status_modified(true);
}
}
);
PCBcontext.getActionLog().log(new VocabularyAction(VocabularyAction.ALTERATTRS,this.picto));
}
private int uploadImg( Img img) throws UnsupportedEncodingException {
int img_id;
if (!img.get_filetype().equalsIgnoreCase("png"))
......@@ -201,13 +163,59 @@ public class PictoUploader {
*Upload local picto. It requires: i) to upload the image, ii) to upload the attributes and iii) to upload the expression
**/
public void upload() throws IOException {
int img_id=uploadImg(this.picto);
if (img_id>0) {
uploadAttributes(img_id);
uploadTranslation(img_id);
PCBcontext.getActionLog().log(new VocabularyAction(VocabularyAction.ADD, this.picto));
if (PCBcontext.getNetService().online()) {
int img_id = uploadImg(this.picto);
if (img_id > 0) {
uploadAttributes(img_id);
uploadTranslation(img_id);
}
}
}
/**
* if the status of a given picto was modifed from the PCB it is uploaded to the server
*
*/
public void uploadState( ){
Hashtable<String, String> params = new Hashtable<String, String>(1);
params.put("attributes", picto.get_json_attrs());
params.put("id_stu", Integer.toString(PCBcontext.getPcbdb().getCurrentUser().get_id_stu()));
params.put("id_pic", Integer.toString(this.picto.get_id()));
if (PCBcontext.getNetService().online()) {
Log.i(this.getClass().getCanonicalName(), "Uploading " + params.toString());
PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu()
+ "/picto"
, params, "put", new iRestapiListener() {
@Override
public void preExecute() {
}
@Override
public void result(JSONArray result) {
Log.i(this.getClass().getCanonicalName(), "Upload Picto ok" + PictoUploader.this.picto.get_id());
picto.status_modified(false);
}
@Override
public void result(JSONObject result) {
Log.i(this.getClass().getCanonicalName(), "Upload ok Picto " + PictoUploader.this.picto.get_id());
picto.status_modified(false);
}
@Override
public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(), "Picto Error: " + e.getLocalizedMessage());
picto.status_modified(true);
}
}
);
}
}
}
......@@ -13,18 +13,12 @@
android:contentDescription="@string/app_name"
android:orientation="vertical"
android:src="@drawable/yottalogo72p"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="presenta..."
android:id="@+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
android:scaleX="0.5"
android:scaleY="0.5"
android:id="@+id/imageView2"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="44dp" />
<ImageView
android:layout_width="200px"
......@@ -32,19 +26,10 @@
android:contentDescription="@string/app_name"
android:orientation="vertical"
android:src="@drawable/logo_pictogram"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="120dp"
android:id="@+id/imageView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Pictogram"
android:id="@+id/textView3"
android:paddingTop="70dp"
android:layout_alignBottom="@+id/imageView"
android:id="@+id/imageView"
android:scaleX="1.5"
android:scaleY="1.5"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
......@@ -53,7 +53,8 @@ constraint ck_users UNIQUE(id_stu,id_sup)
CREATE TABLE collection (
id_stu INTEGER NOT NULL REFERENCES student ON DELETE CASCADE,
id_picto INTEGER NOT NULL REFERENCES picto ON DELETE CASCADE,
attributes VARCHAR(1024)
attributes VARCHAR(1024),
constraint ck_collection UNIQUE(id_stu,id_picto)
)
;--
......@@ -202,7 +203,7 @@ CREATE TRIGGER trg_delete_collection_detail
INSTEAD OF DELETE ON collection_detail
FOR EACH ROW
BEGIN
DELETE FROM collection WHERE id_picto = OLD.id_picto ;
DELETE FROM collection WHERE id_picto = OLD.id_picto AND id_stu = OLD.id_stu;
END
;--
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment