-
Notifications
You must be signed in to change notification settings - Fork 1
/
ffbsee.nodes.pl
executable file
·381 lines (365 loc) · 14.9 KB
/
ffbsee.nodes.pl
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/perl
# This File is managed by ansible. Please change it there:
# https://github.com/ffbsee/ansible and/or there:
# https://github.com/ffbsee/api
use strict;
use warnings;
use JSON;
use utf8;
#
# Hier werden einige globale Parameter festgelegt
# wie zum Beispiel der absolute Speicherpfad der Freifunk JSON.
# Diese kommen aus Parametern wie den group_vars oder den host_vars in ansible
#
# Noch anzulegende variabeln:
# {{ maps_webserver }}
# {{ json_api_path }}
our $nodes = 0;
our $json_source = "/var/www/{{ hostname }}/nodes.json";
#
# Es folgen Communityspezifische variabeln
#
# Variablen Markdorf:
#
our @json_export = ("{{ json_api_path }}/ffbsee.json");
our @json_ffbsee;
our $ff_json;
our @ffcommunity = ("Freifunk Markdorf");
our @ffnodes_link = ("https://{{ maps_webserver }}/ffbsee.json");
our @runFirstTime = (1);
our @community_name = ("markdorf");
our $currentTime = `date +%Y-%m-%dT%H:%M:%S`;
our $debug;
our $git_root = "{{ api_gitroot }}";
chomp $currentTime;
our $version = "0.3";
our $subcommunity = "true";
our @ff_nodes = (0);
our @api = ("https://raw.githubusercontent.com/ffbsee/api/master/ffmarkdorf.json");
our @allnodes = (0);
# Friedrichshafen
push (@json_export, "{{ json_api_path }}/fffn.json");
push (@ffcommunity, "Freifunk Friedrichshafen");
print @ffcommunity if ($debug);
push (@ffnodes_link, "https://{{ maps_webserver }}/fffn.json");
push (@runFirstTime, 1);
push (@community_name, "friedrichshafen");
push (@ff_nodes, 0);
push (@api, "https://raw.githubusercontent.com/ffbsee/api/master/fffriedrichshafen.json");
push (@allnodes, 0);
# Konstanz
push (@json_export, "{{ json_api_path }}/ffkn.json");
push (@ffcommunity, "Freifunk Konstanz");
print @ffcommunity if ($debug);
push (@ffnodes_link, "https://{{ maps_webserver }}/ffkn.json");
push (@runFirstTime, 1);
push (@community_name, "konstanz");
push (@ff_nodes, 0);
push (@api, "https://raw.githubusercontent.com/ffbsee/api/master/ffkonstanz.json");
push (@allnodes, 0);
# Kressbronn
push (@json_export, "{{ json_api_path }}/ffkrb.json");
push (@ffcommunity, "Freifunk Kressbronn");
print @ffcommunity if ($debug);
push (@ffnodes_link, "https://{{ maps_webserver }}/ffkrb.json");
push (@runFirstTime, 1);
push (@community_name, "kressbronn");
push (@ff_nodes, 0);
push (@api, "https://raw.githubusercontent.com/ffbsee/api/master/ffkressbronn.json");
push (@allnodes, 0);
# Lindau
push (@json_export, "{{ json_api_path }}/ffli.json");
push (@ffcommunity, "Freifunk Lindau");
print @ffcommunity if ($debug);
push (@ffnodes_link, "https://{{ maps_webserver }}/ffli.json");
push (@runFirstTime, 1);
push (@community_name, "lindau");
push (@ff_nodes, 0);
push (@api, "https://raw.githubusercontent.com/ffbsee/api/master/fflindau.json");
push (@allnodes, 0);
# Ravensburg
push (@json_export, "{{ json_api_path }}/ffrv.json");
push (@ffcommunity, "Freifunk Ravensburg");
print @ffcommunity if ($debug);
push (@ffnodes_link, "https://{{ maps_webserver }}/ffrv.json");
push (@runFirstTime, 1);
push (@community_name, "ravensburg");
push (@ff_nodes, 0);
push (@api, "https://raw.githubusercontent.com/ffbsee/api/master/ffravensburg.json");
push (@allnodes, 0);
# Ueberlingen
push (@json_export, "{{ json_api_path }}/ffueb.json");
push (@ffcommunity, "Freifunk Ueberlingen");
print @ffcommunity if ($debug);
push (@ffnodes_link, "https://{{ maps_webserver }}/ffueb.json");
push (@runFirstTime, 1);
push (@community_name, "ueberlingen");
push (@ff_nodes, 0);
push (@api, "https://raw.githubusercontent.com/ffbsee/api/master/ffueberlingen.json");
push (@allnodes, 0);
# Tettnangen
push (@json_export, "{{ json_api_path }}/fftettnang.json");
push (@ffcommunity, "Freifunk Tettnang");
print @ffcommunity if ($debug);
push (@ffnodes_link, "https://{{ maps_webserver }}/fftettnang.json");
push (@runFirstTime, 1);
push (@community_name, "tettnang");
push (@ff_nodes, 0);
push (@api, "https://raw.githubusercontent.com/ffbsee/api/master/freifunk-tettnang.json");
# Bodman-Ludwigshafen
push (@json_export, "{{ json_api_path }}/ffbolu.json");
push (@ffcommunity, "Bodman-Ludwigshafen");
print @ffcommunity if ($debug);
push (@ffnodes_link, "https://{{ maps_webserver }}/ffbolu.json");
push (@runFirstTime, 1);
push (@community_name, "bodman-ludwigshafen");
push (@ff_nodes, 0);
push (@api, "https://raw.githubusercontent.com/ffbsee/api/master/bodman-ludwigshafen.json");
while (my $arg = shift @ARGV) {
# Komandozeilenargumente: #print "$arg\n";
if (($arg eq "-h") or ($arg eq "h") or ($arg eq "--help")){
print "Dieses Script generiert ein freifunk-karte.de kompatibles JSON mit allen FFBSee Nodes\n";
print "Seit neusten nun auch mit automagischer communityannaeherung anhand der geokoordinaten\n";
print "\n\n --debug\t Debuging\n";
print " --nodes\t Welche Community hat wie viele Nodes\n";
print "\n";
exit(0);
}
if ($arg eq "--debug"){
$debug = "True";
}
if ($arg eq "--nodes"){
$nodes = "True";
}
}
open(DATEI, $json_source) or die "Datei wurde nicht gefunden\n";
my $daten;
while(<DATEI>){
$daten = $daten.$_;
}
close (DATEI);
our $json_text = $daten;
our $json = JSON->new->utf8; #force UTF8 Encoding
our $ffbsee_json = $json->decode( $json_text ); #decode nodes.json
$version = $ffbsee_json->{"version"};
# Generiert das JSON:
for(my $i = 0; $i < @ffcommunity; $i++) {
$json_ffbsee[$i] .= "\{\n \"comunity\": \{\n \"name\": \"$ffcommunity[$i]\",\n \"href\": \"$ffnodes_link[$i]\"\n \},\n";
$json_ffbsee[$i] .= " \"nodes\": \[\n";
}
#
# Generate FFNodes
#
my $runFirstTime = 1;
my $runFirstTimeFN = 1;
my $runFirstTimeFFTettnang = 1;
my $hashref_ffbsee = $ffbsee_json->{"nodes"};
for my $ffkey (keys %{$hashref_ffbsee}) {
my $keinGeo = 0;
if ($debug) { print "$ffkey\n"; }
$ff_json .= " \{\n";
$ff_json .= " \"id\": \"$ffkey\",\n";
my $ffNodeName = $ffbsee_json->{"nodes"}->{"$ffkey"}->{"nodeinfo"}->{"hostname"};
$ffNodeName =~ s/ä/ae/g;
$ffNodeName =~ s/ö/oe/g;
$ffNodeName =~ s/ü/ue/g;
$ffNodeName =~ s/Ä/Ae/g;
$ffNodeName =~ s/Ö/Oe/g;
$ffNodeName =~ s/Ü/Ue/g;
$ffNodeName =~ s/ß/sz/g;
$ff_json .= " \"name\": \"$ffNodeName\",\n";
my $ffNodeType;
if (defined($ffbsee_json->{"nodes"}->{"$ffkey"}->{"nodeinfo"}->{"software"}->{"firmware"}->{"release"})){
if (( $ffbsee_json->{"nodes"}->{"$ffkey"}->{"nodeinfo"}->{"software"}->{"firmware"}->{"release"} eq "server" )){
$ffNodeType = "AccessPoint";
} else {$ffNodeType = "AccessPoint";}
} else {$ffNodeType = "AccessPoint";}
$ff_json .= " \"node_type\": \"$ffNodeType\",\n";
$ff_json .= " \"position\": \{\n";
my $ff_lat;
my $ff_long;
if (defined($ffbsee_json->{"nodes"}->{"$ffkey"}->{"nodeinfo"}->{"location"}->{"latitude"})){
$ff_lat = $ffbsee_json->{"nodes"}->{"$ffkey"}->{"nodeinfo"}->{"location"}->{"latitude"};
$ff_long = $ffbsee_json->{"nodes"}->{"$ffkey"}->{"nodeinfo"}->{"location"}->{"longitude"};
}
else {
$keinGeo = 1; #Node wird nicht generiert weil kein Geo!
$ff_lat = "";
$ff_long = "";
if ($debug){print "No Geocoordinaten\n";}
}
$ff_json .= " \"lat\": $ff_lat,\n";
$ff_json .= " \"long\": $ff_long\n";
$ff_json .= " \},\n \"status\": \{\n";
my $ffclients = $ffbsee_json->{"nodes"}->{"$ffkey"}->{"statistics"}->{"clients"};
$ff_json .= " \"clients\": \"$ffclients\",\n";
my $ffNodeOnline;
if ($debug) { print $ffbsee_json->{"nodes"}->{"$ffkey"}->{"flags"}->{"online"}; }
if (($ffbsee_json->{"nodes"}->{"$ffkey"}->{"flags"}->{"online"} eq "true") or($ffbsee_json->{"nodes"}->{"$ffkey"}->{"flags"}->{"online"} eq 1) or ($ffbsee_json->{"nodes"}->{"$ffkey"}->{"flags"}->{"online"} eq "True")){
$ffNodeOnline = "true";
} else {$ffNodeOnline = "false";}
$ff_json .= " \"online\": \"$ffNodeOnline\"\n \}\n";
$ff_json .= " \}";
#
# Geo Koordinaten auswerten und JSON Files Communityspezifisch zusammen stellen
#
if ($keinGeo eq 1){
if ($debug) {print "Ueberspringen\n";}
$ff_json = "";
if ($ffNodeOnline){
if ($debug) {print "Node wird trotzdem gezaehlt\n";}
if ($ffNodeOnline){
$allnodes[0]++;
}
}
} else {
if ($subcommunity){ # Wenn es Subcommunitys gibt, dann...
# $community des aktuell auszuwertenden FF Node...
my $community = $ffbsee_json->{"nodes"}->{"$ffkey"}->{"nodeinfo"}->{"system"}->{"site_code"};
if ($community eq "bodensee"){
# Wenn noch die Standard community ("bodensee") eingetragen ist:
if ($debug){print "\nStandard community entdeckt.\nErmittlung von Standort anhand der GEO Position...\n";}
#
# # Ermittlung des ungefaehren Standorts
# # # --- Standortkonzept: ---
# Bodman- | | | | Ravensburg
# Ludwigshafen | Ueberlingen | | |_____________
# _____________| | Markdorf | |
# |--------------| | Friedrichshafen | Tettnang
# | B O D | |______________
# | Konstanz | E N |
# | | | S E E | Kressbronn
# | | | |______________
# | | | |
# | | | | Lindau
# # $ff_lat
if ($ff_long < 9.1){
if ($ff_lat < 47.74){
if ($ff_long < 9.2625){
# ueberlingen oder konstanz
if ($ff_lat < 47.7247){
if ($debug){print "! Konstanz\n";}
$community = "konstanz";
} else {
if ($debug){print "! ueberlingen";}
$community = "ueberlingen";
}
}
if ($debug){print "! bolu";}
$community = "bodman-ludwigshafen";
}
} elsif (($ff_long > 9.2625) and ($ff_long < 9.4065)){
if ($debug){print "! markdorf";}
$community = "markdorf";
} elsif (($ff_long > 9.4065 ) and ($ff_long < 9.5760)){
if ($debug){print "! friedrichshafen"}
$community = "friedrichshafen";
} else {
#rv, tettnang, krb, lindau
if ($ff_lat > 47.6932){
if ($debug){print "! ravensburg";}
$community = "ravensburg";
} elsif (($ff_lat > 47.6323) and ($ff_lat < 47.6932)){
if ($debug){print "! tettnang";}
$community = "tettnang";
} elsif (($ff_lat > 47.5753) and ($ff_lat < 47.6323)){
if ($debug){print "! kressbronn";}
$community = "kressbronn";
} else {
if ($debug){print "! lindau";}
$community = "lindau";
}
}
}
for (my $i = 0; $i < @ffcommunity; $i++) {
if ($community eq $community_name[$i]){
if ($runFirstTime[$i] eq 1){
$runFirstTime[$i] = 0;
} else { $json_ffbsee[$i] .= ",\n"; }
$json_ffbsee[$i] .= $ff_json;
$ff_json = "";
$ff_nodes[$i]++;
if ($ffNodeOnline){
$allnodes[$i]++;
}
if ($debug){print "\nNode zur Community "; print $community_name[$i]; print " hinzugefuegt!\n\n";}
}
}
}
# Falls keine Subcommunitys gefunden wurde die default community...
if ($ff_json ne ""){
if ($runFirstTime[0] eq 1){
$runFirstTime[0] = 0;
} else { $json_ffbsee[0] .= ",\n"; }
$json_ffbsee[0] .= $ff_json;
$ff_json = "";
$ff_nodes[0]++;
if ($ffNodeOnline){
$allnodes[0]++;
}
if ($debug){print "\nNode ist Teil von "; print $community_name[0];print"\n\n";}
}
}
}
#
# EOFFNodes
#
for(my $i = 0; $i < @ffcommunity; $i++) {
$json_ffbsee[$i] .= "\n \],\n \"updated_at\": \"$currentTime\",\n \"version\": \"$version\"\n\}";
# Öffne eine Datei und generiere das JSON
open (DATEI, '>:encoding(UTF-8)', $json_export[$i]) or die $!;
print DATEI $json_ffbsee[$i];
close (DATEI);
}
print "JSON Files wurden erzeugt\n";
if ($nodes){
print "\nAuswertung der Freifunk Communities:\n";
for (my $i = 0; $i < @ffcommunity; $i++) {
print "\nCommunity: ";
print $community_name[$i];
print "\n -> Nodes: ";
print $ff_nodes[$i];
if ($allnodes[$i] ne $ff_nodes[$i]){
print "\n ---> Alternative Nodes:";
print $allnodes[$i];
}
print "\n -> Nodes Laut API File:";
system "curl $api[$i] 2>/dev/null | grep \"nodes\" | cut -d: -f2 | cut -d, -f1";
}
print "\nJSON Files Updaten? (J/n):";
my $update = <STDIN>;
chomp $update;
if (($update eq "n") or ($update eq "N") or ($update eq "^C")){
print "\n\n";
exit;
}else {
print "\nUpdate der JSON Files\n";
for (my $i = 0; $i < @ffcommunity; $i++) {
my $api_nodes;
print "Nodes $community_name[$i]: $allnodes[$i] ";
my $a = <STDIN>;
chomp $a;
if ($a eq ""){
$a = $allnodes[$i];
}
$api_nodes = $a;
my $file = $api[$i];
$file =~ s/https:\/\/raw.githubusercontent.com\/ffbsee\/api\/master\///g;
print "$git_root/$file";
my $apijson;
open (DATEI, "$git_root/$file") or die $!;
while(<DATEI>){
$apijson = $apijson.$_;
}
$apijson =~ s/nodes\"\:\ [0-9]{1,5}/nodes\": $api_nodes/;
my $d = `date +%Y-%d-%mT%R:%S.%NZ`;
chomp $d;
$apijson =~ s/lastchange"\:\ \"[0-9]{1,4}-[0-9]{2}-[0-9]{2}T[0-9]{2}\:[0-9]{2}\:[0-9]{2}\.[0-9]{1,9}Z/lastchange\"\:\ \"$d/;
close (DATEI);
open (DATEI, ">$git_root/$file") or die $!;
print DATEI $apijson;
close (DATEI);
print "\n";
}
}
}