-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfirebaseHelper.js
122 lines (111 loc) · 3.62 KB
/
firebaseHelper.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
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
var ch = require('./lib/chillhub.js');
var fb = require('firebase');
var fs = require('fs');
var token = "";
var chUUID = "";
var url = "";
var hardware_version = "1.0.0";
var software_version = '1.0.0';
var attachments = [];
var hostCallback = {};
var printError = function(e) {
for(var propName in e) {
console.log(propName + ": " + e[propName]);
}
}
function UndefinedPropertyError(property) {
this.kind = 'error#undefined-property';
this.property = property;
}
function ConfigFileNotFoundError(error) {
this.kind = 'error#config-file-not-found';
this.method = 'readFile';
this.error = error;
}
var startConnection = function(configFile, hwVersion, swVersion, callback) {
if(callback) {
if (configFile) {
if(hwVersion) {
if(swVersion) {
hardware_version = hwVersion;
software_version = swVersion;
hostCallback = callback;
fs.readFile(configFile, function(e, data) {
if (e) {
console.log("Error opening config file.");
printError(e);
hostCallback(new ConfigFileNotFoundError(e));
} else {
console.log("Config file opened.");
var obj = JSON.parse(data);
token = obj.token;
chUUID = obj.uuid;
url = obj.firebaseUrl
connectToFirebase(fb, token, chUUID, url);
}
});
} else { // if swVersion
callback(new UndefinedPropertyError('swVersion'));
}
} else { // if hwVersion
callback(new UndefinedPropertyError('hwVersion'));
}
} else { // if configFile
callback(new UndefinedPropertyError('configFile'));
}
} else { // if callback
throw new UndefinedPropertyError('callback');
}
}
var connectToFirebase = function(fb, token, chUUID, url) {
ch(fb, Date).login(token, url, function(e, hub) {
if (e) {
console.log("Error logging into firebase.");
printError(e);
// pass the error back to the host
hostCallback(e);
} else {
console.log("Successful firebase login.");
createChillhub(hub);
}
} );
}
var createChillhub = function(hub) {
hub.create(chUUID, hardware_version, software_version, function(e, attachment) {
if (e) {
console.log("Error creating chillhub device.");
printError(e);
// pass the error back to the host
hostCallback(e);
} else {
console.log("Chillhub device successfully created.");
attachments = attachment;
// Return the attachment back to the host.
// The host can now create attachments and then resources.
hostCallback(null, attachment);
/*
attachment.create("milkyWeighs", "31415927", function(e, attachment) {
if (e) {
console.log("Error creating attachment.");
printError(e);
} else {
attachment.createResource("name", "rob", function(snap) {
console.log("The resource changed in the cloud.");
console.log(snap.val());
}, function(e) {
if (e) {
console.log("Error adding resource.");
printError(e);
} else {
console.log("Resource added.");
}
});
}
});
*/
}
});
}
module.exports = {
startConnection: startConnection
};