Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
To make API calls to CloudBit server to Switch ON/OFF a device
/**
* This class is created to
* make API Calls to CloudBits
* Server
*
* @author Shruti Sridharan
* @since 28/02/2016
* @revisions N/A
*/
global class CloudBit {
/**
* This method is created to
* light the LED in cloudbit
* when a Device is switched
* ON / OFF
*
* @since 28/02/2016
* @revisions N/A
*/
webService static void toggle( Integer percent , Integer duration , String recId ) {
LittleBit__c res = new LittleBit__c();
/**
* Fetching CloudBit's Endpoint,
* Encoded Key, Device Id
* from CloudBit API Custom
* Settings.
*/
String encodedkey = CloudBit_API_Setting__c.getInstance( 'CloudBit Access Key' ).Value__c;
String deviceid = CloudBit_API_Setting__c.getInstance( 'CloudBit Device Id' ).Value__c;
String endpoint = CloudBit_API_Setting__c.getInstance( 'CloudBit Notification Endpoint' ).Value__c;
endpoint = endpoint.replace( 'DEVICE_ID', deviceid );
String body = '{"percent":' + percent + ', "duration_ms":' + duration + '}';
res = [
SELECT Switched_ON__c, Id
FROM LittleBit__c
WHERE Id = :recId
];
if( percent == 100 )
res.Switched_ON__c = true;
else
res.Switched_ON__c = false;
Http http = new Http();
HttpRequest httpReq = new HttpRequest();
HttpResponse httpRes = new HttpResponse();
httpReq.setEndpoint( endpoint );
httpReq.setMethod( 'POST' );
httpReq.setHeader( 'Authorization', 'Bearer ' + encodedkey );
httpReq.setHeader( 'Accept', 'application/vnd.littlebits.master+json' );
httpReq.setHeader( 'Content-Type', 'application/json' );
httpReq.setBody( body );
httpRes = http.send( httpReq );
UPDATE res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment