-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor management.scd
101 lines (84 loc) · 2.73 KB
/
sensor management.scd
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
// set IPs of all accelerometers on the network
~allSensors = ();
(
~setAllIPsAndPorts = {|ipRangeLo = 100, ipRangeHi = 200, networkDevice = "en0", destPort = 8000, numRetries = 4, receivePort = (NetAddr.langPort), actionWhenDone = ({})|
var myIP, myIParr, nameResponder;
var waitTime = 0.05;
myIP = NetAddr.myIP(device: networkDevice); // uses netlib
// myIP = format("ipconfig getifaddr %", networkDevice).unixCmdGetStdOut; //macos only
myIParr = myIP.split($.);//.asInteger; //actually we don't need integer there
"myIP: ".post; myIP.postln;
~allSensors ?? {~allSensors = ()};
nameResponder = OSCdef(\accname, {|msg, time, addr|
var name = msg[1];
// [msg, time, addr].postln;
"received name from sensor %".format(name).postln;
~allSensors[addr.asCompileString.asSymbol] = name;
}, '/name');
Routine.run({
(ipRangeLo..ipRangeHi).do({|thisLastNumber, inc|
var numTriesForThisIP = 0;
var thisDestIP, thisNetAddr;
var thisIPisReachable = true;
thisDestIP = "".catArgs(myIParr[0] ++ "." ++myIParr[1] ++ "." ++ myIParr[2] ++ "." ++ thisLastNumber.asString);
thisNetAddr = NetAddr(thisDestIP, destPort);
Routine.run({
numRetries.do({
// ("IP " ++ thisDestIP ++ ", ").post;
if(thisIPisReachable, {
try {
"sending IP to ".post; thisDestIP.postln;
thisNetAddr.sendMsg('/ip', *myIP.split($.).asInteger);
} {
thisIPisReachable = false;
// ("Couldn't send my IP to " ++ thisDestIP).warn;
};
}, {
thisDestIP.post; " not reachable ".post;
});
if(thisIPisReachable, {
0.1.wait;
try {
thisNetAddr.sendMsg('/port', receivePort);
} {
thisIPisReachable = false;
// ("Couldn't send my port to " ++ thisDestIP).warn;
};
0.1.wait;
// ask for name:
try {
thisNetAddr.sendMsg('/name');
}
});
0.1.wait;
});
});
waitTime.wait;
});
actionWhenDone.();
"asking for names done.".postln;
});
}
)
// see what network devices are available
"ifconfig -l".unixCmd;
"ifconfig".unixCmd;
"ifconfig en0 | grep 'inet '".unixCmd; // check for IP of a particular device
// set all sensor devices on the network to our IP (change the networkDevice accordingly!):
~setAllIPsAndPorts.(ipRangeLo: 1, ipRangeHi: 254, networkDevice: "en0", numRetries: 1);
// ~setAllIPsAndPorts.(ipRangeLo: 100, ipRangeHi: 200, networkDevice: "en11", numRetries: 1);
// check names
~allSensors.keys
~allSensors[\acc21];
// after running ~setAllIPsAndPorts, you can see status messages
(
~statusDef = OSCdef(\status, {|msg, time, addr|
var name;
// msg.postln;
name = ~allSensors[addr.asCompileString.asSymbol];
name !? {
"%: battery %V".format(name, msg[1].round(0.01)).postln;
}
}, '/status')
)
~statusDef.free;