parse_p1 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ * 0.0.4
2
+ Make parsing of a P1 record more robust
3
+
1
4
  * 0.0.3
2
5
  Bug fix in determining if P1 data is valid
3
6
 
data/lib/parse_p1/base.rb CHANGED
@@ -16,7 +16,13 @@ module ParseP1
16
16
  end
17
17
 
18
18
  def device_id
19
- data.match(/^\/([a-zA-Z]{3}\d{1}.+)\r$/)
19
+ match_within_one_p1_record('\/([a-zA-Z]{3}\d{1}.+)\r$')
20
+ end
21
+
22
+ private
23
+
24
+ def match_within_one_p1_record(pattern)
25
+ data.match(/[\W|\w]*#{pattern}[\W|\w]*!/)
20
26
  $1
21
27
  end
22
28
 
@@ -3,13 +3,12 @@ module ParseP1
3
3
  module Electricity
4
4
 
5
5
  def electra_meter_id
6
- data.match(/0:96.1.1\S(\d{1}[A-Z]{1}\d{1,96})\S/)
7
- $1
6
+ match_within_one_p1_record('0:96.1.1\S(\d{1}[A-Z]{1}\d{1,96})\S')
8
7
  end
9
8
 
10
9
  def electricity_tariff_indicator
11
- data.match(/0-0:96.14.0\S(\d{1,9})\S/)
12
- $1.to_i
10
+ result = match_within_one_p1_record('0-0:96.14.0\S(\d{1,9})\S')
11
+ result.to_i if result
13
12
  end
14
13
 
15
14
  def electricity_actual_threshold
@@ -31,13 +30,13 @@ module ParseP1
31
30
  private
32
31
 
33
32
  def get_electricity(obis_code)
34
- data.match(/#{obis_code}\S(\d{1,9}\.\d{1,3})\S/)
35
- $1.to_f
33
+ data.match(/\/[\W|\w]*#{obis_code}\S(\d{1,9}\.\d{1,3})\S[\W|\w]*!/)
34
+ $1.to_f if $1
36
35
  end
37
36
 
38
37
  def get_actual_electricity(type)
39
38
  power = get_electricity("1-0:#{first_electricity_code(type)}.7.0")
40
- (power * 1000).to_i #Return as watts instead of kW
39
+ (power * 1000).to_i if power#Return as watts instead of kW
41
40
  end
42
41
 
43
42
  def first_electricity_code(code)
data/lib/parse_p1/gas.rb CHANGED
@@ -3,24 +3,22 @@ module ParseP1
3
3
  module Gas
4
4
 
5
5
  def gas_meter_id
6
- data.match(/1:96.1.0\S(\d{1,96})\S/)
7
- $1
6
+ match_within_one_p1_record('1:96.1.0\S(\d{1,96})\S')
8
7
  end
9
8
 
10
9
  #Only 2 digits for year!
11
10
  def last_hourly_reading_gas
12
- data.match(/0-1:24.3.0\S(\d{12})\S/)
13
- DateTime.new(('20'+$1[0..1]).to_i, $1[2..3].to_i, $1[4..5].to_i, $1[6..7].to_i, $1[8..9].to_i)
11
+ result = match_within_one_p1_record('0-1:24.3.0\S(\d{12})\S')
12
+ DateTime.new(('20'+result[0..1]).to_i, result[2..3].to_i, result[4..5].to_i, result[6..7].to_i, result[8..9].to_i) if result
14
13
  end
15
14
 
16
15
  def measurement_unit_gas
17
- data.match(/\S0-1:24.2.1\S\S(\w+)\S/)
18
- $1
16
+ match_within_one_p1_record('0-1:24.2.1\S\S(\w+)\S')
19
17
  end
20
18
 
21
19
  def gas_usage
22
- data.match(/\S0-1:24.2.1\S\S\w+\S\r\n\S(\d+.\d+)\S/)
23
- $1.to_f
20
+ result = match_within_one_p1_record('\S0-1:24.2.1\S\S\w+\S\r\n\S(\d+.\d+)\S')
21
+ result.to_f if result
24
22
  end
25
23
 
26
24
  end
@@ -1,3 +1,3 @@
1
1
  module ParseP1
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -3,8 +3,8 @@ require 'helper'
3
3
  class TestParseP1 < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
- #Data string as received on a Ruby on Rails application
7
- @data = "\r\n/ABc1\\1AB123-4567\r\n\r\n0-0:96.1.1(1A123456789012345678901234567890)\r\n1-0:1.8.1(00136.787*kWh)\r\n1-0:1.8.2(00131.849*kWh)\r\n1-0:2.8.1(00002.345*kWh)\r\n1-0:2.8.2(00054.976*kWh)\r\n0-0:96.14.0(0002)\r\n1-0:1.7.0(0003.20*kW)\r\n1-0:2.7.0(0000.12*kW)\r\n0-0:17.0.0(0999.00*kW)\r\n0-0:96.3.10(1)\r\n0-0:96.13.1()\r\n0-0:96.13.0()\r\n0-1:24.1.0(3)\r\n0-1:96.1.0(1234567890123456789012345678901234)\r\n0-1:24.3.0(120502150000)(00)(60)(1)(0-1:24.2.1)(m3)\r\n(00092.112)\r\n0-1:24.4.0(1)\r\n!"
6
+ #Data string as received on a Ruby on Rails application with some uncleared data in front of it
7
+ @data = "sBs\u0012C\u0002\u0002\nK*r\"CKR[Wh)\r\n1-0:2.8.1(00000.000*kWh)\r\n1-0:2.8.2(00000.\r\n/ABc1\\1AB123-4567\r\n\r\n0-0:96.1.1(1A123456789012345678901234567890)\r\n1-0:1.8.1(00136.787*kWh)\r\n1-0:1.8.2(00131.849*kWh)\r\n1-0:2.8.1(00002.345*kWh)\r\n1-0:2.8.2(00054.976*kWh)\r\n0-0:96.14.0(0002)\r\n1-0:1.7.0(0003.20*kW)\r\n1-0:2.7.0(0000.12*kW)\r\n0-0:17.0.0(0999.00*kW)\r\n0-0:96.3.10(1)\r\n0-0:96.13.1()\r\n0-0:96.13.0()\r\n0-1:24.1.0(3)\r\n0-1:96.1.0(1234567890123456789012345678901234)\r\n0-1:24.3.0(120502150000)(00)(60)(1)(0-1:24.2.1)(m3)\r\n(00092.112)\r\n0-1:24.4.0(1)\r\n!"
8
8
  @p1 = ParseP1::Base.new(@data)
9
9
  end
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parse_p1
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-05-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shoulda
16
- requirement: &2151909720 !ruby/object:Gem::Requirement
16
+ requirement: &2151890700 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *2151909720
24
+ version_requirements: *2151890700
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: guard
27
- requirement: &2151909100 !ruby/object:Gem::Requirement
27
+ requirement: &2151889740 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2151909100
35
+ version_requirements: *2151889740
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard-test
38
- requirement: &2151908540 !ruby/object:Gem::Requirement
38
+ requirement: &2151888920 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2151908540
46
+ version_requirements: *2151888920
47
47
  description: Parsing P1 Companion Standard used by Dutch Smart Meters. Used in combination
48
48
  with a Nanode posting the P1 data to emonWeb.org
49
49
  email: