CloudyK Cloud Technology Talk, Tips, Tutorials.

16Sep/110

How I Created “I Integrated Salesforce to my House”

I posted this video a couple of days ago on Vimeo and Youtube and have had a lot of questions on it.  In the video, I showed an example of how you could integrate Salesforce.com to your home automation setup.  Most people want to know what equipment I used at my house.  Here is what was involved...

My Home Automation Setup

My home automation setup utilizes a technology called Insteon, which is a dual-band communication protocol for networking home automation devices like light switches, sensors, doorbells, thermostats, etc.  It is considered dual-band because the network operates over both the power lines in your house, and radio frequencies.  It's considered more reliable than other home automation technologies because of this redundancy.

My setup consists of a central controller, and a ton of devices.  The two pieces of equipment talked about in the video are as follows:

ISY 99i Central Controller

Central Controller:  ISY-99i
Manufacturer:  Universal Devices
Role:  In the demo in the video, I wrote a program that simply commands one of the plug-in light switches in my house to turn on for 10 seconds, then turn off.   The ISY-99i can then be configured to be controlled remotely over RESTful web services.  All I needed to do was to open a port on my router at home, authenticate with my admin username and password, then call this program that I created.

Insteon ApplianceLinc

Switch:  Insteon ApplianceLinc
Manufacturer:  Smarthome
Role:  The ApplianceLinc module is a plug-in module that provides on/off switching for appliances.  Some models allow for dimming, but my scenario required a hard on and a hard off.  This ApplianceLinc module is controlled by my ISY-99i controller.

ISY-99i Setup

I'm not going to go into huge detail about the internal workings of the ISY-99i because there is a lot to cover.  What I did want to show is the program that  put together.  In the screenshot below, you will see a really simple program.  The device labeled 'Bed Lamp Right' is the lamp module that I took from my bedroom and hooked up to the rotating light.  What I like about programming in the ISY is how readable and intuitive your programs are.  Non-technical people can easily see what this program is doing.

ISY-99i Salesforce Test Program

Salesforce

The salesforce portion of this was a breeze.  I simply created a Apex Trigger on Opportunities that looked at two criteria.  (1)  Was the deal over $100,000 and (2) was the deal set to "Closed/Won".  The trigger is below.

trigger OpportunityTrigger on Opportunity (after insert, after update) {

	//Dont run on bulk updates, just single updates
	if(Trigger.new.size() == 1) {

		Opportunity opp = Trigger.new.get(0);

		if(opp.StageName == 'Closed Won' && opp.Amount >= 100000) {

			//Make sure we haven already run this!
			if(Trigger.isInsert || (Trigger.isUpdate &&
					Trigger.oldMap.get(opp.Id).StageName != 'Closed Won')) {

				ISYService.notifyBigDealWon();

			}

		}

	}

}

You will notice that this calls a class called ISYService and a static method called notifyOnBigDealWon.  The ISY uses http basic authentication so I am passing in my username and password in the header.  The ISY supports SSL, but I dont have a signed certificate that Salesforce will accept so I am using http.  Not ideal, but this is just a test scenario.

I also have all of my settings like the ip/host, username, password and port stored in a Custom Settings object that I created in Salesforce.  For those that don't use custom settings, you should.  They are awesome.

Finally, I am using the @future annotation because doing an http callout from a Trigger requires that you do it asynchronously, so @future will do that for us.  I am also adding the (callout=true) parameter to specify that I will be using callouts.

That class looks like this.

public class ISYService {

	@future (callout=true)
	public static void notifyBigDealWon() {

		ISY_Settings__c settings = ISY_Settings__c.getInstance('Default');

		String address = settings.ISY_Address__c;
		String usr = settings.ISY_Username__c;
		String pwd = settings.ISY_Password__c;
		String port = String.valueOf(Integer.valueOf(settings.ISY_Port__c));

		Http http = new Http();

		HttpRequest req = new HttpRequest();

		req.setMethod('GET');
		req.setEndpoint('http://' + address + '/rest/programs/001C/runThen');

		Blob headerValue = Blob.valueOf(usr + ':' + pwd);
		String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
		req.setHeader('Authorization', authorizationHeader);

		System.debug('Trying ' + req.getEndpoint());
		System.debug('Auth Header: ' + req.getHeader('Authorization'));

		HttpResponse resp = http.send(req);

		System.debug('XML RESPONSE: \n\n' + resp.getBody());

	}

}

And here is the video one more time!

VN:F [1.9.14_1148]
Rating: 9.8/10 (4 votes cast)
VN:F [1.9.14_1148]
Rating: +2 (from 2 votes)
How I Created "I Integrated Salesforce to my House", 9.8 out of 10 based on 4 ratings
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.