-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.js
84 lines (70 loc) · 2.46 KB
/
device.js
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
var crypto = require('crypto');
var AWS = require('aws-sdk');
var user = require('./user');
var mqttLock = require('./mqttlock');
var LockEncrypt = require('./lockencrypt');
var device = {};
// registers a device by generate a key, saving the device to the user profile, and then
// returning the plaintext of the key to set on the device.
device.register = function(userid, deviceid, callback) {
var lockEncrypt = new LockEncrypt(userid, deviceid);
lockEncrypt.generateLockKey(function(err, plaintextKey) {
if(err) {
} else {
user.addLock(userid, deviceid, function(updateErr, userdata) {
if(updateErr) {
console.error('Error: registerDevice: ', updateErr);
callback(updateErr, null);
} else {
callback(null, plaintextKey);
}
});
}
});
};
device.deregister = function(userid, deviceid, callback) {
var lockEncrypt = new LockEncrypt(userid, deviceid);
user.removeLock(userid, deviceid, function(updateErr, data) {
if(updateErr) {
console.error('error: ', updateErr);
callback(updateErr, null);
} else {
lockEncrypt.removeLockKey( function(removeErr) {
if(removeErr) {
console.warning('failed delete key file: ', userid, ':', deviceid);
}else{
console.log('removed lock key for ',userid, ':', deviceid);
}
});
callback(null, data);
}
});
};
device.list = function(userid, callback) {
user.getLockList(userid, function(err, locks) {
if(err) {
console.error('list: error: ', err);
callback(err, null);
} else {
callback(null, locks);
}
});
};
function setLockState(userid, deviceid, lockstate, callback) {
var lockEncrypt = new LockEncrypt(userid, deviceid);
mqttLock.setlock(deviceid, lockstate, lockEncrypt, function(lockerr, result) {
if(lockerr) {
console.error('set lock operation failed: ', lockerr);
callback(lockerr);
} else {
callback(null, result);
}
});
}
device.unlock = function(userid, deviceid, callback) {
setLockState(userid, deviceid, false, callback);
};
device.lock = function(userid, deviceid, callback) {
setLockState(userid, deviceid, true, callback);
};
module.exports = device;