|
1 | 1 | classdef InfluxDB < handle
|
2 | 2 |
|
3 | 3 | properties(Access = private)
|
4 |
| - Host, User, Password, Database; |
| 4 | + Host = '' |
| 5 | + User = '' |
| 6 | + Password = '' |
| 7 | + Database = '' |
| 8 | + ReadTimeout = 10 |
| 9 | + WriteTimeout = 10 |
5 | 10 | end
|
6 | 11 |
|
7 | 12 | methods
|
|
13 | 18 | obj.Database = database;
|
14 | 19 | end
|
15 | 20 |
|
| 21 | + % Set the read timeout |
| 22 | + function obj = setReadTimeout(obj, timeout) |
| 23 | + obj.ReadTimeout = timeout; |
| 24 | + end |
| 25 | + |
| 26 | + % Set the write timeout |
| 27 | + function obj = setWriteTimeout(obj, timeout) |
| 28 | + obj.WriteTimeout = timeout; |
| 29 | + end |
| 30 | + |
16 | 31 | % Test the connection with a ping
|
17 | 32 | function [ok, millis] = ping(obj)
|
18 | 33 | timer = tic;
|
|
25 | 40 | % Show databases
|
26 | 41 | function databases = databases(obj)
|
27 | 42 | url = [obj.Host '/query'];
|
28 |
| - opts = weboptions('Username', obj.User, 'Password', obj.Password); |
| 43 | + opts = weboptions('Timeout', obj.ReadTimeout, ... |
| 44 | + 'Username', obj.User, 'Password', obj.Password); |
29 | 45 | response = webread(url, 'q', 'SHOW DATABASES', opts);
|
30 | 46 | databases = [response.results.series.values{:}];
|
31 | 47 | end
|
|
38 | 54 | % Execute a query string
|
39 | 55 | function result = runQuery(obj, query)
|
40 | 56 | url = [obj.Host '/query'];
|
41 |
| - opts = weboptions('Username', obj.User, 'Password', obj.Password); |
| 57 | + opts = weboptions('Timeout', obj.ReadTimeout, ... |
| 58 | + 'Username', obj.User, 'Password', obj.Password); |
42 | 59 | response = webread(url, 'db', obj.Database, 'epoch', 'ms', 'q', query, opts);
|
43 | 60 | result = QueryResult.from(response);
|
44 | 61 | end
|
|
76 | 93 | params{end + 1} = ['consistency=' consistency];
|
77 | 94 | end
|
78 | 95 | url = [obj.Host '/write?' strjoin(params, '&')];
|
79 |
| - opts = weboptions('Username', obj.User, 'Password', obj.Password); |
| 96 | + weboptions('Timeout', obj.WriteTimeout, ... |
| 97 | + 'Username', obj.User, 'Password', obj.Password); |
80 | 98 | webwrite(url, lines, opts);
|
81 | 99 | end
|
82 | 100 |
|
|
0 commit comments