Skip to content

Commit 19005f8

Browse files
committed
Add discovery service
1 parent 32aa4e9 commit 19005f8

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

apps-builtin/wifi-setup/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</div>
1212
<div class="wifiSetup message" style="display:none">
1313
<p><b>The WiFi network is reconfiguring.</b></p>
14-
<p>If you want to use the network you just joined, you should join your computer to it and then visit <a href="http://raspberrypi.local">raspberrypi.local</a></p>
14+
<p>If you want to use the network you just joined, you should join your computer to it and then reload this page (<a href="http://local.headlesspi.org">local.headlesspi.org</a></p>
1515
<p>If you still want to use the built-in access point you may need to re-join it.</p>
1616
</div>
1717
<script src="js/main.js"></script>

lib/Discovery.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var ifconfig = require('wireless-tools/ifconfig');
2+
var os = require('os');
3+
var http = require('http');
4+
5+
var Discovery = function(){
6+
var self = this;
7+
8+
var discovery_host = 'discovery.headlesspi.org';
9+
10+
self.getIPs= function(cb){
11+
ifconfig.status((err, res) => {
12+
var addr = {'eth0':{}, 'wlan0': {}};
13+
for(var i=0; i< res.length; i++){
14+
if(res[i].interface === 'eth0' || res[i].interface === 'wlan0'){
15+
addr[res[i].interface] = {ip: res[i].ipv4_address, mac: res[i].address}
16+
}
17+
}
18+
cb(addr);
19+
});
20+
}
21+
22+
self.sendDiscovery = function(){
23+
self.getIPs((ips) => {
24+
var id = (ips['eth0'].mac || ips['wlan0'].mac).replace(/:/g, '-');
25+
var ip = ips['eth0'].ip || ips['wlan0'].ip
26+
var name = os.hostname();
27+
28+
var post_options = {
29+
host: discovery_host,
30+
port: '80',
31+
path: '/?address=' + ip + '&id=' + id + '&name=' + name,
32+
method: 'POST'
33+
};
34+
35+
var post_req = http.request(post_options, function(res) {
36+
res.on('data', function () {});
37+
});
38+
post_req.on('error', (err) => {
39+
console.log(err);
40+
});
41+
42+
post_req.end();
43+
});
44+
}
45+
46+
self.start = function(interval){
47+
self.sendDiscovery();
48+
setInterval(function(){ self.sendDiscovery() }, interval);
49+
}
50+
51+
}
52+
53+
module.exports = new Discovery();
54+

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Server = require('./Server.js');
22
var AppManager = require('./AppManager.js');
33
var wifiManager = require('./WifiManager');
4+
var discovery = require('./Discovery');
45

56
var HeadlessPi = function(){
67
var self = this;
@@ -12,6 +13,7 @@ var HeadlessPi = function(){
1213
httpd.start(apps);
1314
})
1415
wifiManager.checkHostapdChannel(30000);
16+
discovery.start(30000);
1517
}
1618
}
1719

0 commit comments

Comments
 (0)