Wednesday, November 13, 2013

SQLite.Net ORM with Task Parallel Library in Xamarin

I've recently had the opportunity to work on a Xamarin.iOS project with Envoy. Having previously built iOS applications with Objective C I've been enjoying the chance to investigate techniques to share code across platforms.

With Xamarin, you can share some common code between Android and iOS projects if you structure it in a certain way. One technique that is commonly used to share code is to use the SQLite.NET ORM with SQLite rather than using Core Data or other device specific data access API's. Xamarin themselves have used this for an application which they've open sourced as example code: MWC 2012 

In that application, they create a subclass of SQLiteConnection with a static constructor that initialises a static variable to hold a reference to itself, as a kind of singleton. I followed this pattern in my code, however I've since discovered that this is perhaps not the best approach in all circumstances.

My application makes use of the Task Parallel Library to do some web service calls and subsequent database updates on a background thread. Much like the MWC sample I wrapped my calls to the database with a lock so that multiple threads won't update the database at once, and the application appeared to work correctly. However at various stages during my testing I came across seemingly random crashes, where the application which had been working fine would crash out of the blue with an error similar to the following:

mono-rt: Stacktrace:
mono-rt: at <0xffffffff>
mono-rt: at (wrapper managed-to-native) SQLite.SQLite3.Prepare2 (intptr,string,int,intptr&,intptr)
mono-rt: at SQLite.SQLite3.Prepare2 (intptr,string)
mono-rt: at SQLite.SQLiteCommand.Prepare ()
mono-rt: at SQLite.SQLiteCommand.ExecuteNonQuery ()
mono-rt: at SQLite.SQLiteConnection.Execute (string,object[])

This isn't particularly helpful and originally made me worried that perhaps the SQLite.NET ORM wasn't stable. However after some more research I've discovered that others were having the same problem and that they believed it was caused by the SQLite connection being accessed on different threads, even if it's not at the same time. When you think about it, of course this is an issue - the subclass of SQLiteConnection shouldn't be a singleton, it's a connection similar to those seen in ADO.NET and should be thread specific and closed when not needed. I believe the MWC application musn't have ran into this issue as they were only doing updates to the database on certain threads, not leaving it up to the Task Parallel Library.

So now, instead of having a singleton in my SQLite Connection, I'm using it in the IDisposable fashion it is designed to be used so that a connection is opened and closed with each query.
using (var db = MyDatabse.NewConnection())
{
    db.Insert(itemToInsert);
}
I've since stopped having the random application crashes. Hopefully this helps someone out who is having the same issue.

Tuesday, May 28, 2013

Styling Two or More Fusion Tables Layers in Google Maps

The Google Maps API has a feature called 'Fusion Tables Layer' which lets you overlay data from a Google Fusion Tables. Using this feature, you can upload some KML data into a Fusion Table and see it represented on the map (for example, you can highlight an area on a map to represent some sort of data). The Maps API has in built support for using KML directly, however KML files can get quite large for complex data, so using Fusion Tables is a high performance alternative (Google handles large data sets with ease) This is what my exmaple Fusion Table with KML data uploaded looks like:


You can display markers, polygons, and polylines on a layer and can style each of these as you wish using the options supplied to the API call. An example of the code to do this is available here.

If you have multiple bits of data to show, you can add multiple layers from a range of different Fusion Tables. However, there is one limitation, which is pointed out in the documentation:
Styles can only be applied to a single Fusion Tables layer per map. You may apply up to five styles to that layer.
I recently ran into this limitation when trying to overlay two separate (complex) data sets. For my use case, I wanted to show data from two different Fusion Tables with two different styles, which means I was out of luck. I tried a number of workarounds to avoid this (seemingly odd) limitation. I attempted to combine them into the one table, but the queries don't allow OR statements,  I then tried using my own KML file, however that came out larger than 1MB, which loaded very slowly, whereas the Fusion Tables layer handled that data with ease.

I then attempted to add styling information to the KML file before uploading it into the Fusion Table interface (as KML supports this), however the upload process strips that information out. It was while searching for a way to get around this that I found the solution. I discovered that in the Fusion Tables interface you can add your own map, style it there, and it will give you code to easily embed it. This is designed to allow those without coding skills to style and embed a map on a blog or web page. Great! I thought, a bit annoying that I have to store that information in an external dependency instead of in my own code in source control, but at least I can solve this problem (I could perhaps use the Fusion Tables API to set this style from within my application, at the cost of a few HTTP requests).

However when I went to find this map tab that the documentation mentions, it was greyed out...


There's no mention that I could easily find on the page explaining why it was greyed out, but I had a hunch. Given how terribly Google has dealt with Google Apps accounts, I figured that seeing as I was signed in with a Google Apps email, it might have something to do with that. Signing into my personal Gmail revealed that my hunch was indeed correct, I can add a map (you may need to give that account write permissions or copy the table to your personal account).

Sigh. Some searching uncovered some documentation on this. You have to submit a request to Google asking them to turn it on. Would be nice if they just mentioned this in the interface...

Once you've added a map, tools > change map styles will let you style it.
 
You're note quite done yet though, simple embedding this table into the API won't do it, you need to tell the API which style and which template to use in your map. There's some documentation on getting the style number and setting it in the Maps API here. I couldn't find an easy way to get this Id from the API, but it's probably a small number if you just want to try numbers up from 1,2,3,4 etc. Next you need the template ID, which you can find by clicking on the map tab's dropdown and clicking publish. At the end of the URL the querystring will have the template ID as 'tmplt'. 

You then pass these through in your maps API call like so:
 gridLayer = new googleMap.FusionTablesLayer({
                            query: {
                                select: 'geometry',
                                from: fusionTableId,
                            },
                            map: map,
                            templateId: 2,
                            styleId: 2,
                            clickable: false
                        });


You should them see your styled map embedded in your page. Simple... right?

Friday, June 29, 2012

Using C# Named Parameters for Code Readability

In C# 4.0, Microsoft introduced a feature called 'Named Parameters', which lets you refer to parameters in a method that you are calling in a syntax like this:
 
public void MyMethod(firstParameter: firstValue, secondParameter: secondValue)

This lets you call the method with parameters in a different order, and also helps to facilitate optional parameters. Aside from these benefits, I've come to like using them to improve code clarity, particularly in unit tests.
 
Lets say you have a method like this one:
 
public decimal CalculateFuelCostPerKm(double kilometresTravelled, double litresInFuelTank, decimal costToFillTank)
 
If you were to call that method, you might do something like this:
 
CalculateCostPerKm(500D, 60D, 72.58M);

However if you are reading that and don't know the parameters, you're going to have to dive deeper to understand it. A way to improve it might be:
 
var kilometresTravelled = 500D;
var litresInFuelTank = 60D;
var costToFillTank = 72.58M;
CalculateCostPerKm(kilometresTravelled, litresInFuelTank, costToFillTank);

Which is much clearer, but is a fair bit of effort. But with C# named parameters you can do this:
 
CalculateCostPerKm(kilometresTravelled: 500D, litresInFuelTank: 60D, costToFillTank: 72.58M);

Simple to do, and clear to read. In fact, this is probably one of the rare situations where C# being written
more like Objective-C is a good thing. Take this example for creating an alert dialog in the iOS SDK:
 
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
 
Which you would call like:
 
[[UIAlertView alloc] initWithTitle:@"Error" message:@"You stuffed up" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

Or in C# you might do something like this (ignoring the delegate business):
 
UIAlertView.InitWithTitle("Error", "You stuffed up", myDelegate, "OK", null);

As you can see, the verbose nature of Objective-C makes for clearer code in this instance, however, we could write the C# method like:
 
UIAlertView.InitWithTitle(title: "Error", message: "You stuffed up", delegate: myDelegate, cancelButtonTitle: "OK", otherButtonTitles: null);

Which makes it just as clear.
 
I've found this technique particularly useful in unit tests, which often tend towards using constant values. For instance I might often have a method to setup a certain state in my stubs where I take in the important parameters required, this technique makes this much easier to read.
 
I’d love to hear your thoughts on this technique, either in the comments on this post or via Twitter (@rodh257)

Sunday, June 17, 2012

What's been happening?

It's been a fair while since I last posted to this blog, so as I start lining up a few more blog posts I thought I'd make a quick post to let you know what I've been up to over the last 6 months.

The last post I made was about an experimental technique I'd created for running Revit API add-ins as scheduled tasks. This was a part of my Autodesk University 2011 virtual presentation, which is online now. If you don't have access to the AU Online website I've uploaded the slides and handout and the sample code for the Revit Remote Boot and Html Server Admin are on Github. This was a pretty niche topic, but it was fun to dive into, and I hope people got some good use out of it. It's worth noting that Revit Server 2013 is now out, and makes a few changes which could affect this code (I'd be interested to hear if you were using this code).


After this post was made I have started work with a new company, Envoy Advanced Technologies. Envoy are an agile software development firm that primarily work on web-based business applications. This has given me the opportunity to work on some big software systems with some really brilliant developers using some great development techniques (as a shameless plug, if you're looking to have a software system developed, whether it involves Revit or not, you could do much worse than to get into contact with Envoy).
Aside from the larger projects, I recently worked on the iOS and mobile web apps for this years Revit Technology Conference. Envoy were a sponsor of RTC AUS and the USA version of the conference is starting in a couple of weeks time, if you haven't had a look yet I recommend you head to their website and look over the schedule. It looks to have some really good classes, including one on Revit Server.

This change in employer has meant that I've not been working as much on Revit add-ins as I used to, hence the lack of posts on the topic. I am however still tinkering away with it in my spare time, and have a few ideas floating around my head for nifty ways to use the new 2013 API features which I hope to post about in the future.
In my spare time, I've released an update to Convoy, my iOS app to make it easier to communicate when travelling in a group of vehicles. Primarily it includes a graphics overhaul which is thanks to Vanessa Grixti.

This and other projects I've been working on are great as they give me the chance to dive into some product development/management, which is an interest of mine, I've been blogging about these sorts of topics over at Cejest, but I'm planning on moving those posts over to this blog.

In addition to these posts, I'll be writing more about general software development topics, both technical (C#, ASP.NET MVC, iOS etc) and non-technical, including posts on workflows, development practices etc. A reminder that if you aren't interested in reading this, and just want Revit content, there's a link to a Revit topics only RSS feed on the right of the blogs main page.

If you've got any feedback or questions feel free to email me (mail@rodhowarth.com) or ping me on twitter (@rodh257).

Tuesday, November 15, 2011

[Revit API]– Revit Remote Boot - A technique to run an API add-in on a particular Revit model without user interaction

At last years Autodesk University, someone mentioned that they were looking for a way to run an export on their model once a day, on a schedule. This is not something that the Revit API is generally used for, but it  got me thinking of ways that I could make it happen. While flying around the USA after AU I hacked together some prototype code which did just this, but left it shelved. For my AU 2011 virtual class, I decided to finish it off and make it public, in case someone else wants to use it.
The program is called ‘Revit Remote Boot’, and is really more of a ‘system’ than a program, there are a number of moving parts to make it all happen. Put simply, it is a couple of interacting programs that allow you to perform an operation on a Revit model from a script.
Take a look at this video, which is a small part of my AU Virtual class, for a full demonstration of what it does. Note that while it talks about using Revit Server to create a local model, if you are running just a pure file server, this is fine as well – you could just directly open the file. It’s just that running Revit Server opens up some interesting possibilities and was the topic of my AU presentation.


Revit Remote Boot Demonstration from Rod Howarth
(you should head to the Vimeo page and watch it in high definition there to read the text)
Basically your application can save an xml file in a certain directory, which ads a ‘job’ to the queue. Then, using the RevitServerToolCommand, you can, from a batch file, create a local copy of the Revit Model you want to work on, and open it in Revit. From there, a Revit API add-in will detect that you’ve opened a model that corresponds to a job, and will run the other Revit API add-in that was specified in the job file. After this is done, the job is marked complete, and Revit is closed.
In my example, I’ve done a simple export to DWF of the model, the idea being you could set this up to run at 1am every night, and export the DWF model to a certain location – perhaps for viewing in Design Review on mobile devices. However, you could use it for any Revit API add-in. For instance, you could have a high powered server which you setup as a local Revit Server just purely to run this Revit Remote Boot. On this server you could create a job to run a structural analysis add-in, or other computationally expensive stuff. This way you can setup your own “cloud”, running Revit directly. You could also run certain audits on a model, for example, you could create an add-in that counts the number of warnings present in the model and saves this to a database, for displaying in a ‘hall of shame’ on your company intranet. Smile
This is highly experimental, and has some pitfalls, so should be considered as a proof of concept, rather than a production ready program. The main pitfall is that any errors that come up are shown as dialog boxes, and there is no easy way to deal with this in Revit. To get around this I’ve used AutoHotKey (http://www.autohotkey.com/) to detect certain dialogs and close them, but if there are unexpected ones, it will fall over.
If you are interested in the code behind this, check it out here: https://github.com/RodH257/RevitRemoteBoot you can use the ‘zip’ button there to download it if you don’t have git.
I’d love to hear your thoughts on this, and whether you think Autodesk should work towards making this kind of thing easier to do?

Saturday, November 5, 2011

[General Dev]–How to push to two Git remote locations at once

Now that BitBucket supports Git, I’ve been utilizing both GitHub and BitBucket for my project hosting, and have been looking into migrating to BitBucket for my private repositories (as they are free). This is part of the beauty of using a distributed version control system, you can have your code in multiple locations, rather than one central Subversion server.
In Git, I could add two remotes and each time I want to push/pull code I would type something like:
git push github master
git push bitbucket master
However, I want to make things as easy as possible for myself, and I’m simply using these two repositories as a mirror of each other, so I’ll be telling git to push to multiple URL’s with the same remote.
To do this, I need to edit the .git/config file for my project. You can navigate here via the command line, or in Windows you can turn on hidden file display and open the .git folder in Windows Explorer
image
image
In this file, I’ve already added one of my remotes using the git remote add command, as you can see here:
image
To make it push to BitBucket at the same time as GitHub, you simply duplicate this URL line and add the BitBucket URL to it
image
Then if you do a git push origin master (where master is name of your branch), you will see it will push to these URLs in succession:
image
Unfortunately if you have a password protected ssh key it will still ask twice for your password.
Now each time you push to your origin remote, you’ll be taking full advantage of gits distributed nature, and will have two remote copies of your code.

Monday, October 3, 2011

[Revit]– HTML Version of the Silverlight Revit Server Administrator Tool

If you’ve used Revit Server Before, you might recognize this:

image

It’s the Revit Server Administrator Tool which you access by going to http://servername/revitserveradmin it lets you see what Revit Server projects you have present, what files are stored in them, and their version history. It also lets you create, move, copy, delete or lock files and folders.

It’s a vital tool for anyone who runs a Revit Server, and it generally works well, but it has one problem – it’s written in Silverlight. Whatever your opinion on Silverlight may be, it requires an extra installation and won’t run on any mobile devices. This is where the Revit Server API comes in. Using this REST API, I was able to recreate this website in HTML and Javascript. The result looks like this:

image

As my main motivation for this was to give the Revit Server REST API a run through, I’ve not made it absolutely the same, there are some minor visual and functional differences, but for the most part, it behaves in the same way. All of the action buttons are functional, you can see a submission history and folder structure.

For the moment, the main limitations are:

  • When you create or paste a folder, the treeview on the left gets recreated and starts collapsed again. This is fixable, and if there is interest, I’ll polish this up (probably by replacing the treeview library with a new one)
  • The submission history isn’t paged or sortable, its just a table with all the information in it. This should be fixable as well.

I’m interested to hear if anyone is genuinely interested in using this tool in their environment, if so, I’ll keep improving it, if not, then it will serve as a demonstration of the Revit Server API’s capabilities, and will form a part of my AU Virtual session this year.

The website is available on Github, to install it on your server simply download the zip file from that site, and create a folder called ‘html’ in ‘C:\Program Files\Autodesk\Revit Server 2012\Sites\RevitServerAdmin’ on the machine you have Revit Server installed. Put all the files in there, and you should be able to navigate to http://localhost/RevitServeradmin/html/ to access it.

For those interested in the technical details, the page is simple HTML (it’s named Default.aspx even though it isn’t an ASP.NET web page, it’s named this way because this allows us to access it by going to /html/ instead of /html/default.htm), with a CSS stylesheet and some images to replicate the interface of the Silverlight tool. The real work happens in the RevitServer.js file, which, with the help of Jquery, calls the REST web service for each of the various actions that the user performs, and updates the DOM with the results. There is no server side ASP.NET code involved, it’s all Javascript with the Revit Server REST API doing the heavy lifting.

Of course, if you want to use the Revit Server API, you don’t need to use Javascript, you could write a similar thing in C# or VB code and service up in an ASP.NET web page, or just about any language you like as long as they can make HTTP Requests.

If you want to know more about how it all works, or more about the Revit Server API, I’ll be going into more detail in my AU 2011 Virtual session.