msg-chumby-display 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/bin/msg-chumby-daemon
CHANGED
@@ -6,23 +6,51 @@ $:.unshift << libpath
|
|
6
6
|
require 'rubygems'
|
7
7
|
require 'msg-chumby-daemon.rb'
|
8
8
|
|
9
|
+
#TODO: Make generic. This is the ID of the CC-HPC flukso.
|
10
|
+
sensor_id="67e9fa794684e4902730be208e9d734e"
|
11
|
+
token="bb8640e074de14b9d444be7b1359707f"
|
12
|
+
|
9
13
|
reading_cache=MSG_Chumby::Reading_Cache.new();
|
10
14
|
$demo_importer=MSG_Chumby::DemoReadingImporter.new(reading_cache, 400);
|
11
|
-
$
|
12
|
-
$
|
15
|
+
$lasthour_importer=MSG_Chumby::LastHourImporter.new(reading_cache, sensor_id, token);
|
16
|
+
$lastday_importer=MSG_Chumby::LastDayImporter.new(reading_cache, sensor_id, token);
|
17
|
+
$shortterm_thread=nil;
|
18
|
+
$longterm_thread=nil;
|
19
|
+
$server=MSG_Chumby::HTTP_XML_Server.new("0.0.0.0", 3000, reading_cache);
|
20
|
+
|
13
21
|
|
14
22
|
def startAll
|
15
23
|
$server.start();
|
16
|
-
$
|
17
|
-
|
24
|
+
$shortterm_thread=Thread.new() {
|
25
|
+
loop do
|
26
|
+
puts "Running short term import"
|
27
|
+
begin
|
28
|
+
$demo_importer.doWork();
|
29
|
+
rescue StandardError => e
|
30
|
+
puts "ERROR in short-term thread: #{e}"
|
31
|
+
end
|
32
|
+
sleep(3);
|
33
|
+
end
|
34
|
+
}
|
35
|
+
$longterm_thread=Thread.new() {
|
36
|
+
loop do
|
37
|
+
puts "Running long term import"
|
38
|
+
begin
|
39
|
+
$lasthour_importer.doWork();
|
40
|
+
$lastday_importer.doWork();
|
41
|
+
rescue StandardError => e
|
42
|
+
puts "ERROR in long-term thread: #{e}"
|
43
|
+
end
|
44
|
+
sleep(10);
|
45
|
+
end
|
18
46
|
}
|
19
47
|
$server.join();
|
20
|
-
$importer_thread.join();
|
21
48
|
end
|
22
49
|
|
23
50
|
def stopAll
|
51
|
+
$shortterm_thread.stop() unless $importer_thread.stop?;
|
52
|
+
$longterm_thread.stop() unless $importer_thread.stop?;
|
24
53
|
$server.stop()
|
25
|
-
$importer_thread.stop() unless $importer_thread.stop?;
|
26
54
|
exit;
|
27
55
|
end
|
28
56
|
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'xmlsimple'
|
3
3
|
require 'mongrel'
|
4
|
+
#require 'pp'
|
4
5
|
|
5
6
|
module MSG_Chumby
|
6
7
|
$counter=0;
|
@@ -48,6 +49,74 @@ module MSG_Chumby
|
|
48
49
|
end
|
49
50
|
end
|
50
51
|
|
52
|
+
class LastHourHandler < Mongrel::HttpHandler
|
53
|
+
def initialize(reading_cache)
|
54
|
+
@reading_cache=reading_cache;
|
55
|
+
end
|
56
|
+
def process(request, response)
|
57
|
+
response.start(200) do |head,out|
|
58
|
+
head["Content-Type"] = "text/xml"
|
59
|
+
readings=(@reading_cache.last_hour())
|
60
|
+
# Even if there are currently no readings we need to provide
|
61
|
+
# them.
|
62
|
+
if readings==nil
|
63
|
+
readings = Array.new();
|
64
|
+
(0..59).each {|i|
|
65
|
+
timestamp = Time.now.to_i - (i * 60)
|
66
|
+
readings << Flukso::UTCReading.new(timestamp, 0.0)
|
67
|
+
}
|
68
|
+
end
|
69
|
+
flat_data=Array.new;
|
70
|
+
readings.each{|reading|
|
71
|
+
if not reading.nan? #(reading.value*1.0).nan?
|
72
|
+
# Skip NaN values.
|
73
|
+
#else
|
74
|
+
time=Time.at(reading.utc_timestamp);
|
75
|
+
current_reading=
|
76
|
+
{'time' => [ time.strftime("%H:%M:%S")], 'value' => [reading.value]};
|
77
|
+
flat_data << current_reading
|
78
|
+
end
|
79
|
+
}
|
80
|
+
#pp flat_data
|
81
|
+
out.write(XmlSimple.xml_out( { 'reading' => flat_data} ,{'RootName' => "last_hour"}));
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
class LastDayHandler < Mongrel::HttpHandler
|
86
|
+
def initialize(reading_cache)
|
87
|
+
@reading_cache=reading_cache;
|
88
|
+
end
|
89
|
+
def process(request, response)
|
90
|
+
response.start(200) do |head,out|
|
91
|
+
head["Content-Type"] = "text/xml"
|
92
|
+
readings=(@reading_cache.last_day())
|
93
|
+
# Even if there are currently no readings we need to provide
|
94
|
+
# them.
|
95
|
+
if readings==nil
|
96
|
+
readings = Array.new();
|
97
|
+
(0..95).each {|i|
|
98
|
+
timestamp = Time.now.to_i - (i * 60 * 15)
|
99
|
+
readings << Flukso::UTCReading.new(timestamp, 0.0)
|
100
|
+
}
|
101
|
+
end
|
102
|
+
flat_data=Array.new;
|
103
|
+
readings.each{|reading|
|
104
|
+
if not reading.nan? #(reading.value*1.0).nan?
|
105
|
+
# Skip NaN values.
|
106
|
+
time=Time.at(reading.utc_timestamp);
|
107
|
+
current_reading= {
|
108
|
+
'dayofyear' => [ time.strftime("%j")],
|
109
|
+
'time' => [ time.strftime("%H:%M:%S")],
|
110
|
+
'value' => [reading.value]
|
111
|
+
};
|
112
|
+
flat_data << current_reading
|
113
|
+
end
|
114
|
+
}
|
115
|
+
#pp flat_data
|
116
|
+
out.write(XmlSimple.xml_out( { 'reading' => flat_data} ,{'RootName' => "last_day"}));
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
51
120
|
|
52
121
|
class HTTP_XML_Server
|
53
122
|
def initialize(host, port, reading_cache)
|
@@ -55,6 +124,8 @@ module MSG_Chumby
|
|
55
124
|
@server.register("/time", TimeHandler.new)
|
56
125
|
@server.register("/reset", ResetHandler.new)
|
57
126
|
@server.register("/last_reading", LastReadingHandler.new(reading_cache))
|
127
|
+
@server.register("/last_hour", LastHourHandler.new(reading_cache))
|
128
|
+
@server.register("/last_day", LastDayHandler.new(reading_cache))
|
58
129
|
end
|
59
130
|
def start
|
60
131
|
@threads=@server.run
|
@@ -5,6 +5,10 @@ module MSG_Chumby
|
|
5
5
|
def initialize
|
6
6
|
@last_reading=nil;
|
7
7
|
@last_reading_mutex=Mutex.new();
|
8
|
+
@last_hour=nil;
|
9
|
+
@last_hour_mutex=Mutex.new();
|
10
|
+
@last_day=nil;
|
11
|
+
@last_day_mutex=Mutex.new();
|
8
12
|
end
|
9
13
|
def update_last_reading(reading)
|
10
14
|
raise "Please provide a UTCReading instance" if reading.class != Flukso::UTCReading
|
@@ -19,5 +23,33 @@ module MSG_Chumby
|
|
19
23
|
end
|
20
24
|
return retval
|
21
25
|
end
|
26
|
+
def update_last_hour(readings)
|
27
|
+
raise "Please provide an Array instance" if readings.class != Array
|
28
|
+
@last_hour_mutex.synchronize do
|
29
|
+
@last_hour=readings
|
30
|
+
end
|
31
|
+
end
|
32
|
+
def last_hour
|
33
|
+
retval=nil
|
34
|
+
@last_hour_mutex.synchronize do
|
35
|
+
retval=@last_hour
|
36
|
+
end
|
37
|
+
return retval
|
38
|
+
end
|
39
|
+
def update_last_day(readings)
|
40
|
+
raise "Please provide an Array instance" if readings.class != Array
|
41
|
+
@last_day_mutex.synchronize do
|
42
|
+
@last_day=readings
|
43
|
+
end
|
44
|
+
end
|
45
|
+
def last_day
|
46
|
+
retval=nil
|
47
|
+
@last_day_mutex.synchronize do
|
48
|
+
retval=@last_day
|
49
|
+
end
|
50
|
+
return retval
|
51
|
+
end
|
52
|
+
|
53
|
+
|
22
54
|
end
|
23
55
|
end
|
@@ -5,21 +5,65 @@ if __FILE__ == $0
|
|
5
5
|
end
|
6
6
|
|
7
7
|
module MSG_Chumby
|
8
|
+
BASE_URL="https://api.mysmartgrid.de/sensor"
|
8
9
|
class DemoReadingImporter
|
9
10
|
def initialize(reading_cache, max_reading)
|
10
11
|
@reading_cache=reading_cache
|
11
12
|
@max_reading=max_reading
|
12
13
|
end
|
13
14
|
def doWork
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
@reading_cache.update_last_reading(reading);
|
19
|
-
sleep(3);
|
20
|
-
end
|
15
|
+
value=rand(@max_reading)
|
16
|
+
reading=Flukso::UTCReading.new(Time.now.to_i, value)
|
17
|
+
#puts "generated random reading #{reading}"
|
18
|
+
@reading_cache.update_last_reading(reading);
|
21
19
|
end
|
22
20
|
end
|
21
|
+
class LastHourImporter
|
22
|
+
def initialize(reading_cache, sensor_id, token)
|
23
|
+
@reading_cache=reading_cache
|
24
|
+
auth=Flukso::TokenAuth.new(token);
|
25
|
+
@api=Flukso::API.new(auth, BASE_URL);
|
26
|
+
@query=Flukso::QueryReadings.new(sensor_id, :hour, :watt)
|
27
|
+
end
|
28
|
+
def doWork
|
29
|
+
begin
|
30
|
+
readings=@query.execute(@api);
|
31
|
+
rescue Exception => e
|
32
|
+
puts "Query failed: #{e}"
|
33
|
+
puts "Used #{BASE_URL} as BASE_URL"
|
34
|
+
exit(-10);
|
35
|
+
end
|
36
|
+
#puts "Got Response:"
|
37
|
+
#readings.each{|reading|
|
38
|
+
# puts reading
|
39
|
+
#}
|
40
|
+
@reading_cache.update_last_hour(readings);
|
41
|
+
end
|
42
|
+
end
|
43
|
+
class LastDayImporter
|
44
|
+
def initialize(reading_cache, sensor_id, token)
|
45
|
+
@reading_cache=reading_cache
|
46
|
+
auth=Flukso::TokenAuth.new(token);
|
47
|
+
@api=Flukso::API.new(auth, BASE_URL);
|
48
|
+
@query=Flukso::QueryReadings.new(sensor_id, :day, :watt)
|
49
|
+
end
|
50
|
+
def doWork
|
51
|
+
begin
|
52
|
+
readings=@query.execute(@api);
|
53
|
+
rescue Exception => e
|
54
|
+
puts "Query failed: #{e}"
|
55
|
+
puts "Used #{BASE_URL} as BASE_URL"
|
56
|
+
exit(-10);
|
57
|
+
end
|
58
|
+
#puts "Got Response:"
|
59
|
+
#readings.each{|reading|
|
60
|
+
# puts reading
|
61
|
+
#}
|
62
|
+
@reading_cache.update_last_day(readings);
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
|
23
67
|
end
|
24
68
|
|
25
69
|
if __FILE__ == $0
|
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: msg-chumby-display
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mathias Dalheimer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-06-
|
12
|
+
date: 2010-06-08 00:00:00 +02:00
|
13
13
|
default_executable: msg-chumby-daemon
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|