littleBits Components
1. USB Power Module
2. CloudBit Module
3. Servo Module
Salesforce.com Components
1. Salesforce.com Report & Dashboards
2. Salesforce.com Analytics API
3. Apex Class to invoke API and littleBits Cloud API
Step 1 - Create a Salesforce.com Summary Report & Dashboard
For this example I created a Salesforce.com Summary Report. The report summarizes the total amount (value) of Opportunities that have a Stage of "Closed Won".
Once you create the report you can create the dashboard with the report as the data source. Create a gauge chart in your dashboard and set the minimum amount to 2 million and the maximum amount to 5 million. The gauge chart maps well to the Servo littleBit.
Step 2 - Map the littleBit Servo Arc to the Gauge Value Range
The littleBit Servo has two basic modes: Step and Turn. In turn mode you can move the Servo to a specific degree based on the voltage you pass to the Servo. You can set the voltage from 0 to 100. The littleBit Servo has an arc of 145 degrees. So you will lose some precision as you can only set integer voltage values.
To give a rough voltage value you can take the range of values in your gauge (5 million max - 2 million min - 3 million range) / 145 degrees = 20689 dollars per degree in the chart. The amount of voltage is 1% of voltage = 1.45 degrees (145 degrees / 100% voltage = 1.45 volts percentage per degree).
Step 3 - Call the Salesforce Analytics API to get the Gauge Value
Call the Salesforce.com Analytics API to get the values from the Opportunity Report. You can use the following Apex Class which will call the Salesforce.com API and convert the values into the appropriate voltage for the littleBit API.
public class LittleBitsReportManager
{
public static LittleBitsManager.LittleBitsOutputPayload runOpptyGaugeReport()
{
// Get the report ID
List <Report> reportList = [SELECT Id,DeveloperName FROM Report where
DeveloperName = 'Opportunities_Won'];
String reportId = (String)reportList.get(0).get('Id');
// Run the report
Reports.ReportResults results = Reports.ReportManager.runReport(reportId, true);
System.debug('Synchronous results: ' + results);
// Run a report synchronously
// Get the first down-grouping in the report, in this case the StageName = 'Closed Won' on Oppty's
Reports.Dimension dim = results.getGroupingsDown();
Reports.GroupingValue groupingVal = dim.getGroupings()[0];
// Construct a fact map key, using the grouping key value
String factMapKey = groupingVal.getKey() + '!T';
// Get the fact map from the report results
Reports.ReportFactWithDetails factDetails =
(Reports.ReportFactWithDetails)results.getFactMap().get(factMapKey);
// Get the first summary amount from the fact map
Reports.SummaryValue sumVal = factDetails.getAggregates()[0];
System.debug('Summary Label: ' + sumVal.getLabel());
System.debug('Summary Value: ' + sumVal.getValue());
//Set the output voltage as a percent. More voltage will send the Servo farther down the gauge.
//Guage Chart = 0% Voltage = 2 Million (Minum Amount on Gauge)
//Gaute Chart = 100 Voltage = 5 Million (Maximum Amount on Gauge)
//16666 = 1 voltage percent (3 million between Min and Max / 145 degrees for the servo)
//2 Million is the bottom of gauge so subtract that from the start.
integer outputPercent = math.round((((Decimal) sumVal.getValue()) - 2000000) / 20689.655);
integer duration = -1; //Set the voltage peermamently until it is overwritten
LittleBitsManager.LittleBitsOutputPayload outputPayload = new LittleBitsManager.LittleBitsOutputPayload(outputPercent,duration);
System.debug(JSON.serialize(outputPayload));
return outputPayload;
}
}
Step 4 - Invoke the littleBits CloudBit API
Using the same Apex Class "LittleBitsManager" I created in the previous post you can call the CloudBit to set the Servo to match the Dashboard Gauge in Salesforce.com.
This simple line of code will reuse our code from the previous article and use the new code above to call the CloudBit.
LittleBitsManager.sendOutputReq(LittleBitsReportManager.runOpptyGaugeReport());
Executing this line of code will make the "Real Life" Dashboard match the Dashboard in Salesforce.com. Let's see it in action!
You can take this to the next step and make this run via Scheduled Apex every 5 minutes to create a dashboard in your office! Put additional reports and charts together with more CloudBits and littleBit components to create multiple chart dashboards!