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
#!/usr/bin/env ruby
DWMRP_VERSION = "0.0.1"
DWMRP_RELEASE_DATE = "2008-07-22"

puts "#{$$}"

require 'thread'
require 'net/http'
require 'time'

class File
  def self.mkfifo(name)
    system("mkfifo", name)
    return name
  end
end

command = "ps ax|grep '#{$0} #{ENV['DWM_PIPE']}'|grep -v grep"
proc = `#{command}`.strip.scan(/^[0-9]+/)[0].to_i
if proc > 0 and proc != $$
  system "kill -9 #{proc}" 
end
weather_fifo = File.mkfifo("/tmp/dwm.weather-#{ENV['DISPLAY']}")
dwm_fifo = ARGV[0]
open_mode = "w+"
temperature = ""
ausgabe = ""
datetime = ""

# weather
Thread.new do
  agent = 'dwm-ruby-plugins #{DWMRP_VERSION} (#{DWMRP_RELEASE_DATE})'
  metar = "eddv"
  metar = metar.to_s
  loop do
    begin
      http = Net::HTTP.new('weather.noaa.gov', 80)
      page = http.start do
        req = Net::HTTP::Get.new('http://weather.noaa.gov/pub/data/observations/metar/decoded/'+metar.upcase+'.TXT', 'User-Agent' => agent)
        http.request(req).body
      end
      page = page.to_a
      page[0] = "Location: " + page[0].chomp
      page[1] = "Date: " + page[1].chomp
      feld = Hash.new
      feld["Wind"], feld["Relative Humidity"], feld["Sky conditions"] = '','','';
      page.each do |line|
        name, wert = line.split(/\s*:\s*/, 2)
        feld[name] = wert
      end
      temperature = feld["Temperature"].scan(/\((.+)\)/).to_s
      temperature.sub!(/ /,  ' ')
      ort = feld["Location"].scan(/(^[^)]+\))/).to_s
      date = feld["Date"].scan(/\/ ([0-9]{4})\.([0-9]{2})\.([0-9]{2}) ([0-9]{2})([0-9]{2}) UTC/)
      year, month, day, hour, minute = date[0][0..4]
      gmt_time = Time.gm(year, month, day, hour, minute, 0)
      localtime =  gmt_time.localtime
      ausgabe = <<EOF
Current conditions at #{ort}
Last updated #{localtime}
Wind: #{feld["Wind"].chomp}
Temperature: #{temperature}
Relative Humidity: #{feld["Relative Humidity"].chomp}
Sky conditions: #{feld["Sky conditions"].chomp}
EOF
   rescue
      $stderr.puts $!
      if ausgabe == ""
        ausgabe = $!
        temperature = "--"
      end    
    ensure
      File.open(weather_fifo, open_mode) do |f2|
        f2.puts ausgabe
      end
    end
    sleep 15*60
  end
end

# date and time
Thread.new do
#  currload = nil
#  Thread.new{ loop { currload = `uptime`.chomp.sub(/.*: /,"").gsub(/,/,""); sleep 10 } }
#  text_proc = lambda { "#{Time.new.strftime("%d.%m.%Y %X")} #{currload}" } 
  text_proc = lambda { "#{Time.new.strftime("%d.%m.%Y %X")}" } 
  loop do
    datetime = text_proc.call
    sleep 1
  end
end

# volume
vol=nil
Thread.new do
  loop do
    vol=`mpc`.chomp.split("\n")[-1].scan(/volume: *([0-9]+%)/)
    sleep 1
  end
end

# now playing
np=nil
np_rest=nil
np_maxlength = 50
np_toleranz = 3
np_toleranz_akt = 0
Thread.new do
  loop do
    np_temp=`mpc`.chomp.split("\n")[0].gsub(/volume: *[0-9]+%.*$/, "nothing").to_s
    if(np_temp != "nothing") 
      np_rest=" "+`mpc`.chomp.split("\n")[1].scan(/(\([0-9]+%\))/).to_s
    else 
      np_rest=nil
    end
    if np_temp.length <= np_maxlength + np_toleranz
      np_toleranz_akt = np_toleranz
      np_dots = ""
    else
      np_toleranz_akt = -3
      np_dots = "..."
    end
    np = np_temp.gsub("\n", "")
    sleep 1
  end
end


# main loop
loop do
  File.open(dwm_fifo, open_mode) do |f1|
    f1.puts  "#{np}#{np_rest} | #{vol} | #{temperature.chomp} | #{datetime}"
  end
  sleep 1
end