/** * This file was auto-generated by the Titanium Module SDK helper for Android * Appcelerator Titanium Mobile * Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved. * Licensed under the terms of the Apache Public License * Please see the LICENSE included with this distribution for details. * */ package it.uhopper.mqtt; import it.uhopper.mqtt.service.MqttConstants; import it.uhopper.mqtt.service.MqttService; import java.util.HashMap; import org.appcelerator.kroll.KrollFunction; import org.appcelerator.kroll.KrollModule; import org.appcelerator.kroll.annotations.Kroll; import org.appcelerator.kroll.common.Log; import org.appcelerator.titanium.TiApplication; import android.content.Context; import android.content.Intent; @Kroll.module(name="Mqtt", id="it.uhopper.mqtt") public class MqttModule extends KrollModule { // Standard Debugging variables private static final String TAG = MqttConstants.TAG; private static final String PROPERTY_ON_SUCCESS = "success"; private static final String PROPERTY_ON_ERROR = "error"; private static final String PROPERTY_ON_NOTIFICATION = "callback"; //notification callback // You can define constants with @Kroll.constant, for example: // @Kroll.constant public static final String EXTERNAL_NAME = value; private static KrollFunction onSuccessCallback; private static KrollFunction onErrorCallback; private static KrollFunction onNotificationCallback; // public static ConcurrentHashMap<String, FunctionData> eventsMap = new ConcurrentHashMap<String, FunctionData>(); public static HashMap<String, KrollFunction> eventsMap = null; private static Intent i; private static MqttModule instance = null; public static MqttModule getInstance() { return instance; } public static Context ctx = null; public MqttModule() { super(); onSuccessCallback = null; onErrorCallback = null; onNotificationCallback = null; instance = this; eventsMap = new HashMap<String, KrollFunction>(); ctx = TiApplication.getAppCurrentActivity(); Log.d(TAG, " =============== getAppCurrentActivity =================="); // EventThread.startEventThread(); } @Kroll.onAppCreate public static void onAppCreate(TiApplication app) { Log.d(TAG, "inside onAppCreate"); // put module init code that needs to run when the application is created } // Methods @Kroll.method public void registerCallback(String id, Object obj) { final String str_url = id; System.out.println("URL: " + str_url ); @SuppressWarnings("unchecked") HashMap<String, Object> kd = (HashMap<String, Object>) obj; Object pOnSuccessCallback = kd.get(PROPERTY_ON_SUCCESS); Object pOnErrorCallback = kd.get(PROPERTY_ON_ERROR); Object pOnNotificationCallback = kd.get(PROPERTY_ON_NOTIFICATION); if (pOnSuccessCallback instanceof KrollFunction) { onSuccessCallback = (KrollFunction) pOnSuccessCallback; addEvent("onSuccess", onSuccessCallback); }else{ return; } if (pOnErrorCallback instanceof KrollFunction) { onErrorCallback = (KrollFunction) pOnErrorCallback; addEvent("onError", onErrorCallback); }else{ return; } if (pOnNotificationCallback instanceof KrollFunction) { onNotificationCallback = (KrollFunction) pOnNotificationCallback; addEvent("onNotification", onNotificationCallback); }else{ return; } //TODO start service here i = new Intent(ctx, MqttService.class); i.setAction(MqttConstants.START); i.putExtra("ID", id); i.putExtra("TOPICS", new String[]{id+"/to"}); ctx.startService(i); // addEventListener("success", ); } @Kroll.method public void publishData(String id, String data){ Log.d(TAG,"publishData into " + MqttConstants.HOST); i = new Intent(ctx, MqttService.class); i.setAction(MqttConstants.PUBLISH); i.putExtra("TOPIC", id+"/from"); i.putExtra("PAYLOAD", data); ctx.startService(i); } // Methods @Kroll.method public void unregisterForNotification(Object obj) { @SuppressWarnings("unchecked") HashMap<String, Object> kd = (HashMap<String, Object>) obj; Object pOnSuccessCallback = kd.get(PROPERTY_ON_SUCCESS); Object pOnErrorCallback = kd.get(PROPERTY_ON_ERROR); if (pOnSuccessCallback instanceof KrollFunction) { onSuccessCallback = (KrollFunction) pOnSuccessCallback; }else{ return; } if (pOnErrorCallback instanceof KrollFunction) { onErrorCallback = (KrollFunction) pOnErrorCallback; }else{ return; } if(i != null){ if(ctx.stopService(i)){ fireSuccess("Service stopped"); }else{ fireError("Service could not stop"); } } else{ fireSuccess("Service is already stopped"); } } @Kroll.method public void subscribeToTopic(String topic){ i = new Intent(ctx, MqttService.class); i.setAction(MqttConstants.SUBSCRIBE); i.putExtra("TOPIC", topic); ctx.startService(i); } @Kroll.method public void unSubscribeToTopic(String topic){ i = new Intent(ctx, MqttService.class); i.setAction(MqttConstants.UNSUBSCRIBE); i.putExtra("TOPIC", topic); ctx.startService(i); } @Kroll.method public void addEvent(String eventName, Object obj){ Log.i(TAG, obj + ""); if(obj != null && obj instanceof KrollFunction){ KrollFunction kFunc = (KrollFunction) obj; eventsMap.put(eventName, kFunc); }else{ Log.e("MODULE", "error: not a kroll function"); } } @Kroll.method public void removeEvent(String eventName){ eventsMap.remove(eventName); } public void fireSuccess(String response) { if (onSuccessCallback != null) { HashMap<String, String> result = new HashMap<String, String>(); result.put("data", response); onSuccessCallback.call(getKrollObject(), result); } } public void fireError(String error) { if (onErrorCallback != null) { HashMap<String, String> result = new HashMap<String, String>(); result.put(PROPERTY_ON_ERROR, error); onErrorCallback.call(getKrollObject(), result); } } public void fireNotification(String notification) { if (onNotificationCallback != null) { Log.i(TAG, "message: "+ notification); HashMap<String, String> result = new HashMap<String, String>(); result.put(PROPERTY_ON_NOTIFICATION, notification); onNotificationCallback.call(getKrollObject(), result); } } }