-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAS.js
More file actions
121 lines (107 loc) · 4.63 KB
/
AS.js
File metadata and controls
121 lines (107 loc) · 4.63 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
/*
********************************************************************************
* Authencication Subject Node
* AS in the use case is the philips hue bridge.
* Workflow for this node is:
* 1. Register itself.
* 2. Add related devices.
* Contracts used: devices.sol, relations.sol
********************************************************************************
*/
// requires
var fs = require('fs')
var erisC = require('eris-contracts');
var erisdbURL = "http://localhost:1337/rpc";
// get the abi and deployed data squared away
var contractData = require('./contracts/epm.json');
var devsContractAddress = contractData["deployDeviceK"];
var devsAbi = JSON.parse(fs.readFileSync("./contracts/abi/" + devsContractAddress));
var relsContractAddress = contractData["deployRelationK"];
var relsAbi = JSON.parse(fs.readFileSync("./contracts/abi/" + relsContractAddress))
// properly instantiate the contract objects manager using the erisdb URL
// and the account data (which is a temporary hack)
var accountData = require('./accounts.json');
var addrAS = accountData.authiot_authparticipant_AS.address;
var addrRD1 = accountData.authiot_authparticipant_RD1.address;
var addrRD2 = accountData.authiot_authparticipant_RD2.address;
// AS contract manager
var contractsManager = erisC.newContractManagerDev(erisdbURL,
accountData.authiot_authparticipant_AS);
// properly instantiate the contract objects using the abi and address
var devsContract = contractsManager.newContractFactory(devsAbi).
at(devsContractAddress);
var relsContract = contractsManager.newContractFactory(relsAbi).
at(relsContractAddress);
// Initialize
registerMe(addRelations);
//------------------------------------------------------------------------------
// Register the node itself.
//------------------------------------------------------------------------------
function registerMe(callback) {
register("AS-Hue Bridge", "Wifi, bluetooth", "light, speaker", callback);
}
//------------------------------------------------------------------------------
// Add relations to AS.
// Used as callback after registering itself.
//------------------------------------------------------------------------------
function addRelations() {
// Add related device RD1 and RD2
addRelation(addrAS, addrRD1);
addRelation(addrAS, addrRD2);
}
//------------------------------------------------------------------------------
// Register a device
//------------------------------------------------------------------------------
function register(name, wireless_if, resources, callback) {
devsContract.register(name, wireless_if, resources, function(error, result) {
if (error) { throw error }
if (result) {
// Retrieve and print device info.
devsContract.getDevInfo(addrAS, function(error, result) {
if (error) { throw error }
console.log("Device name: " + result[0]);
console.log("Device address: " + result[1]);
console.log("Wireless I/F: " + result[2]);
console.log("Resources: " + result[3]);
}
);
callback();
} else {
// Unregister existing device. Then register.
unregister();
registerMe(callback);
}
});
}
//------------------------------------------------------------------------------
// Add a related device pair.
//------------------------------------------------------------------------------
function addRelation(addr, related) {
relsContract.addRelation(addr, related, function (error, result) {
if (error) { throw error }
if (result) {
console.log("Added new related device: " + related);
}
// Retrieve related devices.
//relsContract.getRelates(addr,
// function(error, result) {
// if (error) { throw error}
// for (var i=0; i < result.length; i++)
// console.log("Related devices " + "(" + (i+1) + "): " + result[i]);
// }
//);
});
}
//------------------------------------------------------------------------------
// Unregister the device.
//------------------------------------------------------------------------------
function unregister() {
devsContract.unregister( function (error, result) {
if (error) { throw error }
if (result) {
console.log("Unregistered " + accountData.authiot_authexecutor_AE.address)
} else {
console.log("Unregistration failed!")
}
});
}