-
Notifications
You must be signed in to change notification settings - Fork 0
/
nibe_uplink_parser.rb
102 lines (93 loc) · 3.12 KB
/
nibe_uplink_parser.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
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
# nibe_uplink_parser.rb
# frozen_string_literal: true
# Helper methods for interfacing nibe uplink with OpenHAB
module NibeUplinkParser
# Splits parameters into smaller arrays to comply with the nibe uplink API
def split_parameters(par)
pages = (par.size - 1) / 15 + 1
parameters = []
pages.times do |i|
remainder = par.size - (i * 15)
parameters[i] = par.slice(i * 15, remainder < 15 ? remainder : 15)
end
parameters
end
# Parses the returned parameters from Nibe Uplink and puts into a hash
# of parameterId => value
def parse_parameters(json)
hash = JSON.parse(json)
result = {}
hash.each { |h| result[h['parameterId']] = h['rawValue'] }
result
end
# Convert raw values to correct decimal value
def convert_parameters(hash)
hash.update(hash) do |k, v|
case k
when 40004, 40067, 40013, 40014, 43136, 40050, 40032, 43009, 40047, 40048, 43008, 40007, 40129, 43005
v / 10.0
when 43084
v / 100.0
else
v
end
end
hash
end
# Creates a hash with ON or OFF values based on active modules
def parse_status(json)
hash = JSON.parse(json)
result = {
'Ventilation' => 'OFF',
'Heating Medium Pump' => 'OFF',
'Holiday' => 'OFF',
'Hot Water' => 'OFF',
'Compressor' => 'OFF',
'Addition' => 'OFF',
'Heating' => 'OFF'
}
hash.each { |h| result[h['title']] = 'ON' }
result.transform_keys! { |k| k.delete(' ').capitalize }
end
# Converts values returned fron Nibe Uplink 'status' endpoint
# to values readable by OpenHAB
def parse_system(json)
hash = JSON.parse(json).select { |k| %w[lastActivityDate connectionStatus hasAlarmed].include? k }
hash['Alarm'] = @uplink.notifications if hash['hasAlarmed']
hash['hasAlarmed'] = hash['hasAlarmed'] ? 'ON' : 'OFF'
hash['connectionStatus'] = case hash['connectionStatus']
when 'ONLINE' then 0
when 'PENDING' then 1
else 2
end
hash['lastActivityDate'] = hash['lastActivityDate'].chop + '+0000'
hash.transform_keys!(&:capitalize)
end
# Converts values returned fron Nibe Uplink 'software' endpoint
# to values readable by OpenHAB
def parse_software(json)
hash = JSON.parse(json)
if hash['upgrade'].nil?
'OFF'
else
@log.info "New software upgrade available: #{hash['upgrade']}"
'ON'
end
end
# Convert incoming mqtt messages from OpenHAB to send to Nibe Uplink
def parse_mqtt(topic, message)
command = topic.split('/')
command[0, 3] = command[2].downcase.to_sym
command[1] = case command[0]
when :mode then message
when :parameters then { command[1].downcase.to_sym => message }
when :thermostats then JSON.parse(message)
else raise ArgumentError, 'Not a valid topic or message'
end
command
end
def parse_parameter_response(response)
hash = JSON.parse(response)
{ hash[0]['parameter']['parameterId'] => hash[0]['parameter']['rawValue'] }
end
end