Skip to content

Commit 7705388

Browse files
committed
0.0.3: rebuilt based on new APIs
1 parent 82c1d0e commit 7705388

19 files changed

+200
-145
lines changed

.gitignore

100644100755
File mode changed.

.npmignore

100644100755
File mode changed.

README.en.md

100644100755
File mode changed.

README.md

100644100755
-23
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,6 @@ wifi.token(function(err,result){
4646

4747
可以使用类似上方那样的`token`这类快捷方法,也可以使用下方的API:
4848

49-
与用户相关的API:
50-
````javascript
51-
// 登录
52-
wifi.user.signin({
53-
body: {
54-
username: '123',
55-
password: '123'
56-
}
57-
},function(err,result){
58-
console.log(result);
59-
});
60-
61-
// 登出
62-
wifi.user.signout({
63-
body: {
64-
token: 123
65-
}
66-
},function(err,result){
67-
// console.log result token
68-
console.log(result)
69-
});
70-
````
71-
7249
与设备相关的API:
7350

7451
````javascript

apis.js

-37
This file was deleted.

index.js

100644100755
+1-39
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,4 @@
77
// @brief: the sdk and package manager of wifi.io based on Node.js
88
// @author: [turingou](http://guoyu.me)
99

10-
var sdk = require('sdk'),
11-
apis = require('./apis'),
12-
led = require('./modules/led'),
13-
dht11 = require('./modules/dht11'),
14-
rgb = require('./modules/rgb'),
15-
camera = require('./modules/camera');
16-
17-
var Wifi = function(params) {
18-
this.account = params;
19-
this.server = 'http://api.wifi.io';
20-
Wifi.prototype.user = new sdk(apis.user, this);
21-
Wifi.prototype.device = new sdk(apis.device, this);
22-
Wifi.prototype.data = new sdk(apis.data, this);
23-
};
24-
25-
// shotcut method to fetch token
26-
Wifi.prototype.token = function(callback) {
27-
var self = this;
28-
self.user.signin({
29-
body: self.account
30-
},function(err, result){
31-
if (result.body && result.body.token && result.body.token != '') self.token = result.body.token;
32-
callback(err, result.body);
33-
})
34-
}
35-
36-
// shotcut method to store device id
37-
Wifi.prototype.connect = function(id) {
38-
if (id && typeof(id) == 'number') this.did = id;
39-
return this;
40-
}
41-
42-
// load modules
43-
Wifi.prototype.led = led;
44-
Wifi.prototype.dht11 = dht11;
45-
Wifi.prototype.rgb = rgb;
46-
Wifi.prototype.camera = camera;
47-
48-
module.exports = Wifi;
10+
module.exports = require('./libs/wifi');

libs/apis.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = {
2+
device: {
3+
get: {
4+
url: '/device/{{did}}'
5+
},
6+
post: {
7+
method: 'post',
8+
url: '/device/{{did}}'
9+
},
10+
exec: {
11+
method: 'post',
12+
url: '/device/{{did}}/{{action}}'
13+
}
14+
},
15+
data: {
16+
get: {
17+
url: '/data/{{key}}'
18+
},
19+
post: {
20+
method: 'post',
21+
url: '/data'
22+
}
23+
}
24+
}

libs/callback.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// define callbacks
2+
var errorMap = {
3+
'0': '接口调用正常,没有错误发生',
4+
'-101': 'appkey无效',
5+
'-102': '访问权限不足',
6+
'-103': '访问频率超过了限制',
7+
'-201': '要访问的设备没有回应',
8+
'-202': '要访问的设备当前不在线',
9+
'-203': '设备执行过程中发生了错误',
10+
'-301': '访问超时',
11+
'-302': '数据库操作失败',
12+
'-303': '未找到相应的数据内容',
13+
'-304': '服务器发生了内部错误',
14+
'-401': '请求的参数格式不合法'
15+
};
16+
17+
exports = module.exports = function(callback) {
18+
return function(err, result) {
19+
if (err) return callback(err);
20+
var results = result.body;
21+
if (results.error < 0) return callback(new Error(errorMap(results.error)));
22+
return callback(results);
23+
}
24+
}

libs/data.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// extends from basic api instance
2+
var cb = require('./callback');
3+
4+
exports = module.exports = function(data, wifi) {
5+
6+
var self = data,
7+
key = wifi.key;
8+
9+
// shorthand of fetch
10+
data.fetch = function(params, callback) {
11+
var query = { body:{}};
12+
if (typeof(params) === 'string') query.key = params;
13+
if (typeof(params) === 'object') query.body = params;
14+
query.body.appkey = key;
15+
self.get(query, cb(callback));
16+
};
17+
18+
// shorthand of create
19+
data.create = function(params, callback) {
20+
var body = params;
21+
body.appkey = key;
22+
self.post({
23+
body: body
24+
}, cb(callback));
25+
};
26+
27+
};

libs/device.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// extends from basic api instance
2+
var cb = require('./callback');
3+
4+
exports = module.exports = function(device, wifi) {
5+
6+
var self = device,
7+
key = wifi.key;
8+
9+
// shorthand of list
10+
device.list = function(params, callback) {
11+
var body = params;
12+
body.appkey = key;
13+
self.get({
14+
query: body
15+
}, cb(callback));
16+
};
17+
18+
// shorthand of fetch
19+
device.fetch = function(did, callback) {
20+
var body = {};
21+
body.appkey = key;
22+
self.get({
23+
did: did,
24+
query: body
25+
}, cb(callback));
26+
};
27+
28+
// shorthand of bind
29+
device.bind = function(did, params, callback) {
30+
var body = params;
31+
body.appkey = key;
32+
self.post({
33+
did: did,
34+
body: body
35+
}, cb(callback));
36+
};
37+
38+
// shorthand of notify
39+
device.notify = function(did, params, callback) {
40+
var body = params;
41+
body.appkey = key;
42+
self.exec({
43+
action: 'notify',
44+
did: did,
45+
body: body
46+
}, cb(callback));
47+
};
48+
49+
// shorthand of command
50+
device.command = function(did, params, callback) {
51+
var body = params;
52+
body.appkey = key;
53+
self.exec({
54+
action: 'command',
55+
did: did,
56+
body: body
57+
}, cb(callback));
58+
};
59+
60+
// shorthand of unbind
61+
device.unbind = function(did, callback) {
62+
var body = {};
63+
body.appkey = key;
64+
self.exec({
65+
action: 'unbind',
66+
did: did,
67+
body: body
68+
}, cb(callback));
69+
};
70+
71+
};

libs/wifi.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var sdk = require('sdk'),
2+
apis = require('./apis'),
3+
device = require('./device'),
4+
data = require('./data');
5+
6+
var Wifi = function(key) {
7+
this.key = key;
8+
this.server = 'http://api.wifi.io';
9+
Wifi.prototype.device = device(new sdk(apis.device, this), this);
10+
Wifi.prototype.data = data(new sdk(apis.data, this), this);
11+
};
12+
13+
// shorthand to store device id
14+
Wifi.prototype.connect = function(id) {
15+
if (id && !isNaN(parseInt(did))) this.did = id;
16+
return this;
17+
};
18+
19+
// load modules
20+
Wifi.prototype.led = require('../modules/led');
21+
Wifi.prototype.dht11 = require('../modules/dht11');
22+
Wifi.prototype.rgb = require('../modules/rgb');
23+
Wifi.prototype.camera = require('../modules/camera');
24+
25+
exports = module.exports = Wifi;

modules/camera.js

100644100755
+4-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
module.exports = function(params, callback) {
2-
var self = this;
3-
self.device.exec({
4-
body: {
5-
token: self.token ? self.token : params.token,
6-
did: self.did ? self.did : params.did,
7-
method: 'camera.shot',
8-
params: {}
9-
}
10-
}, callback);
11-
}
2+
if (!this.did) return callback(new Error('deviceID required'));
3+
params.method = 'camera.shot';
4+
this.device.command(this.did, params, callback);
5+
};

modules/dht11.js

100644100755
+4-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
module.exports = function(params, callback) {
2-
var self = this,
3-
cb = (!callback && typeof(params) == 'function') ? params : callback,
2+
if (!this.did) return callback(new Error('deviceID required'));
3+
var cb = (!callback && typeof(params) == 'function') ? params : callback,
44
params = (typeof(params) == 'object') ? params : {};
5-
self.device.exec({
6-
body: {
7-
token: self.token ? self.token : params.token,
8-
did: self.did ? self.did : params.did,
9-
method: 'dht11.read',
10-
params: {}
11-
}
12-
}, cb);
5+
params.method = 'dht11.read';
6+
this.device.command(this.did, params, cb);
137
}

modules/led.js

100644100755
+9-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1+
var defaults = {
2+
OutPP: [1, 2],
3+
Toggle: [1, 2]
4+
};
5+
16
module.exports = function(params, callback) {
2-
var self = this;
3-
self.device.exec({
4-
body: {
5-
token: self.token ? self.token : params.token,
6-
did: self.did ? self.did : params.did,
7-
method: 'wifiIO.io_op',
8-
params: {
9-
OutPP:[1,2],
10-
Toggle:[1,2]
11-
}
12-
}
13-
}, callback);
7+
if (!this.did) return callback(new Error('deviceID required'));
8+
params.method = 'wifiIO.io_op';
9+
params.params = params.params ? params.params : defaults;
10+
this.device.command(this.did, params, callback);
1411
}

modules/max7219/index.html

100644100755
File mode changed.

modules/max7219/max7219.js

100644100755
File mode changed.

modules/rgb.js

100644100755
+11-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1+
var defaults = {
2+
r: 88,
3+
g: 12,
4+
b: 99
5+
};
6+
17
module.exports = function(params, callback) {
2-
var self = this;
3-
self.device.exec({
4-
body: {
5-
token: self.token ? self.token : params.token,
6-
did: self.did ? self.did : params.did,
7-
method: 'rgb.pwm',
8-
params: {
9-
r: params.r ? params.r : 88,
10-
g: params.g ? params.g : 12,
11-
b: params.b ? params.b : 99
12-
}
13-
}
14-
}, callback);
15-
}
8+
if (!this.did) return callback(new Error('deviceID required'));
9+
params.method = 'rgb.pwm';
10+
params.params = params.params ? params.params : defaults;
11+
this.device.command(this.did, params, callback);
12+
};

package.json

100644100755
File mode changed.

test/test.js

100644100755
File mode changed.

0 commit comments

Comments
 (0)