forked from davidMbrooke/MQTT-Perl-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnibe2mqtt.pl
executable file
·154 lines (139 loc) · 5.3 KB
/
nibe2mqtt.pl
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use LWP::Authen::OAuth2;
use AnyEvent::MQTT;
use AnyEvent::DateTime::Cron;
# File-scope variables
our $filename = $ENV{ "HOME" }.'/.NIBEUplinkAPI/token.json';
our $oauth2;
our $mqtt;
# Subroutine to save new OAuth2 token when it changes
sub save_tokens {
my $token_string = shift;
open( my $fh, '>', $filename );
print $fh $token_string;
close $fh;
}
# Subroutine for AnyEvent Callbacks
sub nibe_callback {
my $url;
my $response;
my $decoded;
my $hashref;
# Get the Systems for this Account
$url = "https://api.nibeuplink.com/api/v1/systems";
$response = $oauth2->get( $url );
if ( $response->is_error ) { print $response->error_as_HTML }
if ( $response->is_success ) {
$decoded = decode_json( $response->content );
# Expect $decoded to be a Reference to a Hash
my $objects = $decoded->{ 'objects' };
# Expect $objects to be a Reference to an Array of Hashes
for my $hashref ( @ { $objects } ) {
my $system = $hashref->{ 'systemId' };
# Get the Categories for this System
$url = "https://api.nibeuplink.com/api/v1/systems/$system/serviceinfo/categories";
$response = $oauth2->get( $url );
if ( $response->is_error ) { print $response->error_as_HTML }
if ( $response->is_success ) {
$decoded = decode_json( $response->content );
# Expect $decoded to be a Reference to an Array of Hashes
for my $hashref ( @ { $decoded } ) {
my $category = $hashref->{ 'categoryId' };
# Get the Parameters for this Category
$url = "https://api.nibeuplink.com/api/v1/systems/$system/serviceinfo/categories/status?categoryId=$category";
$response = $oauth2->get( $url );
if ( $response->is_error ) { print $response->error_as_HTML }
if ( $response->is_success ) {
$decoded = decode_json( $response->content );
# Expect $decoded to be a Reference to an Array of Hashes
for my $hashref ( @ { $decoded } ) {
# Crude way to spot which Parameters are Temperatures
if ( $hashref->{ 'unit' } =~ /C$/ ) {
# Hack to exclude avg. outdoor temp
if ( $hashref->{ 'name' } ne '40067' ) {
my $designation = $hashref->{ 'designation' };
my $rawvalue = $hashref->{ 'rawValue' };
my $floatvalue = $rawvalue / 10;
my $topic = "raw/nibeuplink/$system/$designation/temperature";
# Publish MQTT message to Broker
my $cv_mqtt = $mqtt->publish( topic => $topic,
message => $floatvalue );
}
# Crude way to spot which Parameters are Percentages
} elsif ( $hashref->{ 'unit' } eq '%' ) {
my $designation = $hashref->{ 'designation' };
my $rawvalue = $hashref->{ 'rawValue' };
# Already full-scale, no need to /10
my $floatvalue = $rawvalue;
my $topic = "raw/nibeuplink/$system/$designation/percentage";
# Publish MQTT message to Broker
my $cv_mqtt = $mqtt->publish( topic => $topic,
message => $floatvalue );
# Crude way to spot which Parameters are Degree Minutes
} elsif ( $hashref->{ 'unit' } eq 'DM' ) {
my $parameterid = $hashref->{ 'parameterId' };
my $rawvalue = $hashref->{ 'rawValue' };
my $floatvalue = $rawvalue / 10;
my $topic = "raw/nibeuplink/$system/$parameterid/degreeminutes";
# Publish MQTT message to Broker
my $cv_mqtt = $mqtt->publish( topic => $topic,
message => $floatvalue );
# Crude way to spot which Parameters are Hours
} elsif ( $hashref->{ 'unit' } eq 'h' ) {
my $parameterid = $hashref->{ 'parameterId' };
my $rawvalue = $hashref->{ 'rawValue' };
# Already full-scale, no need to /10
my $floatvalue = $rawvalue;
my $topic = "raw/nibeuplink/$system/$parameterid/hours";
# Publish MQTT message to Broker
my $cv_mqtt = $mqtt->publish( topic => $topic,
message => $floatvalue );
# Crude way to spot which Parameters are kilo Watts
} elsif ( $hashref->{ 'unit' } eq 'kW' ) {
my $parameterid = $hashref->{ 'parameterId' };
my $rawvalue = $hashref->{ 'rawValue' };
my $floatvalue = $rawvalue / 100;
my $topic = "raw/nibeuplink/$system/$parameterid/kilowatts";
# Publish MQTT message to Broker
my $cv_mqtt = $mqtt->publish( topic => $topic,
message => $floatvalue );
}
}
}
}
}
}
}
}
# Main Program
my $token_string;
# Connect to the MQTT Broker
$mqtt = AnyEvent::MQTT->new( host => 'mqtt',
client_id => 'nibe2mqtt',
user_name => 'USERNAME',
password => 'PASSWORD' );
# Read saved token_string from file
open( my $fh, '<', $filename )
or die "Could not open file $filename: $!";
$token_string = <$fh>;
chomp $token_string;
close( $fh );
# Construct the OAuth2 object
$oauth2 = LWP::Authen::OAuth2->new(
service_provider => "NIBEUplink",
client_id => "SECRET",
client_secret => 'SECRET',
redirect_uri => "https://www.marshflattsfarm.org.uk/openhab/oauth2callback",
token_string => $token_string,
save_tokens => \&save_tokens
);
# Create the cron object
my $cron = AnyEvent::DateTime::Cron->new( );
# Add the cron timer
$cron->add( '*/2 * * * *', \&nibe_callback );
# Start cron and run the event loop
my $cv = $cron->start( );
$cv->recv;