-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoller.rb
executable file
·65 lines (59 loc) · 1.63 KB
/
poller.rb
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
#!/usr/bin/ruby -w
require 'rubygems'
require 'nokogiri'
require 'mysql2'
# Reads the XML file, inserts changes into database
#
def poller
#mysql connection
db = Mysql2::Client.new(:host => 'localhost', :username => 'shop', :password => '123p4ss', :database => 'sensor')
f = File.open('status.xml')
# XML object
doc = Nokogiri::XML(f)
f.close
# parsing XML file
sensors = doc.xpath('//UPRAISED/*')
#one timestamp for every change in this iteration
t = Time.new.to_i.to_s
q = "SELECT
u1.sensor_id,
u1.direction,
u1.timestamp
FROM
upraised u1
JOIN (
SELECT
sensor_id,
MAX(timestamp) AS ts
FROM
upraised
GROUP BY
sensor_id) u2 ON u1.sensor_id = u2.sensor_id AND u1.timestamp = u2.ts"
res = db.query(q)
# previous states from the database
previous_sensors = []
res.each { |r|
previous_sensors << r
}
sensors.each do |s|
# array of strings
data = [s.name.gsub(/Sensor/, ''), s.text=='OFF'?'0':'1', t]
# from the given data only the new ones should be inserted
if 0 == previous_sensors.count or previous_sensors.detect { |ps| ps['sensor_id'].to_s == s.name.gsub(/Sensor/, '') and ps['direction'].to_s != data[1] }
q = "INSERT INTO upraised (sensor_id, direction, timestamp) VALUES (" + data[0] + ", " + data[1] + ", " + data[2] + ")"
db.query(q)
end
end
sensors
end
# Main loop for polling the XML file into the database
#
def main
sensor_data = nil
while true
sensor_data = poller
#sleep 2 seconds
sleep 2
end
end
main