Node.js Sprinkler Controller – Part 1

Why is it that every spring there is something broken on my sprinkler system? This year my, supposedly waterproof, commercial-grade sprinkler controller ended up being non-responsive; leaving my whole sprinkler system as a useless, mindless, zombie moaning for brains.

What does a software/hardware engineer do when he runs into a problem like this? Can’t rely on other people’s designs, that’s for sure! So I pulled one of our early prototype, ethernet-connected, relay boards and wired it for use as a sprinkler controller.

The wiring process is very very simple (24VAC to relay common, valve solenoid to relay normally open) so I won’t detail it here, and it is made even easier with the common bussing feature on our hardware. I only have 4 zones, but this board would be able to handle up to 10 discrete valves.

Sprinkler Relay Controller

Now that the easy part was done I needed something to operate the zones at the proper times. I have a Linux server/gateway in the basement that is always on, and what tool does a Linux admin use to do things at regular intervals? CRON!

Relay Board Control

I guess I should start with a quick primer on how to remotely control our boards. The firmware on our board uses an intentionally simple UTF-8 (haha, ASCII really), line-oriented protocol. Simply connect to its IP address on port 10001 and start sending commands terminated with a carriage return, line feed, or both.

The command structure is simple, first you specify the port (relay) then the operation, then the desired state. For example, to turn relay 1 to the ON state you would send `1R1\r` (port 1, operate relay, 1 = on state, carriage return terminated), then to turn it off send `1R0\r`; for Relay 2 `2R1\r` and `2R0\r`, etc…

Another command type that the board supports, timed commands, are also useful for this project, they’re the same as the relay commands but have an additional delay time parameter appended. To turn relay 1 off in 5 seconds send `1T050\r` (port 1, timed command, 0 = off state, tenth-second delay: 50 = 5 seconds, carriage return terminated).

Port Command State Parameter
1 R 1
1 R 0
2 R 1
1 T 0 50
All commands terminated with \r, \n or both

I must mention a very important ordering requirement of these commands, first sending a timed command to turn the relay off after the required runtime, THEN send the command to activate the zone. This will ensure that the zone turns off even if something goes wrong while sending the commands.

Just ask my anonymous friend, who wrote his own sprinkler control system years ago, how important this is. Using relay boards that didn’t support timed commands, his software sent the activation command to the board and then because of a forgotten assert, broke into the debugger, freezing execution. He woke to a thoroughly flooded basement; this has honestly been a bit of a deterrent to me as I’ve thought of doing this exact thing many times over the years.

CRON Control

So we want to use CRON to control the IP connected board. What’s the easiest way to send arbitrary data to a host from the shell? NetCat! Here’s the initial crontab that I set up to run my sprinkler zones, I wanted two start-times with relatively short watering times. Using a piece of graph paper I plotted the zone start and end times to derive the cron rules from.

# Sprinkler Schedule
#Schedule A
#Zone 1 1:00 - 15 minutes
0 1 * 4-10 * echo -ne "1T09000\r1R1\r" | netcat 192.168.0.225 > /dev/null 2>&1
#Zone 2 1:15 - 15 minutes
15 1 * 4-10 * echo -ne "2T09000\r2R1\r" | netcat 192.168.0.225 > /dev/null 2>&1
#Zone 3 1:30 - 20 minutes
30 1 * 4-10 * echo -ne "3T012000\r3R1\r" | netcat 192.168.0.225 > /dev/null 2>&1
#Zone 4 1:50 - 20 minutes
50 1 * 4-10 * echo -ne "4T012000\r4R1\r" | netcat 192.168.0.225 > /dev/null 2>&1

#Schedule B
#Zone 1 2:15 - 15 minutes
15 2 * 4-10 * echo -ne "1T09000\r1R1\r" | netcat 192.168.0.225 > /dev/null 2>&1
#Zone 2 2:30 - 15 minutes
30 2 * 4-10 * echo -ne "2T012000\r2R1\r" | netcat 192.168.0.225 > /dev/null 2>&1
#Zone 3 2:45 - 20 minutes
45 2 * 4-10 * echo -ne "3T012000\r3R1\r" | netcat 192.168.0.225 > /dev/null 2>&1
#Zone 4 3:05 - 20 minutes
5 3 * 4-10 * echo -ne "4T012000\r4R1\r" | netcat 192.168.0.225 > /dev/null 2>&1

Echo by default appends a linefeed and does not process escaped sequences, the `-ne` command-line parameters resolve these issues for us.

Sweet! 30 minutes after I started this project and my lawn is getting watered. Goal 1 accomplished, now my lawn won’t die while we have some real fun!

Umm Node.js

The title of this post is Node.js Sprinkler Controller, right? So where’s the node? One of the only reasons I decided to do this project was because I have been wanting to learn some of the new web technologies with a real project. For this one I’ve decided to learn Node.js on the server side; Knockout.js on the client; and Socket.IO, and Coffeescript on both. For this project I’ll be taking advantage of the express web framework running on Node.js.

So the node… This introductory article is too long! Jump to Part 2 to where we’ll get node set up and get it talking to the relay board.

Posted in Javascript, Open Source, Technology | Tagged , | 1 Comment

jQuery Form Serialization Plugin

For a number of projects I’ve needed to serialize all of the values in a form on the client to JSON in order to post the data back to the server via AJAX.

Here’s a little jQuery plugin I wrote to help with this task. It finds each input element of the selected form and adds a property with the name and value of the input element to the object that is passed in.

(function($) {
    $.fn.formToObj = function(target) {
        // Serialize all the inputs except radio buttons and checkboxes
        this
            .find(":input")
            .not(":radio:unchecked, :checkbox")
            .filter(function() { return this.name; })
            .each(function(idx, input) {
                target[input.name] = $(this).val();
            }); 

        // Serialize the checkboxes and radio buttons
        this
            .find(":input:checkbox")
            .filter(function() { return this.name; })
            .each(function(idx, input) {
                target[input.name] = new Boolean($(input).attr("checked"));
            }); 

        return this;
    };  
})(jQuery);

This extension can be used simply with the jQuery AJAX methods. This example posts the data object back to the server as JSON in a POST variable called data and then puts the server’s response into a page element with an ID of response.

// Serialize the form                                                                                                                                           
var data = {}; 
$("form").formToObj(data);

// AJAX post the data to the server
$.post("post.php", { "data": JSON.stringify(data) }, function(rdata) {
    // Display the result from the server
    $("#response")
        .html(rdata)
        .slideDown();
});

And here’s some server-side PHP code for a simple form with a single text input for someone to signup for notifications on our site. PHP json_decode will take the JSON that was posted and create a PHP object which we can access properties of the same name as the input elements on the form.

<?php                                                                                                                                                           
    if($_SERVER['REQUEST_METHOD'] != "POST") {
        header("Status: 405 Method Not Allowed");
        header("Allow: POST");
        return;
    }

    $data = json_decode($_POST['data']);
    mail('josh@example.com', 'site signup', "$data->email signed up for your site.", "From: josh@example.com (Josh Perry)");

    echo "Thank you for signing up to be notified!";
?>
Posted in Open Source, Software Development, Technology | Leave a comment

Bitbucket Support for git and Attractive Pricing

I’ve been using github for quite a while for working against open-source code bases, but continued to use my own git installation for private repositories; and I did this because of their pricing model.

Their UI is very nice, and their features unbeatable, but I’m not a company with a handful of products who wants to facilitate collaboration with an internal dev team, I want to store a bunch of small repositories, and maybe privately collaborate with a small number of people sporadically. Even with their highest self-subscribing plan (at $22/month) you only get 20 private repositories.

This morning I received an email from bitbucket, purporting to — among a swath of other new features — rock git(my preferred DVCS). This makes them a viable alternative for consolidating both my open repositories — that I would usually use github for — and repositories that I host on my private server.

Along with their new features the pricing model fits my use-case perfectly; their FREE plan has unlimited private repositories with 5 private collaborators. As the price increases the number of collaborators that can view and edit your private repositories increases; which seems simple and fair.

I really want to love — and use — github, and if they would provide a cost-effective plan with unlimited repositories (even if it was still size limited as it is now) I would definitely take them up on it.

As Atlassian continues to progress bitbucket and bring feature-parity with github, hopefully this new direct competition with motivate github to be a little more competitive price-wise.

Posted in Software Development, Technology | Leave a comment

Why Use an Object Relational Mapper

I occasionally (still!) get in heated discussions with fellow developers on the finer points of writing — or generating — data access code versus using an O/RM. Usually this discussion is happening at a restaurant or some social gathering place (yes we socialize) where I’m not at a computer so, it’s hard to answer the call of “Talk is cheap; show me the code!”.

The last couple days I’ve been doing an initial refactoring of a wad of old code that needs some bugs fixed but is suffering from a serious case of code-rot. This code is a pretty straight forward — if bare-bones — version of a shopping cart, so everyone in e-commerce should be familiar with the concept. Well, if a picture is worth a thousand words, then this code should be worth at least that much.

This was found in the catalog ASPX code-behind where it needed, in order to display them, all of the items in a chosen category:

string qrySubCategory =
"select distinct categoryId, subCategoryId, subCategory " +
"from dbo.vw_fcCategoryItems " +
"where categoryId = " + categoryId + " " +
"order by subCategory, subCategoryId";
string qryItem =
"select itm.itemId, itm.categoryId, itm.subCategoryId, itm.subCategory, itm.itemName, itm.itemShortDescription," +
" itm.itemType, dbo.fn_fcItemIcon(itm.itemId) as iconUrl, itm.created, itm.updated " +
"from dbo.vw_fcCategoryItems itm " +
"where itm.categoryId = " + categoryId + " and deleted is null " +
"order by itm.subCategory, itm.subCategoryId, itm.itemName";
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnStoreX"].ToString());
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
try {
cn.Open();
da.SelectCommand = new SqlCommand(qrySubCategory, cn);
da.Fill(ds, "subCategory");
da.SelectCommand = new SqlCommand(qryItem, cn);
da.Fill(ds, "item");
ds.Relations.Add(new DataRelation("relSubCategoryId", ds.Tables["subCategory"].Columns["subCategoryId"], ds.Tables["item"].Columns["subCategoryId"]));
gvSubCategory.DataSource = ds.Tables["subCategory"].DefaultView;
gvSubCategory.DataBind();
}
finally
{
if (cn.State != ConnectionState.Closed)
{
cn.Close();
}
}

For the sake of our sanity, we’ll disregard the anti-patterns that even my adversaries in these debates wouldn’t use. In fact, the code that I’m about to show you — that was used to replace this — will indeed look very similar to what their hand-crafted or generated code would look like.

So, why? Why write this code? Why generate and compile this code? We use generalized libraries every day so that we don’t have to duplicate work. Why is this solved problem of persisting data to a relational store such a polarizing subject?

I’ll leave these questions as a rhetorical giant pink elephant in the room as I bestow on you the code that replaced this mess:

Category cat = CategoryRepository.GetById(categoryId);
gvItem.DataSource = cat.Items;

Posted in Software Development, Technology | Tagged | Leave a comment

Hiding ReaderWriterLockSlim housekeeping in a using statement

Fredrik Mörk has an interesting post on using extension methods to hide housekeeping code related to protecting access to a share resource using a ReaderWriterLockSlim.

I recommend reading the article, but the short of it is that the standard try, lock, operate, finally, unlock process is moved to an extension method and the operate step is passed in at the call-site as a delegate using a lambda.

I like this method and this post is mostly just a jam session with Fredrik, I don’t know if you’ll prefer this method to his, but I did enough to write this post.

The general idea is to create a proxy object that implements IDisposable that can be wrapped in a using statement; when created this proxy will take the lock, and when disposed it will release it. We’re taking advantage of the using statement’s guarantee that it will always call Dispose on its owned object when execution leaves its scope, whether by successful program flow or by a thrown exception.

First the proxy objects:

public class ReadLockProxy : IDisposable {
ReaderWriterLockSlim _rwlock;
public ReadLockProxy(ReaderWriterLockSlim rwlock) {
_rwlock = rwlock;
_rwlock.EnterReadLock();
}

public void Dispose() {
_rwlock.ExitReadLock();
}
}

public class WriteLockProxy : IDisposable {
ReaderWriterLockSlim _rwlock;
public WriteLockProxy(ReaderWriterLockSlim rwlock) {
_rwlock = rwlock;
_rwlock.EnterWriteLock();
}

public void Dispose() {
_rwlock.ExitWriteLock();
}
}

public class UpgradeableReadLockProxy : IDisposable {
ReaderWriterLockSlim _rwlock;
public UpgradeableReadLockProxy(ReaderWriterLockSlim rwlock) {
_rwlock = rwlock;
_rwlock.EnterUpgradeableReadLock();
}

public void Dispose() {
_rwlock.ExitUpgradeableReadLock();
}
}

These proxies are enough to make use of our pattern; a usage (to borrow Fredrik’s example) would look like this:


private static int GetFromQueueWithProxies() {
int result = -1;

using(new UpgradeableReadLockProxy(_lock))
{
if (_sharedResource.Count > 0)
{
using(new WriteLockProxy(_lock))
{
result = _sharedResource.Dequeue();
}
}
}

return result;
}

This code is arguably a tad cleaner than passing a lambda into an extension method, but as far as mental workload is concerned, the new operator and passing in the lock object don’t seem like an equitable trade-off. Lets also take advantage of some extension methods to make this code even more straight forward.


public static class LockExtensions {
public static ReadLockProxy UseReadLock(this ReaderWriterLockSlim rwlock) {
return new ReadLockProxy(rwlock);
}

public static WriteLockProxy UseWriteLock(this ReaderWriterLockSlim rwlock) {
return new WriteLockProxy(rwlock);
}

public static UpgradeableReadLockProxy UseUpgradeableReadLock(this ReaderWriterLockSlim rwlock) {
return new UpgradeableReadLockProxy(rwlock);
}
}

I think these are pretty self explanitory, they just wrap the creation of the proxies into extension methods; let’s take a look at a revised example:


private static int GetFromQueueUsingExtensions() {
int result = -1;

using(_lock.UseUpgradeableReadLock())
{
if (_sharedResource.Count > 0)
{
using(_lock.UseWriteLock())
{
result = _sharedResource.Dequeue();
}
}
}

return result;
}

Ah, much better, we are able to make our code more readable and much more succinct by taking advantage of two C# language features, using statements and extension methods.

I hope you enjoyed this jam session, I know I did; big thanks to Fredrik for the initial riff!

Posted in .NET, Software Development, Technology | Tagged | 3 Comments

Exiting a Windows Phone Application

<UPDATE>
Turns out that the Application Certification Requirements 4.2.5 expressly disallows calling any methods in the Microsoft.Xna.Framework.Game and .Graphics assemblies when your application uses any methods from the System.Windows.Controls namespace. So using the method below in an application that you intend to publish will cause it to be rejected.
</UPDATE>

So, I started working on a WP7 application with a friend recently and I really wanted to do test first, which makes me more comfortable, and my favorite test framework is Machine.Specifications (MSpec). Unfortunately MSpec doesn’t exist for WP7 or Silverlight, so I spent the weekend porting it over.

As I was working on the test runner I wanted to run the tests, post the results to a listening result-display application running on the desktop and quit the runner. It turns out, however, that there is no way to quit a WP7 application, and that this was by design.

It looks like people are doing (as was alluded to by Peter’s article) is throwing an exception of a specific type and not handling it in the UnhandledException handler for the application. This is effective if not a little dirty, though this could be an issue if in the future Microsoft starts providing crash reports to developers.

I found a slightly cleaner way to do this, and since it’s a very uncommon use-case I think it’s a decent compromise. In Peter’s article, besides throwing an exception, he says that XNA games calling Game.Exit() is the only other way for programatically exiting an application running on the phone.

So simply add a reference to the Microsoft.Xna.Framework.Game assembly to your WP7 Silverlight app and then anywhere that you want to exit just execute this one-liner.

new Microsoft.Xna.Framework.Game().Exit();

This quickly and effectively terminates the application, and since Microsoft.Xna.Framework.Game.dll is included in the ROM on the device you don’t need to worry about it bloating your application. Though, I haven’t tried submitting an application to the marketplace that actually uses this method so YMMV.

Posted in .NET, Software Development, Technology | Tagged , | Leave a comment

Budgeting Cisco PoE for Aastra SIP Phones

I recently implemented a 45 phone FreeSWITCH system (if Asterisk is a mean and unforgiving Chef Ramsay, FreeSWITCH is Paula Deen, but that’s for another post). With this number of handsets I definitely wanted to use a Power-over-Ethernet (PoE) switch feed them rather than having wall-warts locally at each station.

We already had a 48-port Cisco 3750-24PS at another location which was only powering a few access points, so we racked it up for its new job powering Aastra phones. Most of the phones we had decided on were the Aastra 57i with a couple 53i phones at rarely used stations.

Power Classification and Budget

Every PoE device, when attached to a PoE has the opportunity to communicate a power class back to the switch, this class tells the switch how many Watts the device needs in order to operate. Class 1 is 4W, Class 2 is 7W, and Class 3 is 15.4 Watts. This classification communication system is needed because reading actual current usage is impractical and because the switch needs to know that it will still be in budget before actually providing power.

This particular switch has a total power budget of 370 Watts, or an average of 7.7W per port. Think of the switch’s power budget as a rope that can hold 370 pounds and each device’s power requirement adds that much weight to the load. If you overload your rope IT WILL BREAK.

Now the problem with the Aastra phones is that they don’t communicate their PoE class back to the switch. If the device doesn’t communicate its class then it is classified as Class 0 (Unclassified) and budgeted the max of 15.4 W. We quickly max out the estimated power budget with only 24 phones, and once that happens the switch will refuse power requests for subsequent devices.

Not only do the phones not communicate with the switch, their tech specs, admin guide, and user’s manual didn’t communicate with me; I needed to know how many Watts the phones really required. To answer that question I had to get in touch with an Aastra reseller who opened a ticket with Aastra’s support group. The answer? 4.5 Watts for the 57i.

Elucidating the Hardware

At 4.5W we can feel comfortable providing power on all 48 ports of this 3750 switch. The problem though is that the switch doesn’t share our surety. So we need to find a way to share our newfound knowledge with Cisco IOS. Luckily Cisco has rather comprehensive documentation on their products, we’ll take a look at Budgeting Power for Devices Connected to a PoE Port.

Turns out that the switch has a nifty command power inline consumption default that lets us tell it to ignore the IEEE classification and use a hard-coded number of milliwatts for a ports power budget. Since this command takes values in milliwatts (mW) we need to multiply our values by 1000; 4,500mW per phone and a total budget of 370,000mW.

Since at 4.5W we’ll be quite under our power budget even when using 48 ports we’re just going to tell the switch to budget 370,000/48 = 7700mW per port by default.

So simply log into your switch, get into enabled mode and execute these commands:

sac-switch-01#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
sac-switch-01(config)#power inline consumption default 7700
sac-switch-01(config)#end

It’s that easy, but please heed the warning that you get when you use the power inline consumption default command. “Breaking your rope” may have been a metaphor, but “The switch is on fire!” will be an aphorism to your client!

%CAUTION: Interface interface-id: Misconfiguring the ‘power inline consumption/allocation’ command may cause damage to the switch and void your warranty. Take precaution not to oversubscribe the power supply. Refer to documentation.

Don’t forget to save out your running config copy running-config startup-config.

The Payoff

Great! We’re done, and now running show power inline will show us how the switch is budgeting power, here’s mine with about 30 of the phones attached:

Interface Admin  Oper       Power   Device              Class Max
                            (Watts)
--------- ------ ---------- ------- ------------------- ----- ----
Fa3/0/1   auto   off        0.0     n/a                 n/a   15.4
Fa3/0/2   auto   on         7.7     Ieee PD             0     15.4
Fa3/0/3   auto   on         7.7     Ieee PD             1     15.4
Fa3/0/4   auto   on         7.7     Ieee PD             0     15.4
Fa3/0/5   auto   on         7.7     Ieee PD             0     15.4
Fa3/0/6   auto   on         7.7     Ieee PD             0     15.4
Fa3/0/7   auto   on         7.7     Ieee PD             1     15.4
Fa3/0/8   auto   on         7.7     Ieee PD             1     15.4
Fa3/0/9   auto   on         7.7     Ieee PD             0     15.4
Fa3/0/10  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/11  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/12  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/13  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/14  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/15  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/16  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/17  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/18  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/19  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/20  auto   on         7.7     AIR-AP1121G-A-K9    n/a   15.4
Fa3/0/21  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/22  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/23  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/24  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/25  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/26  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/27  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/28  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/29  auto   on         7.7     Ieee PD             0     15.4
Fa3/0/30  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/31  auto   on         7.7     Ieee PD             1     15.4
Fa3/0/32  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/33  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/34  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/35  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/36  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/37  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/38  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/39  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/40  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/41  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/42  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/43  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/44  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/45  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/46  auto   off        0.0     n/a                 n/a   15.4
Fa3/0/47  off    off        0.0     n/a                 n/a   15.4
Fa3/0/48  off    off        0.0     n/a                 n/a   15.4
Posted in Uncategorized | Leave a comment

Using Git with Visual Studio C# and VB.NET projects

I just recently started using git for source control with some C++ projects and have been loving it! Naturally, I also wanted to start using it with some C# .NET projects in Visual Studio.

There are a number git add-ons for Visual Studio but the source control addon system doesn’t lend itself to the distributed, pull-edit-merge, workflow of distributed SCMs. To work around this limitation most add-ons require you to use a secondary interface to add/remove files, commit, and push changes. Using the git shell directly turned out to be the most simple and effective interface.

.gitignore

When using the git shell, managing all of the intermediate and non-source files that an IDE and build system create can be a bit of a pain. Because git doesn’t automatically use the IDE’s solution and project to know which files should be placed under source control we need to provide it a blacklist of files to ignore. This practice is common with projects that are not managed by an IDE. When using git, the .gitignore file is what tells git which files it should ignore (I know, big shocker!).

Where to put it?

When you run a git command like git add, git will build a list of ignore rules starting at the root of the work tree and working down through the directory hierarchy to where the file that it is operating on is located. Conflicting rules further down the tree will override rules above them.

If your repository only contains your Visual Studio project then you can put these rules in the .git\info\exclude file, this is like a .gitignore that applies to the whole repository. However, if you have a number of heterogeneous projects dotted throughout your repo then just drop the .gitignore file in the same directory as your solution file.

What should I put in it?

I harvested the rules I’m using from a number of sources and aggregated them into this list. These rules will exclude build output and local, user or machine specific, files. If you have other rules that you’ve found useful please post them in the comments and I’ll expand this list.

*.user           #User specific solution and project settings
*.suo
*.sln.cache
[bB]in/         #Build output
[oO]bj/
*.xap
_ReSharper.*  #Resharper database
TestResults/   #MSTest result files

Now the magic happens, you can simply run git add -A . from your solution directory any time that you add, change, or remove a file from your solution. Git will now only pay attention to files that you haven’t blacklisted in your .gitignore. The git status command will also exclude these files from it’s Untracked Files list; this makes finding what’s changed in your local tree much easier.

Awesome!

This small bit of up-front work has made using git with Visual Studio much more productive for myself. If you’ve tried git in the past and found it to be a bit too much work, give this a try; I think you’ll like it.

Posted in .NET, Open Source, Software Development, Technology | Tagged , | Leave a comment

Linq Aggregate Empty Sequence

Linq is great for its compact expressions, and for its — usually efficient — lazy-evaluation. These two virtues seem to be spit upon by the Aggregate function.

Say you want to aggregate a list of ints.

var s = new int[] {1, 2, 3};
int sum = ints.Aggregate((a,b) => a + b);

This works great if your list has elements in it, or they aren’t filtered out in a where clause. Say you had a function that took a list of ints and someone passed that list of 1,2,3 to your function:

int SumOverTen(IEnumerable<int> ints)
{
int agg = ints.Where(i => i > 10).Aggregate((a,b)=> a+b);
return agg;
}

Everything now goes pear-shaped and you get an InvalidOperationException “Sequence contains no elements”.

So what do you do to fix this? You could rewrite your function to check for an empty result first:

int SumOverTen(IEnumerable<int> ints)
{
int agg = 0;
var bigints = ints.Where(i => i > 10);
if(bigints.Count() > 0)
agg = bigints.Aggregate((a,b)=> a+b);

return agg;
}

This makes a nice Linq expression into a nasty branching mess. The list will now be enumerated twice, once for Count and once for Aggregate.

Well, it turns out that there is a better way, using Linq:

int SumOverTen(IEnumerable<int> ints)
{
int agg = ints.Where(i => i > 10)
.DefaultIfEmpty()
.Aggregate((a,b)=> a+b);

return agg;
}

This results in the same value as the previous function but is much more legible and throws no exceptions on an empty list. However, I don’t know how DefaultIfEmpty checks for an empty list so it may not be more efficient than the previous code.

What I want to know is, why does Aggregate throw instead of returning the same result as using DefaultIfEmpty?

Posted in Software Development, Technology | Tagged , | 6 Comments

Opera Mini on the iPhone, for now

So, today(2010-04-13) it was announced that Opera Mini for the iPhone had been accepted by Apple and indeed it was revealed in the Appstore within a few hours.

Many have written on the loophole that Opera used to make this happen; Opera mini isn’t technically a browser, it can probably be more accurately described as a remote image viewer. The browser sends out a request for a URI to the Opera proxy servers who then pull down the requested page, and let Javascript run for up to 2 seconds to build out any Web 2.0 goodness on the page. The server then renders the resultant HTML (I assume, judging by the rendering discontinuities, with their very own layout engine) to an image which is then sent back to your mobile device as a pixelated rendition of the web page you asked for.

Questionable Recursion

More, however than an image is returned to your phone, data about active regions on the page are also sent back allowing you to touch links, or even “interact” with Javascript powered widgets. Touch one of these active elements and the server again clatters into action, churning and whirring to grab HTML, execute script, and render a sepia image of the website for view through its dirty little port hole.

This does mean that even though you don’t get any nice sliding or swooshing Javascript animations they are replaced with a faithful approximation of the animation’s final state. This can, understandably, cause sites to have a jumpy and jarring, if fast, feel. However, touch a Javascript widget in Safari and you’re greeted with a nice slide or fade, not by a URL bar as Opera mini does, yet another, web request to have a server run the script only to show you the final frame of the animation.

Unfortunately, the last awards that this yesterday-tech browser received were before the epoch of Safari on the iPhone. Compared to the sorry state of the browser on my BlackBerry, yes, this is a nice change; but it just doesn’t deliver the experience that people expect on contemporary devices, let alone a Safari commensurate experience that is already present on the iPhone.

Now this is all fine and good, if this web experience speaks to you; but is this just Steve having fun with Opera? Something akin to the poison pill that he force fed Adobe and others in the as-yet-to-be-released iPhone OS 4 developer agreement?

The thing is, Opera, like any good software developer, likes to make maximum reuse of it’s code. Case in point, their Android version of Opera mini, instead of rewriting their software to work with the OS, they rewrote the OS; they went as far as implementing a J2ME translation layer for Android to allow their browser to run unmodified.

Now, it is difficult to tell if Opera Mini on the iPhone is using native widgets, they all have a kind of an uncanny valley look to them, and there is explicit evidence against them being native. For one, try doing a copy and paste operation, this is definitely not the native iPhone OS cut and paste experience. This duality of experience is exactly what Apple is attempting to quash with their new developer agreement.

Cut and Paste

Are you; a user of the original iPhone with only Edge connectivity (or running the 3G variants hacked on T-Mobile, ahem); into a jolting, pixelated web experience; yearn for the days of RIP BBSs; or an Opera fan-person? Don’t mind that this experience may end abruptly when you upgrade your iPhone to OS4? Then this may be the browser for you.

Posted in Bleeding Edge, Editorial, Technology | Tagged , , | Leave a comment