Monday, August 22, 2016

Gulp command not found on Mac OSX

Was getting following error after installing gulp on mac.

Sachins-MBP:MyApp sachin$ gulp

-bash: gulp: command not found

After fighting for hours and searching on internet, tuned out to be a very simple fix. While installing gulp missed the parameter to install globally.

Sachins-MBP:MyApp sachin$ npm install -g gulp

Yay !!!! Its working, thought to write to save time for somebody else.

Monday, August 15, 2016

MQTT - Eclipse Paho integration for Android

For MQTT integration, recently explored Paho Android project, very simple to use, here are the steps:

Intialize a client, set required options and connect.

    MqttAndroidClient mqttClient = new MqttAndroidClient(BaseApplication.getAppContext(), broker, MQTT_CLIENT_ID);
    //Set call back class
    mqttClient.setCallback(new MqttCallbackHandler(BaseApplication.getAppContext()));
    MqttConnectOptions connOpts = new MqttConnectOptions();
    IMqttToken token = mqttClient.connect(connOpts);
   

Subscribe to a topic.

    token.setActionCallback(new IMqttActionListener() {
      @Override
      public void onSuccess(IMqttToken arg0) {
           mqttClient.subscribe("TOPIC_NAME" + userId, 2, null, new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    Log.d(LOG_TAG, "Successfully subscribed to topic.");
                }

                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    Log.d(LOG_TAG, "Failed to subscribed to topic.");
                }
            });
      }
   
      @Override
      public void onFailure(IMqttToken arg0, Throwable arg1) {
           Log.d(LOG_TAG, errorMsg);
      }
    });

Define your callback handler class.

    public class MqttCallbackHandler implements MqttCallbackExtended {
        @Override
            public void connectComplete(boolean b, String s) {
                Log.w("mqtt", s);
            }
   
            @Override
            public void connectionLost(Throwable throwable) {
   
            }
   
            @Override
            public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
                Log.w("Anjing", mqttMessage.toString());
            }
   
            @Override
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
   
            }
      }
             
Also set following in manifest file.

    <service android:name="org.eclipse.paho.android.service.MqttService" >
    </service>

Would recommend visiting their project on GitHub and going through samples online.

https://github.com/eclipse/paho.mqtt.android

Hoping this helps.

Cheers !


Monday, November 3, 2014

Parsing mailing address in Java

Recently came across a problem to parse postal address String using Java, did some research here are some findings.

Given
String s = "60 Strawberry Hill Ave Stamford CT 06901"
Required Result
Street = Strawberry Hill Ave
City = Stamford
Number = 60
zip = 06901
state = CT
You can use JGeocoder

public static void main(String[] args) {
Map<AddressComponent, String> parsedAddr  =     AddressParser.parseAddress("Google Inc, 1600 Amphitheatre Parkway, Mountain View, CA 94043");
System.out.println(parsedAddr);
     
Map<AddressComponent, String> normalizedAddr  = AddressStandardizer.normalizeParsedAddress(parsedAddr);
        System.out.println(normalizedAddr);
}
Output will be:

{street=Amphitheatre, city=Mountain View, number=1600, zip=94043, state=CA, name=Google Inc, type=Parkway}
{street=AMPHITHEATRE, city=MOUNTAIN VIEW, number=1600, zip=94043, state=CA, name=GOOGLE INC, type=PKWY}

There is another library International Address Parser , it provides a trial version. It supports country as well.


AddressParser addressParser = AddressParser.getInstance();
AddressStandardizer standardizer = AddressStandardizer.getInstance();  
AddressFormater formater = AddressFormater.getInstance();
String rawAddress = "101 Avenue des Champs-Elysées 75008 Paris";

//you can try to detect the country
CountryDetector detector = CountryDetector.getInstance();
String countryCode = detector.getCountryCode("7580 Commerce Center Dr ALABAMA");
System.out.println("detected country=" + countryCode);

Also, please check Implemented Countries

Hope you find this helpful

Cheers !!