This repository has been archived by the owner on Nov 13, 2019. It is now read-only.
/
osd
executable file
·694 lines (630 loc) · 17 KB
/
osd
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#!/usr/bin/env ruby
require 'open-uri'
require "curses"
require 'sqlite3'
require 'thread'
require 'nokogiri'
require 'json'
require 'rexml/document'
scrlck = Mutex.new
include Curses
class Menu
attr_reader :window
attr_reader :items
def initialize(x = 0, y = 0, width = 80, height = 25, items = [])
@x, @y, @width, @height = x, y, width, height
@items = items
@index = 0
@scroll = 0
end
def visible_first
@scroll
end
def visible_last
[@items.count - 1, @scroll + @height - 3].min
end
def visible_range
visible_first..visible_last
end
def scroll
@scroll
end
def scroll=(v)
max = [0, @items.count + 2 - @height].max
@scroll = [v, max].min
end
def scroll_to index
while index > visible_last
self.scroll += 1
end
while index < visible_first
self.scroll -= 1
end
end
def refresh
@window.clear
active = [1, 1]
@items.each_with_index { |i, index|
next unless visible_range.include? index
@window.setpos index + 1 - @scroll, 1
@window.attrset(A_NORMAL)
@window.addstr " " * (@width - 2)
@window.setpos index + 1 - @scroll, 1
@window.attrset(index == @index ? A_STANDOUT : A_NORMAL)
@window.addstr i
active = [index + 1 - @scroll, 1] if index == @index
}
@window.attrset A_NORMAL
@window.box '|', '-'
@window.setpos *active
end
def run
@window = Window.new @height, @width, @y, @x
$refreshable.push @window
refresh
while ch = @window.getch
case ch
when 'w'
@index -= 1
when 's'
@index += 1
when 'x'
exit
when 27
ch = @window.getch
case ch
when '['
ch = @window.getch
case ch
when 'A'
@index -= 1
when 'B'
@index += 1
end
end
end
@index += @items.count
@index %= @items.count
scroll_to @index
refresh
end
$refreshable.remove @window
@window.close
@window = nil
end
end
# RX is receive, incoming. TX is transmitting, outgoing.
init_screen
start_color
noecho
start_color
init_pair 1, COLOR_RED, COLOR_BLACK
init_pair 2, COLOR_GREEN, COLOR_BLACK
init_pair 3, COLOR_YELLOW, COLOR_BLACK
init_pair 4, COLOR_BLACK, COLOR_RED
init_pair 5, COLOR_BLACK, COLOR_GREEN
init_pair 6, COLOR_BLACK, COLOR_YELLOW
def clearrange(x, y, w, h)
for yy in y...y+h
setpos yy, x
addstr " " * w
end
end
timer = Thread.new do
font = <<EOF.split("\n").map{|i|i.split('|')}
█▀▀█| ▄█ |█▀▀█|█▀▀█|█ █|█▀▀▀|█▀▀▀|█▀▀█|█▀▀█|█▀▀█
* *| * | ▄▀ |▄ ▀*|▀▀▀*|▀▀▀█|█▀▀█| *|*▀▀*|▀▀▀█
▀▀▀▀| ▀ |▀▀▀▀|▀▀▀▀| ▀|▀▀▀▀|▀▀▀▀| ▀|▀▀▀▀|▀▀▀▀
EOF
left = 63
blk = true
while true
scrlck.synchronize {
begin
attrset A_NORMAL
clearrange 62, 0, 23, 3
setpos 0, left
attron(color_pair(2))
now = Time.now
digi = now.strftime('%H%M').split('')
digi.each_with_index do |d,x|
3.times do |i|
setpos i, left + 5 * x + (x > 1 ? 2 : 0)
font[i][d.to_i].split('').each do |st|
if st == "*"
attrset A_STANDOUT
attron(color_pair(2))
addstr " "
else
attrset A_NORMAL
attron(color_pair(2))
addstr st
end
end
end
end
attrset A_NORMAL
attron(color_pair(2))
if blk = !blk
setpos 0, left+10
addstr "▄"
setpos 1, left+10
addstr "▄"
end
setpos 3, 62
addstr now.strftime("%Y-%m-%d 星期" + "日一二三四五六"[now.wday])
setpos 3, 82
addstr now.strftime(":%S")
attrset A_NORMAL
setpos 4, 62
addstr '周:一 二 三 四 五 六 日'
setpos 5, 62
monday = Date.today
today = Date.today
monday -= 1 while !monday.monday?
addstr '%02d' % ((monday.cweek < 35 ? 52 : 0) + monday.cweek - 35)
addstr ':'
7.times do |i|
if monday.day != today.day
attrset A_NORMAL
else
attrset A_STANDOUT
end
attron(color_pair(2)) if i >= 5
addstr monday.strftime("%d")
attrset A_NORMAL
addstr ' ' unless i == 6
monday += 1
end
rescue
end
}
sleep 1
end
end
pm25 = Thread.new do
top = 6
getpm25 = lambda {
body = open 'https://www.soupm25.com/ajax.php?mod=citydata&cityid=32' do |io| io.read end rescue nil
result = {}
if body
result = JSON.parse body
end
result
}
setcolor = lambda { |i|
if i > 200
attron(color_pair(4))
elsif i > 100
attron(color_pair(1))
elsif i > 50
attron(color_pair(3))
else
attron(color_pair(2))
end
}
resetcolor = lambda {
attrset A_STANDOUT
}
levelstr = lambda { |i|
if i > 300
return "危险"
elsif i > 200
return "很不健康"
elsif i > 100
return "不健康"
elsif i > 50
return "中"
else
return "优"
end
}
data = nil
while true
new_data = getpm25.()
if new_data
data = new_data
open 'pm25', "wb" do |io| io.write Marshal.dump data end
end
if !data
data = open 'pm25', 'rb' do |io| Marshal.load io.read end rescue nil
end
scrlck.synchronize {
begin
attrset A_STANDOUT
clearrange 62, top + 0, 23, 1
setpos top + 0, 62
time = Time.at data['citydata']['lasttime'].to_i
addstr time.strftime "空气质量 %m/%d %H:%M"
attrset A_NORMAL
clearrange 62, top + 1, 23, 2
setpos top + 1, 62
addstr "PM 2.5"
addstr "*" if data['citydata']['aqitype'] != '1'
setcolor.(data['citydata']['aqi25'].to_i)
setpos top + 1, 70
addstr " %s" % levelstr.(data['citydata']['aqi25'].to_i)
setpos top + 1, 82
addstr "%3d" % data['citydata']['aqi25'].to_i
resetcolor.()
attrset A_NORMAL
setpos top + 2, 62
addstr "*" if data['citydata']['aqitype'] == '1'
addstr "PM 10"
setcolor.(data['citydata']['aqi10'].to_i)
setpos top + 2, 70
addstr " %s" % levelstr.(data['citydata']['aqi10'].to_i)
setpos top + 2, 82
addstr "%3d" % data['citydata']['aqi10'].to_i
resetcolor.()
rescue
attrset A_STANDOUT
clearrange 62, top + 0, 23, 1
setpos top + 0, 62
addstr "空气质量 不可用"
attrset A_NORMAL
clearrange 62, top + 1, 23, 2
setpos top + 1, 62
addstr $!.message
end
refresh
}
sleep 60
end
end
weather = Thread.new do
top = 9
getweather = lambda {|d=0|
body = open 'http://php.weather.sina.com.cn/xml.php?city=%d5%f2%bd%ad&password=DJOYnieT8234jlsK&day='+d do |io| io.read end rescue nil
}
data = {}
new_data = {}
l = lambda { |x|
new_data[x] = getweather.(x)
if new_data[x]
data[x] = REXML::Document.new new_data[x]
open 'weather' + x, "wb" do |io| io.write new_data[x] end
end
if !data[x]
data[x] = open 'weather' + x, 'rb' do |io| REXML::Document.new io.read end rescue nil
end
}
pstatus = lambda { |t|
case t
when "晴", "多云"
attron(color_pair(2))
when /雨/, /雪/
attron(color_pair(1))
when "阴"
attron(color_pair(3))
end
addstr t
}
ppower = pwind = lambda { |t,pw|
case pw.split('-').last.to_i
when 0..4
when 5
attron(color_pair(3))
else
attron(color_pair(1))
end
addstr t
}
while true
3.times do |i| l.(i.to_s) end
scrlck.synchronize {
begin
attrset A_STANDOUT
clearrange 62, top + 0, 23, 1
setpos top + 0, 62
data['0'].to_s[/!-- published at ([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9]) --/]
time = Time.parse($1, '%Y-%m-%d %H:%M:%S')
addstr time.strftime "天气 %m/%d %H:%M"
attrset A_NORMAL
clearrange 62, top + 1, 23, 6
['0', '1', '2'].each do |day|
d = data[day]
date = Date.parse d.elements['//savedate_weather'].text
setpos top + 2 + day.to_i * 2, 62
addstr "日一二三四五六"[date.wday]
setpos top + 1 + day.to_i * 2, 62
addstr date.strftime "%d "
status1 = d.elements['//status1'].text
status2 = d.elements['//status2'].text
if status1 == status2
pstatus.(status1)
else
pstatus.(status1)
attrset A_NORMAL
addstr "转"
pstatus.(status2)
end
attrset A_NORMAL
setpos top + 1 + day.to_i * 2, 76
addstr "#{"%3d" % d.elements['//temperature1'].text.to_i}~#{"%3d" % d.elements['//temperature2'].text.to_i}°C"
attrset A_NORMAL
setpos top + 2 + day.to_i * 2, 65
wind1 = d.elements['//direction1'].text.gsub(/风$/,'')
power1 = d.elements['//power1'].text
wind2 = d.elements['//direction2'].text.gsub(/风$/,'')
power2 = d.elements['//power2'].text
if wind1 == wind2 && power1 == power2
pwind.(wind1,power1)
addstr "风"
ppower.(power1,power1)
attrset A_NORMAL
addstr "级"
elsif wind1 != wind2 && power1 == power2
pwind.(wind1,power1)
addstr "风"
attrset A_NORMAL
addstr "转"
pwind.(wind2,power1)
addstr "风"
ppower.(power1,power1)
attrset A_NORMAL
addstr "级"
elsif wind1 == wind2 && power1 != power2
pwind.(wind1,power1)
addstr "风"
ppower.(power1,power1)
attrset A_NORMAL
addstr "转"
ppower.(power2,power2)
attrset A_NORMAL
addstr "级"
elsif wind1 != wind2 && power1 != power2
pwind.(wind1,power1)
addstr "风"
ppower.(power1,power1)
attrset A_NORMAL
addstr "级转"
pwind.(wind2,power2)
addstr "风"
ppower.(power2,power2)
attrset A_NORMAL
addstr "级"
end
end
rescue
attrset A_STANDOUT
clearrange 62, top + 0, 23, 1
setpos top + 0, 62
addstr "天气 不可用"
attrset A_NORMAL
clearrange 62, top + 1, 23, 3
setpos top + 1, 62
addstr $!.message
addstr $!.backtrace.inspect
end
refresh
}
sleep 60
end
end
$status = {}
netcheck = Thread.new do
while true
result = open 'http://127.0.0.1:4567/ping' do |io| io.read end rescue nil
if result
result.split('&').each{|i|
ifname, status = i.split('=')
$status[ifname.to_sym] = status.to_i == 0 ? true : false
}
end
sleep 10
end
end
bwmon = Thread.new do
zone = {
"sum" => [],
"lwan" => [ "pppoe-lwan" ],
"gfw" => [ "tun+" ],
"wan" => [ "eth0.3", "pppoe-wan_uni" ]
}
rzone = {}
zone.each do |k, v|
v.each do |i|
rzone[i] = k
end
end
top = 0
limit = 9
width = 60
beforename = lambda { |i|
if i == "wan"
if $status[:wan]
attron(color_pair(2))
elsif $status[:wwan]
attron(color_pair(3))
else
attron(color_pair(1))
end
return " 公网"
elsif i == "lwan"
if $status[:lwan]
attron(color_pair(2))
else
attron(color_pair(1))
end
return " 校园"
elsif i == "gfw"
if $status[:gfw]
attron(color_pair(2))
else
attron(color_pair(1))
end
return " 加密"
elsif i == "sum"
return " Σ"
end
"%5s" % i
}
last_bw = nil
updatetitle = lambda {
if last_bw
scrlck.synchronize {
setpos top+0, 0
attrset A_STANDOUT
addstr " " * width
setpos top+0, 0
addstr "流量 (#{last_bw[0][1] - last_bw[0][0]} 秒平均) - #{Time.now.to_i - last_bw[0][1]} 秒前"
zone.keys.each_with_index do |i, index|
setpos top+0, width - (index + 1) * 7 + 1
attrset A_STANDOUT
addstr beforename.(i)
end
attrset A_NORMAL
refresh
}
end
}
while true
bw = (open 'http://127.0.0.1:4567/bwmon' do |io| eval io.read end rescue nil)
unless bw && (bw.is_a?(Array) && bw.length > 0) && last_bw != bw
5.times do
updatetitle.call
sleep 1
end
next
end
hosts = Hash[File.read('hosts').split("\n").map{|i|a=i.split(" ", 2);[a.first.downcase,a.last]}]
arp = (open 'http://127.0.0.1:4567/arp' do |io| eval io.read end rescue nil)
rarp = {}
arp.each do |k, v| rarp[v] = k end if arp
last_bw = bw
bws = bw.group_by{|i| i[2]}.map do |k, v|
pp = v.group_by{|i|[i[4], rzone[i[3]]]}
sum = [0, 0]
pp.keys.each do |group|
pp[group] = pp[group].inject(0){|i,k|i += k[5]}
sum[group[0]] += pp[group]
end
pp[[0, "sum"]] = sum[0]
pp[[1, "sum"]] = sum[1]
[k, pp]
end
bws = bws.sort_by {|i| i[1][[0, "sum"]] + i[1][[1, "sum"]]}.reverse
sum = {}
bws.each do |k,v|
v.each do |group, value|
sum[group] ||= 0
sum[group] += value
end
end
bws << ["sum", sum]
timestart = Time.at(bw[0][0]).strftime("%H:%M:%S")
timeend = Time.at(bw[0][1]).strftime("%H:%M:%S")
timelen = bw[0][1] - bw[0][0]
scrlck.synchronize {
attrset A_NORMAL
clearrange 0, top+0, width, 3 + limit * 2
setpos top+0, 0
attrset A_STANDOUT
addstr " " * width
setpos top+0, 0
addstr "流量 (#{bw[0][1] - bw[0][0]} 秒平均) - #{Time.now.to_i - bw[0][1]} 秒前"
zone.keys.each_with_index do |i, index|
setpos top+0, width - (index + 1) * 7 + 1
attrset A_STANDOUT
addstr beforename.(i)
end
attrset A_NORMAL
setpos top+limit * 2 + 1, 0
attrset A_STANDOUT
addstr " Σ "
attrset A_NORMAL
addstr " ... "
if bws.length > limit
addstr "(+ #{bws.length - limit})"
end
attron A_UNDERLINE
clearrange 0, top+limit * 2, width, 1
attroff A_UNDERLINE
alwaysunderline = false
bws.each_with_index do |i, index|
next if index >= limit && i[0] != "sum"
index = limit if i[0] == "sum"
host = i[0]
data = i[1]
unless i[0] == "sum"
setpos top+index * 2 + 1, 0
addstr "#{host} - #{rarp[host]}"
setpos top+index * 2 + 2, 2
attron A_UNDERLINE if alwaysunderline || index + 1 == limit
addstr hosts[host] if hosts[host]
attroff A_UNDERLINE if alwaysunderline || index + 1 == limit
end
setpos top+index * 2 + 1, width - (zone.keys.count) * 7 - 1
attron(color_pair(2))
addstr "↓"
attroff(color_pair(2))
setpos top+index * 2 + 2, width - (zone.keys.count) * 7 - 1
attron A_UNDERLINE if alwaysunderline || index + 1 == limit
attron(color_pair(1))
addstr "↑"
attroff(color_pair(1))
attroff A_UNDERLINE if alwaysunderline || index + 1 == limit
2.times do |dir|
zone.keys.each_with_index do |i, i2|
setpos top+index * 2 + 1 + dir, width - (i2 + 1) * 7 + 1
d = data[[dir, i]] || 0
d /= 1.0 * timelen
d *= 8.0
unit = " "
if d >= 1000
d /= 1024.0
unit = "K"
end
if d >= 1000
d /= 1024.0
unit = "M"
end
if d >= 1000
d /= 1024.0
unit = "G"
end
attron A_UNDERLINE if (alwaysunderline || index + 1 == limit) && dir == 1
attron(color_pair(2)) if unit == "M"
attron(color_pair(3)) if unit == "K"
addstr "%5.1f%c" % [d, unit]
attroff(color_pair(2)) if unit == "M"
attroff(color_pair(3)) if unit == "K"
attroff A_UNDERLINE if (alwaysunderline || index + 1 == limit) && dir == 1
end
end
end
refresh
}
end
end
statusbar = Thread.new do
loop do
scrlck.synchronize {
attrset A_STANDOUT
clearrange 0, 21, 85, 1
setpos 21,0
addstr "#{Time.now.strftime("%Y-%m-%d")}#{`uptime`.chomp}"
refresh
}
sleep 1
end
end
=begin
result = `iw wlan0 scan`
aps = result.split(/^BSS/).select { |i| i.length > 0 }
ssids = aps.map { |ap|
ap[/^\tSSID: (.*)$/] ? $1 : nil
}.sort.uniq.map { |i|
i.gsub(/\\x(..)/) { $1.to_i(16).chr }.force_encoding("utf-8")
}
p ssids
menu = Menu.new 0, 0, 20, 10, ssids
menu.run
=end
bwmon.join
statusbar.join
pm25.join
weather.join
timer.join
exit