It’s a public holiday. It’s raining. What better time to experiment and play with a technology mini-project?
Make a Visual Studio Team Services andon style build lamp with LIFX colour LED and Azure Logic Apps or Microsoft Flow
For this experiment you will need:
- A regular table lamp (AUD $20)
- A LIFX colour changing LED light bulb (AUD $70)
I ordered the “Mena Touch Table Lamp” priced at AUD $20 including a sale discount AND free delivery from Mica Lighting. However ANY regular table lamp will do.
Having watched a CNET showdown comparing Philips Hue 3rd Gen and LIFX 3rd Gen bulbs I chose LIFX for their brighter punchier colours. When a build fails, I want people to know about it!
A pack of 6 lights cost AUD $390 ($65 each) including free delivery from the LIFX Store, while purchasing light bulbs individually costs AUD $80 each. I found a single bulb on ebay for $70 including delivery – just ensure you match 3rd gen model numbers if you do this.
Setup LIFX Light
Screw in the LIFX light, install the LIFX app on your mobile, and the app automatically detects and connects to the light. All you need to do is enter a username to create a LIFX account and the app will prompt for your WIFI password to connect the light.
It’s pretty mistake-proof (the app walks you through the process) and within minutes you’ll be able to turn your light on and off with your mobile phone, and change the colour and brightness levels with a colour wheel.
LIFX has out of the box support for IFTTT but not Microsoft Flow or Azure Logic Apps so we’ll make direct calls to the LIFX API instead.
Connect light to the cloud
Sign in to https://cloud.lifx.com with your LIFX account, select Settings from your account profile name. You need to authorize the LIFX HTTP API and generate a new Personal Access Token.
Go to the Developer Zone HTTP API pages and scroll down to the ‘try it out’ section.
Click on the green key icon and enter the PAT you generated.
You can then enter parameters and test your connection.
I was initially getting an empty response because my WIFI router was blocking connections.
If the API call does not seem to be working i.e. the light does nothing, try checking the light settings in the mobile app. Ensure the light is online and shows Cloud: Connected.
The serial number printed on the light is the same as the light’s MAC address so I modified my router access rules to allow the light to connect.
Once you have got the API calls working you are ready to hook up to Azure or Microsoft Flow.
Create an Azure Logic App or Microsoft Flow
I used Microsoft Flow with my Office 365 account because the Flow interface is quicker to navigate than the Azure portal, however the steps within Azure Logic Apps and Microsoft Flow are the same.
Create a new blank flow or logic app and add a trigger for ‘Visual Studio Team Services – When a build completes’.
If you have not previously made a flow or logic app connection to VSTS you will be prompted to create and authorize a connection to VSTS.
Select the VSTS account and project containing the build(s) that you want to monitor.
Note that you can filter by result ‘succeeded’, ‘partiallySucceeded’, ‘failed’ or ‘canceled’ but since we want to change the colour of the light based on the result, leave this filter blank.
Add a new step under the build step and select the More ellipsis ‘…’ and Add a switch case. This will avoid creating lots of nested if…then condition steps.
Click in the Switch On field and select the Result option from the previous step (see example below).
Type the word ‘failed’ (without quotes) into the Case Equals field and Add an action for HTTP – HTTP.
Select the URI for the LIFX API with the parameters in the body in JSON format. In the example below all lights on my LIFX account will power on red with 0.2 brightness.
Show the advanced options and select Raw authentication and enter the value ‘Bearer’ with your PAT string as shown (without quotes or square braces)
You may want to save the flow and give it a try now.
You can explore other colours and effects – the LIFX API is pretty straightforward and easily implemented following the above syntax.
Change light colour when build is successful or failed
Queue a new build (ensure that it fails) and the lamp should turn red.
For testing purposes I created an empty build definition with an inline PowerShell script task (by Peter Groenewegen) to force the build to fail.
Throw "Build will fail if this task is enabled"
It actually takes up to 60 seconds for the Microsoft Flow to detect the failed build status and turn the lamp red, so a little patience is needed. Azure Logic Apps have finer control over the poll frequency with different price models.
Having successfully triggered the light API, edit the flow and add a second case to the switch for when the results equals succeeded to set the light green in a similar manner. Remember to add the PAT authentication in the advanced options. I renamed the HTTP step to ‘Set Green Light’ to make it easier to see what the step actually does.
You could expand this further with other colours for cancelled or partiallySucceeded builds.
It may also be helpful to have another step that sends an email or other notification if the body of the API response contains “status”: “offline” so that you are alerted if it fails to connect for some reason. You don’t want the light to remain green when the latest build failed just because the light had lost its network connection.
A physical bright light has much greater presence than an overhead dashboard, especially if the lamp is visible from the other end of the office! It definitely seems to demand more attention from developers to fix the build with more gusto than a red square on a dashboard monitor.
You could build an andon style array of lamps for different build definitions or different teams. The uri for the API call would need to be modified to select which lights to control.
I have been impressed with the experience and how easy it is to connect systems with Microsoft Flow and Logic Apps that would have previously taken hours and days.
For a total cost of AUD $90 (around USD $65 / GBP £50 at time of writing) and about half an hour (fifteen minutes playing with the cloud API pages and fifteen minutes creating the Flow or Logic App) I recommend this experiment to all VSTS development teams.