Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Gebhardt, Albrecht
frewe-rpi
Commits
c61d4960
Commit
c61d4960
authored
Jan 16, 2016
by
aloz77
Browse files
Separate read interval for fhem.txt, run each 48 seconds for FHEM
parent
5705fe39
Changes
1
Show whitespace changes
Inline
Side-by-side
frewe-client.c
View file @
c61d4960
...
...
@@ -62,6 +62,7 @@
* 2015-03-25 Send UTC time to frewe-server
* 2015-04-24 Handle lasttime as UTC
* 2015-08-20 Don't write fhem.txt for older records
* 2016-01-16 Separate read interval for fhem.txt, run each 48 seconds for FHEM
* TODO: Handle rain counter overflow
*/
...
...
@@ -81,7 +82,7 @@
#include <openssl/md5.h>
#include "http_fetcher.h"
#define PROGRAM_VERSION "1.1
8
"
#define PROGRAM_VERSION "1.1
9
"
#define DEFAULT_VENDOR 0x1941
#define DEFAULT_PRODUCT 0x8021
...
...
@@ -153,6 +154,7 @@ char *ws_type="WH1080"; // default to WH1080
char
ws_entry_size
;
int
run_interval
=
0
;
// in seconds, 0 means run once and exit, change it by -r option or RunInterval cfg
int
fhem_interval
=
48
;
int
read_period
;
// Minutes between each stored reading (set in the WS configuration)
int
altitude
=
0
;
// default altitude is sea level in meter - change it by -A option or Altitude cfg
uint16_t
vendor
=
DEFAULT_VENDOR
,
product
=
DEFAULT_PRODUCT
;
...
...
@@ -220,11 +222,12 @@ int main(int argc, char **argv)
int
pos60
,
pos0h
;
int
data_count
;
int
last_age
;
int
read_weather
,
read_fhem
;
uint16_t
address
,
address0
,
address60
,
address0h
;
long
pause
;
time_t
starttime
,
curtime
,
lasttime
;
struct
timeval
tact
,
tlast
;
struct
timeval
tact
,
tlast
,
tlastfhem
;
struct
tm
*
tmptr
,
tm
;
char
*
output
;
...
...
@@ -508,10 +511,13 @@ int main(int argc, char **argv)
// Start main loop for run_interval repetitions
w
.
ok
=
w1
.
ok
=
0
;
read_weather
=
read_fhem
=
1
;
do
{
// Start reading
rv
=
0
;
// reset errors
startpos
=
endpos
=
position
;
// default
...
...
@@ -519,7 +525,11 @@ int main(int argc, char **argv)
time
(
&
curtime
);
tmptr
=
localtime
(
&
curtime
);
if
(
read_weather
)
gettimeofday
(
&
tlast
,
NULL
);
if
(
read_fhem
)
gettimeofday
(
&
tlastfhem
,
NULL
);
// Get the current data count (records actually saved on ws)
...
...
@@ -537,7 +547,7 @@ int main(int argc, char **argv)
// Get last time saved on frewe-server and calculate positions
if
(
rv
==
0
&&
frewe_server_url_lasttime
!=
NULL
)
if
(
rv
==
0
&&
read_weather
&&
frewe_server_url_lasttime
!=
NULL
)
{
logger
(
LOG_DEBUG
,
"main"
,
"Getting lasttime from server URL: %s"
,
frewe_server_url_lasttime
);
rv
=
ws_submit
(
frewe_server_url_lasttime
,
&
filebuf
);
...
...
@@ -673,7 +683,7 @@ int main(int argc, char **argv)
// Format and submit data to frewe-server
if
(
rv
==
0
&&
frewe_server_url_submit
!=
NULL
)
if
(
rv
==
0
&&
read_weather
&&
frewe_server_url_submit
!=
NULL
)
{
output
=
malloc
(
strlen
(
frewe_server_url_submit
)
+
100
);
if
(
!
output
)
{
logger
(
LOG_ERROR
,
"main"
,
"Could not allocate %u bytes for output"
,
strlen
(
frewe_server_url_submit
)
+
100
);
...
...
@@ -745,7 +755,7 @@ int main(int argc, char **argv)
// Format and submit data to known weather services
for
(
i
=
0
;
i
<
sizeof
(
ws
)
/
sizeof
(
ws
[
0
])
&&
rv
==
0
;
i
++
)
for
(
i
=
0
;
i
<
sizeof
(
ws
)
/
sizeof
(
ws
[
0
])
&&
read_weather
&&
rv
==
0
;
i
++
)
{
if
(
ws
[
i
].
user
==
NULL
||
ws
[
i
].
pass
==
NULL
)
continue
;
// Skip if service is not in use
...
...
@@ -795,7 +805,7 @@ int main(int argc, char **argv)
// Format and print data for FHEM into FHEM_file
if
(
rv
==
0
&&
FHEM_format
!=
NULL
&&
FHEM_file
!=
NULL
)
if
(
rv
==
0
&&
read_fhem
&&
FHEM_format
!=
NULL
&&
FHEM_file
!=
NULL
)
{
output
=
malloc
(
strlen
(
FHEM_format
)
+
100
);
if
(
!
output
)
{
logger
(
LOG_ERROR
,
"main"
,
"Could not allocate %u bytes for FHEM output"
,
strlen
(
FHEM_format
)
+
100
);
...
...
@@ -822,7 +832,7 @@ int main(int argc, char **argv)
// Format and submit data to additional URLs according to -u or WeatherURL cfg
for
(
i
=
0
;
i
<
add_url_counter
&&
rv
==
0
;
i
++
)
for
(
i
=
0
;
i
<
add_url_counter
&&
read_weather
&&
rv
==
0
;
i
++
)
{
if
(
add_url
[
i
]
!=
NULL
)
{
output
=
malloc
(
strlen
(
add_url
[
i
])
+
100
);
...
...
@@ -855,12 +865,19 @@ int main(int argc, char **argv)
{
gettimeofday
(
&
tact
,
NULL
);
pause
=
run_interval
*
1000000
-
diff_time
(
&
tact
,
&
tlast
);
if
(
pause
>
0
&&
pause
<=
run_interval
*
1000000
)
{
logger
(
LOG_DEBUG
,
"main"
,
"Sleeping until next run %d microseconds
\n
"
,
pause
);
usleep
(
pause
);
while
(
run_interval
*
1000000
>
diff_time
(
&
tact
,
&
tlast
)
&&
fhem_interval
*
1000000
>
diff_time
(
&
tact
,
&
tlastfhem
))
{
logger
(
LOG_DEBUG
,
"main"
,
"Sleeping a second prior to the next timers check"
);
sleep
(
1
);
gettimeofday
(
&
tact
,
NULL
);
}
read_weather
=
read_fhem
=
0
;
if
(
run_interval
*
1000000
<=
diff_time
(
&
tact
,
&
tlast
))
read_weather
=
1
;
if
(
fhem_interval
*
1000000
<=
diff_time
(
&
tact
,
&
tlastfhem
))
read_fhem
=
1
;
}
}
while
(
run_interval
>
0
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment