Sunday, September 29, 2013

How to Add Barcode Scanning to your Web App using Xamarin

Recently I had a client who wanted to add barcode scanning capabilities to their web app. Their web app was already responsive thanks to Bootstrap but there isn't a convenient way to scan a bar code from mobile Safari or Chrome.

This turned out to be fairly trivial thanks to Xamarin and two components in their Component Store:
JsBridge - eases Native to Javascript communication
ZXing.Net.Mobile - handles the barcode scanning

Here are some screenshots of what the web app looks like when accessed via the iOS app:



One of the requirements of the bar code scanning was to not show the Scan button if the user is not using the native app. To do this I customize the UserAgent of the UIWebView and then do UserAgent sniffing on the server side to hide or show the Scan button.

You'll notice if you view the web app in a browser the Scan button is not displayed:
http://xamarinbarcodesample.apphb.com/
To enable this functionality in the iOS app we append a XamarinBarcodeSampleApp string to the default UserAgent of the UIWebView:

Then in the web app we do UserAgent sniffing for this string.

To wire up the Scan button we take advantage of JsBridge. The first thing we do is register the mt.js library. Secondly we listen for the scanComplete event which is triggered on the iOS app side. Thirdly we handle the Scan button click event and let the iOS side know about it by firing the scanBarcode event.

On the iOS side we need to enable JsBridge, then listen for the scanBarcode event. When scanBarcode is fired we display the ZXing.Net.Mobile scanner. If the user successfully scans a barcode we fire the scanComplete event and pass the barcode string as an event parameter.

The native / hybrid iOS app source code can be downloaded here: 
https://github.com/crdeutsch/Xamarin.iOS.BarcodeSample

The web app source code is here:
https://github.com/crdeutsch/Xamarin.Web.BarcodeSample

Final thoughts:

Xamarin tools are flexible enough to handle the requirements of the Web Application developer dipping their toes into native development. 

In fact the Xamarin tools work so nicely with UIWebView based web apps that I'd urge any developer looking at PhoneGap or Titanium to add Xamarin to your review list and avoid the limitations you may run into. 

For instance, I built a series of HTML5 based game for a client that play audio via Flash in a desktop browser. To make them work on an iPad I used Titanium to martial the audio playback to native code. At the time I figured Titanium was the best solution since the bulk of the code was already Javascript. I eventually ran into limitations with Titanium when I wanted to detect custom gestures and realized they don't have a 1 to 1 mapping with the iOS API like Xamarin does. I plan on porting this app to Xamarin in the near future to take advantage of more advanced native iOS functionality.

In a future blog post I will demonstrate how to use JsBridge and another library I wrote to martial audio calls to the native side like I did for those HTML5 games.

DISCLAIMER: I've been work full time for Xamarin for about 3 weeks as a web developer. The following experiences and opinions were developed during my 3 years as a freelance consultant.

Runaway Open Source Project - Lessons learned from developing HiSRC

TL;DR: Never reference files on servers you pay for in Open Source projects.

Today I noticed my monthly Amazon AWS bill was $20 more then normal.

Somehow 143GB of outbound transfer had happened in Sept. After an hour or two of investigation and deleting things out of buckets that may have caused it, I finally turned on logging and waited.

The logging revealed a plethora of requests for a file called 50K. I couldn't even remember what the file was there for. I thought maybe my account had been hacked. I finally Googled cdeutsch/50K and it immediately clicked.



Back in April of 2012 I helped developed the open source javascript library HiSRC. HiSRC checks for high resolution support and does a quick bandwidth test, if it detects either, high resolution images are served up dynamically on the client side.

Unfortunately I had placed the 50K test file in one of my Amazon S3 buckets and didn't change or remove that Url when making the library available publicly.

Fast forward to August 2013 and adoption of the HiSRC library has started to catch on by big names sites such as the following:
https://uber.com/
https://www.converse.com/
http://www.kia.com/us/en/vehicle/soul/2014/experience
http://www.tennis-warehouse.com/
http://balzac.montblanc.com/
http://www.rolandsands.com/mobile/bikes
https://www.bresicwhitney.com.au/

While it's flattering to have my nick cdeutsch appear in the source code of these sites, it's costing me money.

I've deleted the 50K file, but Amazon S3 will still charge me for the 403 errors that are now happening.

I'm waiting to hear back from Amazon on how to resolve this and am working on contacting the 3 biggest sites to get them to make the necessary changes.

Monday, June 17, 2013

What's The Best Structure for API Responses?

I learned something new while listening to Episode 12 of the Traffic and Weather podcast.

John Sheehan mentioned that when you return your XML or JSON from an API you should have an error envelop separate from the data envelop.

The main reason is if you're deserializing the output and mapping it to a class in a static language you can use error.message or error.code without namespace conflicts. This also gives you a consistent target for a given endpoint. John recommends avoiding returning one structure for success and another structure for error (something that I've been doing to date).

My take away is that John recommends structuring your return data like so:

Success:
{
  data: {
    id: 1,
    model: 'Mustang',
    make: 'Ford'
  },
  error: null
}

Error:
{
  error: {
    code: 3,
    message: 'Car not found'
  }
}

Continue reading to see the NOT RECOMMENDED way

The way I traditionally did it was without envelopes but the structure changes based on success vs error:

Success (the car object):
{
  id: 1,
  model: 'Mustang',
  make: 'Ford'
}
Error (the error object):
{
  code: 3,
  message: 'Car not found'
}

What I liked about my method is that you can deserialize directly to the class you want ("Car") without creating a wrapper class. For example, my way you don't have to have a class like this.

public class CarResponse 
{
  public Car Data { get; set; }
 
  public ApiError Error { get; set; }
}

The downside of my method is you have to do some extra work to parse errors into ApiError instead of Car.

I'm not 100% convinced I want to switch and have a bunch of response wrapper classes, but it's debatable whether there is another person more involved with APIs then John, so I'll probably be switching. ;)

Some questions I have for John's method (UPDATE: see comments for John's answers, my examples have been updated to reflect them).
  1. What's the recommendation for naming the envelop of the success data. Ex. "car" or always use something generic like "data"
  2. On success do you return the error envelop at all? If so, do you indicate a "no error" status code? Ex. "code: 0"

If you're working with APIs be sure to checkout John's excellent tool Runscope. It's good stuff.

Tuesday, November 27, 2012

Fixing 404 Errors for ASP.NET MVC Apps that Bundle Assets when Deployed to IIS

UPDATE 07/19/2013: As pointed out in the comments by Ray Moro, a better way to fix this may be to remove the extension from your bundle name. So /bundle/myscrips.js becomes /bundle/myscripts this will cause it to run the module without having to change the config settings below.

That's a mouthful of a title.

If you're using ScriptBundle and StyleBundle's in an ASP.NET MVC4 app and you're suddenly getting 404 errors even though EnableOptimizations is set to true make sure you have the following in your Web.Config in the system.webServer section:

<modules runAllManagedModulesForAllRequests="true">
  <remove name="BundleModule" />
  <add name="BundleModule" type="System.Web.Optimization.BundleModule" />
</modules>

I've wasted incalculable amounts of time on this issue, TWICE!!! I don't know why this isn't in the web.config by default or better yet not needed to use Bundling.

Sunday, October 14, 2012

Trip to San Francisco Guide

Here are my lessons learned after spending 9 days in San Francisco which is a follow up to my post Planning a trip to San Francisco where I solicited advice on what to do and where to eat.

Tips
  • Be prepared to do some walking, so bring comfortable shoes. Some days I walked up to 6 or 7 miles. There are also very steep hills/streets so keep that in mind before following the route your GPS gives you. The shortest distance may not be the easiest. For instance, it was brutal walking from the bay to Union Square via Mason St. Had I gone down a couple blocks I could have avoided the hill.
  • Biking across the Golden Gate Bridge over to Sausalito and taking a ferry back is a great idea. There are many places to rent bikes including Blazing Saddles.Walking to Sausalito is not such a great idea since there is a long stretch of walking on the side of road.
  • Sign up and try Uber. Much nicer service then a cab but typically more expensive.
  • Avoid walking through the Tenderloin, especially at night. Ask locals or your hotel concierge where that is if you can't figure it out.
  • Take advantage of the BART when you can. Great for getting from Union Square to the Mission or Pier 1. The best service I received while in San Francisco was from the vagrant lady who helped me buy my first BART ticket. She was a pro at working that thing. I should have given her a bigger tip considering the service I got elsewhere.
  • If you want to spend a whole day exploring the city do one of the open bus tours that allows you to hop-on and hop-off. The one I did had about 20 stops and another bus came about every 20 minutes, so for example you could hop-off and hang out at Haight Ashbury for a while and then continue on after you've spent some time there.
  • If you're going to visit Alcatraz and take the Cellhouse Audio Tour (recommended) allow for a good 3-4 hours minimum with travel time. Also, sign up in advance. I wanted to go on a Monday and it was sold out until Wednesday.
  • Be ware of the fog. If you're going to do something like take the open top bus tour or visit the Golden Gate, you want to do it on a clear day. The fog can come in and ruin the amazing scenery.
My Favorites
  • Zeitgeist - great beer selection reasonably priced. Great bloody marries too!
  • Mission Beach Cafe for weekend breakfast - Get a side of their bacon. I'm still thinking about it! There's usually a good hour wait to get in so be prepared for that. We put our name on the list and walked to Zeitgeist to get a bloody marry and timed it perfectly. 
  • Zero Zero - we had the Avocado Bruschetta, Geary pizza, Little Shells, and build your own desert sunday. It was all excellent. Some of the best food I had while I was there.
  • Fog Harbor Fish House - I ate lunch here and had an excellent view of Alcatraz and the Golden Gate. I also had dinner and it was a bit dark to take in the view but the food was still excellent both times.
  • Blue Bottle Coffee - excellent coffee
Disappointments
  • You can't get into a Giants game using a StubHub ticket/barcode that's on your phone. This is the city that arguably has more startups and innovation then the rest of the world combined AND charges $.10 for each grocery bag you use. I'm dumbfounded that I had to go to will call and have a paper ticket printed off.
  • Versus Minnesota, service (like at restaurants) is on average slower and less attentive, but relax you're on vacation. ;)






Sunday, September 23, 2012

Planning a Trip to San Francisco

UPDATE 10/14/2012: Here's a follow up on how the trip went.

I'll be taking my first trip to San Francisco, Sept 29 - Oct 8, 2012. I'm looking for recommendations for restaurants and attractions and some space to do some work/programming (coffee shop or coworking space).

Here is what I've crowd sourced from Twitter so far. Big thanks to johnsheehanjustinpeck, masteinhauser, mronge, Solome33, kaufenberg, matt_krieger, dberkholz, bassistance

Attractions / ToDo


Restaurants


Coffee Shops


Pubs


Transportation


Coworking


Sunday, September 16, 2012

Best Mug, Cups, and Water Bottle for Techies

Here are the four liquid receptacles that support my freelance web developer career. They're listed in order of usage.

(I need to build a special rack for these guys)

For water, I take my Camelbak Podium Chill Water Bottle everywhere. Home, co-working space, working out, trips. Tip of the hat to Donn Felker for doing the leg work on finding this excellent bottle. My only complaint is that it "talks" sometimes. If pressure builds up it will start making noise when it leaks out until you give it a little squeeze.


For coffee this Nissan Leak-Proof Travel Mug has been my goto for about 8 years. I own 3 of them and have bought at least 3 more as gifts. When closed this thing seals like no other. As good for road trips as it is for working around your laptop.



If you don't like the bulky handle, this other Travel Mug from Thermos Nissan is a great alternative recommended by my wife.



For tea, I have my third Thermos Nissan pick, a Tumbler with Infuser. I'm just getting into tea so there are probably some other great options I haven't discovered yet, but I've been really happy with this unit so far.



This summer I got addicted to Starbucks' iced coffee with a shot of white moca. Instead of paying $4 each day I decided to try make my own. First I tracked down the white mocha Starbucks uses. Next, since I use a Keurig for speed and convenience, I went with these Iced Coffee K-Cups. To complete the experience, my wife found these 16oz re-usable Starbucks cups with re-usable straws. To make the drink, put a couple of pumps of white chocolate in first, then fill to pretty much the top with ice, set the Keurig to the 8 oz setting, and brew directly onto the ice. The Iced K-Cups are specially formulated to brew stronger then regular K-Cups, so when the ice melts it balances out. Stir it up to blend in the white chocolate. Then I usually add a few more ice cubes if there is space.


Tuesday, August 28, 2012

Force Content Types to Json in .NET WebApi

I ran into a situation where I couldn't set the Content-Type header in the http request client I was using. (Crossrider's appAPI.request.post)

The client was either not setting a Content-Type or defaulting to application/x-www-form-urlencoded

If you put the code below in Application_Start you should be able to force form-urlencoded data to the JsonFormatter and by removing the XmlFormatter, Json will also be the default.

HttpConfiguration config = GlobalConfiguration.Configuration;
foreach (var mediaType in config.Formatters.FormUrlEncodedFormatter.SupportedMediaTypes)
{
    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(mediaType);
}
config.Formatters.Remove(config.Formatters.FormUrlEncodedFormatter);
config.Formatters.Remove(config.Formatters.XmlFormatter);

Saturday, August 18, 2012

Using Less and Twitter Bootstrap in ASP.NET MVC4

UPDATE 05/02/2013:

Changed ScriptBundle and StyleBundle to just Bundle, per Andrey Taritsyn:
Bundle Transformer it is not recommended to use together with the StyleBundle and ScriptBundle classes, because these classes already contain transformations (instances of the built-in minifiers: CssMinify and JsMinify). Use a Bundle class.

UPDATE 11/08/2012:
  1. Create a new MVC4 Internet Application
  2. Add the following Nuget Packages
  3. Rename /Content/site.css to /Content/site.less 
    • Edit BundleConfig.cs in the App_Start folder and update the css file name to site.less like so:
      bundles.Add(new var cssTransformer = new CssTransformer();
      var jsTransformer = new JsTransformer();
      var nullOrderer = new NullOrderer();
      
      var defaultScriptsBundle = new Bundle("~/bundles/default.js").Include(
                  "~/Scripts/jquery-{version}.js",
                   "~/Scripts/jquery-ui-{version}.js",
                  "~/Scripts/site.js");
      defaultScriptsBundle.Transforms.Add(jsTransformer);
      defaultScriptsBundle.Orderer = nullOrderer;
      bundles.Add(defaultScriptsBundle);
      
      var defaultStylesBundle = new Bundle("~/Content/default.css").Include("~/Content/site.less");
      defaultStylesBundle.Transforms.Add(cssTransformer);
      defaultStylesBundle.Orderer = nullOrderer;
      bundles.Add(defaultStylesBundle);
      
    • Add the following to the top of /Content/site.less in order to have access to all of the Bootstrap mixins:
      @import "less/bootstrap.less"; 
      body {
            padding-top: 60px;
            padding-bottom: 40px;
      }
      @import "less/responsive.less"; 
      

    =================== THE REST OF THIS POST IS OUT OF DATE ==========

    UPDATE 8/20/2012: I've created a Nuget package that performs the steps below. I'd suggest reviewing what the package does below and then running:
    Install-Package Twitter.Bootstrap.Less.MVC4

    ASP.NET MVC4 has a great new feature that can bundle and minify your CSS and Javascript files.

    But in order to get Twitter Bootstrap to work it takes a bit more work. Here are the steps that worked for me.

    1. Create a new MVC4 Internet Application
    2. Add the following Nuget Packages
    3. Create an Infrastructure folder and add the following two files: (hat tip to this Stackoverflow question)
    4. Rename /Content/site.css to /Content/site.less 
    5. Edit BundleConfig.cs in the App_Start folder and replace the default Content/css bundle with the following:
      var css = new Bundle("~/Content/css").Include("~/Content/site.less");
      css.Transforms.Add(new LessMinify());
      bundles.Add(css);
      

    6. Add the following to the top of /Content/site.less in order to have access to all of the Bootstrap mixins:
      @import "less/bootstrap.less";
      

    If you have "shared" .less files from other projects I found importing them in site.less right after boostrap.less to work pretty well. They'll have access to all the mixins.
    @import "less/bootstrap.less";
    @import "less/shared.less";
    

    The completed solution is available on Github here

    If there is a better/easier way to do this please let me know in the comments!




    Friday, August 3, 2012

    Passing Cookies to a Node.js REST Client

    In Node.js if you want to pass the current user's Cookies to a REST call your making from the server here's a simple way to do it:

    var cookies = _.map(req.cookies, function(val, key) {
        return key + "=" + encodeURIComponent(val);
    }).join("; ");
    
    rest.get("http://makerocketgonow.com/", {
        headers: {
            Cookie: cookies,
            "Content-Type": "application/json"
        }
      }).on("complete", function(data) {
        return console.log(data);
    });
    

    This example uses the popular Restler library, but I assume you could do something similar with other libraries or without using a library.

    CAUTION: I'm using encodeURIComponent on the cookie value. Ruby on Rails seems to expect their cookies to be encoded like this. On the other hand, I've looked throw a few Node.js libraries that deal with cookies and they don't seem to manipulate the value at all. So you can play with adding or removing encodeURIComponent.

    If you start to get errors when making the REST request, be sure to check the cookie encoding/format versus what Firebug sends on a normal request to the site. Pay particular attention to any special characters.

    Monday, July 16, 2012

    Using Mongoid 3 (MongoHQ or MongoLab) on Heroku

    Here's what you need to get Mongoid working on Heroku, if you're getting errors like:
    NoMethodError (undefined method `[]' for nil:NilClass)

    Make sure the following is in your gem file (reference).
    source 'http://rubygems.org' 
    ruby '1.9.3'
    gem  'rails', '3.2.3'

    If you get an error like undefined method `ruby' run the following:
    gem install bundler --pre

    A lot of places have outdate documentation for configuring your mongoid.yml file which may result in the following error:
    No database provided for session configuration: :default

    Your mongoid.yml file should look like the following.
    production:
      sessions:
        default:
          uri: <%= ENV['MONGOHQ_URL'] %>
          options:
            skip_version_check: true
            safe: true
    The ENV variable above is for MongoHQ. For MongoLab use ENV['MONGOLAB_URI'] 


    Big thanks to the guys in this StackOverflow thread for helping me put all this together!



    Sunday, May 13, 2012

    Add support for LESS to Node.js connect-assetmanager package

    The connect-assetmanager Node.js package allows you to combine multiple javascript and CSS files into one and also do other manipulations like minification.

    It's part of Mathias Pettersson's (mape) excellent node-express-boilerplate template.

    If you're also using LESS for CSS here is some code you can add to get connect-assetmanager to process you're LESS files:

    var assetsSettings;
    assetsSettings = {
      js: {
        route: /\/static\/js\/[a-z0-9]+\/.*\.js/,
        path: "./public/javascripts/",
        dataType: "javascript",
        files: ["libs/underscore-min.js" "libs/jquery.mobile-1.1.0.min.js", siteConf.uri + "/nowjs/now.js", "site.js"],
        debug: siteConf.debug,
        stale: !siteConf.debug
      },
      css: {
        route: /\/static\/css\/[a-z0-9]+\/.*\.css/,
        path: "./public/stylesheets/",
        dataType: "css",
        files: ["libs/jquery.mobile-1.1.0.min.css", "style.less"],
        debug: siteConf.debug,
        stale: !siteConf.debug,
        preManipulate: {
          "^": [
            function(file, path, index, isLast, callback) {
              var match;
              match = path.match(/\.less$/);
              if ((match != null) && match.length > 0) {
                return less.render(file, function(e, css) {
                  return callback(css);
                });
              } else {
                return callback(file);
              }
            }
          ]
        }
      }
    };
    

    The import part is the preManipulate line under css but I include my entire assetsSettings from a project to make it easier to understand.

    Tuesday, May 1, 2012

    MonoTouch JsBridge - Communicate with the UIWebView

    Today I'm open sourcing JsBridge for MonoTouch which allows for bidirectional communication between the javascript in your UIWebViews and your native C# code in your MonoTouch app. Go directly to GitHub for the documentation on how to use it.

    This project was inspired by doing a project using Appcelerator's Titanium. In fact the javascript used in JsBridge was taken directly from that project, so if you're used to using Titanium you should be right at home using JsBridge.

    At this time it requires MonoTouch 5.3.3, which as of 5/1/2012 is in Alpha, because JsBridge needs to register a custom url protocol using NSUrlProtocol.

    I'm not 100% satisfied with the implementation of the event listeners on the native side, so I'm open to suggestions on how to make it better. Ideally some day the Xamarin team will implement something like this in a future version. ;)

    I've already submitted my first app to use it the iOS AppStore and it relies heavily on bidirectional communication between javascript and native. The app is a remote control for Rdio's new UI. A nicely executed feature by Rdio (even if it came way after the chrome extension remote I made for their first UI ;) ), but unfortunately it doesn't work on iOS partially due to the Rdio servers not supporting the Websockets protocol that the UIWebView has. Using JsBridge I was able to implement the WebSocket connection on the native side and override the Rdio javascript calls that use WebSockets and pass them to the native side and vice versa. I'm quite pleased with the results so far. ;)

    Wednesday, March 28, 2012

    Solved: Video.Js Playback Error in IE9

    TL;DR:

    Make sure your server (including Amazon CloudFront) is setting the correct mime type when returning the video file.

    For .mp4 files the mime type should be video/mp4

    ISSUE:

    I spent about an hour baffled why IE9 wouldn't play an H.264 video that Chrome and Firefox played fine.

    The only lead I had was this message output by Video.js using console.log:

    Video Error[object Object] 

    What's more annoying is that IE9 doesn't let you inspect objects that are output by console.log. To get around that you can install the Firebug Lite bookmarklet. Unfortunately in this case the object doesn't give you much more to go on.

    I tried swapping videos and found it worked with the sample Video.js video, which usually indicates a mime type issue (which seems to be a recurring issue with Microsoft products).

    I was using Amazon CloudFront to server the videos (download style as opposed to streaming) and figured I couldn't set the mime type but it turns out you can:
    1. Go into S3 and find your video file.
    2. Right click and select Properties
    3. Select the Metadata tab
    4. In this case, set KeyContent-Type and Value = video/mp4
    5. Press Save button

    Wednesday, February 29, 2012

    MonoTouch.Dialog UIPicker

    In iOS a UIPicker looks like this:

    I'm working on an iPhone application that's built using MonoTouch.Dialog and after a full day of trying to get the MonoTouch.Dialog compatible Picker in ClanceyLib to work, I've decided to package up my work and release it on GitHub.

    The main issues with ClanceyLib is that it requires a heavily modified and out of date version of MonoTouch.Dialog. If you try to compile it with the built-in version of MonoTouch.Dialog you'll get the following two errors:

    /Users/guivho/Mono/ClanceyLib/ClanceysLib/MT.D/ButtonElement.cs(49,49):
    Error CS0115: `ClanceysLib.ButtonElement.GetCell(MonoTouch.Dialog.DialogViewController,
    MonoTouch.UIKit.UITableView)' is marked as an override but no suitable
    method found to override (CS0115) (ClanceysLib)


    /Users/guivho/Mono/ClanceyLib/ClanceysLib/MT.D/ComboBoxElement.cs(49,49):
    Error CS0115: `ClanceysLib.ComboBoxElement.GetCell(MonoTouch.Dialog.DialogViewController,
    MonoTouch.UIKit.UITableView)' is marked as an override but no suitable
    method found to override (CS0115) (ClanceysLib)


    Ripping out the elements you need from ClanceyLib wasn't as easy as I hoped on the first try but I now have it working along with some other improvements:
    • Updated it to hide the keyboard or picker when selecting different cells to edit.
    • Changed so the Items in the Picker list are UIView's for greater customization.
    • Actually has a sample of how to use it. ;)
    UPDATE 3/1/2012: It works out of the box with the version of MonoTouch.Dialog that is now packaged with MonoTouch. But, it will not dismiss the picker when selecting a different cell. To enable that feature it requires a custom version of MonoTouch.Dialog and you'll need to comment in 3 lines of code in PickerElement.cs.

    I sent a pull request to get my minor change to MonoTouch.Dialog included in the core. If/when it's pulled I will update PickerElement.cs to take advantage of it and the dependency on the custom version of MonoTouch.Dialog will be no more. ;)

    Thursday, February 9, 2012

    setTimeout Setting Mysterious Callback Parameters

    I got burned by this twice in the last month.

    Say you want to call the following function after 5 seconds.
    function doSomething(optionalParam) {
      // check if optional param passed in.
      if (optionalParam) {
        optionalParam.foo(); // this is where the error sometimes happens.
      }
      // do more stuff.
    }
    
    // delay 5 seconds.
    setTimeout(doSomething, 5000);
    

    Periodically I was getting undefined errors when trying to call foo(). After some googling, it turns out Gecko browsers have setTimeout pass back an extra parameter indicating the "actual lateness" of the timeout in milliseconds.
    https://developer.mozilla.org/en/DOM/window.setTimeout

    I haven't run into this with other browsers but to avoid the issue it's best to do the following when your callback functions have optional params (Not having optional params would obviously be even safer).
    // delay 5 seconds.
    setTimeout(function() {
      doSomething();
    }, 5000);
    

    Tuesday, October 25, 2011

    Using Siri For Home Automation

    I've created a Twilio, Node.js mash up that allows me to control appliances and the thermostat in my house using the iPhone 4S's Siri voice recognition.

    Here's a demo of turning on my bedroom fan.



    How does this work? From Siri to the end result the chain goes:
    1. Siri 
    2. Twilio SMS number
    3. Node web application
    4. Indigo Web Server
    5. Insteon thermostat/appliance

    Let's go through the setup in reverse order in more detail.


    Insteon and Indigo

    I purchased the following items last year from Amazon to add some basic home automation to my house.

    I have a Mac Mini I use as a server which is connected to the PowerLinc Modem via USB. The Indigo software communicates two-way with the Insteon devices in my home via the PowerLinc. In addition to a native iPhone app, the Indigo software has both a web interface and RESTful Api you can use to control your devices.


    Node.js Web Application

    The Node.js application is the middle man between Twilio and the Indigo web server. When Twilio POSTs the incoming SMS message to the Node app, it parses the message and determines the appropriate Api call to make on the Indigo web server. I wrote some semi-fuzzy logic so the phrases you use don't have to be exact.

    For hosting the Node app I picked Heroku because it's convenient to use and free.


    Twilio

    Setting up Twilio was super easy. I created a Twilio account, purchased a phone number for $1/month, and entered the Url of my Node app that receives the incoming SMS messages.


    Siri

    To make communicating with my Twilio phone number easy I added a contact called "Gladys" (could be anything but I'm a Portal fan) and associated the Twilio number with her.

    I can now control my appliances using the following commands:
    • Tell Gladys to set thermostat to 73
    • Tell Gladys to turn off the bedroom fan


    I originally wanted to turn this into a public Siri to Url web service, but I question the demand for such a thing considering trying to make this "generic" would take a lot of time. So if you're interested in adding Siri control to your own use case and don't have programming skills, I'm available for hire and can whip you up something to suite your exact needs. ;)

    UPDATE 10/28/2011:
    Big thanks to technabob for the coverage! He brought up a good point though, this could easily be faked. Here's a screen shot of my Heroku logs with debugging output on the left and Node.js code for the "fuzzy logic" on the right. Not indisputable evidence but I assure you it's working exactly like it does in the video. ;)

    Sunday, October 16, 2011

    Windows 7 Registry Entry to Search All File Contents

    If you do a search in Windows 7 you may notice that it didn't find some files that you know contain the search terms you are looking for.
    This is due to the What to search setting in Windows Explorer under Folder Options -> Search as pictured below.


    If you don't have Always search file names and contents selected Windows will only search the files it has indexed.

    You can change the setting using the UI as shown above but you can also set it by changing the following Registry setting (which is useful if you want to programmatically set if for users):

    [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Search\PrimaryProperties\UnindexedLocations]

    "SearchOnly"=dword:00000000

    Originally I tried to change the following key based on Scott Forsyth's solution for Windows XP and Windows Server 2003 but it didn't work for me on Windows 7:

    [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ContentIndex]

    "FilterFilesWithUnknownExtensions"=dword:00000001


    As a bonus tip, this is how I found where the Windows 7 registry key was.
    1. Open Registry Editor
    2. Right click the registry hive you suspect the setting to be in (usually either HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE) and select Export and save the .reg file (ex: hklm1.reg)
    3. Change the setting via normal means.
    4. Repeat steps 2 and 3 and save the file with a new name (ex: hklm2.reg).
    5. Use a Diff tool such as the one include with Tortoise SVN or Tortoise Git to search for changes.

    Wednesday, September 28, 2011

    jQuery Mobile Displaying A Dialog

    While working on a mobile web app that is using jQuery Mobile I wanted to display a dialog without making an Ajax call to the server to load it which is the "out of the box" way to do it.

    Initially I couldn't figure out how to do this but after some reading and thinking it's ridiculously easy.

    You have to use a multi-page template which means you have more then one jQuery Mobile "page" container. Example:
    <div data-role="page" data-theme="a">
        <div data-role="header" class="header">
            <a href="#menu" data-icon="grid" data-theme="b" data-iconpos="notext" data-transition="pop">&nbsp;</a>
            <h1>My Web App</h1>
            
        </div>
    
        <div id="map" data-role="content" data-theme="d">
            <p>This is my main content</p>
        </div>
        
        <div data-role="footer">
            <div>
            by <a href="http://cdeutsch.com" rel="external">CDeutsch</a>
            </div>
        </div>
    </div>
    
    <!--start menu-->
    <div id="menu" data-role="dialog" data-theme="a" data-url="menu">
        <div data-role="header" class="header">
            <h1>Main Menu</h1>
        </div>
        <div data-role="content" data-theme="d">
            <ul data-role="listview" data-theme="c">
                <li><a href="http://blog.cdeutsch.com" rel="external">My Blog</a></li>
                <li><a href="http://twitter.com/cdeutsch" rel="external">Twitter</a></li>
            </ul>
        </div>
    </div>
    

    I'm using a button on the left side of the header to trigger showing the dialog. Just set the href to the id of the dialog (in this case menu) and you're all set. No more repeatedly hitting the server for a frequently used resource.

    See this jsfiddle for a working example.

    Tuesday, September 6, 2011

    Entity Framework Code First Error: Could not create constraint

    While using Entity Framework Code First you will run into an error similar to the one below if you create objects that have a circular reference.

    Introducing FOREIGN KEY constraint 'File_Folder' on table 'Filess' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
    Could not create constraint. See previous errors.


    The fix is pretty simple but not very intuitive.

    Add the following line to your OnModelCreating override of your DbContext:

    modelBuilder.Entity<File>().HasRequired(oo => oo.Folder).WithMany(oo => oo.Files).WillCascadeOnDelete(false);
    

    So it looks something like this:

    public class FilesystemDB : DbContext
    {
     public DbSet<File> Files { get; set; }
     public DbSet<Folder> Folders { get; set; }
     
     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
      modelBuilder.Entity<File>().HasRequired(oo => oo.Folder).WithMany(oo => oo.Files).WillCascadeOnDelete(false);
     }
    
    }
    

    For the best information on Entity Framework Code First read Morteza Manavi's blog.