-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebService.js
More file actions
executable file
·135 lines (127 loc) · 3.91 KB
/
Copy pathwebService.js
File metadata and controls
executable file
·135 lines (127 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* A class that encapsulates the web services needed by the Node JS wrapper.
*/
var HttpHelper = require( './httpHelper' ).HttpHelper;
var httpHelper = null;
var deviceSecret = 'none';
var apiSecret = '';
var applicationToken = '';
/**
*
* @param secret - A secret identifier for an application obtained by the user during the registration process.
* Without this, the app will not have access to Thingdom.
* @constructor
*/
exports.WebService = function ( secret ) {
httpHelper = new HttpHelper();
apiSecret = secret;
};
/**
* Ping Thingdom.
*/
exports.WebService.prototype.pingServer = function ( callback ) {
httpHelper.getData( 'ping', function ( result ) {
callback( result );
} );
};
/**
* Authorize this application by sending the application secret to Thingdom. If valid, Thingdom will send back a token
* which must be used for subsequent communications.
*
* @param callback - A callback function that gets invoked with the request results.
*/
exports.WebService.prototype.getAuthorization = function( callback ) {
//
// Authorization request object.
//
var data = {
api_secret: apiSecret,
device_secret: deviceSecret
};
//
// Post authorization request. Save local copy of application token.
//
httpHelper.postData( 'token', data, function( result ) {
applicationToken = result.application_token;
callback( result );
});
}
/**
* Retrieve a thing, if it doesn't exist then add it.
*
* @param thing - The thing to get/add.
* @param callback - A callback function that gets invoked with the request results.
*/
exports.WebService.prototype.addThing = function( thing, callback ) {
//
// Thing request object.
//
var data = {
token : applicationToken,
name : thing.name,
display_name: thing.displayName
};
//
// Only post product type if it exists.
//
if( thing.productType.length > 0 ) {
data.product_type = thing.productType;
}
//
// Post thing request.
//
httpHelper.postData( 'thing', data, function( result ) {
callback( result );
});
};
/**
* Update a status variable associated with a Thing.
*
* @param thing - The Thing for which status is being updated.
* @param statusArray - An array of status updates.
* @param callback - A callback function that gets invoked with the request results.
*/
exports.WebService.prototype.addStatus = function( thing, statusArray, callback ) {
//
// Status request object.
//
var data = {
token : applicationToken,
thing_id : thing.id,
id : null,
status_array: statusArray
};
//
// Post status request.
//
httpHelper.postData( 'status', data, function( result ) {
callback( result );
});
};
/**
* Add a feed that is associated with a Thing.
*
* @param thing - The Thing associated with the feed.
* @param category - The feed category which is defined during the registration process.
* @param message - A feed message.
* @param feedOption - (optional) An additional option/object to attach to the feed: icon, image, or progress indicator.
* @param callback - A callback function that gets invoked with the request results.
*/
exports.WebService.prototype.addFeed = function( thing, category, message, feedOption, callback ) {
//
// Feed request object.
//
var data = {
token : applicationToken,
thing_id : thing.id,
feed_category: category,
message : message,
options : feedOption
};
//
// Post feed request.
//
httpHelper.postData( 'feed', data, function( result ) {
callback( result );
});
};