1 | #!/usr/bin/env ruby |
---|
2 | # |
---|
3 | # Author: Norbert Wigbels - foobla.wigbels.de/uber-foobla |
---|
4 | # |
---|
5 | # HotHotRead continously reads the status of a |
---|
6 | # reed contact attached to the avr-net-io |
---|
7 | # |
---|
8 | # The reed measures gas usage. |
---|
9 | # |
---|
10 | # A magig URL is formed and called; the url |
---|
11 | # stores the sensor-data to wigbels.net |
---|
12 | # |
---|
13 | # todo: status-led |
---|
14 | # |
---|
15 | |
---|
16 | #------------------------------------------ |
---|
17 | # Tainted mode 0-4 |
---|
18 | $SAFE=0 |
---|
19 | |
---|
20 | |
---|
21 | #------------------------------------------ |
---|
22 | require 'logger' |
---|
23 | require 'socket' |
---|
24 | require 'net/http' |
---|
25 | |
---|
26 | #------------------------------------------ |
---|
27 | class HotHotRead < Logger::Application |
---|
28 | attr_accessor :socket, :socketaddr |
---|
29 | |
---|
30 | def initialize(application_name) |
---|
31 | super(application_name) |
---|
32 | @socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) |
---|
33 | @sockaddr = Socket.sockaddr_in(2701, '192.168.14.42') |
---|
34 | end |
---|
35 | |
---|
36 | def run |
---|
37 | begin |
---|
38 | @socket.connect(@sockaddr) |
---|
39 | high2low = false; |
---|
40 | while true do |
---|
41 | @socket.print "io get pin 0\r\n" |
---|
42 | rawdata = @socket.recvfrom(128) |
---|
43 | # data contains "port 0: 0xfe"; extract last part |
---|
44 | data = rawdata.to_s.split(" ")[2] |
---|
45 | # we are only interested in the changes from high to low of the reed sensor |
---|
46 | if (data=="0xff") |
---|
47 | high2low = true |
---|
48 | else |
---|
49 | if (high2low==true) |
---|
50 | high2low = false |
---|
51 | Net::HTTP.get(URI.parse('http://www.wigbels.net/cgi-bin/hhw.rb?sensorid=1&data=10')) |
---|
52 | # fixme: response.body auswerten |
---|
53 | # bei fehler, lokales backlog schreibe |
---|
54 | log(INFO, 'added 100 liter of gas to sensor-database') |
---|
55 | end |
---|
56 | end |
---|
57 | sleep(0.2) |
---|
58 | end |
---|
59 | # and terminate the connection when we're done |
---|
60 | rescue => msg |
---|
61 | log(ERROR, "error: #{msg}") |
---|
62 | retry |
---|
63 | ensure |
---|
64 | @socket.close |
---|
65 | end |
---|
66 | #puts "Filename: "+__FILE__.to_s |
---|
67 | #puts "Linenumber: "+__LINE__.to_s |
---|
68 | #log(WARN, 'warning', 'mymethod' ) |
---|
69 | #@log.error('my_method2') { 'Error!' } |
---|
70 | end |
---|
71 | end |
---|
72 | |
---|
73 | |
---|
74 | #------------------------------------------ |
---|
75 | status = HotHotRead.new("HotHotRead").start |
---|
76 | |
---|
77 | #if status != 0 |
---|
78 | # puts "Some error occured." |
---|
79 | #end |
---|