-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththing.js
More file actions
executable file
·224 lines (201 loc) · 7.32 KB
/
Copy paththing.js
File metadata and controls
executable file
·224 lines (201 loc) · 7.32 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* A class the encapsulates a Thing.
*/
var UserResponse = require( './userResponse' ).UserResponse;
var web = null;
var applicationToken = '';
/**
* Create a new Thing.
*
* @param webService - A webservice that provides the communication with Thingdom.
* @param name - The name of the Thing.
* @param productType - (optional) The product type associated with the Thing.
* @param displayName - (optional) The display name for the Thing.
* @param callback - A callback function that gets invoked with the result of this constructor.
* @constructor
*/
exports.Thing = function( webService, name, productType, displayName, callback ) {
web = webService;
this.id = '';
this.name = name;
this.productType = productType;
this.displayName = displayName;
this.code = '';
web.addThing( this, function( result ) {
callback( result );
});
};
/**
* Update a status variable associated with this Thing.
*
* @param name - The status variable name.
* @param value - The new value for the status variable.
* @param unit - (optional) The unit of measure for the variable.
* @param callback - A callback function that gets invoked with the result of the status request.
*
* Alternate parameter list.
*
* @param statusArray - An array of status objects: [{name: 'statusName', value: 'statusValue', unit: 'unitOfMeasure'}]
* @param callback - Same as above.
*/
exports.Thing.prototype.status = function( name, value, unit, callback) {
var theThing = this;
validateStatus( arguments, function( result ) {
callback = result.callback;
if( result.isSuccess ) {
web.addStatus( theThing, result.statusArray, function( result ) {
callback( new UserResponse( result ) );
});
} else {
if( typeof callback == 'function' ) {
callback( new UserResponse( result ) );
} else {
throw result.msg;
}
}
});
};
/**
* Add a feed and associate with this Thing.
*
* @param category - The feed category.
* @param message - The 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 result of the feed request.
*/
exports.Thing.prototype.feed = function( category, message, feedOption, callback ) {
var theThing = this;
validateFeed( arguments, function( result ) {
callback = result.callback;
if( result.isSuccess ) {
catetory = result.category;
message = result.message;
feedOption = result.feedOption;
web.addFeed( theThing, category, message, feedOption, function( result ) {
callback( new UserResponse( result ) );
});
} else {
if( typeof callback == 'function' ) {
callback( new UserResponse( result ) );
} else {
throw result.msg;
}
}
});
}
// ****************************************************************************
// Private Helper Methods
// ****************************************************************************
//
// Validate status parameters.
//
var validateStatus = function( statusArgs, callback ) {
process.nextTick( function() {
var result = {
isSuccess: false,
msg: '',
statusArray: [],
callback: null
};
var argCount = statusArgs.length;
if( argCount > 0 ) {
if( typeof statusArgs[ argCount-1 ] != 'function' ) {
result.msg = 'Invalid arguments for status update. Callback is not a function';
} else {
result.callback = statusArgs[ argCount-1 ];
}
}
if( argCount <= 1 ) {
result.msg = 'Missing arguments for status update. At minimum, status array and callback required.';
}
if( argCount >= 2 && argCount <= 4) {
switch( argCount ) {
case 2:
if( validateStatusArray( statusArgs[0] ) ) {
result.statusArray = statusArgs[0];
} else {
result.msg = 'Invalid arguments for status update. Error in status array.';
}
break;
case 3:
result.statusArray.push( { name: statusArgs[0], value: statusArgs[1], unit: ""} );
break;
case 4:
result.statusArray.push( { name: statusArgs[0], value: statusArgs[1], unit: statusArgs[2]} );
break;
}
}
if( argCount > 4 ) {
result.msg = 'Too many arguments for status update. More than 4 detected.';
}
result.isSuccess = ( result.msg.length == 0 ) ? true : false;
callback( result );
});
}
//
// Validate a status array. Make sure each entry has a name, value and unit.
//
var validateStatusArray = function( statusArray ) {
//
// Make sure status array is a array.
//
if( !(statusArray instanceof Array) ) {
return false;
}
//
// Check each item in array for valid properties (name, value, unit).
//
for( var i = 0; i < statusArray.length; i++ ) {
if( !statusArray[i].hasOwnProperty( 'name' ) || !statusArray[i].hasOwnProperty( 'value' ) ) {
return false;
}
if( !statusArray[i].hasOwnProperty( 'unit' ) ) {
statusArray[i].unit = '';
}
}
return true;
}
//
// Validate feed parameters.
//
var validateFeed = function( feedArgs, callback ) {
process.nextTick( function() {
var result = {
isSuccess : false,
msg : '',
category : '',
message : '',
feedOption: null,
callback : null
};
var argCount = feedArgs.length;
if( argCount > 0 ) {
if( typeof feedArgs[ argCount-1 ] != 'function' ) {
result.msg = 'Invalid arguments for feed. Callback is not a function.';
} else {
result.callback = feedArgs[ argCount-1 ];
}
}
if( argCount <= 2 ) {
result.msg = 'Missing arguments for feed. At minimum, category, message and callback are required.';
}
if( argCount >= 3 && argCount <= 4 ) {
switch( argCount ) {
case 3:
result.category = feedArgs[0];
result.message = feedArgs[1];
break;
case 4:
result.category = feedArgs[0];
result.message = feedArgs[1];
result.feedOption = feedArgs[2];
break;
}
}
if( argCount > 4 ) {
result.msg = 'Too many arguments for feed. More than 4 detected.';
}
result.isSuccess = ( result.msg.length == 0 ) ? true : false;
callback( result );
});
}