You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4242 lines
160 KiB

4 years ago
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
4 years ago
4 years ago
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
3 years ago
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
3 years ago
​
3 years ago
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
3 years ago
​
​
​
​
​
3 years ago
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
4 years ago
​
​
​
3 years ago
3 years ago
​
3 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
3 years ago
4 years ago
​
​
​
​
4 years ago
​
​
4 years ago
4 years ago
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
4 years ago
4 years ago
4 years ago
​
4 years ago
​
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
4 years ago
4 years ago
​
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
​
​
3 years ago
4 years ago
3 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
​
​
​
​
​
​
​
3 years ago
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
3 years ago
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
3 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
3 years ago
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
3 years ago
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
4 years ago
​
4 years ago
4 years ago
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
3 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
  1. #include "SQLite_dataBinder.h"
  2. #include "../../components/base/basicEvent.h"
  3. #define LOG( X ) m_pLog->Log( "SDBC", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG2w34[256];\
  7. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "SDBC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "SDBC", this, _LOG_ERROR, X );
  12. #endif
  13. sqliteDBC *sqliteDBC::_instance = NULL;
  14. /**
  15. * Returns the single instance of the object.
  16. */
  17. sqliteDBC *sqliteDBC::getInstance( config_setting_t * Settings ) {
  18. // check if the instance has been created yet
  19. if( 0 == _instance ){
  20. // if not, then create it
  21. _instance = new sqliteDBC( Settings );
  22. };
  23. // return the single instance
  24. return _instance;
  25. };
  26. uint32_t sqliteDBC::getTsFromRDF( REPORT_DIRINFO &RDF ){
  27. struct tm ltm;
  28. memset( &ltm, 0, sizeof( struct tm ));
  29. ltm.tm_mday = atoi( RDF.DAY );
  30. ltm.tm_mon = atoi( RDF.MONTH ) - 1;
  31. ltm.tm_year = atoi( RDF.YEAR ) - 1900;
  32. return (uint32_t) timelocal( &ltm );
  33. };
  34. void sqliteDBC::fillRDF( uint32_t ts, REPORT_DIRINFO &RDF ){
  35. memset( &RDF, 0, sizeof(REPORT_DIRINFO));
  36. if ( ts ) {
  37. time_t tt = ts;
  38. struct tm * tmp = localtime( &tt );
  39. if ( tmp ) {
  40. sprintf( RDF.DAY, "%02d", tmp->tm_mday);
  41. sprintf( RDF.MONTH, "%02d", 1+tmp->tm_mon );
  42. sprintf( RDF.YEAR, "%04d", 1900+tmp->tm_year );
  43. RDF.ts = getTsFromRDF( RDF );
  44. };
  45. };
  46. };
  47. bool sqliteDBC::createAccessibleDir( CSTR path ){
  48. if( mkdir( path, STORAGE_DIR_MODE ) != 0 ) return false;
  49. chown( path, web_access_user, web_access_group );
  50. return true;
  51. };
  52. bool sqliteDBC::checkRDFDir( CSTR basePath, REPORT_DIRINFO &RDF, bool recreate ){
  53. char tBuff[MAX_PATH];
  54. sprintf( tBuff, "%s/%s", basePath, RDF.YEAR );
  55. #ifdef DEBUG
  56. LOG( tBuff );
  57. #endif
  58. if( fileExist( tBuff ) == 0 ){
  59. if ( recreate ) {
  60. if (!createAccessibleDir(tBuff)) return false;
  61. } else return false;
  62. };
  63. sprintf( tBuff, "%s/%s/%s", basePath, RDF.YEAR, RDF.MONTH );
  64. #ifdef DEBUG
  65. LOG( tBuff );
  66. #endif
  67. if( fileExist( tBuff ) == 0 ){
  68. if ( recreate ) {
  69. if( !createAccessibleDir( tBuff ) ) return false;
  70. } else return false;
  71. };
  72. sprintf( tBuff, "%s/%s/%s/%s", basePath, RDF.YEAR, RDF.MONTH, RDF.DAY );
  73. #ifdef DEBUG
  74. LOG( tBuff );
  75. #endif
  76. if( fileExist( tBuff ) == 0 ){
  77. if ( recreate ) {
  78. if( !createAccessibleDir( tBuff ) ) return false;
  79. } else return false;
  80. };
  81. return true;
  82. };
  83. sqliteDBC::sqliteDBC( config_setting_t * Settings ):dataBinder(){
  84. m_pLog = basicLogCollection::getInstance();
  85. m_pSettings = basicSettingsProvider::getInstance()->getSubNode( Settings, "DATABASE" );
  86. m_bStoIsConnected = false;
  87. m_bMemIsConnected = false;
  88. m_bRepIsConnected = false;
  89. m_bStaIsConnected = false;
  90. m_bLastEventWasClosed = false;
  91. m_pDataStorage = NULL;
  92. m_pDataMemory = NULL;
  93. m_pDataReports = NULL;
  94. m_pDataStatistics = NULL;
  95. m_nServerID = 1;
  96. };
  97. sqliteDBC::~sqliteDBC(){
  98. if ( m_pDataStorage ) delete( m_pDataStorage );
  99. if ( m_pDataMemory ) delete( m_pDataMemory );
  100. if ( m_pDataReports ) delete( m_pDataReports );
  101. if ( m_pDataStatistics ) delete( m_pDataStatistics );
  102. m_bStoIsConnected = false;
  103. m_bMemIsConnected = false;
  104. m_bRepIsConnected = false;
  105. m_bStaIsConnected = false;
  106. _instance = NULL;
  107. };
  108. bool sqliteDBC::connect( void ){
  109. if ( m_pSettings == NULL) return false;
  110. bool bRecreateMemStructures = false;
  111. config_setting_t * pSetting;
  112. const char * tBuff;
  113. //Disconnect if Connected
  114. m_bStoIsConnected = false;
  115. m_bMemIsConnected = false;
  116. m_bRepIsConnected = false;
  117. m_bStaIsConnected = false;
  118. m_bLastEventWasClosed = false;
  119. //Apply New Config
  120. #ifdef DEBUG
  121. LOG( "Reading DB Settings ... " );
  122. #endif
  123. if(!config_setting_lookup_string(m_pSettings, "DATA_FILE_NAME", (const char**)&tBuff )){
  124. pSetting = config_setting_add(m_pSettings, "DATA_FILE_NAME", CONFIG_TYPE_STRING);
  125. sprintf( m_pDbFile, "data/mysqlitedb.db" );
  126. config_setting_set_string( pSetting, m_pDbFile );
  127. basicSettingsProvider::getInstance()->compromise();
  128. LOG("Data Storage not set - using default");
  129. } else {
  130. sprintf( m_pDbFile, tBuff );
  131. };
  132. if(!config_setting_lookup_string(m_pSettings, "REPORTS_DIR_NAME", (const char**)&tBuff )){
  133. pSetting = config_setting_add(m_pSettings, "REPORTS_DIR_NAME", CONFIG_TYPE_STRING);
  134. sprintf( m_pDbReportBase, "data/reports" );
  135. config_setting_set_string( pSetting, m_pDbReportBase );
  136. basicSettingsProvider::getInstance()->compromise();
  137. LOG("Reports Storage not set - using default");
  138. } else {
  139. sprintf( m_pDbReportBase, tBuff );
  140. };
  141. if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_USER", (const char**)&tBuff )){
  142. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_USER", CONFIG_TYPE_STRING);
  143. sprintf( web_access_user, "www-data" );
  144. config_setting_set_string( pSetting, web_access_user );
  145. basicSettingsProvider::getInstance()->compromise();
  146. LOG("Web Access User not set - using default");
  147. } else {
  148. sprintf( web_access_user, tBuff );
  149. };
  150. if(!config_setting_lookup_string(m_pSettings, "WEB_ACCESS_GROUP", (const char**)&tBuff )){
  151. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_GROUP", CONFIG_TYPE_STRING);
  152. sprintf( web_access_group, "www-data" );
  153. config_setting_set_string( pSetting, web_access_group );
  154. basicSettingsProvider::getInstance()->compromise();
  155. LOG("Web Access Group not set - using default");
  156. } else {
  157. sprintf( web_access_group, tBuff );
  158. };
  159. if( !fileExist( DB_SHMEM_POINT )) {
  160. bRecreateMemStructures = true;
  161. mkdir( DB_SHMEM_POINT, STORAGE_DIR_MODE );// S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  162. chown( DB_SHMEM_POINT, web_access_user, web_access_group ); //To make Accessed from PHP
  163. chmod( DB_SHMEM_POINT, STORAGE_DIR_MODE );
  164. };
  165. char tB[128];
  166. if (!config_setting_lookup_string(m_pSettings, "DATA_MEMORY_NAME", (const char **)&tBuff)) {
  167. pSetting = config_setting_add(m_pSettings, "DATA_MEMORY_NAME", CONFIG_TYPE_STRING);
  168. tBuff = tB;
  169. sprintf( tB, "mem_db.db" );
  170. config_setting_set_string( pSetting, tB );
  171. basicSettingsProvider::getInstance()->compromise();
  172. LOG("Memory Storage not set - using default");
  173. };
  174. sprintf( m_pDbMemory, "%s/%s", DB_SHMEM_POINT, tBuff );
  175. basicSettingsProvider::getInstance()->validate();
  176. // Kill Connection if any
  177. if ( m_pDataStorage ) delete( m_pDataStorage );
  178. if ( m_pDataMemory ) delete( m_pDataMemory );
  179. if ( m_pDataReports ) delete( m_pDataReports );
  180. if ( m_pDataStatistics ) delete( m_pDataStatistics );
  181. m_pDataMemory = NULL;
  182. m_pDataStorage = NULL;
  183. m_pDataReports = NULL;
  184. m_pDataStatistics = NULL;
  185. #ifdef DEBUG
  186. LOG( m_pDbFile );
  187. #endif
  188. // Create New Connections
  189. m_pDataStorage = new SQLite_Connector();
  190. if ( m_pDataStorage ) {
  191. if( m_pDataStorage->Connect( m_pDbFile, 0, true ) ){
  192. #ifdef DEBUG
  193. LOG("DB File Opened");
  194. #endif
  195. chown( m_pDbFile, web_access_user, web_access_group );
  196. chmod( m_pDbFile, DB_FILE_MODE );
  197. m_bStoIsConnected = true;
  198. } else{
  199. LOG_ERROR("Cannot Connect to DB File");
  200. delete( m_pDataStorage );
  201. m_pDataStorage = NULL;
  202. LOG("Storage Deleted!");
  203. return false;
  204. };
  205. };
  206. #ifdef DEBUG
  207. LOG( m_pDbMemory );
  208. #endif
  209. if( !fileExist( m_pDbMemory )) {
  210. bRecreateMemStructures = true;
  211. //Always recreate
  212. unlink( m_pDbMemory );
  213. };
  214. m_pDataMemory = new SQLite_Connector();
  215. if ( m_pDataMemory ) {
  216. if( m_pDataMemory->Connect( m_pDbMemory, 0, true ) ){
  217. #ifdef DEBUG
  218. LOG("DB Memory Opened");
  219. #endif
  220. chown( m_pDbMemory, web_access_user, web_access_group );
  221. chmod( m_pDbMemory, DB_FILE_MODE );
  222. m_pDataMemory->SilentQuery("PRAGMA journal_mode=WAL");
  223. //Recreate at Register Server
  224. m_bMemIsConnected = true;
  225. } else{
  226. LOG_ERROR("Cannot Connect to DB Memory");
  227. delete( m_pDataStorage );
  228. delete( m_pDataMemory );
  229. m_bStoIsConnected = false;
  230. m_pDataStorage = NULL;
  231. m_pDataMemory = NULL;
  232. return false;
  233. };
  234. };
  235. sprintf( m_pDbStat, "%s/stat.db", m_pDbReportBase );
  236. m_pDataStatistics = new SQLite_Connector();
  237. if ( m_pDataStatistics ) {
  238. if ( fileExist( m_pDbStat )) {
  239. m_bStaIsCreated = false;
  240. if( m_pDataStatistics->Connect( m_pDbStat, 0, true) ) {
  241. #ifdef DEBUG
  242. LOG("DB Statistics Opened");
  243. #endif
  244. m_pDataStatistics->SilentQuery("PRAGMA journal_mode=WAL");
  245. m_bStaIsConnected = true;
  246. } else {
  247. LOG_ERROR("Cannot Connect to DB Statistics");
  248. delete( m_pDataStorage );
  249. delete( m_pDataMemory );
  250. delete( m_pDataStatistics );
  251. m_bMemIsConnected = false;
  252. m_bStoIsConnected = false;
  253. m_pDataStorage = NULL;
  254. m_pDataMemory = NULL;
  255. m_pDataStatistics = NULL;
  256. return false;
  257. };
  258. } else {
  259. m_bStaIsCreated = true;
  260. if( m_pDataStatistics->Connect( m_pDbStat, 0, true) ) {
  261. #ifdef DEBUG
  262. LOG("DB Statistics Opened");
  263. #endif
  264. chown( m_pDbStat, web_access_user, web_access_group );
  265. chmod( m_pDbStat, DB_FILE_MODE );
  266. m_pDataStatistics->SilentQuery("PRAGMA journal_mode=WAL");
  267. m_pDataStatistics->SilentQuery("CREATE TABLE IF NOT EXISTS \"days\" (\"year\" INTEGER,\"month\" INTEGER,\"day\" INTEGER,\"evt_count\" INTEGER DEFAULT 0,\"session_count\" INTEGER DEFAULT 0,\"videos_count\" INTEGER DEFAULT 0,\"session_closed\" INTEGER DEFAULT 0, CONSTRAINT \"date_uni\" UNIQUE(\"year\",\"month\",\"day\") );");
  268. m_bStaIsConnected = true;
  269. } else {
  270. LOG_ERROR("Cannot Connect to DB Statistics");
  271. delete( m_pDataStorage );
  272. delete( m_pDataMemory );
  273. delete( m_pDataStatistics );
  274. m_bMemIsConnected = false;
  275. m_bStoIsConnected = false;
  276. m_pDataStorage = NULL;
  277. m_pDataMemory = NULL;
  278. m_pDataStatistics = NULL;
  279. return false;
  280. };
  281. };
  282. };
  283. bool create_tables = false;
  284. m_pDataReports = new SQLite_Connector();
  285. if ( m_pDataReports ) {
  286. LOG("RDB Created!");
  287. memset( &m_sPRepInfo, 0, sizeof(REPORT_DIRINFO));
  288. if( !m_bStaIsCreated ){
  289. LOG("OLD STA PRESENT RDB Created!");
  290. AAssoc * result = m_pDataStatistics->Query( "SELECT * FROM `days` ORDER BY `year` DESC, `month` DESC, `day` DESC LIMIT 1" );
  291. if ( result ) {
  292. if( result->size() > 0 ) {
  293. Assoc * cArr;
  294. Assoc::iterator it;
  295. cArr = result->at(0);
  296. it = cArr->find("year");
  297. if( it != cArr->end() ){
  298. strncpy( m_sPRepInfo.YEAR, it->second.c_str(), 4 );
  299. };
  300. it = cArr->find("month");
  301. if( it != cArr->end() ){
  302. sprintf( m_sPRepInfo.MONTH, "%02d", atoi( it->second.c_str() ) );
  303. //strncpy( m_sPRepInfo.MONTH, it->second.c_str(), 2 );
  304. };
  305. it = cArr->find("day");
  306. if( it != cArr->end() ){
  307. sprintf( m_sPRepInfo.DAY, "%02d", atoi( it->second.c_str() ) );
  308. //strncpy( m_sPRepInfo.DAY, it->second.c_str(), 2 );
  309. };
  310. m_sPRepInfo.ts = getTsFromRDF( m_sPRepInfo );
  311. } else {
  312. create_tables = true;
  313. #ifdef DEBUG_ALL
  314. LOG("No Rows Fetched!");
  315. #endif
  316. };
  317. };
  318. m_pDataMemory->deleteResult( result );
  319. };
  320. if ( m_sPRepInfo.ts != 0 ) {
  321. LOG("Old !!");
  322. if( checkRDFDir( m_pDbReportBase, m_sPRepInfo, false ) == 0 ) {
  323. LOG_ERROR("Error Connecting to Prev Report!");
  324. m_bLastEventWasClosed = true;
  325. create_tables = true;
  326. } else {
  327. LOG("Create R-Base Connection");
  328. sprintf( m_pDbReport, "%s/%s/%s/%s/report.db", m_pDbReportBase, m_sPRepInfo.YEAR, m_sPRepInfo.MONTH, m_sPRepInfo.DAY );
  329. LOG( m_pDbReport );
  330. if( fileExist( m_pDbReport ) ){
  331. SQLite_Connector *tDBC = new SQLite_Connector();
  332. if ( tDBC ) {
  333. if( tDBC->Connect( m_pDbReport, 0, true) ) {
  334. char tQBuf[CTEXT];
  335. AAssoc *result;
  336. sprintf( tQBuf, "SELECT `evt_type` FROM `events` WHERE `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_MA, EVT_LRF_END_SESSION_TO );
  337. #ifdef DEBUG
  338. LOG(tQBuf);
  339. #endif
  340. result = tDBC->Query( tQBuf );
  341. if( result!= NULL ) {
  342. if( result->size() > 0 ) {
  343. Assoc *cArr;
  344. Assoc::iterator it;
  345. cArr = result->at(0);
  346. it = cArr->find("evt_type");
  347. if( it != cArr->end() ){
  348. if( atoi( it->second.c_str() ) != EVT_LRF_START_SESSION ) m_bLastEventWasClosed = true;
  349. };
  350. } else m_bLastEventWasClosed = true;
  351. };
  352. deleteResult( result );
  353. } else {
  354. LOG_ERROR("Error Connecting to Prev Report!");
  355. m_bLastEventWasClosed = true;
  356. create_tables = true;
  357. };
  358. delete( tDBC );
  359. } else {
  360. LOG_ERROR("Error Create DB Connection Object!");
  361. m_bLastEventWasClosed = true;
  362. create_tables = true;
  363. };
  364. } else {
  365. LOG_ERROR("Error No Prev Report Database!");
  366. m_bLastEventWasClosed = true;
  367. create_tables = true;
  368. };
  369. };
  370. } else {
  371. m_bLastEventWasClosed = true;
  372. create_tables = true;
  373. };
  374. if ( m_bLastEventWasClosed ) {
  375. LOG("Event Closed!");
  376. //If Last Event Was Closed -> Create/Open Today's Report
  377. fillRDF( time( NULL ), m_sCRepInfo );
  378. } else {
  379. LOG("Event not Closed!");
  380. //Stay On Last Report database since Event not closed
  381. memcpy( &m_sCRepInfo, &m_sPRepInfo, sizeof(REPORT_DIRINFO) );
  382. };
  383. LOG("Check+Create Path!");
  384. LOG( m_pDbReportBase );
  385. if( checkRDFDir( m_pDbReportBase, m_sCRepInfo ) == 0 ) {
  386. LOG_ERROR("Cannot Open Report!!");
  387. return false;
  388. };
  389. sprintf( m_pDbReport, "%s/%s/%s/%s/report.db", m_pDbReportBase, m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY );
  390. LOG("New Path");
  391. LOG( m_pDbReport );
  392. if (m_pDataReports->Connect( m_pDbReport, 0, true )) {
  393. #ifdef DEBUG
  394. LOG("Report File Opened");
  395. #endif
  396. chown( m_pDbReport, web_access_user, web_access_group );
  397. chmod( m_pDbReport, DB_FILE_MODE );
  398. //Recreate Table in any case
  399. //if ( m_sCRepInfo.ts != m_sPRepInfo.ts || create_tables ) {
  400. // Created New Report ??
  401. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events\" ( \"id\" INTEGER NOT NULL, \"node_id\" INTEGER NOT NULL, \"uuid\" TEXT NOT NULL UNIQUE, \"ts\" INTEGER NOT NULL, \"ts_ms\" INTEGER NOT NULL, \"evt_type\" INTEGER NOT NULL, \"lrf_id\" INTEGER, \"range_digital\" INTEGER, \"azimuth\" REAL, \"elevation\" REAL, \"range\" REAL, \"area\" REAL, \"lat\" REAL, \"lon\" REAL, \"alt\" REAL, \"ref_id\" INTEGER, \"ref_uuid\" TEXT, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  402. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events_2_pictures\" ( \"evt_id\" INTEGER NOT NULL, \"picture_id\" INTEGER NOT NULL )");
  403. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events_2_videos\" ( \"evt_id\" INTEGER NOT NULL, \"video_id\" INTEGER NOT NULL, \"frame\" INTEGER DEFAULT 0, \"tss\" INTEGER, \"tms\" INTEGER )");
  404. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"pictures\" ( \"id\" INTEGER NOT NULL, \"media_stream_id\" INTEGER NOT NULL, \"md5\" TEXT NOT NULL UNIQUE, \"uts\" INTEGER NOT NULL, \"uts_ms\" INTEGER NOT NULL, \"ext\" TEXT NOT NULL, \"data\" BLOB COLLATE BINARY, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  405. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"videos\" ( \"id\" INTEGER NOT NULL, \"media_stream_id\" INTEGER NOT NULL, \"md5\" TEXT NOT NULL UNIQUE, \"uts\" INTEGER NOT NULL, \"uts_ms\" INTEGER NOT NULL, \"ext\" TEXT NOT NULL, \"fps\" INTEGER NOT NULL, \"flength\" INTEGER, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  406. //};
  407. m_bRepIsConnected = true;
  408. } else{
  409. LOG_ERROR("Cannot Connect to DB File");
  410. delete( m_pDataReports );
  411. m_pDataReports = NULL;
  412. LOG("Storage Deleted!");
  413. return false;
  414. };
  415. };
  416. return ( m_bRepIsConnected && m_bMemIsConnected && m_bStoIsConnected && m_bStaIsConnected );
  417. };
  418. void sqliteDBC::initMemDB( uint32_t sid ){
  419. m_pDataMemory->SilentQuery("DROP TABLE \"nodes_qcq\"");
  420. m_pDataMemory->SilentQuery("CREATE TABLE \"nodes_qcq\" ( \
  421. \"id\" INTEGER NOT NULL,\
  422. \"node_id\" INTEGER NOT NULL,\
  423. \"cmd\" INTEGER NOT NULL,\
  424. \"status\" INTEGER DEFAULT 0,\
  425. \"parameter\" TEXT,\
  426. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  427. );");
  428. m_pDataMemory->SilentQuery("DROP TABLE \"servers\"");
  429. m_pDataMemory->SilentQuery("CREATE TABLE \"servers\" (\
  430. \"id\" INTEGER NOT NULL,\
  431. \"ts\" INTEGER DEFAULT 0,\
  432. \"pid\" INTEGER DEFAULT 0,\
  433. \"hw_info\" TEXT,\
  434. \"os_info\" TEXT,\
  435. \"cpu_cores\" INTEGER DEFAULT 1,\
  436. \"stat_cpu1\" REAL DEFAULT 0.0,\
  437. \"stat_cpu5\" REAL DEFAULT 0.0,\
  438. \"stat_cpu15\" REAL DEFAULT 0.0,\
  439. \"stat_mem\" REAL DEFAULT 0.0,\
  440. \"stat_disk\" REAL DEFAULT 0.0,\
  441. \"val_mem_tottal\" INTEGER DEFAULT 0,\
  442. \"val_mem_current\" INTEGER DEFAULT 0,\
  443. \"val_disk_tottal\" INTEGER DEFAULT 0,\
  444. \"val_disk_current\" INTEGER DEFAULT 0,\
  445. \"temp0\" REAL DEFAULT 0.0,\
  446. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  447. );");
  448. char tB[128];
  449. sprintf( tB, "INSERT INTO `servers` (`id`,`ts`) VALUES (%d, strftime('%%s') )", sid );
  450. m_pDataMemory->SilentQuery( tB );
  451. };
  452. CSTR sqliteDBC::getCurrentReportDir( void ){
  453. CSTR tV = NULL;
  454. if ( m_sCRepInfo.ts ) {
  455. char tBuf[256];
  456. sprintf( tBuf, "%s/%s/%s", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY );
  457. tV = strdup( tBuf );
  458. };
  459. return tV;
  460. };
  461. DBC_PUSHED_CMD *sqliteDBC::getNodePushedCMD( uint32_t NodeID ){
  462. DBC_PUSHED_CMD * RetVal = NULL;
  463. if ( m_bMemIsConnected ) {
  464. char QS[MAX_PATH];
  465. AAssoc * result;
  466. sprintf( QS, "select * FROM `nodes_qcq` WHERE `node_id`=%d AND `status`=0 ORDER BY `id` LIMIT 1", NodeID );
  467. #ifdef DEBUG_ALL
  468. LOG( QS );
  469. #endif
  470. result = m_pDataMemory->Query( QS );
  471. if ( result ) {
  472. if( result->size() > 0 ) {
  473. RetVal = (DBC_PUSHED_CMD*)malloc(sizeof(DBC_PUSHED_CMD));
  474. if( RetVal ) {
  475. Assoc * cArr;
  476. Assoc::iterator it;
  477. memset( RetVal, 0, sizeof(DBC_PUSHED_CMD));
  478. cArr = result->at(0);
  479. it = cArr->find("id");
  480. if( it != cArr->end() ){
  481. RetVal->id = atoi( it->second.c_str() );
  482. };
  483. it = cArr->find("cmd");
  484. if( it != cArr->end() ){
  485. RetVal->CMD = atoi( it->second.c_str() );
  486. };
  487. it = cArr->find("parameter");
  488. if( it != cArr->end() ){
  489. strncpy( RetVal->PARAM, it->second.c_str(), 256 );
  490. RetVal->PARAM[255] = (char)0;
  491. };
  492. };
  493. #ifdef DEBUG_ALL
  494. } else {
  495. LOG("No Rows Fetched!");
  496. #endif
  497. };
  498. };
  499. m_pDataMemory->deleteResult( result );
  500. };
  501. return RetVal;
  502. };
  503. bool sqliteDBC::updateNodePushedCMD( uint32_t CmdID, uint8_t status ){
  504. if ( m_bMemIsConnected ) {
  505. char QS[MAX_PATH];
  506. sprintf( QS, "UPDATE `nodes_qcq` SET `status`=%d WHERE `id`=%d ", status, CmdID );
  507. return m_pDataMemory->SilentQuery( QS );
  508. };
  509. return false;
  510. };
  511. bool sqliteDBC::cleanNodePushedCMD( void ){
  512. if ( m_bMemIsConnected ) {
  513. return m_pDataMemory->SilentQuery( "DELETE FROM `nodes_qcq` WHERE `status` > 200" );
  514. };
  515. return false;
  516. };
  517. void sqliteDBC::deleteNIF( uint32_t NifID ){
  518. if ( m_bStoIsConnected ) {
  519. char tQBuf[MAX_PATH_LEN];
  520. sprintf( tQBuf, "", NifID );
  521. #ifdef DEBUG_ALL
  522. LOG( tQBuf );
  523. #endif
  524. m_pDataStorage->SilentQuery( tQBuf );
  525. };
  526. };
  527. void sqliteDBC::deleteNIS( uint32_t NisID ){
  528. };
  529. void sqliteDBC::deleteNetwork( uint32_t NetID ){
  530. };
  531. bool sqliteDBC::deleteReportStat( REPORT_DIRINFO &RDF ){
  532. if ( m_bStaIsConnected ) {
  533. char tQBuf[MAX_PATH_LEN];
  534. sprintf( tQBuf, "DELETE FROM `days` WHERE `year`=%s AND `month`=%s AND `day`=%s", RDF.YEAR, RDF.MONTH, RDF.DAY );
  535. #ifdef DEBUG_ALL
  536. LOG( tQBuf );
  537. #endif
  538. m_pDataStatistics->SilentQuery( tQBuf );
  539. return true;
  540. };
  541. return false;
  542. };
  543. void sqliteDBC::deleteResource( uint32_t ResourceID ){
  544. };
  545. void sqliteDBC::deleteSensor( uint32_t SensorID ){
  546. };
  547. void sqliteDBC::deleteUART( uint32_t UARTID ){
  548. };
  549. bool sqliteDBC::updateServerUtilisation( DBC_SYS_LOAD_INFO *ptr ){
  550. if( m_bMemIsConnected ) {
  551. char tQBuf[MAX_PATH_LEN];
  552. sprintf(tQBuf, "UPDATE `servers` SET `ts`=STRFTIME('%%s'), `cpu_cores`=%d, `stat_cpu1`=%0.1f, `stat_cpu5`=%0.1f, `stat_cpu15`=%0.1f, `stat_mem`=%0.1f, `stat_disk`=%0.1f, `temp0`=%0.1f, `val_mem_tottal`=%" PRIu64 ", `val_mem_current`=%" PRIu64 ", `val_disk_tottal`=%" PRIu64 ", `val_disk_current`=%" PRIu64 " WHERE `id`=%d",
  553. ptr->CPU_CORES,
  554. ptr->cpu1,
  555. ptr->cpu5,
  556. ptr->cpu15,
  557. ptr->mem,
  558. ptr->disk,
  559. ptr->temperature,
  560. ptr->T_DISK,
  561. ptr->C_DISK,
  562. ptr->T_MEM,
  563. ptr->C_MEM,
  564. m_nServerID
  565. );
  566. #ifdef DEBUG_ALL
  567. LOG(tQBuf);
  568. #endif
  569. m_pDataMemory->SilentQuery( tQBuf );
  570. return true;
  571. };
  572. return false;
  573. };
  574. /********************************************************************
  575. * *
  576. * Slow DB Functions *
  577. * *
  578. ********************************************************************/
  579. bool sqliteDBC::addLRFDraw( uint32_t node_id, CSTR uniq_id, uint8_t lrf_id, uint8_t type ){
  580. if ( m_bStoIsConnected ){
  581. char tQBuf[1024];
  582. sprintf( tQBuf, "INSERT INTO `lrf_draw`(`node_id`,`lrf_id`,`type`,`ts`,`data`) VALUES(%d,%d,%d,STRFTIME('%%s'),'%s')",
  583. node_id, lrf_id, type, uniq_id );
  584. #ifdef DEBUG
  585. LOG(tQBuf);
  586. #endif
  587. m_pDataStorage->SilentQuery( tQBuf );
  588. return true;
  589. };
  590. return false;
  591. };
  592. bool sqliteDBC::addDeviceDraw( uint32_t DBID, uint8_t type, uint8_t lrf, void *th_data, uint32_t th_size ){
  593. if ( m_bStoIsConnected ){
  594. if ( type == 0 ) {
  595. char tQBuf[1024];
  596. CSTR uid = getUniqID_MD5();
  597. if( uid == NULL ) return false;
  598. sprintf( tQBuf, "data/device_draws/%s.raw", uid );
  599. FILE *fd = fopen( tQBuf, "wb+" );
  600. if( fd!= NULL ) {
  601. uint32_t sisi = th_size >> 2;
  602. fwrite( &sisi, 4, 1, fd );
  603. fwrite( th_data, 4, sisi, fd );
  604. fclose( fd );
  605. bool rV = addLRFDraw( DBID, uid, lrf, type );
  606. free ( uid );
  607. return rV;
  608. };
  609. free( uid );
  610. } else {
  611. return addLRFDraw( DBID, (CSTR) th_data, lrf, type );
  612. };
  613. };
  614. return false;
  615. };
  616. void sqliteDBC::updateStats( bool evt, bool video, uint8_t ses_status ){
  617. if (m_bStaIsConnected ) {
  618. char tQBuf[1024];
  619. switch ( ses_status ) {
  620. case SESSION_CONTINUE:{
  621. if ( video ) {
  622. sprintf(tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`videos_count`) VALUES(%s,%s,%s,1) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `videos_count`=`videos_count`+1",
  623. m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY );
  624. } else {
  625. sprintf(tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`evt_count`) VALUES(%s,%s,%s,1) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `evt_count`=`evt_count`+1",
  626. m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY );
  627. };
  628. #ifdef DEBUG_ALL
  629. LOG( tQBuf );
  630. #endif
  631. m_pDataStatistics->SilentQuery( tQBuf );
  632. break;
  633. };
  634. case SESSION_START:{
  635. sprintf( tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`session_count`,`session_closed`) VALUES(%s,%s,%s,1,0) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `session_count`=`session_count`+1, `session_closed`=0",
  636. m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY );
  637. #ifdef DEBUG_ALL
  638. LOG( tQBuf );
  639. #endif
  640. m_pDataStatistics->SilentQuery( tQBuf );
  641. break;
  642. };
  643. case SESSION_END:{
  644. sprintf( tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`session_count`,`session_closed`) VALUES(%s,%s,%s,0,1) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `session_closed`=1",
  645. m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY );
  646. #ifdef DEBUG_ALL
  647. LOG( tQBuf );
  648. #endif
  649. m_pDataStatistics->SilentQuery( tQBuf );
  650. REPORT_DIRINFO tRR = {0};
  651. fillRDF( time( NULL ), tRR );
  652. if( tRR.ts > m_sCRepInfo.ts ) {
  653. //New Day Report
  654. if ( m_bRepIsConnected ) {
  655. m_bRepIsConnected = false;
  656. LOG("Disconnect Report!");
  657. m_pDataReports->Disconnect();
  658. };
  659. memcpy( &m_sPRepInfo, &m_sCRepInfo, sizeof(REPORT_DIRINFO) );
  660. memcpy( &m_sCRepInfo, &tRR, sizeof(REPORT_DIRINFO) );
  661. if( checkRDFDir( m_pDbReportBase, m_sCRepInfo ) ){
  662. sprintf( m_pDbReport, "%s/%s/%s/%s/report.db", m_pDbReportBase, m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY );
  663. LOG("Connect Report!");
  664. LOG( m_pDbReport );
  665. if( m_pDataReports->Connect( m_pDbReport, 0, true ) ){
  666. chown( m_pDbReport, web_access_user, web_access_group );
  667. chmod( m_pDbReport, DB_FILE_MODE );
  668. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events\" ( \"id\" INTEGER NOT NULL, \"node_id\" INTEGER NOT NULL, \"uuid\" TEXT NOT NULL UNIQUE, \"ts\" INTEGER NOT NULL, \"ts_ms\" INTEGER NOT NULL, \"evt_type\" INTEGER NOT NULL, \"lrf_id\" INTEGER, \"range_digital\" INTEGER, \"azimuth\" REAL, \"elevation\" REAL, \"range\" REAL, \"area\" REAL, \"lat\" REAL, \"lon\" REAL, \"alt\" REAL, \"ref_id\" INTEGER, \"ref_uuid\" TEXT, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  669. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events_2_pictures\" ( \"evt_id\" INTEGER NOT NULL, \"picture_id\" INTEGER NOT NULL )");
  670. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events_2_videos\" ( \"evt_id\" INTEGER NOT NULL, \"video_id\" INTEGER NOT NULL, \"frame\" INTEGER DEFAULT 0, \"tss\" INTEGER, \"tms\" INTEGER )");
  671. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"pictures\" ( \"id\" INTEGER NOT NULL, \"media_stream_id\" INTEGER NOT NULL, \"md5\" TEXT NOT NULL UNIQUE, \"uts\" INTEGER NOT NULL, \"uts_ms\" INTEGER NOT NULL, \"ext\" TEXT NOT NULL, \"data\" BLOB COLLATE BINARY, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  672. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"videos\" ( \"id\" INTEGER NOT NULL, \"media_stream_id\" INTEGER NOT NULL, \"md5\" TEXT NOT NULL UNIQUE, \"uts\" INTEGER NOT NULL, \"uts_ms\" INTEGER NOT NULL, \"ext\" TEXT NOT NULL, \"fps\" INTEGER NOT NULL, \"flength\" INTEGER, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  673. m_bRepIsConnected = true;
  674. LOG("Report Connected!");
  675. // Create New Day Record
  676. sprintf(tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`session_count`,`session_closed`) VALUES(%s,%s,%s,0,1) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `session_count`=`session_count`+1, `session_closed`=1",
  677. m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY );
  678. LOG( tQBuf );
  679. m_pDataStatistics->SilentQuery( tQBuf );
  680. };
  681. };
  682. };
  683. break;
  684. };
  685. };
  686. };
  687. };
  688. uint32_t sqliteDBC::addEvent( DBC_EVT_RECORD *ptr ){
  689. if ( ptr == NULL ) return 0;
  690. if ( m_bRepIsConnected ) {
  691. char tQBuf[1024];
  692. switch ( ptr->type ) {
  693. case EVT_LRF_END_SESSION_TO:
  694. case EVT_LRF_END_SESSION_MA:
  695. updateStats( false, false, SESSION_END );
  696. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)",
  697. ptr->node_id,
  698. ptr->uuid,
  699. ptr->ts_s,
  700. ptr->ts_ms,
  701. ptr->type
  702. );
  703. break;
  704. case EVT_LRF_START_SESSION:
  705. updateStats( false, false, SESSION_START );
  706. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)",
  707. ptr->node_id,
  708. ptr->uuid,
  709. ptr->ts_s,
  710. ptr->ts_ms,
  711. ptr->type
  712. );
  713. break;
  714. case EVT_FLT_LATF_CHK:
  715. case EVT_FLT_LATF_UP_DETEC:
  716. case EVT_FLT_LATF_BT_DETEC:
  717. case EVT_FLT_LATF_TRP_DET:
  718. case EVT_FLT_LATF_TRP_UPDT:
  719. case EVT_FLT_LATF_SUSP_START:
  720. case EVT_FLT_LATF_SUSP_NAK:
  721. case EVT_FLT_LATF_REGULAR:
  722. case EVT_FLT_LATF_DISABLED:
  723. case EVT_FLT_LATF_SUSP_DET:
  724. case EVT_FLT_LATF_DIFF_DET:
  725. case EVT_FLT_LATF_OP_DISABLED:
  726. case EVT_FLT_LATF_OP_TD:
  727. case EVT_FLT_LATF_OP_TG:
  728. case EVT_FLT_LATF_OP_EXP:
  729. case EVT_OP_ACK_MASK:
  730. case EVT_OP_NACK_MASK:
  731. case EVT_OP_ON_MASK:
  732. case EVT_OP_OFF_MASK:
  733. case EVT_CONNECTED:
  734. case EVT_DISCONNECTED:
  735. case EVT_COMMUNICATION_JAMED:
  736. case EVT_STUDY:
  737. case EVT_BUSY:
  738. case EVT_FLIP:
  739. case EVT_READY:
  740. case EVT_ERR_LRF_NO_IRQ:
  741. case EVT_ERR_LRF_NO_ZERO:
  742. case EVT_ERR_LRF_ANGLE:
  743. case EVT_ERR_LRF_VOLTAGE:
  744. case EVT_ERR_LRF_BROKEN:
  745. case EVT_LRF_VIS_CHANGED:
  746. case EVT_ERR_OVERFLOW:
  747. case EVT_SST_OVERFLOW:
  748. case EVT_VIDEO_FPS_DEGRADED:
  749. case EVT_VIDEO_FPS_RAISED:{
  750. updateStats();
  751. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)",
  752. ptr->node_id,
  753. ptr->uuid,
  754. ptr->ts_s,
  755. ptr->ts_ms,
  756. ptr->type
  757. );
  758. break;
  759. };
  760. case EVT_ROI_ACTIVE:
  761. case EVT_TARGET_3D_WHITE:
  762. case EVT_TARGET_3D_BLACK:{
  763. updateStats();
  764. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`lrf_id`,`range_digital`,`azimuth`,`elevation`,`range`,`area`,`lat`,`lon`,`alt`)" \
  765. " VALUES(%d,'%s',%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  766. ptr->node_id,
  767. ptr->uuid,
  768. ptr->ts_s,
  769. ptr->ts_ms,
  770. ptr->type,
  771. ptr->lrf_id,
  772. ptr->range_digital,
  773. ptr->az,
  774. ptr->el,
  775. ptr->ra,
  776. ptr->area,
  777. ptr->lat,
  778. ptr->lon,
  779. ptr->alt
  780. );
  781. break;
  782. };
  783. case EVT_TARGET_2D_WHITE:
  784. case EVT_TARGET_2D_BLACK:{
  785. updateStats();
  786. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`lrf_id`,`range_digital`,`azimuth`,`range`,`area`,`lat`,`lon`,`alt`)" \
  787. " VALUES(%d,'%s',%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  788. ptr->node_id,
  789. ptr->uuid,
  790. ptr->ts_s,
  791. ptr->ts_ms,
  792. ptr->type,
  793. ptr->lrf_id,
  794. ptr->range_digital,
  795. ptr->az,
  796. ptr->ra,
  797. ptr->area,
  798. ptr->lat,
  799. ptr->lon,
  800. ptr->alt
  801. );
  802. break;
  803. };
  804. default: return 0;
  805. };
  806. #ifdef DEBUG_EXTRA
  807. LOG( tQBuf );
  808. #endif
  809. if ( m_bRepIsConnected ) {
  810. return ptr->id = m_pDataReports->QueryID(tQBuf);
  811. } else {
  812. LOG_ERROR("Report DB Droped!");
  813. }
  814. };
  815. return 0;
  816. };
  817. bool sqliteDBC::addIPCam( OBSERVER_RESULT *tPtr ){
  818. if(m_bStoIsConnected) {
  819. char tQBuf[1024];
  820. char tIpBuf[32];
  821. char tProto[32];
  822. char tPath[CTEXT];
  823. char *tPos;
  824. char *tPos2;
  825. uint16_t tPort = 0;
  826. memcpy( tProto, tPtr->XAddrs, 5 );
  827. tProto[5] = (char)0;
  828. if( tProto[4] == ':') tProto[4] = (char)0;
  829. memset( tPath, 0, CTEXT );
  830. tPos = strstr(tPtr->XAddrs,"//");
  831. if( tPos != NULL ) {
  832. tPos2 = strstr( tPos + 2,":");
  833. if( tPos2 != NULL ) {
  834. LOG("IP Port Defined!");
  835. int len, len2, len3;
  836. len = tPos2 - tPos - 2;
  837. strncpy( tIpBuf, tPos + 2, len);
  838. tIpBuf[len] = (char)0;
  839. tPort = atoi( tPos2 + 1 );
  840. strncpy( tPath, tPos + 2 + len, CTEXT );
  841. } else {
  842. tPos2 = strstr( tPos + 2,"/");
  843. if( tPos2 != NULL ) {
  844. int len, len2, len3;
  845. len = tPos2 - tPos - 2;
  846. strncpy( tIpBuf, tPos + 2, len);
  847. tIpBuf[len] = (char)0;
  848. strncpy( tPath, tPos + 2 + len, CTEXT );
  849. } else {
  850. tPos2 = strstr( tPos + 2,"/");
  851. LOG_ERROR("Pos 2 - err");
  852. tIpBuf[0] = (char)0;
  853. };
  854. };
  855. } else {
  856. LOG_ERROR("Pos 0 - err");
  857. tIpBuf[0] = (char)0;
  858. };
  859. if ( tPort ) {
  860. sprintf(tQBuf, "INSERT INTO cameras (`online`,`uuid`,`onvif_ctrl`,`ip`,`port`,`type`,`status`,`mac`,`hardware`,`name`,`onvif_access`) VALUES (1,'%s','%s','%s',%d,12,0,'%s','%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1, `ip`='%s', `port`=%d",
  861. tPtr->RefID,
  862. //tPtr->XAddrs,
  863. tPath,
  864. tIpBuf,
  865. tPort,
  866. tPtr->MAC,
  867. tPtr->Hardware,
  868. tPtr->Name,
  869. tProto,
  870. //tPtr->XAddrs,
  871. tIpBuf,
  872. tPort
  873. );
  874. } else {
  875. sprintf(tQBuf, "INSERT INTO cameras (`online`,`uuid`,`onvif_ctrl`,`ip`,`type`,`status`,`mac`,`hardware`,`name`,`onvif_access`) VALUES (1,'%s','%s','%s',12,0,'%s','%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1, `ip`='%s'",
  876. tPtr->RefID,
  877. //tPtr->XAddrs,
  878. tPath,
  879. tIpBuf,
  880. tPtr->MAC,
  881. tPtr->Hardware,
  882. tPtr->Name,
  883. tProto,
  884. //tPtr->XAddrs,
  885. tIpBuf
  886. );
  887. }
  888. #ifdef DEBUG
  889. LOG(tQBuf);
  890. #endif
  891. m_pDataStorage->SilentQuery( tQBuf );
  892. return true;
  893. };
  894. return false;
  895. };
  896. uint32_t sqliteDBC::addLocalCam( CSTR name, CSTR hardware, CSTR path, uint32_t width, uint32_t height, uint16_t fps, uint16_t den ){
  897. uint32_t RetVal = 0;
  898. if( m_bStoIsConnected ) {
  899. char tQBuf[1024];
  900. char tM5[33];
  901. CSTR tMD5 = MD5String( path );
  902. if( tMD5 != NULL ) {
  903. memcpy( tM5, tMD5, 33 );
  904. tM5[32]=(char)0;
  905. free( tMD5 );
  906. sprintf(tQBuf,"INSERT INTO cameras (`online`,`uuid`,`type`,`status`,`hardware`,`name`,`ip`) VALUES (1,'%s',10,0,'%s','%s','%s') ON CONFLICT(`uuid`) DO UPDATE SET `online`=1",
  907. tM5,
  908. hardware,
  909. name,
  910. path
  911. );
  912. #ifdef DEBUG_ALL
  913. LOG(tQBuf);
  914. #endif
  915. uint32_t camID = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  916. if ( camID == 0 ) {
  917. camID = getCamId( tM5 );
  918. };
  919. sprintf(tQBuf, "INSERT INTO `media_streams`(`source_id`,`source_type`,`valid`,`name`,`token`,`img_width`,`img_height`,`mrl_proto`,`mrl`,`picture_proto`,`fps`) VALUES(%d,'A',1,'%s','%s',%d,%d,'v4l2','%s','v4l2',%d) ON CONFLICT(`source_id`,`token`) DO UPDATE SET `valid`=1, img_width=%d, img_height=%d, `fps`=%d",
  920. camID, name, tM5, width, height, path, fps, width, height, fps );
  921. #ifdef DEBUG_EXTRA
  922. LOG(tQBuf);
  923. #endif
  924. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  925. if( RetVal == 0 ){
  926. RetVal = getMediaStreamID( camID, tM5 );
  927. };
  928. };
  929. };
  930. return RetVal;
  931. };
  932. uint32_t sqliteDBC::addLocalNetwork( uint32_t NIF_ID, NETWORK_TYPE net_type, CSTR address, CSTR mask, CSTR gateway, CSTR broadcast ){
  933. uint32_t RetVal = 0;
  934. if( m_bStoIsConnected ) {
  935. char tQBuf[MAX_PATH_LEN];
  936. sprintf( tQBuf, "INSERT INTO `networks`(`nif_id`,`type`,`address`,`gateway`,`mask`,`broadcast`) VALUES (%d,%d,'%s','%s','%s','%s') ON CONFLICT(`nif_id`,`type`) DO UPDATE SET `address`='%s', `gateway`='%s', `mask`='%s', `broadcast`='%s'",
  937. NIF_ID,
  938. net_type,
  939. address,
  940. gateway,
  941. mask,
  942. broadcast,
  943. address,
  944. gateway,
  945. mask,
  946. broadcast
  947. );
  948. #ifdef DEBUG_ALL
  949. LOG(tQBuf);
  950. #endif
  951. RetVal = m_pDataStorage->QueryID( tQBuf );
  952. // On Update IF data no need to return ID
  953. //if( RetVal == 0 ) {
  954. // RetVal = getNetworkInterfaceID( tKey );
  955. //};
  956. };
  957. return RetVal;
  958. };
  959. uint32_t sqliteDBC::addLocalNetworkInfrastructure( uint32_t NIF_ID, uint32_t OUTBOUND_NIF, bool ena_dhcp, CSTR BRANG, CSTR ERANG, CSTR MASK, CSTR LEASE_TIME ){
  960. uint32_t RetVal = 0;
  961. if( m_bStoIsConnected ) {
  962. char tQBuf[MAX_PATH_LEN];
  963. bool ena_routing = true;
  964. if( OUTBOUND_NIF == 0 ){
  965. ena_routing = false;
  966. };
  967. if( ena_dhcp ){
  968. if ( BRANG == NULL || ERANG == NULL || MASK == NULL || LEASE_TIME == NULL ) {
  969. LOG_ERROR("Wrong DHCP parameters to create Network Infrastructure!");
  970. return 0;
  971. };
  972. sprintf(tQBuf, "INSERT INTO `network_infrastructure`(`nif_id`,`enable_routing`,`outbond_nif`,`enable_dhcp`,`dhcp_range_begin`,`dhcp_range_end`,`dhcp_range_mask`,`dhcp_lease_time`) VALUES (%d,%d,%d,1,'%s','%s','%s','%s') ON CONFLICT(`nif_id`) DO UPDATE SET `enable_routing`=%d, `outbond_nif`=%d, `enable_dhcp`=1, `dhcp_range_begin`='%s', `dhcp_range_end`='%s', `dhcp_range_mask`='%s', `dhcp_lease_time`='%s'",
  973. NIF_ID,
  974. ena_routing,
  975. OUTBOUND_NIF,
  976. BRANG,
  977. ERANG,
  978. MASK,
  979. LEASE_TIME,
  980. ena_routing,
  981. OUTBOUND_NIF,
  982. BRANG,
  983. ERANG,
  984. MASK,
  985. LEASE_TIME
  986. );
  987. } else {
  988. if( ena_routing ){
  989. sprintf(tQBuf, "INSERT INTO `network_infrastructure`(`nif_id`,`enable_routing`,`outbond_nif`) VALUES(%d,1,%d) ON CONFLICT(`nif_id`) DO UPDATE SET `enable_routing`=1, `outbond_nif`=%d",
  990. NIF_ID,
  991. OUTBOUND_NIF,
  992. OUTBOUND_NIF
  993. );
  994. } else {
  995. LOG_ERROR("No Special Service Defined To Network Infrastructure!");
  996. return 0;
  997. };
  998. };
  999. #ifdef DEBUG_ALL
  1000. LOG(tQBuf);
  1001. #endif
  1002. RetVal = m_pDataStorage->QueryID( tQBuf );
  1003. // On Update IF data no need to return ID
  1004. if( RetVal == 0 ) {
  1005. RetVal = getNetworkInfrastructureID( NIF_ID );
  1006. };
  1007. };
  1008. return RetVal;
  1009. };
  1010. uint32_t sqliteDBC::addLocalNIF( CSTR device, CSTR mac, NIF_TYPE dtype, NIF_MODE dmode, uint32_t aux_id ){
  1011. uint32_t RetVal = 0;
  1012. if( m_bStoIsConnected ) {
  1013. char tQBuf[MAX_PATH_LEN];
  1014. char * tKey = MD5String( device );
  1015. if( tKey != NULL ) {
  1016. sprintf( tQBuf, "INSERT INTO `network_interfaces` (`type`,`key`,`device_name`,`mac`,`mode`,`aux_id`) VALUES (%d,'%s','%s','%s',%d,%d) ON CONFLICT(`key`) DO UPDATE SET `mode`=%d,`aux_id`=%d,`mac`='%s'",
  1017. dtype,
  1018. tKey,
  1019. device,
  1020. mac,
  1021. dmode,
  1022. aux_id,
  1023. dmode,
  1024. aux_id,
  1025. mac
  1026. );
  1027. #ifdef DEBUG_ALL
  1028. LOG(tQBuf);
  1029. #endif
  1030. RetVal = m_pDataStorage->QueryID( tQBuf );
  1031. if( RetVal == 0 ) {
  1032. RetVal = getNIF_ID( tKey );
  1033. };
  1034. free( tKey );
  1035. };
  1036. };
  1037. return RetVal;
  1038. };
  1039. uint32_t sqliteDBC::addLocalUART( uint8_t utype, uint32_t speed, uint8_t bit, char parity, float stop_bit, CSTR PATH, uint8_t mode, uint16_t port ){
  1040. uint32_t RetVal = 0;
  1041. if( m_bStoIsConnected ) {
  1042. char tQBuf[MAX_PATH_LEN];
  1043. sprintf( tQBuf, "INSERT INTO `uart_settings`(`node_id`,`type`,`speed`,`bit`,`parity`,`stop_bit`,`mode`,`port`,`path`) VALUES (%d,%d,%d,%d,'%c',%.1f,%d,%d,'%s') ON CONFLICT(`node_id`,`type`,`path`) DO UPDATE SET `id`=`id`",
  1044. m_nServerNodeID,
  1045. utype,
  1046. speed,
  1047. bit,
  1048. parity,
  1049. stop_bit,
  1050. mode,
  1051. port,
  1052. PATH
  1053. );
  1054. #ifdef DEBUG_ALL
  1055. LOG(tQBuf);
  1056. #endif
  1057. RetVal = m_pDataStorage->QueryID( tQBuf );
  1058. if( RetVal == 0 ) {
  1059. RetVal = getUartID( m_nServerNodeID, utype, PATH );
  1060. };
  1061. };
  1062. return RetVal;
  1063. };
  1064. uint32_t sqliteDBC::addLocalWiFi( uint32_t NIF_ID, WIFI_MODE mode, CSTR SSID, CSTR password, WIFI_ENCRIPTION encryption, uint32_t router_id ){
  1065. uint32_t RetVal = 0;
  1066. if( m_bStoIsConnected ) {
  1067. char tQBuf[MAX_PATH_LEN];
  1068. sprintf( tQBuf, "INSERT INTO `wifis` (`nif_id`,`mode`,`aux_id`,`SSID`,`password`,`encryption_type`) VALUES (%d,%d,%d,'%s','%s',%d) ON CONFLICT (`nif_id`) DO UPDATE SET `mode`=%d, `aux_id`=%d, `SSID`='%s', `password`='%s', `encryption_type`=%d",
  1069. NIF_ID,
  1070. mode,
  1071. router_id,
  1072. SSID,
  1073. password,
  1074. encryption,
  1075. mode,
  1076. router_id,
  1077. SSID,
  1078. password,
  1079. encryption
  1080. );
  1081. #ifdef DEBUG_ALL
  1082. LOG(tQBuf);
  1083. #endif
  1084. RetVal = m_pDataStorage->QueryID( tQBuf );
  1085. if( RetVal == 0 ) {
  1086. RetVal = getWiFi_ID( NIF_ID );
  1087. };
  1088. };
  1089. return RetVal;
  1090. };
  1091. bool sqliteDBC::cleanLRFMask( uint32_t maskID, bool deleteMask ){
  1092. if( m_bStoIsConnected ) {
  1093. char tQBuf[MAX_PATH_LEN];
  1094. //sprintf( tQBuf, "DELETE FROM `lrf_mask_trace_zones` WHERE `zone_id` IN ( SELECT `id` FROM `lrf_mask_traces` WHERE `mask_id`=%d )", maskID );
  1095. #ifdef DEBUG_EXTRA
  1096. //LOG(tQBuf);
  1097. #endif
  1098. //m_pDataStorage->SilentQuery( tQBuf );
  1099. //sprintf( tQBuf, "DELETE FROM `lrf_mask_traces` WHERE `mask_id`=%d", maskID );
  1100. #ifdef DEBUG_EXTRA
  1101. //LOG(tQBuf);
  1102. #endif
  1103. //m_pDataStorage->SilentQuery( tQBuf );
  1104. sprintf( tQBuf, "DELETE FROM `lrf_mask_vertex_points` WHERE `vertex_id` IN ( SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d )", maskID );
  1105. #ifdef DEBUG
  1106. LOG(tQBuf);
  1107. #endif
  1108. m_pDataStorage->SilentQuery( tQBuf );
  1109. sprintf( tQBuf, "DELETE FROM `lrf_mask_vertexes` WHERE `mask_id`=%d", maskID );
  1110. #ifdef DEBUG
  1111. LOG(tQBuf);
  1112. #endif
  1113. m_pDataStorage->SilentQuery( tQBuf );
  1114. //Delete Mask Itself!
  1115. if ( deleteMask ) {
  1116. sprintf( tQBuf, "DELETE FROM `lrf_masks` WHERE `id`=%d", maskID );
  1117. #ifdef DEBUG_EXTRA
  1118. LOG(tQBuf);
  1119. #endif
  1120. m_pDataStorage->SilentQuery( tQBuf );
  1121. };
  1122. return true;
  1123. };
  1124. return false;
  1125. };
  1126. bool sqliteDBC::cleanLRFWidthTable( uint32_t sensorID ){
  1127. if( m_bStoIsConnected ) {
  1128. char tQBuf[MAX_PATH_LEN];
  1129. sprintf( tQBuf, "DELETE FROM `lrf_detection_width_tables` WHERE `aux_id`=%d", sensorID );
  1130. #ifdef DEBUG_EXTRA
  1131. LOG(tQBuf);
  1132. #endif
  1133. m_pDataStorage->SilentQuery( tQBuf );
  1134. return true;
  1135. };
  1136. return false;
  1137. };
  1138. bool sqliteDBC::cleanResources( bool all ){
  1139. if( m_bStoIsConnected ) {
  1140. char tQBuf[MAX_PATH_LEN];
  1141. if( all ) {
  1142. sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id`=%d", m_nServerNodeID );
  1143. } else {
  1144. sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE ( `node_id`=%d ) AND ( `type`=100 OR `type`=101 OR `type`=103 OR `type`=120 )", m_nServerNodeID );
  1145. };
  1146. #ifdef DEBUG_ALL
  1147. LOG(tQBuf);
  1148. #endif
  1149. m_pDataStorage->SilentQuery( tQBuf );
  1150. return true;
  1151. };
  1152. return false;
  1153. };
  1154. bool sqliteDBC::wipeServerResources(){
  1155. if( m_bStoIsConnected ) {
  1156. char tQBuf[MAX_PATH_LEN];
  1157. sprintf( tQBuf, "DELETE FROM `resources` WHERE `node_id`=%d", m_nServerNodeID );
  1158. #ifdef DEBUG_ALL
  1159. LOG(tQBuf);
  1160. #endif
  1161. m_pDataStorage->SilentQuery( tQBuf );
  1162. return true;
  1163. };
  1164. return false;
  1165. };
  1166. uint32_t sqliteDBC::addLRFMask( uint32_t devID, uint8_t lrfID, uint8_t layerID ){
  1167. uint32_t RetVal = 0;
  1168. if( m_bStoIsConnected ) {
  1169. char tQBuf[MAX_PATH_LEN];
  1170. sprintf( tQBuf, "INSERT INTO `lrf_masks`(`sensor_id`,`lrf_id`,`layer`) VALUES(%d,%d,%d) ON CONFLICT(`sensor_id`,`lrf_id`,`layer`) DO UPDATE SET `layer`=%d",
  1171. devID,
  1172. lrfID,
  1173. layerID,
  1174. layerID
  1175. );
  1176. #ifdef DEBUG
  1177. LOG(tQBuf);
  1178. #endif
  1179. RetVal = m_pDataStorage->QueryID( tQBuf );
  1180. if( RetVal == 0 ) {
  1181. RetVal = getLRFMaskID( devID, lrfID, layerID );
  1182. };
  1183. };
  1184. return RetVal;
  1185. };
  1186. uint32_t sqliteDBC::addLRFMaskPolygone( uint32_t maskID, uint8_t vertexID, uint8_t r, uint8_t g, uint8_t b ){
  1187. uint32_t RetVal = 0;
  1188. if( m_bStoIsConnected ) {
  1189. char tQBuf[MAX_PATH_LEN];
  1190. sprintf( tQBuf, "INSERT INTO `lrf_mask_vertexes`(`mask_id`,`vertex_id`,`r`,`g`,`b`) VALUES(%d,%d,%d,%d,%d)",
  1191. maskID,
  1192. vertexID,
  1193. r,
  1194. g,
  1195. b
  1196. );
  1197. #ifdef DEBUG
  1198. LOG(tQBuf);
  1199. #endif
  1200. RetVal = m_pDataStorage->QueryID( tQBuf );
  1201. if( RetVal == 0 ) {
  1202. RetVal = getLRFMaskPolygoneID( maskID, vertexID );
  1203. };
  1204. };
  1205. return RetVal;
  1206. };
  1207. bool sqliteDBC::addLRFMaskPolygonePoint( uint32_t polyID, int16_t x, int16_t y ){
  1208. if( m_bStoIsConnected ) {
  1209. char tQBuf[MAX_PATH_LEN];
  1210. sprintf( tQBuf, "INSERT INTO `lrf_mask_vertex_points`(`vertex_id`,`x`,`y`) VALUES(%d,%d,%d)",
  1211. polyID,
  1212. x,
  1213. y
  1214. );
  1215. #ifdef DEBUG
  1216. LOG(tQBuf);
  1217. #endif
  1218. m_pDataStorage->SilentQuery( tQBuf );
  1219. return true;
  1220. };
  1221. return false;
  1222. };
  1223. bool sqliteDBC::addLRFWidthTable( uint32_t sensorID, PWIDTH_TABLE ptr ){
  1224. if( m_bStoIsConnected ) {
  1225. char tQBuf[MAX_PATH_LEN];
  1226. float kK = 100.0f / ptr->nCount;
  1227. for( int i = 0; i < ptr->nCount; i++ ) {
  1228. uint16_t CC = kK * i;
  1229. sprintf(tQBuf, "INSERT INTO `lrf_detection_width_tables`(`aux_id`,`width_k`,`range_k`) VALUES(%d,%d,%d)",
  1230. sensorID,
  1231. ptr->pTable[i],
  1232. CC
  1233. );
  1234. #ifdef DEBUG
  1235. LOG(tQBuf);
  1236. #endif
  1237. m_pDataStorage->SilentQuery( tQBuf );
  1238. };
  1239. return true;
  1240. };
  1241. return false;
  1242. };
  1243. bool sqliteDBC::addPicture( DBC_SNAPSHOT_DATA *ptr, CSTR ext ){
  1244. bool rVal = false;
  1245. if( ptr != NULL ){
  1246. if( m_bRepIsConnected ){
  1247. char tQBuf[MAX_PATH_LEN];
  1248. uint32_t pic_id;
  1249. sprintf( tQBuf, "%s/%s/%s/%s/%s.%s",
  1250. m_pDbReportBase, m_sPRepInfo.YEAR, m_sPRepInfo.MONTH, m_sPRepInfo.DAY, ptr->md5, ext );
  1251. FILE *fd = fopen( tQBuf, "wb+" );
  1252. if( fd ){
  1253. fwrite( ptr->data, 1, ptr->data_size, fd );
  1254. fclose(fd);
  1255. chown( tQBuf, web_access_user, web_access_group );
  1256. } else {
  1257. LOG_ERROR("Error Saving Captured picture!");
  1258. return rVal;
  1259. };
  1260. sprintf( tQBuf, "INSERT INTO pictures(`media_stream_id`,`uts`,`uts_ms`,`md5`,`ext`) VALUES(%d,%d,0,'%s','%s')",
  1261. ptr->stream_id, ptr->ts, ptr->md5, ext );
  1262. pic_id = (uint32_t) m_pDataReports->QueryID( tQBuf );
  1263. if ( pic_id ) {
  1264. for( DBC_INT_LIST::iterator idx = ptr->assoc_event_ids.begin(); idx < ptr->assoc_event_ids.end(); idx++ ) {
  1265. sprintf( tQBuf, "INSERT INTO `events_2_pictures`(`evt_id`,`picture_id`) VALUES(%d,%d)", (*idx), pic_id );
  1266. m_pDataReports->SilentQuery( tQBuf );
  1267. };
  1268. rVal = true;
  1269. };
  1270. };
  1271. };
  1272. return rVal;
  1273. };
  1274. uint32_t sqliteDBC::addResource( uint8_t type, CSTR devName, uint32_t auxID ){
  1275. uint32_t RetVal = 0;
  1276. if( m_bStoIsConnected ){
  1277. char tQBuf[2048];
  1278. sprintf( tQBuf, "%d:%d:%s", m_nServerNodeID, type, devName );
  1279. CSTR tMD5 = MD5String( tQBuf );
  1280. if( tMD5 ) {
  1281. sprintf( tQBuf, "INSERT INTO `resources`(`node_id`,`type`,`md5`,`aux_id`,`device`,`active`) VALUES (%d,%d,'%s',%d,'%s',1) ON CONFLICT(`md5`) DO UPDATE SET `active`=1, `aux_id`=%d",
  1282. m_nServerNodeID,
  1283. type,
  1284. tMD5,
  1285. auxID,
  1286. devName,
  1287. auxID
  1288. );
  1289. free( tMD5 );
  1290. #ifdef DEBUG_ALL
  1291. LOG(tQBuf);
  1292. #endif
  1293. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  1294. if( RetVal == 0 ) {
  1295. RetVal = getResourceID( m_nServerNodeID, type, auxID );
  1296. };
  1297. };
  1298. };
  1299. return RetVal;
  1300. };
  1301. bool sqliteDBC::addVideoEvent( uint32_t evtID, uint32_t videoID, uint32_t frame, uint32_t tss, uint32_t tms ){
  1302. if( m_bRepIsConnected ){
  1303. char tQBuf[2048];
  1304. sprintf( tQBuf, "INSERT INTO `events_2_videos`(`evt_id`,`video_id`,`frame`,`tss`,`tms`) VALUES(%d,%d,%d,%d,%d)",
  1305. evtID,
  1306. videoID,
  1307. frame,
  1308. tss,
  1309. tms
  1310. );
  1311. #ifdef DEBUG_EXTRA
  1312. LOG(tQBuf);
  1313. #endif
  1314. m_pDataReports->SilentQuery( tQBuf );
  1315. };
  1316. return false;
  1317. };
  1318. uint32_t sqliteDBC::addVideoRecord( uint32_t streamID, CSTR md5, CSTR ext, uint32_t tss, uint32_t tms, uint32_t fps ){
  1319. uint32_t RetVal = 0;
  1320. if( m_bRepIsConnected ){
  1321. updateStats( false, true );
  1322. char tQBuf[2048];
  1323. sprintf( tQBuf, "INSERT INTO `videos`(`media_stream_id`,`md5`,`uts`,`uts_ms`,`ext`,`fps`) VALUES(%d,'%s',%d,%d,'%s',%d)",
  1324. streamID,
  1325. md5,
  1326. tss,
  1327. tms,
  1328. ext,
  1329. fps
  1330. );
  1331. #ifdef DEBUG_EXTRA
  1332. LOG(tQBuf);
  1333. #endif
  1334. RetVal = (uint32_t) m_pDataReports->QueryID( tQBuf );
  1335. };
  1336. return RetVal;
  1337. };
  1338. DBC_MODULE_EMBED_VIDEO_DATA *sqliteDBC::getEmbedVideoData( uint32_t ID ){
  1339. DBC_MODULE_EMBED_VIDEO_DATA *tPtr = NULL;
  1340. if( m_bStoIsConnected ) {
  1341. char tQBuf[MAX_PATH_LEN];
  1342. AAssoc *result;
  1343. sprintf( tQBuf, "SELECT * FROM `module_eve` WHERE `id`=%d", ID );
  1344. #ifdef DEBUG_ALL
  1345. LOG(tQBuf);
  1346. #endif
  1347. result = m_pDataStorage->Query( tQBuf );
  1348. if( result!= NULL ) {
  1349. if( result->size() > 0 ) {
  1350. Assoc *cArr;
  1351. tPtr = (DBC_MODULE_EMBED_VIDEO_DATA*)malloc(sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  1352. if( tPtr!= NULL ) {
  1353. Assoc::iterator it;
  1354. memset(tPtr,0,sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  1355. cArr = result->at(0);
  1356. it = cArr->find("output_mode");
  1357. if( it != cArr->end() ){
  1358. tPtr->output_mode = atoi( it->second.c_str() );
  1359. };
  1360. it = cArr->find("web_port");
  1361. if( it != cArr->end() ){
  1362. tPtr->port = atoi( it->second.c_str() );
  1363. };
  1364. it = cArr->find("width");
  1365. if( it != cArr->end() ){
  1366. tPtr->width = atoi( it->second.c_str() );
  1367. };
  1368. it = cArr->find("height");
  1369. if( it != cArr->end() ){
  1370. tPtr->height = atoi( it->second.c_str() );
  1371. };
  1372. it = cArr->find("compression_quality");
  1373. if( it != cArr->end() ){
  1374. tPtr->compression_quality = atoi( it->second.c_str() );
  1375. };
  1376. it = cArr->find("show_ts");
  1377. if( it != cArr->end() ){
  1378. tPtr->show_ts = (bool)atoi( it->second.c_str() );
  1379. };
  1380. it = cArr->find("ts_x");
  1381. if( it != cArr->end() ){
  1382. tPtr->ts_x = atoi( it->second.c_str() );
  1383. };
  1384. it = cArr->find("ts_y");
  1385. if( it != cArr->end() ){
  1386. tPtr->ts_y = atoi( it->second.c_str() );
  1387. };
  1388. it = cArr->find("ts_font_face");
  1389. if( it != cArr->end() ){
  1390. tPtr->ts_font_face = atoi( it->second.c_str() );
  1391. };
  1392. it = cArr->find("ts_font_scale");
  1393. if( it != cArr->end() ){
  1394. tPtr->ts_font_scale = atof( it->second.c_str() );
  1395. };
  1396. it = cArr->find("ts_font_thickness");
  1397. if( it != cArr->end() ){
  1398. tPtr->ts_font_thickness = atoi( it->second.c_str() );
  1399. };
  1400. it = cArr->find("ts_line_type");
  1401. if( it != cArr->end() ){
  1402. tPtr->ts_line_type = atoi( it->second.c_str() );
  1403. };
  1404. it = cArr->find("ts_transperant");
  1405. if( it != cArr->end() ){
  1406. tPtr->ts_transperant = (bool)atoi( it->second.c_str() );
  1407. };
  1408. it = cArr->find("show_label");
  1409. if( it != cArr->end() ){
  1410. tPtr->show_label = (bool)atoi( it->second.c_str() );
  1411. };
  1412. it = cArr->find("label_x");
  1413. if( it != cArr->end() ){
  1414. tPtr->label_x = atoi( it->second.c_str() );
  1415. };
  1416. it = cArr->find("label_y");
  1417. if( it != cArr->end() ){
  1418. tPtr->label_y = atoi( it->second.c_str() );
  1419. };
  1420. it = cArr->find("label_font_face");
  1421. if( it != cArr->end() ){
  1422. tPtr->label_font_face = atoi( it->second.c_str() );
  1423. };
  1424. it = cArr->find("label_font_scale");
  1425. if( it != cArr->end() ){
  1426. tPtr->label_font_scale = atof( it->second.c_str() );
  1427. };
  1428. it = cArr->find("label_font_thickness");
  1429. if( it != cArr->end() ){
  1430. tPtr->label_font_thickness = atoi( it->second.c_str() );
  1431. };
  1432. it = cArr->find("label_line_type");
  1433. if( it != cArr->end() ){
  1434. tPtr->label_line_type = atoi( it->second.c_str() );
  1435. };
  1436. it = cArr->find("label_transperant");
  1437. if( it != cArr->end() ){
  1438. tPtr->label_transperant = (bool) atoi( it->second.c_str() );
  1439. };
  1440. it = cArr->find("show_logo");
  1441. if( it != cArr->end() ){
  1442. tPtr->show_logo = (bool) atoi( it->second.c_str() );
  1443. };
  1444. it = cArr->find("logo_w");
  1445. if( it != cArr->end() ){
  1446. tPtr->logo_w = atoi( it->second.c_str() );
  1447. };
  1448. it = cArr->find("logo_h");
  1449. if( it != cArr->end() ){
  1450. tPtr->logo_h = atoi( it->second.c_str() );
  1451. };
  1452. it = cArr->find("logo_x");
  1453. if( it != cArr->end() ){
  1454. tPtr->logo_x = atoi( it->second.c_str() );
  1455. };
  1456. it = cArr->find("logo_y");
  1457. if( it != cArr->end() ){
  1458. tPtr->logo_y = atoi( it->second.c_str() );
  1459. };
  1460. it = cArr->find("logo_md5");
  1461. if( it != cArr->end() ){
  1462. strncpy( tPtr->logo_md5, it->second.c_str(),32);
  1463. tPtr->logo_md5[32]=(char)0;
  1464. };
  1465. };
  1466. };
  1467. };
  1468. deleteResult( result );
  1469. };
  1470. return tPtr;
  1471. };
  1472. DBC_MEDIA_STREAM_DATA *sqliteDBC::getMediaStreamData(uint32_t ID){
  1473. DBC_MEDIA_STREAM_DATA *tVal = NULL;
  1474. if( m_bStoIsConnected ){
  1475. char tQBuf[CTEXT];
  1476. AAssoc *result;
  1477. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID );
  1478. #ifdef DEBUG_ALL
  1479. LOG(tQBuf);
  1480. #endif
  1481. result = m_pDataStorage->Query( tQBuf );
  1482. if( result!= NULL ) {
  1483. if( result->size() > 0 ) {
  1484. Assoc *cArr;
  1485. tVal = (DBC_MEDIA_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  1486. if( tVal != NULL ) {
  1487. memset( tVal, 0, sizeof(DBC_MEDIA_STREAM_DATA) );
  1488. Assoc::iterator it;
  1489. cArr = result->at(0);
  1490. tVal->id = ID;
  1491. it = cArr->find("source_id");
  1492. if( it != cArr->end() ){
  1493. tVal->source_id = atoi( it->second.c_str() );
  1494. };
  1495. it = cArr->find("source_type");
  1496. if( it != cArr->end() ){
  1497. sprintf( tVal->source_type, it->second.c_str() );
  1498. };
  1499. it = cArr->find("valid");
  1500. if( it != cArr->end() ){
  1501. tVal->valid = (bool) atoi( it->second.c_str() );
  1502. };
  1503. it = cArr->find("token");
  1504. if( it != cArr->end() ){
  1505. sprintf( tVal->token, it->second.c_str() );
  1506. };
  1507. it = cArr->find("img_width");
  1508. if( it != cArr->end() ){
  1509. tVal->width = atoi( it->second.c_str() );
  1510. };
  1511. it = cArr->find("img_height");
  1512. if( it != cArr->end() ){
  1513. tVal->height = atoi( it->second.c_str() );
  1514. };
  1515. it = cArr->find("fps");
  1516. if( it != cArr->end() ){
  1517. tVal->fps = atoi( it->second.c_str() );
  1518. };
  1519. it = cArr->find("compression_type");
  1520. if( it != cArr->end() ){
  1521. sprintf(tVal->c_type, it->second.c_str());
  1522. };
  1523. it = cArr->find("mrl_proto");
  1524. if( it != cArr->end() ){
  1525. sprintf( tVal->mrl_proto, it->second.c_str() );
  1526. };
  1527. it = cArr->find("mrl_port");
  1528. if( it != cArr->end() ){
  1529. tVal->mrl_port = atoi( it->second.c_str() );
  1530. };
  1531. it = cArr->find("mrl");
  1532. if( it != cArr->end() ){
  1533. sprintf( tVal->mrl, it->second.c_str() );
  1534. };
  1535. };
  1536. };
  1537. };
  1538. deleteResult( result );
  1539. };
  1540. return tVal;
  1541. };
  1542. uint32_t sqliteDBC::getMediaStreamID( uint32_t source_id, CSTR token ){
  1543. uint32_t RetVal = 0;
  1544. if( m_bStoIsConnected ){
  1545. char tQBuf[CTEXT];
  1546. AAssoc *result;
  1547. sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token );
  1548. #ifdef DEBUG_ALL
  1549. LOG(tQBuf);
  1550. #endif
  1551. result = m_pDataStorage->Query( tQBuf );
  1552. if( result!= NULL ) {
  1553. if( result->size() > 0 ) {
  1554. Assoc *cArr;
  1555. Assoc::iterator it;
  1556. cArr = result->at(0);
  1557. it = cArr->find("id");
  1558. if( it != cArr->end() ){
  1559. RetVal = atoi( it->second.c_str() );
  1560. };
  1561. };
  1562. };
  1563. deleteResult( result );
  1564. };
  1565. return RetVal;
  1566. };
  1567. DBC_MODULE_DATA *sqliteDBC::getModuleData( uint32_t ID ){
  1568. DBC_MODULE_DATA *tVal = NULL;
  1569. if( m_bStoIsConnected ){
  1570. char tQBuf[CTEXT];
  1571. AAssoc *result;
  1572. sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID );
  1573. #ifdef DEBUG_ALL
  1574. LOG(tQBuf);
  1575. #endif
  1576. result = m_pDataStorage->Query( tQBuf );
  1577. if( result!= NULL ) {
  1578. if( result->size() > 0 ) {
  1579. Assoc *cArr;
  1580. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  1581. if( tVal != NULL ) {
  1582. Assoc::iterator it;
  1583. cArr = result->at(0);
  1584. it = cArr->find("id");
  1585. if( it != cArr->end() ){
  1586. tVal->module_id = atoi( it->second.c_str() );
  1587. };
  1588. it = cArr->find("aux_id");
  1589. if( it != cArr->end() ){
  1590. tVal->aux_id = atoi( it->second.c_str() );
  1591. };
  1592. it = cArr->find("type");
  1593. if( it != cArr->end() ){
  1594. tVal->type = atoi( it->second.c_str() );
  1595. };
  1596. it = cArr->find("enabled");
  1597. if( it != cArr->end() ){
  1598. tVal->enabled = (bool)atoi( it->second.c_str() );
  1599. };
  1600. };
  1601. };
  1602. };
  1603. deleteResult( result );
  1604. };
  1605. return tVal;
  1606. };
  1607. DBC_LRF_INI *sqliteDBC::getINIData( uint32_t lrfID ){
  1608. DBC_LRF_INI * tVal = NULL;
  1609. if( m_bStoIsConnected ){
  1610. char tQBuf[CTEXT];
  1611. AAssoc *result;
  1612. sprintf(tQBuf, "SELECT * FROM `lrf_ini_settings` WHERE `sensor_id`=%d", lrfID );
  1613. #ifdef DEBUG
  1614. LOG(tQBuf);
  1615. #endif
  1616. result = m_pDataStorage->Query( tQBuf );
  1617. if( result!= NULL ) {
  1618. if( result->size() > 0 ) {
  1619. Assoc *cArr;
  1620. tVal = (DBC_LRF_INI*) malloc(sizeof(DBC_LRF_INI));
  1621. if( tVal != NULL ) {
  1622. memset( tVal, 0 , sizeof(DBC_LRF_INI));
  1623. Assoc::iterator it;
  1624. cArr = result->at(0);
  1625. it = cArr->find("AUTORESET_ENABLE");
  1626. if( it != cArr->end() ){
  1627. tVal->_AUTO_RESET_ENABLE = (bool) atoi( it->second.c_str() );
  1628. };
  1629. it = cArr->find("AUTORESET_INTERLEAVE");
  1630. if( it != cArr->end() ){
  1631. tVal->_AUTO_RESET_INTERLEAVE = atoi( it->second.c_str() );
  1632. };
  1633. it = cArr->find("AUTORESET_TIMEOUT");
  1634. if( it != cArr->end() ){
  1635. tVal->_AUTO_RESET_TIMEOUT = atoi( it->second.c_str() );
  1636. };
  1637. it = cArr->find("BOARD_TEMPERATURE");
  1638. if( it != cArr->end() ){
  1639. tVal->_BOARD_TEMPERATURE = atoi( it->second.c_str() );
  1640. };
  1641. it = cArr->find("BOARD_ZEROSHIFT");
  1642. if( it != cArr->end() ){
  1643. tVal->_BOARD_ZEROSHIFT = atoi( it->second.c_str() );
  1644. };
  1645. it = cArr->find("BOARD_SPEED");
  1646. if( it != cArr->end() ){
  1647. tVal->_BOARD_SPEED = atoi( it->second.c_str() );
  1648. };
  1649. it = cArr->find("RUN_DIR");
  1650. if( it != cArr->end() ){
  1651. tVal->_RUN_DIR = atoi( it->second.c_str() );
  1652. };
  1653. it = cArr->find("NO_BLACK_TARGETS");
  1654. if( it != cArr->end() ){
  1655. tVal->_NO_BLACK_TARGETS = atoi( it->second.c_str() );
  1656. };
  1657. it = cArr->find("JUST_MOVING_TARGETS");
  1658. if( it != cArr->end() ){
  1659. tVal->_JUST_MOVING_TARGETS = (bool) atoi( it->second.c_str() );
  1660. };
  1661. it = cArr->find("JUST_MOVING_RADIAL");
  1662. if( it != cArr->end() ){
  1663. tVal->_JUST_MOVING_RADIAL = (bool) atoi( it->second.c_str() );
  1664. };
  1665. it = cArr->find("JUST_MOVING_DISTANCE");
  1666. if( it != cArr->end() ){
  1667. tVal->_JUST_MOVING_DISTANCE = (bool) atoi( it->second.c_str() );
  1668. };
  1669. it = cArr->find("FREEZ_FIRST_TH");
  1670. if( it != cArr->end() ){
  1671. tVal->_FREEZ_FIRST_TH = (bool) atoi( it->second.c_str() );
  1672. };
  1673. it = cArr->find("SAVE_FIRST_TH");
  1674. if( it != cArr->end() ){
  1675. tVal->_SAVE_FIRST_TH = (bool) atoi( it->second.c_str() );
  1676. };
  1677. };
  1678. };
  1679. };
  1680. deleteResult( result );
  1681. };
  1682. return tVal;
  1683. };
  1684. bool sqliteDBC::getEventSessionStatus( uint32_t nodeID ){
  1685. bool rv = false;
  1686. if( m_bRepIsConnected ){
  1687. char tQBuf[CTEXT];
  1688. AAssoc *result;
  1689. //Since Unimodule -> no need to check NodeID
  1690. //sprintf( tQBuf, "SELECT `evt_type` FROM `events` WHERE `node_id`=%d AND ( `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d ) ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1", nodeID, EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_MA, EVT_LRF_END_SESSION_TO );
  1691. sprintf( tQBuf, "SELECT `evt_type` FROM `events` WHERE `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_MA, EVT_LRF_END_SESSION_TO );
  1692. #ifdef DEBUG_ALL
  1693. LOG(tQBuf);
  1694. #endif
  1695. result = m_pDataReports->Query( tQBuf );
  1696. if( result!= NULL ) {
  1697. if( result->size() > 0 ) {
  1698. Assoc *cArr;
  1699. Assoc::iterator it;
  1700. cArr = result->at(0);
  1701. it = cArr->find("evt_type");
  1702. if( it != cArr->end() ){
  1703. if( atoi( it->second.c_str() ) == EVT_LRF_START_SESSION ) rv = true;
  1704. };
  1705. };
  1706. };
  1707. deleteResult( result );
  1708. };
  1709. return rv;
  1710. };
  1711. /*
  1712. * The Last TS is from the current report (as latest)
  1713. */
  1714. uint32_t sqliteDBC::getLastEventTS(){
  1715. uint32_t rv = 0;
  1716. if( m_bRepIsConnected ){
  1717. char tQBuf[CTEXT];
  1718. AAssoc *result;
  1719. sprintf( tQBuf, "SELECT `ts` FROM `events` ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1" );
  1720. #ifdef DEBUG_ALL
  1721. LOG(tQBuf);
  1722. #endif
  1723. result = m_pDataReports->Query( tQBuf );
  1724. if( result!= NULL ) {
  1725. if( result->size() > 0 ) {
  1726. Assoc *cArr;
  1727. Assoc::iterator it;
  1728. cArr = result->at(0);
  1729. it = cArr->find("ts");
  1730. if( it != cArr->end() ){
  1731. rv = atoi( it->second.c_str() );
  1732. };
  1733. };
  1734. };
  1735. deleteResult( result );
  1736. };
  1737. return rv;
  1738. };
  1739. /*
  1740. * The first TS is generaly must show a DAY of first registered event so TS fetched
  1741. * from general statistics
  1742. */
  1743. uint32_t sqliteDBC::getFirstEventTS(){
  1744. uint32_t rv = 0;
  1745. if( m_bStaIsConnected ){
  1746. char tQBuf[CTEXT];
  1747. AAssoc *result;
  1748. sprintf( tQBuf, "SELECT `year`,`month`,`day` FROM `days` ORDER BY `year`,`month`,`day` LIMIT 1" );
  1749. #ifdef DEBUG_ALL
  1750. LOG(tQBuf);
  1751. #endif
  1752. result = m_pDataStatistics->Query( tQBuf );
  1753. if( result!= NULL ) {
  1754. if( result->size() > 0 ) {
  1755. REPORT_DIRINFO RDF = { 0 };
  1756. Assoc *cArr;
  1757. Assoc::iterator it;
  1758. cArr = result->at(0);
  1759. it = cArr->find("year");
  1760. if( it != cArr->end() ){
  1761. strncpy( RDF.YEAR, it->second.c_str(), 4 );
  1762. };
  1763. it = cArr->find("month");
  1764. if( it != cArr->end() ){
  1765. strncpy( RDF.MONTH, it->second.c_str(), 2 );
  1766. };
  1767. it = cArr->find("day");
  1768. if( it != cArr->end() ){
  1769. strncpy( RDF.DAY, it->second.c_str(), 2 );
  1770. };
  1771. rv = getTsFromRDF( RDF );
  1772. };
  1773. };
  1774. deleteResult( result );
  1775. };
  1776. return rv;
  1777. };
  1778. bool sqliteDBC::deleteReportDate( uint32_t ts ){
  1779. if( ts ){
  1780. REPORT_DIRINFO tRDF = {0};
  1781. fillRDF( ts, tRDF );
  1782. LOG("Delete RDF Dir");
  1783. if( deleteRDFDir( m_pDbReportBase, tRDF ) ){
  1784. LOG("Dir Deleted OK");
  1785. return deleteReportStat( tRDF );
  1786. };
  1787. };
  1788. return false;
  1789. };
  1790. bool sqliteDBC::deleteRDFDir( CSTR Base, REPORT_DIRINFO &RDF ){
  1791. char tBuff[MAX_PATH];
  1792. sprintf( tBuff, "%s/%s/%s/%s", Base, RDF.YEAR, RDF.MONTH, RDF.DAY );
  1793. // No Checking if dir is empty - dir contents all we need to delete!
  1794. if( fileExist( tBuff ) ){
  1795. sprintf( tBuff, "rm -f -R \"%s/%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH, RDF.DAY );
  1796. system( tBuff );
  1797. };
  1798. sprintf( tBuff, "%s/%s/%s", Base, RDF.YEAR, RDF.MONTH );
  1799. if( fileExist( tBuff ) ){
  1800. if( dirEmpty( tBuff ) ){
  1801. sprintf( tBuff, "rm -f -R \"%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH );
  1802. system( tBuff );
  1803. };
  1804. };
  1805. sprintf( tBuff, "%s/%s", Base, RDF.YEAR );
  1806. if( fileExist( tBuff ) ){
  1807. if( dirEmpty( tBuff ) ){
  1808. sprintf( tBuff, "rm -f -R \"%s/%s\"", Base, RDF.YEAR );
  1809. system( tBuff );
  1810. };
  1811. };
  1812. return true;
  1813. };
  1814. DBC_LRF_DATA *sqliteDBC::getLRFData( uint32_t ID ){
  1815. DBC_LRF_DATA * tVal = NULL;
  1816. if( m_bStoIsConnected ){
  1817. char tQBuf[CTEXT];
  1818. AAssoc *result;
  1819. sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID );
  1820. #ifdef DEBUG_ALL
  1821. LOG(tQBuf);
  1822. #endif
  1823. result = m_pDataStorage->Query( tQBuf );
  1824. if( result!= NULL ) {
  1825. if( result->size() > 0 ) {
  1826. Assoc *cArr;
  1827. tVal = (DBC_LRF_DATA*) malloc(sizeof(DBC_LRF_DATA));
  1828. if( tVal != NULL ) {
  1829. memset( tVal, 0 , sizeof(DBC_LRF_DATA));
  1830. Assoc::iterator it;
  1831. tVal->id = ID;
  1832. cArr = result->at(0);
  1833. it = cArr->find("data_status");
  1834. if( it != cArr->end() ){
  1835. tVal->data_status = atoi( it->second.c_str() );
  1836. };
  1837. it = cArr->find("serial");
  1838. if( it != cArr->end() ){
  1839. strncpy( tVal->serial, it->second.c_str(), sizeof(tVal->serial)-1 );
  1840. tVal->serial[sizeof(tVal->serial)-1]=(char)0;
  1841. };
  1842. it = cArr->find("version");
  1843. if( it != cArr->end() ){
  1844. strncpy( tVal->version, it->second.c_str(), sizeof(tVal->version)-1 );
  1845. tVal->version[sizeof(tVal->version)-1]=(char)0;
  1846. };
  1847. it = cArr->find("x");
  1848. if( it != cArr->end() ){
  1849. tVal->x = atof( it->second.c_str() );
  1850. };
  1851. it = cArr->find("y");
  1852. if( it != cArr->end() ){
  1853. tVal->y = atof( it->second.c_str() );
  1854. };
  1855. it = cArr->find("z");
  1856. if( it != cArr->end() ){
  1857. tVal->z = atof( it->second.c_str() );
  1858. };
  1859. it = cArr->find("x_rot");
  1860. if( it != cArr->end() ){
  1861. tVal->x_rot = atof( it->second.c_str() );
  1862. };
  1863. it = cArr->find("y_rot");
  1864. if( it != cArr->end() ){
  1865. tVal->y_rot = atof( it->second.c_str() );
  1866. };
  1867. it = cArr->find("z_rot");
  1868. if( it != cArr->end() ){
  1869. tVal->z_rot = atof( it->second.c_str() );
  1870. };
  1871. it = cArr->find("uart_id");
  1872. if( it != cArr->end() ){
  1873. tVal->uart_id = atoi( it->second.c_str() );
  1874. };
  1875. it = cArr->find("device_id");
  1876. if( it != cArr->end() ){
  1877. tVal->device_id = atoi( it->second.c_str() );
  1878. };
  1879. it = cArr->find("ip");
  1880. if( it != cArr->end() ){
  1881. sprintf( tVal->master_host, it->second.c_str() );
  1882. };
  1883. it = cArr->find("aux_ip");
  1884. if( it != cArr->end() ){
  1885. sprintf( tVal->slave_host, it->second.c_str() );
  1886. };
  1887. it = cArr->find("port");
  1888. if( it != cArr->end() ){
  1889. tVal->master_port = atoi( it->second.c_str() );
  1890. };
  1891. it = cArr->find("aux_port");
  1892. if( it != cArr->end() ){
  1893. tVal->slave_port = atoi( it->second.c_str() );
  1894. };
  1895. it = cArr->find("auto_draw");
  1896. if( it != cArr->end() ){
  1897. tVal->auto_draw = atoi( it->second.c_str() );
  1898. };
  1899. it = cArr->find("filter_id");
  1900. if( it != cArr->end() ){
  1901. tVal->filter_id = atoi( it->second.c_str() );
  1902. };
  1903. it = cArr->find("enabled_lrf");
  1904. if( it != cArr->end() ){
  1905. tVal->enabled_lrf = atoi( it->second.c_str() );
  1906. };
  1907. it = cArr->find("max_range");
  1908. if( it != cArr->end() ){
  1909. tVal->max_range = atof( it->second.c_str() );
  1910. };
  1911. it = cArr->find("base_shift");
  1912. if( it != cArr->end() ){
  1913. tVal->base_shift = atoi( it->second.c_str() );
  1914. };
  1915. it = cArr->find("mirror_shift");
  1916. if( it != cArr->end() ){
  1917. tVal->mirror_shift = atoi( it->second.c_str() );
  1918. };
  1919. it = cArr->find("detection_profile_id");
  1920. if( it != cArr->end() ){
  1921. tVal->detection_profile_id = atoi( it->second.c_str() );
  1922. };
  1923. it = cArr->find("draw_lrf_id");
  1924. if( it != cArr->end() ){
  1925. tVal->draw_lrf_id = atoi( it->second.c_str() );
  1926. };
  1927. it = cArr->find("width_table_id");
  1928. if( it != cArr->end() ){
  1929. tVal->width_table_id = atoi( it->second.c_str() );
  1930. };
  1931. it = cArr->find("zoom_table_id");
  1932. if( it != cArr->end() ){
  1933. tVal->zoom_table_id = atoi( it->second.c_str() );
  1934. };
  1935. it = cArr->find("embd_cam");
  1936. if( it != cArr->end() ){
  1937. tVal->embd_cam = atoi( it->second.c_str() );
  1938. };
  1939. it = cArr->find("embd_cam_auto");
  1940. if( it != cArr->end() ){
  1941. tVal->embd_cam_auto = (bool)atoi( it->second.c_str() );
  1942. };
  1943. it = cArr->find("embd_dc");
  1944. if( it != cArr->end() ){
  1945. tVal->embd_dc = atoi( it->second.c_str() );
  1946. };
  1947. it = cArr->find("autoreset");
  1948. if( it != cArr->end() ){
  1949. tVal->autoreset = (bool)atoi( it->second.c_str() );
  1950. };
  1951. it = cArr->find("force_settings");
  1952. if( it != cArr->end() ){
  1953. tVal->force_settings = (bool)atoi( it->second.c_str() );
  1954. };
  1955. };
  1956. };
  1957. };
  1958. deleteResult( result );
  1959. };
  1960. return tVal;
  1961. };
  1962. PLGS_2D_PARAMETERS sqliteDBC::getLRFDetectionProfile( uint32_t lrfID, uint8_t profileID, bool master ){
  1963. PLGS_2D_PARAMETERS retVal = NULL;
  1964. if( m_bStoIsConnected ){
  1965. char tQBuf[CTEXT];
  1966. AAssoc *result;
  1967. sprintf(tQBuf, "SELECT * FROM `lrf_detection_profiles` WHERE `aux_id`=%d AND `profile_id`=%d AND `master`=%d", lrfID, profileID,((master)?1:0) );
  1968. #ifdef DEBUG
  1969. LOG(tQBuf);
  1970. #endif
  1971. result = m_pDataStorage->Query( tQBuf );
  1972. if( result!= NULL ) {
  1973. if( result->size() > 0 ) {
  1974. retVal = (PLGS_2D_PARAMETERS) malloc(sizeof(LGS_2D_PARAMETERS));
  1975. memset( retVal, 0, sizeof(LGS_2D_PARAMETERS));
  1976. Assoc *cArr;
  1977. Assoc::iterator it;
  1978. cArr = result->at(0);
  1979. it = cArr->find("order");
  1980. if( it != cArr->end() ){
  1981. retVal->nOrder = atoi(it->second.c_str());
  1982. };
  1983. it = cArr->find("offset");
  1984. if( it != cArr->end() ){
  1985. retVal->nOffset = atoi(it->second.c_str());
  1986. };
  1987. it = cArr->find("delta");
  1988. if( it != cArr->end() ){
  1989. retVal->nDelta = atoi(it->second.c_str());
  1990. };
  1991. it = cArr->find("flip");
  1992. if( it != cArr->end() ){
  1993. retVal->nFlip = atoi(it->second.c_str());
  1994. };
  1995. it = cArr->find("width");
  1996. if( it != cArr->end() ){
  1997. retVal->nWidth = atoi(it->second.c_str());
  1998. };
  1999. it = cArr->find("sst");
  2000. if( it != cArr->end() ){
  2001. retVal->bSST = atoi(it->second.c_str());
  2002. };
  2003. it = cArr->find("sun");
  2004. if( it != cArr->end() ){
  2005. retVal->nSun = atoi(it->second.c_str());
  2006. };
  2007. };
  2008. };
  2009. deleteResult( result );
  2010. };
  2011. return retVal;
  2012. };
  2013. RELAY_PARAMETERS *sqliteDBC::getLRFDryContact( uint32_t lrfID ){
  2014. RELAY_PARAMETERS *retVal = NULL;
  2015. if( m_bStoIsConnected ){
  2016. char tQBuf[CTEXT];
  2017. AAssoc *result;
  2018. sprintf(tQBuf, "SELECT `dc_auto`, `dc_no`, `dc_window` FROM `lrf_sensors` WHERE `id`=%d", lrfID );
  2019. #ifdef DEBUG
  2020. LOG(tQBuf);
  2021. #endif
  2022. result = m_pDataStorage->Query( tQBuf );
  2023. if( result!= NULL ) {
  2024. if( result->size() > 0 ) {
  2025. retVal = (RELAY_PARAMETERS*) malloc(sizeof(RELAY_PARAMETERS));
  2026. if( retVal ) {
  2027. Assoc *cArr;
  2028. Assoc::iterator it;
  2029. cArr = result->at(0);
  2030. it = cArr->find("dc_auto");
  2031. if( it != cArr->end() ){
  2032. retVal->bAuto = atoi( it->second.c_str() );
  2033. };
  2034. it = cArr->find("dc_no");
  2035. if( it != cArr->end() ){
  2036. retVal->bNormallyClose = ( atoi( it->second.c_str() ) == 0 );
  2037. };
  2038. it = cArr->find("dc_window");
  2039. if( it != cArr->end() ){
  2040. retVal->nAutoDelay = atoi( it->second.c_str() );
  2041. };
  2042. };
  2043. };
  2044. };
  2045. deleteResult( result );
  2046. };
  2047. return retVal;
  2048. };
  2049. uint8_t sqliteDBC::getLRFDataStatus( uint32_t ID ){
  2050. uint8_t RetVal = 0;
  2051. if( m_bStoIsConnected ){
  2052. char tQBuf[CTEXT];
  2053. AAssoc *result;
  2054. sprintf(tQBuf, "SELECT `data_status` FROM `lrf_sensors` WHERE `id`=%d", ID );
  2055. #ifdef DEBUG
  2056. LOG(tQBuf);
  2057. #endif
  2058. result = m_pDataStorage->Query( tQBuf );
  2059. if( result!= NULL ) {
  2060. if( result->size() > 0 ) {
  2061. Assoc *cArr;
  2062. Assoc::iterator it;
  2063. cArr = result->at(0);
  2064. it = cArr->find("data_status");
  2065. if( it != cArr->end() ){
  2066. RetVal = atoi( it->second.c_str() );
  2067. };
  2068. };
  2069. };
  2070. deleteResult( result );
  2071. };
  2072. return RetVal;
  2073. };
  2074. uint32_t sqliteDBC::getLRFMaskID( uint32_t deviceID, uint8_t lrfID, uint8_t layer ){
  2075. uint32_t RetVal = 0;
  2076. if( m_bStoIsConnected ){
  2077. char tQBuf[CTEXT];
  2078. AAssoc *result;
  2079. sprintf(tQBuf, "SELECT `id` FROM `lrf_masks` WHERE `sensor_id`=%d AND `lrf_id`=%d AND `layer`=%d ", deviceID, lrfID, layer );
  2080. #ifdef DEBUG
  2081. LOG(tQBuf);
  2082. #endif
  2083. result = m_pDataStorage->Query( tQBuf );
  2084. if( result!= NULL ) {
  2085. if( result->size() > 0 ) {
  2086. Assoc *cArr;
  2087. Assoc::iterator it;
  2088. cArr = result->at(0);
  2089. it = cArr->find("id");
  2090. if( it != cArr->end() ){
  2091. RetVal = atoi( it->second.c_str() );
  2092. };
  2093. };
  2094. };
  2095. deleteResult( result );
  2096. };
  2097. return RetVal;
  2098. };
  2099. uint32_t sqliteDBC::getLRFMaskPolygoneID( uint32_t maskID, uint8_t vertecID ){
  2100. uint32_t RetVal = 0;
  2101. if( m_bStoIsConnected ){
  2102. char tQBuf[CTEXT];
  2103. AAssoc *result;
  2104. sprintf(tQBuf, "SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d AND `vertex_id`=%d", maskID, vertecID );
  2105. #ifdef DEBUG
  2106. LOG(tQBuf);
  2107. #endif
  2108. result = m_pDataStorage->Query( tQBuf );
  2109. if( result!= NULL ) {
  2110. if( result->size() > 0 ) {
  2111. Assoc *cArr;
  2112. Assoc::iterator it;
  2113. cArr = result->at(0);
  2114. it = cArr->find("id");
  2115. if( it != cArr->end() ){
  2116. RetVal = atoi( it->second.c_str() );
  2117. };
  2118. };
  2119. };
  2120. deleteResult( result );
  2121. };
  2122. return RetVal;
  2123. };
  2124. uint8_t sqliteDBC::getLRFMasksStatus( uint32_t deviceID ){
  2125. uint8_t RetVal = 0;
  2126. if( m_bStoIsConnected ){
  2127. char tQBuf[CTEXT];
  2128. AAssoc *result;
  2129. sprintf(tQBuf, "SELECT `layer` FROM `lrf_masks` WHERE `sensor_id`=%d AND `status`='E'", deviceID );
  2130. #ifdef DEBUG
  2131. LOG(tQBuf);
  2132. #endif
  2133. result = m_pDataStorage->Query( tQBuf );
  2134. if( result!= NULL ) {
  2135. if( result->size() > 0 ) {
  2136. Assoc *cArr;
  2137. Assoc::iterator it;
  2138. for(int i=0;i<result->size();i++) {
  2139. cArr = result->at(i);
  2140. it = cArr->find("layer");
  2141. if( it != cArr->end() ){
  2142. RetVal |= ( 1<< atoi( it->second.c_str() ));
  2143. };
  2144. };
  2145. };
  2146. };
  2147. deleteResult( result );
  2148. };
  2149. return RetVal;
  2150. };
  2151. DBC_LRF_UPLOAD_DATA *sqliteDBC::getLRFUploadData( uint32_t upID ){
  2152. DBC_LRF_UPLOAD_DATA *RetVal = NULL;
  2153. if( m_bStoIsConnected ){
  2154. char tQBuf[CTEXT];
  2155. AAssoc *result;
  2156. sprintf(tQBuf, "SELECT * FROM `lrf_uploads` WHERE `id`=%d", upID );
  2157. #ifdef DEBUG
  2158. LOG(tQBuf);
  2159. #endif
  2160. result = m_pDataStorage->Query( tQBuf );
  2161. if( result->size() > 0 ) {
  2162. Assoc *cArr;
  2163. RetVal = (DBC_LRF_UPLOAD_DATA*) malloc(sizeof(DBC_LRF_UPLOAD_DATA));
  2164. if( RetVal != NULL ) {
  2165. memset( RetVal, 0, sizeof(DBC_LRF_UPLOAD_DATA));
  2166. Assoc::iterator it;
  2167. RetVal->id = upID;
  2168. cArr = result->at(0);
  2169. it = cArr->find("md5");
  2170. if( it != cArr->end() ){
  2171. strncpy( RetVal->md5, it->second.c_str(), 32 );
  2172. };
  2173. it = cArr->find("file");
  2174. if( it != cArr->end() ){
  2175. strncpy( RetVal->fname, it->second.c_str(), 16 );
  2176. };
  2177. it = cArr->find("issue_reset");
  2178. if( it != cArr->end() ){
  2179. RetVal->transfer_mode = atoi( it->second.c_str() );
  2180. };
  2181. };
  2182. };
  2183. deleteResult( result );
  2184. };
  2185. return RetVal;
  2186. };
  2187. DBC_WIDTH_TABLE *sqliteDBC::getLRFWidthTable( uint32_t lrfID ){
  2188. DBC_WIDTH_TABLE *retVal = NULL;
  2189. if( m_bStoIsConnected ){
  2190. retVal = (DBC_WIDTH_TABLE *)malloc(sizeof(DBC_WIDTH_TABLE));
  2191. if ( retVal ) {
  2192. char tQBuf[CTEXT];
  2193. AAssoc *result;
  2194. sprintf(tQBuf, "SELECT `range_k`, `width_k` FROM `lrf_detection_width_tables` WHERE `aux_id`=%d ORDER BY `range_k`", lrfID );
  2195. #ifdef DEBUG_EXTRA
  2196. LOG(tQBuf);
  2197. #endif
  2198. memset( retVal, 0 , sizeof( DBC_WIDTH_TABLE ));
  2199. result = m_pDataStorage->Query( tQBuf );
  2200. if( result!= NULL ) {
  2201. if( result->size() > 0 ) {
  2202. retVal->count = result->size();
  2203. retVal->wt_records = (DBC_WT_RECORD*) calloc( retVal->count, sizeof(DBC_WT_RECORD));
  2204. if ( retVal->wt_records ) {
  2205. Assoc *cArr;
  2206. Assoc::iterator it;
  2207. for( int i = 0; i < retVal->count; i++ ) {
  2208. cArr = result->at(i);
  2209. it = cArr->find("range_k");
  2210. if( it != cArr->end() ){
  2211. retVal->wt_records[i].rangePercent = atoi( it->second.c_str() );
  2212. };
  2213. it = cArr->find("width_k");
  2214. if( it != cArr->end() ){
  2215. retVal->wt_records[i].widthPercent = atoi( it->second.c_str() );
  2216. };
  2217. };
  2218. } else {
  2219. //retVal->count = 0;
  2220. LOG_ERROR("No Memory To create WT Record!");
  2221. free( retVal );
  2222. retVal = NULL;
  2223. };
  2224. };
  2225. };
  2226. deleteResult( result );
  2227. } else {
  2228. LOG_ERROR("No Memory To create WT!");
  2229. };
  2230. };
  2231. return retVal;
  2232. };
  2233. uint32_t sqliteDBC::getNodeAuxParam( uint32_t NodeID ){
  2234. uint32_t retVal = 0;
  2235. if( m_bStoIsConnected ){
  2236. char tQBuf[CTEXT];
  2237. AAssoc *result;
  2238. sprintf(tQBuf, "SELECT `aux_parameter` FROM `nodes` WHERE `id`=%d", NodeID );
  2239. #ifdef DEBUG_EXTRA
  2240. LOG(tQBuf);
  2241. #endif
  2242. result = m_pDataStorage->Query( tQBuf );
  2243. if( result!= NULL ) {
  2244. if( result->size() > 0 ) {
  2245. Assoc *cArr;
  2246. Assoc::iterator it;
  2247. cArr = result->at(0);
  2248. it = cArr->find("aux_parameter");
  2249. if( it != cArr->end() ){
  2250. retVal = atoi( it->second.c_str() );
  2251. };
  2252. };
  2253. };
  2254. deleteResult( result );
  2255. };
  2256. return retVal;
  2257. };
  2258. DBC_NODE_DATA *sqliteDBC::getNodeData( uint32_t ID ){
  2259. DBC_NODE_DATA * tVal = NULL;
  2260. if( m_bStoIsConnected ){
  2261. char tQBuf[CTEXT];
  2262. AAssoc *result;
  2263. sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID );
  2264. #ifdef DEBUG_EXTRA
  2265. LOG(tQBuf);
  2266. #endif
  2267. result = m_pDataStorage->Query( tQBuf );
  2268. if( result!= NULL ) {
  2269. if( result->size() > 0 ) {
  2270. Assoc *cArr;
  2271. tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
  2272. if( tVal != NULL ) {
  2273. memset( tVal, 0, sizeof(DBC_NODE_DATA));
  2274. Assoc::iterator it;
  2275. tVal->node_id = ID;
  2276. cArr = result->at(0);
  2277. it = cArr->find("type");
  2278. if( it != cArr->end() ){
  2279. tVal->type = /*(NODE_TYPE)*/ atoi( it->second.c_str() );
  2280. };
  2281. it = cArr->find("aux_id");
  2282. if( it != cArr->end() ){
  2283. tVal->aux_id = atoi( it->second.c_str() );
  2284. };
  2285. it = cArr->find("aux_parameter");
  2286. if( it != cArr->end() ){
  2287. tVal->aux_parameter = atoi( it->second.c_str() );
  2288. };
  2289. it = cArr->find("online");
  2290. if( it != cArr->end() ){
  2291. tVal->online = (bool)atoi( it->second.c_str() );
  2292. };
  2293. it = cArr->find("enable");
  2294. if( it != cArr->end() ){
  2295. tVal->enabled = (bool)atoi( it->second.c_str() );
  2296. };
  2297. it = cArr->find("name");
  2298. if( it != cArr->end() ){
  2299. sprintf( tVal->name, it->second.c_str() );
  2300. };
  2301. };
  2302. };
  2303. };
  2304. deleteResult( result );
  2305. };
  2306. return tVal;
  2307. };
  2308. uint32_t sqliteDBC::getNodeIdByTypeAndAux( uint8_t type, uint32_t aux_id ){
  2309. uint32_t retVal = 0;
  2310. if( m_bStoIsConnected ){
  2311. char tQBuf[CTEXT];
  2312. AAssoc *result;
  2313. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type`=%d AND `aux_id`=%d", type, aux_id);
  2314. #ifdef DEBUG_EXTRA
  2315. LOG(tQBuf);
  2316. #endif
  2317. result = m_pDataStorage->Query( tQBuf );
  2318. if( result!= NULL ) {
  2319. if( result->size() > 0 ) {
  2320. Assoc *cArr;
  2321. Assoc::iterator it;
  2322. cArr = result->at(0);
  2323. it = cArr->find("id");
  2324. if( it != cArr->end() ){
  2325. retVal = atoi( it->second.c_str() );
  2326. };
  2327. };
  2328. };
  2329. deleteResult( result );
  2330. };
  2331. return retVal;
  2332. };
  2333. CSTR sqliteDBC::getNodeName( uint32_t NodeID ){
  2334. CSTR retVal = NULL;
  2335. if( m_bStoIsConnected ){
  2336. char tQBuf[CTEXT];
  2337. AAssoc *result;
  2338. sprintf(tQBuf, "SELECT `name` FROM `nodes` WHERE `id`=%d", NodeID );
  2339. #ifdef DEBUG_EXTRA
  2340. LOG(tQBuf);
  2341. #endif
  2342. result = m_pDataStorage->Query( tQBuf );
  2343. if( result!= NULL ) {
  2344. if( result->size() > 0 ) {
  2345. Assoc *cArr;
  2346. Assoc::iterator it;
  2347. cArr = result->at(0);
  2348. it = cArr->find("name");
  2349. if( it != cArr->end() ){
  2350. retVal = strdup( it->second.c_str() );
  2351. };
  2352. };
  2353. };
  2354. deleteResult( result );
  2355. };
  2356. return retVal;
  2357. };
  2358. DBC_NET_IF_DATA *sqliteDBC::getNIF_Data( uint32_t nif_id ){
  2359. DBC_NET_IF_DATA * tVal = NULL;
  2360. if( m_bStoIsConnected ){
  2361. char tQBuf[CTEXT];
  2362. AAssoc *result;
  2363. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id );
  2364. #ifdef DEBUG_EXTRA
  2365. LOG(tQBuf);
  2366. #endif
  2367. result = m_pDataStorage->Query( tQBuf );
  2368. if( result!= NULL ) {
  2369. if( result->size() > 0 ) {
  2370. Assoc *cArr;
  2371. tVal = (DBC_NET_IF_DATA*) malloc(sizeof(DBC_NET_IF_DATA));
  2372. if( tVal != NULL ) {
  2373. Assoc::iterator it;
  2374. tVal->id = nif_id;
  2375. cArr = result->at(0);
  2376. it = cArr->find("type");
  2377. if( it != cArr->end() ){
  2378. tVal->type = atoi(it->second.c_str());
  2379. };
  2380. it = cArr->find("mode");
  2381. if( it != cArr->end() ){
  2382. tVal->mode = atoi(it->second.c_str());
  2383. };
  2384. it = cArr->find("device_name");
  2385. if( it != cArr->end() ){
  2386. strncpy( tVal->device_name, it->second.c_str(),11);
  2387. tVal->device_name[10]=(char)0;
  2388. };
  2389. it = cArr->find("mac");
  2390. if( it != cArr->end() ){
  2391. strncpy( tVal->mac, it->second.c_str(), 33 );
  2392. tVal->mac[32]=(char)0;
  2393. };
  2394. it = cArr->find("aux_id");
  2395. if( it != cArr->end() ){
  2396. tVal->aux_id = atoi(it->second.c_str());
  2397. };
  2398. };
  2399. };
  2400. };
  2401. deleteResult( result );
  2402. };
  2403. return tVal;
  2404. };
  2405. uint32_t sqliteDBC::getNIF_ID( CSTR key ){
  2406. uint32_t tVal = 0;
  2407. if( m_bStoIsConnected ){
  2408. char tQBuf[CTEXT];
  2409. AAssoc *result;
  2410. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key );
  2411. #ifdef DEBUG_ALL
  2412. LOG(tQBuf);
  2413. #endif
  2414. result = m_pDataStorage->Query( tQBuf );
  2415. if( result!= NULL ) {
  2416. if( result->size() > 0 ) {
  2417. Assoc *cArr;
  2418. Assoc::iterator it;
  2419. cArr = result->at(0);
  2420. it = cArr->find("id");
  2421. if( it != cArr->end() ){
  2422. tVal = atoi( it->second.c_str() );
  2423. };
  2424. };
  2425. };
  2426. deleteResult( result );
  2427. };
  2428. return tVal;
  2429. };
  2430. uint32_t sqliteDBC::getNIF_IDbyDevice( CSTR device ){
  2431. uint32_t tVal = 0;
  2432. if( m_bStoIsConnected ){
  2433. char tQBuf[CTEXT];
  2434. AAssoc *result;
  2435. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device );
  2436. #ifdef DEBUG_ALL
  2437. LOG(tQBuf);
  2438. #endif
  2439. result = m_pDataStorage->Query( tQBuf );
  2440. if( result!= NULL ) {
  2441. if( result->size() > 0 ) {
  2442. Assoc *cArr;
  2443. Assoc::iterator it;
  2444. cArr = result->at(0);
  2445. it = cArr->find("id");
  2446. if( it != cArr->end() ){
  2447. tVal = atoi( it->second.c_str() );
  2448. };
  2449. };
  2450. };
  2451. deleteResult( result );
  2452. };
  2453. return tVal;
  2454. };
  2455. uint32_t sqliteDBC::getNetworkInfrastructureID( uint32_t nif_id ){
  2456. uint32_t tVal = 0;
  2457. if( m_bStoIsConnected ){
  2458. char tQBuf[CTEXT];
  2459. AAssoc *result;
  2460. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id );
  2461. #ifdef DEBUG_ALL
  2462. LOG(tQBuf);
  2463. #endif
  2464. result = m_pDataStorage->Query( tQBuf );
  2465. if( result!= NULL ) {
  2466. if( result->size() > 0 ) {
  2467. Assoc *cArr;
  2468. Assoc::iterator it;
  2469. cArr = result->at(0);
  2470. it = cArr->find("id");
  2471. if( it != cArr->end() ){
  2472. tVal = atoi( it->second.c_str() );
  2473. };
  2474. };
  2475. };
  2476. deleteResult( result );
  2477. };
  2478. return tVal;
  2479. };
  2480. REPORT_DIRINFO *sqliteDBC::getReportDirInfo( uint32_t id ){
  2481. REPORT_DIRINFO *tVal = NULL;
  2482. if( m_bStaIsCreated ){
  2483. char tQBuf[CTEXT];
  2484. AAssoc *result;
  2485. sprintf(tQBuf, "SELECT * FROM `days` WHERE `ROWID`=%d", id );
  2486. #ifdef DEBUG_ALL
  2487. LOG(tQBuf);
  2488. #endif
  2489. result = m_pDataStorage->Query( tQBuf );
  2490. if( result!= NULL ) {
  2491. if( result->size() > 0 ) {
  2492. tVal = (REPORT_DIRINFO *) malloc( sizeof(REPORT_DIRINFO));
  2493. if ( tVal ) {
  2494. Assoc *cArr;
  2495. Assoc::iterator it;
  2496. memset( tVal, 0 , sizeof( REPORT_DIRINFO));
  2497. cArr = result->at(0);
  2498. it = cArr->find("year");
  2499. if( it != cArr->end() ){
  2500. strncpy( tVal->YEAR, it->second.c_str(), 5 );
  2501. };
  2502. it = cArr->find("month");
  2503. if( it != cArr->end() ){
  2504. strncpy( tVal->MONTH, it->second.c_str(), 3 );
  2505. };
  2506. it = cArr->find("day");
  2507. if( it != cArr->end() ){
  2508. strncpy( tVal->DAY, it->second.c_str(), 3 );
  2509. };
  2510. };
  2511. };
  2512. };
  2513. deleteResult( result );
  2514. };
  2515. return tVal;
  2516. };
  2517. DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){
  2518. DBC_RESOURCE_DATA *tVal = NULL;
  2519. if( m_bStoIsConnected ){
  2520. char tQBuf[CTEXT];
  2521. AAssoc *result;
  2522. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID );
  2523. #ifdef DEBUG_ALL
  2524. LOG(tQBuf);
  2525. #endif
  2526. result = m_pDataStorage->Query( tQBuf );
  2527. if( result!= NULL ) {
  2528. if( result->size() > 0 ) {
  2529. Assoc *cArr;
  2530. tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA));
  2531. if( tVal != NULL ) {
  2532. Assoc::iterator it;
  2533. cArr = result->at(0);
  2534. tVal->id = ResourceID;
  2535. it = cArr->find("node_id");
  2536. if( it != cArr->end() ){
  2537. tVal->node_id = atoi( it->second.c_str() );
  2538. };
  2539. it = cArr->find("type");
  2540. if( it != cArr->end() ){
  2541. tVal->type = atoi( it->second.c_str() );
  2542. };
  2543. it = cArr->find("aux_id");
  2544. if( it != cArr->end() ){
  2545. tVal->aux_id = atoi( it->second.c_str() );
  2546. };
  2547. it = cArr->find("device");
  2548. if( it != cArr->end() ){
  2549. strncpy( tVal->device, it->second.c_str(), 64 );
  2550. tVal->device[63] = (char)0;
  2551. };
  2552. };
  2553. };
  2554. };
  2555. deleteResult( result );
  2556. };
  2557. return tVal;
  2558. };
  2559. uint32_t sqliteDBC::getResourceID( uint32_t NodeID, uint8_t type, uint32_t auxID ){
  2560. uint32_t RetVal = 0;
  2561. if( m_bStoIsConnected ){
  2562. char tQBuf[CTEXT];
  2563. sprintf(tQBuf,"SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID );
  2564. #ifdef DEBUG_ALL
  2565. LOG(tQBuf);
  2566. #endif
  2567. AAssoc *result;
  2568. result = m_pDataStorage->Query( tQBuf );
  2569. if( result!= NULL ) {
  2570. if( result->size() > 0 ) {
  2571. Assoc * cArr;
  2572. Assoc::iterator it;
  2573. cArr = result->at(0);
  2574. it = cArr->find("id");
  2575. if( it != cArr->end() ){
  2576. RetVal = atoi( it->second.c_str() );
  2577. };
  2578. };
  2579. };
  2580. deleteResult( result );
  2581. };
  2582. return RetVal;
  2583. };
  2584. DBC_SITE_DATA *sqliteDBC::getSiteData( uint32_t ID ){
  2585. DBC_SITE_DATA * tVal = NULL;
  2586. if( m_bStoIsConnected ){
  2587. char tQBuf[CTEXT];
  2588. AAssoc *result;
  2589. sprintf(tQBuf, "SELECT * FROM `system_props` WHERE `id`=%d", ID);
  2590. #ifdef DEBUG_ALL
  2591. LOG(tQBuf);
  2592. #endif
  2593. result = m_pDataStorage->Query( tQBuf );
  2594. if( result!= NULL ) {
  2595. if( result->size() > 0 ) {
  2596. Assoc *cArr;
  2597. tVal = (DBC_SITE_DATA*) malloc(sizeof(DBC_SITE_DATA));
  2598. if( tVal != NULL ) {
  2599. memset( tVal, 0 , sizeof(DBC_SITE_DATA));
  2600. Assoc::iterator it;
  2601. cArr = result->at(0);
  2602. it = cArr->find("type");
  2603. if( it != cArr->end() ){
  2604. tVal->account_id = atoi( it->second.c_str() );
  2605. };
  2606. it = cArr->find("name");
  2607. if( it != cArr->end() ){
  2608. sprintf( tVal->name, it->second.c_str() );
  2609. };
  2610. //it = cArr->find("server_ident");
  2611. //if( it != cArr->end() ){
  2612. // sprintf( tVal->ident, it->second.c_str() );
  2613. //};
  2614. it = cArr->find("anchor_lat");
  2615. if( it != cArr->end() ){
  2616. tVal->lat = atof( it->second.c_str() );
  2617. };
  2618. it = cArr->find("anchor_lon");
  2619. if( it != cArr->end() ){
  2620. tVal->lon = atof( it->second.c_str() );
  2621. };
  2622. it = cArr->find("anchor_alt");
  2623. if( it != cArr->end() ){
  2624. tVal->alt = atof( it->second.c_str() );
  2625. };
  2626. it = cArr->find("hemisphere");
  2627. if( it != cArr->end() ){
  2628. tVal->SH = ( it->second.c_str()[0] == 'S' );
  2629. };
  2630. it = cArr->find("map_id");
  2631. if( it != cArr->end() ){
  2632. tVal->map_id = atoi( it->second.c_str() );
  2633. };
  2634. it = cArr->find("local_binding");
  2635. if( it != cArr->end() ){
  2636. tVal->local_binding = (bool) atoi(it->second.c_str());
  2637. };
  2638. it = cArr->find("work_mode");
  2639. if( it != cArr->end() ){
  2640. tVal->work_mode = atoi(it->second.c_str());
  2641. };
  2642. it = cArr->find("report_cleanup");
  2643. if( it != cArr->end() ){
  2644. tVal->report_cleanup = atoi(it->second.c_str());
  2645. };
  2646. it = cArr->find("preserve_empty");
  2647. if( it != cArr->end() ){
  2648. tVal->preserve_empty = atoi(it->second.c_str());
  2649. };
  2650. };
  2651. };
  2652. };
  2653. deleteResult( result );
  2654. };
  2655. return tVal;
  2656. };
  2657. uint32_t sqliteDBC::getServerID( CSTR identity ){
  2658. uint32_t RetVal = 0;
  2659. if( m_bStoIsConnected ){
  2660. char tQBuf[CTEXT];
  2661. sprintf(tQBuf,"SELECT `id` FROM `servers` WHERE `identity`='%s'", identity );
  2662. #ifdef DEBUG_ALL
  2663. LOG(tQBuf);
  2664. #endif
  2665. AAssoc *result;
  2666. result = m_pDataStorage->Query( tQBuf );
  2667. if( result!= NULL ) {
  2668. if( result->size() > 0 ) {
  2669. Assoc * cArr;
  2670. Assoc::iterator it;
  2671. cArr = result->at(0);
  2672. it = cArr->find("id");
  2673. if( it != cArr->end() ){
  2674. RetVal = atoi( it->second.c_str() );
  2675. };
  2676. };
  2677. };
  2678. deleteResult( result );
  2679. };
  2680. return RetVal;
  2681. };
  2682. uint32_t sqliteDBC::getServerNode( uint32_t SID ){
  2683. uint32_t RetVal = 0;
  2684. if( m_bStoIsConnected ){
  2685. char tQBuf[CTEXT];
  2686. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  2687. #ifdef DEBUG_ALL
  2688. LOG(tQBuf);
  2689. #endif
  2690. AAssoc *result;
  2691. result = m_pDataStorage->Query( tQBuf );
  2692. if( result!= NULL ) {
  2693. if( result->size() > 0 ) {
  2694. Assoc * cArr;
  2695. Assoc::iterator it;
  2696. cArr = result->at(0);
  2697. it = cArr->find("id");
  2698. if( it != cArr->end() ){
  2699. RetVal = atoi( it->second.c_str() );
  2700. };
  2701. };
  2702. };
  2703. deleteResult( result );
  2704. };
  2705. return RetVal;
  2706. }
  2707. DBC_TARGET_FLITER_DATA *sqliteDBC::getTargetFilterData( uint32_t ID ){
  2708. DBC_TARGET_FLITER_DATA * tVal = NULL;
  2709. if( m_bStoIsConnected ){
  2710. char tQBuf[CTEXT];
  2711. AAssoc *result;
  2712. sprintf(tQBuf, "SELECT * FROM `target_filters` WHERE `id`=%d", ID );
  2713. #ifdef DEBUG
  2714. LOG(tQBuf);
  2715. #endif
  2716. result = m_pDataStorage->Query( tQBuf );
  2717. if( result!= NULL ) {
  2718. if( result->size() > 0 ) {
  2719. Assoc *cArr;
  2720. tVal = (DBC_TARGET_FLITER_DATA*) malloc(sizeof(DBC_TARGET_FLITER_DATA));
  2721. if( tVal != NULL ) {
  2722. memset( tVal, 0 , sizeof(DBC_TARGET_FLITER_DATA));
  2723. Assoc::iterator it;
  2724. tVal->id = ID;
  2725. cArr = result->at(0);
  2726. it = cArr->find("type");
  2727. if( it != cArr->end() ){
  2728. tVal->type = (TARGET_FILTER_TYPE) atoi( it->second.c_str() );
  2729. };
  2730. it = cArr->find("aux_id");
  2731. if( it != cArr->end() ){
  2732. tVal->aux_id = atoi( it->second.c_str() );
  2733. };
  2734. it = cArr->find("enabled");
  2735. if( it != cArr->end() ){
  2736. tVal->enabled = (bool) atoi( it->second.c_str() );
  2737. };
  2738. };
  2739. };
  2740. };
  2741. deleteResult( result );
  2742. };
  2743. return tVal;
  2744. };
  2745. TRAIN_FILTER_DATA *sqliteDBC::getTrainFilterData( uint32_t ID ){
  2746. TRAIN_FILTER_DATA * tVal = NULL;
  2747. if( m_bStoIsConnected ){
  2748. char tQBuf[CTEXT];
  2749. AAssoc *result;
  2750. sprintf(tQBuf, "SELECT * FROM `target_filter_train` WHERE `id`=%d", ID );
  2751. #ifdef DEBUG_EXTRA
  2752. LOG(tQBuf);
  2753. #endif
  2754. result = m_pDataStorage->Query( tQBuf );
  2755. if( result!= NULL ) {
  2756. if( result->size() > 0 ) {
  2757. Assoc *cArr;
  2758. tVal = (TRAIN_FILTER_DATA*) malloc(sizeof(TRAIN_FILTER_DATA));
  2759. if( tVal != NULL ) {
  2760. memset( tVal, 0 , sizeof(TRAIN_FILTER_DATA));
  2761. Assoc::iterator it;
  2762. cArr = result->at(0);
  2763. it = cArr->find("UPPER_SENSOR");
  2764. if( it != cArr->end() ){
  2765. tVal->nUpperSensorID = atoi( it->second.c_str() );
  2766. };
  2767. it = cArr->find("LOWER_SENSOR");
  2768. if( it != cArr->end() ){
  2769. tVal->nBottomSensorID = atoi( it->second.c_str() );
  2770. };
  2771. it = cArr->find("TRAIN_DETECT");
  2772. if( it != cArr->end() ){
  2773. tVal->bDetectTrain = (bool)atoi( it->second.c_str() );
  2774. };
  2775. it = cArr->find("TRAIN_WIDTH");
  2776. if( it != cArr->end() ){
  2777. tVal->nTrainWidth = atoi( it->second.c_str() );
  2778. };
  2779. it = cArr->find("DECISION_TIME");
  2780. if( it != cArr->end() ){
  2781. tVal->nDecisionTime = 1000 * atoi( it->second.c_str() );
  2782. };
  2783. it = cArr->find("SUSPECT_TIME");
  2784. if( it != cArr->end() ){
  2785. tVal->nSuspectTime = 1000 * atoi( it->second.c_str() );
  2786. };
  2787. it = cArr->find("TRACK_TRAIN");
  2788. if( it != cArr->end() ){
  2789. tVal->bTrackTrain = (bool)atoi( it->second.c_str() );
  2790. };
  2791. it = cArr->find("AUX_IP");
  2792. if( it != cArr->end() ){
  2793. strncpy( tVal->aux_ip, it->second.c_str(), 15 );
  2794. };
  2795. //it = cArr->find("AUX_PORT");
  2796. //if( it != cArr->end() ){
  2797. // tVal->aux_port = atoi( it->second.c_str() );
  2798. //};
  2799. it = cArr->find("DEBUG");
  2800. if( it != cArr->end() ){
  2801. tVal->bDebugMode = (bool)atoi( it->second.c_str() );
  2802. };
  2803. };
  2804. };
  2805. };
  2806. deleteResult( result );
  2807. };
  2808. return tVal;
  2809. };
  2810. DBC_UART_DATA *sqliteDBC::getUartData( uint32_t UartID ){
  2811. DBC_UART_DATA *tVal = NULL;
  2812. if( m_bStoIsConnected ) {
  2813. char tQBuf[MAX_PATH_LEN];
  2814. AAssoc *result;
  2815. sprintf( tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID );
  2816. #ifdef DEBUG_ALL
  2817. LOG(tQBuf);
  2818. #endif
  2819. result = m_pDataStorage->Query( tQBuf );
  2820. if( result!= NULL ) {
  2821. if( result->size() > 0 ) {
  2822. Assoc *cArr;
  2823. tVal = (DBC_UART_DATA*) malloc(sizeof(DBC_UART_DATA));
  2824. if( tVal != NULL ) {
  2825. Assoc::iterator it;
  2826. cArr = result->at(0);
  2827. it = cArr->find("type");
  2828. if( it != cArr->end() ){
  2829. tVal->type = atoi( it->second.c_str() );
  2830. };
  2831. it = cArr->find("speed");
  2832. if( it != cArr->end() ){
  2833. tVal->speed = atoi( it->second.c_str() );
  2834. };
  2835. it = cArr->find("mode");
  2836. if( it != cArr->end() ){
  2837. tVal->mode = atoi( it->second.c_str() );
  2838. };
  2839. it = cArr->find("port");
  2840. if( it != cArr->end() ){
  2841. tVal->port = atoi( it->second.c_str() );
  2842. };
  2843. it = cArr->find("path");
  2844. if( it != cArr->end() ){
  2845. strncpy( tVal->path, it->second.c_str(), 128 );
  2846. };
  2847. };
  2848. };
  2849. };
  2850. deleteResult( result );
  2851. };
  2852. return tVal;
  2853. };
  2854. uint32_t sqliteDBC::getUartID( uint32_t NodeID, uint8_t Type, CSTR path ){
  2855. uint32_t RetVal = 0;
  2856. if( m_bStoIsConnected ){
  2857. char tQBuf[CTEXT];
  2858. sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path );
  2859. #ifdef DEBUG_ALL
  2860. LOG(tQBuf);
  2861. #endif
  2862. AAssoc *result;
  2863. result = m_pDataStorage->Query( tQBuf );
  2864. if( result!= NULL ) {
  2865. if( result->size() > 0 ) {
  2866. Assoc * cArr;
  2867. Assoc::iterator it;
  2868. cArr = result->at(0);
  2869. it = cArr->find("id");
  2870. if( it != cArr->end() ){
  2871. RetVal = atoi( it->second.c_str() );
  2872. };
  2873. };
  2874. };
  2875. deleteResult( result );
  2876. };
  2877. return RetVal;
  2878. };
  2879. uint32_t sqliteDBC::getWiFi_ID( uint32_t nif_id ){
  2880. uint32_t tVal = 0;
  2881. if( m_bStoIsConnected ){
  2882. char tQBuf[CTEXT];
  2883. AAssoc *result;
  2884. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id );
  2885. #ifdef DEBUG_ALL
  2886. LOG(tQBuf);
  2887. #endif
  2888. result = m_pDataStorage->Query( tQBuf );
  2889. if( result!= NULL ) {
  2890. if( result->size() > 0 ) {
  2891. Assoc *cArr;
  2892. Assoc::iterator it;
  2893. cArr = result->at(0);
  2894. it = cArr->find("id");
  2895. if( it != cArr->end() ){
  2896. tVal = atoi( it->second.c_str() );
  2897. };
  2898. };
  2899. };
  2900. deleteResult( result );
  2901. };
  2902. return tVal;
  2903. };
  2904. DBC_INT_LIST *sqliteDBC::listFilters( uint32_t ServerID ){
  2905. DBC_INT_LIST * tVal = NULL;
  2906. if( m_bStoIsConnected ){
  2907. char tQBuf[CTEXT];
  2908. AAssoc *result;
  2909. sprintf(tQBuf, "SELECT `id` FROM `target_filters` WHERE `enabled`=1" );
  2910. #ifdef DEBUG
  2911. LOG(tQBuf);
  2912. #endif
  2913. result = m_pDataStorage->Query( tQBuf );
  2914. if( result!= NULL ) {
  2915. if( result->size() > 0 ) {
  2916. Assoc *cArr;
  2917. tVal = new DBC_INT_LIST();
  2918. if( tVal != NULL ) {
  2919. Assoc::iterator it;
  2920. for(int i=0;i<result->size();i++) {
  2921. cArr = result->at(i);
  2922. it = cArr->find("id");
  2923. if( it != cArr->end() ){
  2924. tVal->push_back( atoi( it->second.c_str() ));
  2925. };
  2926. };
  2927. };
  2928. };
  2929. };
  2930. deleteResult( result );
  2931. };
  2932. return tVal;
  2933. };
  2934. DBC_INT_LIST *sqliteDBC::listLRFUploads( uint32_t deviceID ){
  2935. DBC_INT_LIST * tVal = NULL;
  2936. if( m_bStoIsConnected ){
  2937. char tQBuf[CTEXT];
  2938. AAssoc *result;
  2939. sprintf(tQBuf, "SELECT `id` FROM `lrf_uploads` WHERE `status`=-1 AND `sensor_id`=%d", deviceID );
  2940. #ifdef DEBUG
  2941. LOG(tQBuf);
  2942. #endif
  2943. result = m_pDataStorage->Query( tQBuf );
  2944. if( result!= NULL ) {
  2945. if( result->size() > 0 ) {
  2946. Assoc *cArr;
  2947. tVal = new DBC_INT_LIST();
  2948. if( tVal != NULL ) {
  2949. Assoc::iterator it;
  2950. for(int i=0;i<result->size();i++) {
  2951. cArr = result->at(i);
  2952. it = cArr->find("id");
  2953. if( it != cArr->end() ){
  2954. tVal->push_back( atoi( it->second.c_str() ));
  2955. };
  2956. };
  2957. };
  2958. };
  2959. };
  2960. deleteResult( result );
  2961. };
  2962. return tVal;
  2963. };
  2964. DBC_INT_LIST *sqliteDBC::listMediaStreams( uint32_t SiteId, uint8_t listMode ){
  2965. DBC_INT_LIST * tVal = NULL;
  2966. if( m_bStoIsConnected ){
  2967. char tQBuf[CTEXT];
  2968. AAssoc *result;
  2969. switch( listMode ){
  2970. case LIST_VALID_RECORDS:
  2971. sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13) AND `ms`.`valid`=1 AND `nd`.`online`=1" );
  2972. break;
  2973. case LIST_INVALID_RECORDS:
  2974. sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13) AND `ms`.`valid`=1 AND `nd`.`online`=0" );
  2975. break;
  2976. default:
  2977. sprintf(tQBuf, "SELECT `ms`.`id` FROM `nodes` `nd` LEFT JOIN `media_streams` `ms` ON (`ms`.`id`=`nd`.`aux_parameter`) WHERE `nd`.`type` IN (10,11,12,13) AND `ms`.`valid`=1" );
  2978. };
  2979. #ifdef DEBUG_ALL
  2980. LOG(tQBuf);
  2981. #endif
  2982. result = m_pDataStorage->Query( tQBuf );
  2983. if( result!= NULL ) {
  2984. if( result->size() > 0 ) {
  2985. Assoc *cArr;
  2986. tVal = new DBC_INT_LIST();
  2987. if( tVal != NULL ) {
  2988. Assoc::iterator it;
  2989. for(int i=0;i<result->size();i++) {
  2990. cArr = result->at(i);
  2991. it = cArr->find("id");
  2992. if( it != cArr->end() ){
  2993. tVal->push_back( atoi( it->second.c_str() ));
  2994. };
  2995. };
  2996. };
  2997. };
  2998. };
  2999. deleteResult( result );
  3000. };
  3001. return tVal;
  3002. };
  3003. DBC_INT_LIST *sqliteDBC::listModules(){
  3004. DBC_INT_LIST * tVal = NULL;
  3005. if( m_bStoIsConnected ){
  3006. AAssoc *result;
  3007. result = m_pDataStorage->Query( "SELECT `id` FROM `modules` WHERE `enabled`=1" );
  3008. if( result!= NULL ) {
  3009. if( result->size() > 0 ) {
  3010. Assoc *cArr;
  3011. tVal = new DBC_INT_LIST();
  3012. if( tVal != NULL ) {
  3013. Assoc::iterator it;
  3014. for(int i=0;i<result->size();i++) {
  3015. cArr = result->at(i);
  3016. it = cArr->find("id");
  3017. if( it != cArr->end() ){
  3018. tVal->push_back( atoi( it->second.c_str() ));
  3019. };
  3020. };
  3021. };
  3022. };
  3023. };
  3024. deleteResult( result );
  3025. };
  3026. return tVal;
  3027. };
  3028. DBC_INT_LIST *sqliteDBC::listNewModules( DBC_INT_LIST * old_ids ){
  3029. DBC_INT_LIST * tVal = NULL;
  3030. if( m_bStoIsConnected ){
  3031. char tParam[CTEXT];
  3032. //memset( tParam, 0, CTEXT );
  3033. sprintf(tParam,"0");
  3034. if( old_ids != NULL ) {
  3035. char tBuffer[CTEXT];
  3036. for(int i = 0; i< old_ids->size(); i++ ) {
  3037. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  3038. strncpy( tParam, tBuffer, CTEXT );
  3039. };
  3040. };
  3041. char tQBuf[CTEXT];
  3042. AAssoc *result;
  3043. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam );
  3044. #ifdef DEBUG_ALL
  3045. LOG(tQBuf);
  3046. #endif
  3047. result = m_pDataStorage->Query( tQBuf );
  3048. if( result!= NULL ) {
  3049. if( result->size() > 0 ) {
  3050. Assoc *cArr;
  3051. tVal = new DBC_INT_LIST();
  3052. if( tVal != NULL ) {
  3053. Assoc::iterator it;
  3054. for(int i=0;i<result->size();i++) {
  3055. cArr = result->at(i);
  3056. it = cArr->find("id");
  3057. if( it != cArr->end() ){
  3058. tVal->push_back( atoi( it->second.c_str() ));
  3059. };
  3060. };
  3061. };
  3062. };
  3063. };
  3064. deleteResult( result );
  3065. };
  3066. return tVal;
  3067. };
  3068. DBC_INT_LIST *sqliteDBC::listNewNodes( DBC_INT_LIST * old_ids ){
  3069. DBC_INT_LIST * tVal = NULL;
  3070. if( m_bStoIsConnected ){
  3071. char tParam[CTEXT];
  3072. //memset( tParam, 0, CTEXT );
  3073. sprintf(tParam,"0");
  3074. if( old_ids != NULL ) {
  3075. char tBuffer[CTEXT];
  3076. for(int i = 0; i< old_ids->size(); i++ ) {
  3077. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  3078. strncpy( tParam, tBuffer, CTEXT );
  3079. };
  3080. };
  3081. char tQBuf[CTEXT];
  3082. AAssoc *result;
  3083. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `enable`=1 AND `id` NOT IN(%s)", tParam );
  3084. #ifdef DEBUG_EXTRA
  3085. LOG(tQBuf);
  3086. #endif
  3087. result = m_pDataStorage->Query( tQBuf );
  3088. if( result!= NULL ) {
  3089. if( result->size() > 0 ) {
  3090. Assoc *cArr;
  3091. tVal = new DBC_INT_LIST();
  3092. if( tVal != NULL ) {
  3093. Assoc::iterator it;
  3094. for(int i=0;i<result->size();i++) {
  3095. cArr = result->at(i);
  3096. it = cArr->find("id");
  3097. if( it != cArr->end() ){
  3098. tVal->push_back( atoi( it->second.c_str() ));
  3099. };
  3100. };
  3101. };
  3102. };
  3103. };
  3104. deleteResult( result );
  3105. };
  3106. return tVal;
  3107. };
  3108. DBC_INT_LIST *sqliteDBC::listNodes( bool all ){
  3109. DBC_INT_LIST * tVal = NULL;
  3110. if( m_bStoIsConnected ){
  3111. char tQBuf[1024];
  3112. AAssoc *result;
  3113. if( all ) {
  3114. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  3115. } else {
  3116. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0 AND `enable`=1" );
  3117. };
  3118. #ifdef DEBUG_EXTRA
  3119. LOG(tQBuf);
  3120. #endif
  3121. result = m_pDataStorage->Query( tQBuf );
  3122. if( result!= NULL ) {
  3123. if( result->size() > 0 ) {
  3124. Assoc *cArr;
  3125. tVal = new DBC_INT_LIST();
  3126. if( tVal != NULL ) {
  3127. Assoc::iterator it;
  3128. for(int i=0; i <result->size();i++) {
  3129. cArr = result->at(i);
  3130. it = cArr->find("id");
  3131. if( it != cArr->end() ){
  3132. tVal->push_back( atoi( it->second.c_str() ));
  3133. };
  3134. };
  3135. };
  3136. };
  3137. };
  3138. deleteResult( result );
  3139. };
  3140. return tVal;
  3141. };
  3142. DBC_INT_LIST *sqliteDBC::listOlderReportStat( CSTR Y, CSTR M, CSTR D ){
  3143. DBC_INT_LIST * tVal = NULL;
  3144. if ( m_bStaIsConnected ) {
  3145. char tQBuf[MAX_PATH_LEN];
  3146. AAssoc *result;
  3147. sprintf(tQBuf, "SELECT `ROWID` FROM `days` WHERE ((`year`*12*31)+(`month`*31)+`day`)<=((%s*12*31)+(%s*31)+%s) ORDER BY `ROWID`", Y, M, D );
  3148. result = m_pDataStatistics->Query( tQBuf );
  3149. if( result!= NULL ) {
  3150. if( result->size() > 0 ) {
  3151. Assoc *cArr;
  3152. tVal = new DBC_INT_LIST();
  3153. if( tVal != NULL ) {
  3154. Assoc::iterator it;
  3155. for(int i=0;i<result->size();i++) {
  3156. cArr = result->at(i);
  3157. it = cArr->find("ROWID");
  3158. if( it != cArr->end() ){
  3159. tVal->push_back( atoi( it->second.c_str() ));
  3160. };
  3161. };
  3162. };
  3163. };
  3164. };
  3165. deleteResult( result );
  3166. };
  3167. return tVal;
  3168. };
  3169. DBC_INT_LIST *sqliteDBC::listOlderReportStat( uint32_t TS ){
  3170. DBC_INT_LIST * tVal = NULL;
  3171. if ( m_bStaIsConnected ) {
  3172. char tQBuf[MAX_PATH_LEN];
  3173. AAssoc *result;
  3174. sprintf(tQBuf, "SELECT `ROWID` FROM `days` WHERE `ts`< %d", TS );
  3175. result = m_pDataStatistics->Query( tQBuf );
  3176. if( result!= NULL ) {
  3177. if( result->size() > 0 ) {
  3178. Assoc *cArr;
  3179. tVal = new DBC_INT_LIST();
  3180. if( tVal != NULL ) {
  3181. Assoc::iterator it;
  3182. for(int i=0;i<result->size();i++) {
  3183. cArr = result->at(i);
  3184. it = cArr->find("ROWID");
  3185. if( it != cArr->end() ){
  3186. tVal->push_back( atoi( it->second.c_str() ));
  3187. };
  3188. };
  3189. };
  3190. };
  3191. };
  3192. deleteResult( result );
  3193. };
  3194. return tVal;
  3195. };
  3196. DBC_INT_LIST *sqliteDBC::listResources( uint32_t NodeID ){
  3197. DBC_INT_LIST * tVal = NULL;
  3198. if( m_bStoIsConnected ){
  3199. char tQBuf[CTEXT];
  3200. AAssoc *result;
  3201. if ( NodeID == 0 ) NodeID = m_nServerNodeID;
  3202. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID );
  3203. #ifdef DEBUG
  3204. LOG(tQBuf);
  3205. #endif
  3206. result = m_pDataStorage->Query( tQBuf );
  3207. if( result!= NULL ) {
  3208. if( result->size() > 0 ) {
  3209. Assoc *cArr;
  3210. tVal = new DBC_INT_LIST();
  3211. if( tVal != NULL ) {
  3212. Assoc::iterator it;
  3213. for(int i=0;i<result->size();i++) {
  3214. cArr = result->at(i);
  3215. it = cArr->find("id");
  3216. if( it != cArr->end() ){
  3217. tVal->push_back( atoi( it->second.c_str() ));
  3218. };
  3219. };
  3220. };
  3221. };
  3222. };
  3223. deleteResult( result );
  3224. };
  3225. return tVal;
  3226. };
  3227. void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
  3228. if( m_bStoIsConnected ){
  3229. m_nSiteID = SiteID;
  3230. char tQBuf[MAX_PATH_LEN];
  3231. #ifdef DEFAULT_SERVER_ID
  3232. sprintf( tQBuf, "INSERT INTO `servers`(`id`,`site_id`,`identity`,`version_string`) VALUES (%d,%d,'%s','%s') ON CONFLICT(`id`) DO UPDATE SET `identity`='%s',`version_string`='%s'",
  3233. DEFAULT_SERVER_ID, SiteID, identity, _BUILD_STRING_, identity, _BUILD_STRING_ );
  3234. #else
  3235. sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'",
  3236. SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ );
  3237. #endif
  3238. #ifdef DEBUG_ALL
  3239. LOG(tQBuf);
  3240. #endif
  3241. m_pDataStorage->SilentQuery( tQBuf );
  3242. #ifdef DEFAULT_SERVER_ID
  3243. m_nServerID = DEFAULT_SERVER_ID;
  3244. #else
  3245. m_nServerID = getServerID( identity );
  3246. #endif
  3247. *ServID = m_nServerID;
  3248. if( m_nServerID ) {
  3249. //if ( bRecreateMemStructures )
  3250. initMemDB( m_nServerID );
  3251. sprintf( tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID );
  3252. #ifdef DEBUG_ALL
  3253. LOG(tQBuf);
  3254. #endif
  3255. m_pDataMemory->SilentQuery( tQBuf );
  3256. sprintf(tQBuf,"INSERT INTO `nodes`(`aux_id`,`type`,`online`,`name`) VALUES (%d, 0,1,'Controller') ON CONFLICT(`type`,`aux_id`) DO UPDATE SET `online`=1", *ServID );
  3257. #ifdef DEBUG_ALL
  3258. LOG(tQBuf);
  3259. #endif
  3260. m_nServerNodeID = ( uint32_t ) m_pDataStorage->QueryID( tQBuf );
  3261. if( m_nServerNodeID== 0 ){
  3262. m_nServerNodeID = getServerNode( m_nServerID );
  3263. };
  3264. *ServNode = m_nServerNodeID;
  3265. // Clenup Nodes and services ...
  3266. sprintf(tQBuf,"UPDATE `nodes` SET `online`=0" );
  3267. #ifdef DEBUG_ALL
  3268. LOG(tQBuf);
  3269. #endif
  3270. m_pDataStorage->SilentQuery( tQBuf );
  3271. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0 WHERE `manual`=0" );
  3272. #ifdef DEBUG_ALL
  3273. LOG(tQBuf);
  3274. #endif
  3275. m_pDataStorage->SilentQuery( tQBuf );
  3276. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `manual`=0 )" );
  3277. #ifdef DEBUG_ALL
  3278. LOG(tQBuf);
  3279. #endif
  3280. m_pDataStorage->SilentQuery( tQBuf );
  3281. //sprintf(tQBuf,"UPDATE `cameras` SET `online`=1 WHERE `manual`=1" );
  3282. #ifdef DEBUG_ALL
  3283. //LOG(tQBuf);
  3284. #endif
  3285. //m_pDataStorage->SilentQuery( tQBuf );
  3286. //sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `online`=1 )" );
  3287. #ifdef DEBUG_ALL
  3288. //LOG(tQBuf);
  3289. #endif
  3290. //m_pDataStorage->SilentQuery( tQBuf );
  3291. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  3292. #ifdef DEBUG_ALL
  3293. // LOG(tQBuf);
  3294. #endif
  3295. // m_pDataStorage->SilentQuery( tQBuf );
  3296. //cleanResources( tServerNodeID );
  3297. setNodeOnline( m_nServerNodeID );
  3298. };
  3299. };
  3300. };
  3301. bool sqliteDBC::setLRFDataStatus( uint32_t lrfID, uint8_t ST ){
  3302. if( m_bStoIsConnected ) {
  3303. char tQBuf[MAX_PATH_LEN];
  3304. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `data_status`=%d WHERE `id`=%d", ST, lrfID );
  3305. #ifdef DEBUG
  3306. LOG(tQBuf);
  3307. #endif
  3308. m_pDataStorage->SilentQuery( tQBuf );
  3309. return true;
  3310. };
  3311. return false;
  3312. };
  3313. bool sqliteDBC::setLRFDetectionProfile( uint32_t lrfID, uint8_t profileID ){
  3314. if( m_bStoIsConnected ) {
  3315. char tQBuf[MAX_PATH_LEN];
  3316. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `detection_profile_id`=%d WHERE `id`=%d", profileID, lrfID );
  3317. #ifdef DEBUG_ALL
  3318. LOG(tQBuf);
  3319. #endif
  3320. m_pDataStorage->SilentQuery( tQBuf );
  3321. return true;
  3322. };
  3323. return false;
  3324. };
  3325. bool sqliteDBC::setLRFMaskStatus( uint32_t maskID, bool status ){
  3326. if( m_bStoIsConnected ) {
  3327. char tQBuf[MAX_PATH_LEN];
  3328. if( status ) {
  3329. sprintf( tQBuf, "UPDATE `lrf_masks` SET `status`='E' WHERE `id`=%d", maskID );
  3330. } else {
  3331. sprintf( tQBuf, "UPDATE `lrf_masks` SET `status`='D' WHERE `id`=%d", maskID );
  3332. };
  3333. #ifdef DEBUG_ALL
  3334. LOG(tQBuf);
  3335. #endif
  3336. m_pDataStorage->SilentQuery( tQBuf );
  3337. return true;
  3338. };
  3339. return false;
  3340. };
  3341. bool sqliteDBC::setLRFAltSerial( uint32_t lrfID, CSTR Serial ){
  3342. if( m_bStoIsConnected ) {
  3343. char tQBuf[MAX_PATH_LEN];
  3344. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_serial`='%s' WHERE `id`=%d", Serial, lrfID );
  3345. #ifdef DEBUG_ALL
  3346. LOG(tQBuf);
  3347. #endif
  3348. m_pDataStorage->SilentQuery( tQBuf );
  3349. return true;
  3350. };
  3351. return false;
  3352. };
  3353. bool sqliteDBC::setLRFAltVerison( uint32_t lrfID, CSTR version ){
  3354. if( m_bStoIsConnected ) {
  3355. char tQBuf[MAX_PATH_LEN];
  3356. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_version`='%s' WHERE `id`=%d", version, lrfID );
  3357. #ifdef DEBUG_ALL
  3358. LOG(tQBuf);
  3359. #endif
  3360. m_pDataStorage->SilentQuery( tQBuf );
  3361. return true;
  3362. };
  3363. return false;
  3364. };
  3365. bool sqliteDBC::setNodeOffline( uint32_t NodeID ){
  3366. if( m_bStoIsConnected ){
  3367. char tQBuf[CTEXT];
  3368. if( NodeID ) {
  3369. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  3370. #ifdef DEBUG_ALL
  3371. LOG(tQBuf);
  3372. #endif
  3373. m_pDataStorage->SilentQuery( tQBuf );
  3374. sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
  3375. #ifdef DEBUG_ALL
  3376. LOG(tQBuf);
  3377. #endif
  3378. m_pDataStorage->SilentQuery( tQBuf );
  3379. return true;
  3380. };
  3381. };
  3382. return false;
  3383. };
  3384. bool sqliteDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
  3385. if( m_bStoIsConnected ){
  3386. char tQBuf[CTEXT];
  3387. if( NodeID ) {
  3388. sprintf(tQBuf,"UPDATE `nodes` SET `online`=%d WHERE `id`=%d", status, NodeID );
  3389. #ifdef DEBUG
  3390. LOG(tQBuf);
  3391. #endif
  3392. m_pDataStorage->SilentQuery( tQBuf );
  3393. sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
  3394. #ifdef DEBUG
  3395. LOG(tQBuf);
  3396. #endif
  3397. m_pDataStorage->SilentQuery( tQBuf );
  3398. return true;
  3399. };
  3400. };
  3401. return false;
  3402. };
  3403. bool sqliteDBC::setRessourceActive( uint32_t RessourceID, bool Status ){
  3404. if( m_bStoIsConnected ){
  3405. char tQBuf[CTEXT];
  3406. if( RessourceID ) {
  3407. sprintf(tQBuf,"UPDATE `resources` SET `active`=%d WHERE `id`=%d", Status, RessourceID );
  3408. #ifdef DEBUG
  3409. LOG(tQBuf);
  3410. #endif
  3411. m_pDataStorage->SilentQuery( tQBuf );
  3412. return true;
  3413. };
  3414. };
  3415. return false;
  3416. };
  3417. bool sqliteDBC::setStreamValid( uint32_t streamID, bool status ){
  3418. if( m_bStoIsConnected ) {
  3419. char tQBuf[MAX_PATH_LEN];
  3420. if ( status ) {
  3421. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  3422. } else {
  3423. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  3424. };
  3425. #ifdef DEBUG
  3426. LOG(tQBuf);
  3427. #endif
  3428. m_pDataStorage->SilentQuery( tQBuf );
  3429. return true;
  3430. };
  3431. return false;
  3432. };
  3433. void sqliteDBC::unregisterServer( void ){
  3434. if( m_bStoIsConnected ) {
  3435. char tQBuf[MAX_PATH_LEN];
  3436. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0" );
  3437. #ifdef DEBUG_ALL
  3438. LOG(tQBuf);
  3439. #endif
  3440. m_pDataStorage->SilentQuery( tQBuf );
  3441. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0" );
  3442. #ifdef DEBUG_ALL
  3443. LOG(tQBuf);
  3444. #endif
  3445. m_pDataStorage->SilentQuery( tQBuf );
  3446. };
  3447. setNodeOffline( m_nServerNodeID );
  3448. };
  3449. bool sqliteDBC::updateSystemInfo( DBC_SYS_INFO *ptr ){
  3450. if( m_bStoIsConnected ) {
  3451. char tQBuf[MAX_PATH_LEN];
  3452. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d",
  3453. ptr->OS_INFO,
  3454. ptr->HW_INFO,
  3455. m_nSiteID
  3456. );
  3457. #ifdef DEBUG_ALL
  3458. LOG(tQBuf);
  3459. #endif
  3460. m_pDataStorage->SilentQuery( tQBuf );
  3461. return true;
  3462. };
  3463. return false;
  3464. };
  3465. bool sqliteDBC::fillShadow2D( uint32_t ID, SHADOW & SH ){
  3466. if( m_bStoIsConnected ){
  3467. char tQBuf[CTEXT];
  3468. AAssoc *result;
  3469. sprintf(tQBuf, "SELECT `lv`.`id`, `lm`.`layer`, `lm`.`status` FROM `lrf_mask_vertexes` `lv` LEFT JOIN `lrf_masks` `lm` ON (`lm`.`id` = `lv`.`mask_id`) WHERE `lm`.`sensor_id`=%d ORDER BY `lv`.`id`", ID );
  3470. LOG(tQBuf);
  3471. result = m_pDataStorage->Query( tQBuf );
  3472. if( result!= NULL ) {
  3473. if( result->size() > 0 ) {
  3474. SH.m_nEnabled = 0xFFFFFFFF;
  3475. SH.m_nPolygonCount = result->size();
  3476. SH.pPolygones = (SHADOW_POLY *) calloc( SH.m_nPolygonCount, sizeof( SHADOW_POLY ) );
  3477. if ( SH.pPolygones ) {
  3478. Assoc * cArr;
  3479. for ( uint8_t idx = 0; idx < result->size(); idx++ ) {
  3480. Assoc::iterator it;
  3481. cArr = result->at(idx);
  3482. it = cArr->find("id");
  3483. if( it != cArr->end() ){
  3484. SH.pPolygones[idx].m_nDBID = atoi(it->second.c_str());
  3485. };
  3486. it = cArr->find("layer");
  3487. if( it != cArr->end() ){
  3488. SH.pPolygones[idx].bType = atoi(it->second.c_str());
  3489. };
  3490. it = cArr->find("status");
  3491. if( it != cArr->end() ){
  3492. SH.m_nEnabled &= ( it->second.c_str()[0] == 'E' )?0xFFFFFFFF:~(1<<SH.pPolygones[idx].bType);
  3493. };
  3494. sprintf(tQBuf, "SELECT * FROM `lrf_mask_vertex_points` WHERE `vertex_id`=%d ORDER BY `id`", SH.pPolygones[idx].m_nDBID );
  3495. LOG(tQBuf);
  3496. AAssoc *v_result = m_pDataStorage->Query( tQBuf );
  3497. if( v_result!= NULL ) {
  3498. if( v_result->size() > 0 ) {
  3499. SH.pPolygones[idx].wCount = v_result->size();
  3500. SH.pPolygones[idx].pPoint = (HZ_POINT *) calloc( SH.pPolygones[idx].wCount, sizeof( HZ_POINT ) );
  3501. if(SH.pPolygones[idx].pPoint ) {
  3502. for ( uint32_t zdx =0; zdx < SH.pPolygones[idx].wCount; zdx++ ) {
  3503. cArr = v_result->at(zdx);
  3504. it = cArr->find("x");
  3505. if( it != cArr->end() ){
  3506. SH.pPolygones[idx].pPoint[zdx].wX = atoi(it->second.c_str());
  3507. };
  3508. it = cArr->find("y");
  3509. if( it != cArr->end() ){
  3510. SH.pPolygones[idx].pPoint[zdx].wY = atoi(it->second.c_str());
  3511. };
  3512. LOG("Point Added!");
  3513. };
  3514. } else {
  3515. LOG("No mem for Points!");
  3516. };
  3517. } else {
  3518. LOG("No Points!");
  3519. SH.pPolygones[idx].wCount = 0;
  3520. SH.pPolygones[idx].pPoint = NULL;
  3521. };
  3522. deleteResult( v_result );
  3523. };
  3524. };
  3525. };
  3526. };
  3527. deleteResult( result );
  3528. };
  3529. return true;
  3530. };
  3531. return false;
  3532. };
  3533. bool sqliteDBC::getAppDebugMode(){
  3534. bool rVal = false;
  3535. if( m_bStoIsConnected ){
  3536. char tQBuf[CTEXT];
  3537. AAssoc *result;
  3538. sprintf(tQBuf, "SELECT `work_mode` FROM `system_props` WHERE `id`=1" );
  3539. #ifdef DEBUG_ALL
  3540. LOG(tQBuf);
  3541. #endif
  3542. result = m_pDataStorage->Query( tQBuf );
  3543. if( result!= NULL ) {
  3544. if( result->size() > 0 ) {
  3545. Assoc *cArr;
  3546. Assoc::iterator it;
  3547. cArr = result->at(0);
  3548. it = cArr->find("work_mode");
  3549. if( it != cArr->end() ){
  3550. rVal = (bool) atoi( it->second.c_str() );
  3551. };
  3552. };
  3553. };
  3554. deleteResult( result );
  3555. };
  3556. return rVal;
  3557. };
  3558. DBC_CAM_DATA *sqliteDBC::getCamData( uint32_t ID ){
  3559. DBC_CAM_DATA *tVal = NULL;
  3560. if( m_bStoIsConnected ){
  3561. char tQBuf[CTEXT];
  3562. AAssoc *result;
  3563. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  3564. #ifdef DEBUG_ALL
  3565. LOG(tQBuf);
  3566. #endif
  3567. result = m_pDataStorage->Query( tQBuf );
  3568. if( result!= NULL ) {
  3569. if( result->size() > 0 ) {
  3570. Assoc *cArr;
  3571. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  3572. if( tVal != NULL ) {
  3573. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  3574. tVal->id = ID;
  3575. Assoc::iterator it;
  3576. cArr = result->at(0);
  3577. it = cArr->find("type");
  3578. if( it != cArr->end() ){
  3579. tVal->type = atoi( it->second.c_str() );
  3580. };
  3581. it = cArr->find("online");
  3582. if( it != cArr->end() ){
  3583. tVal->online = (bool) atoi( it->second.c_str() );
  3584. };
  3585. it = cArr->find("stream_id");
  3586. if( it != cArr->end() ){
  3587. tVal->stream_id = atoi( it->second.c_str() );
  3588. };
  3589. it = cArr->find("hardware");
  3590. if( it != cArr->end() ){
  3591. strncpy( tVal->hardware, it->second.c_str(), 32 );
  3592. };
  3593. it = cArr->find("name");
  3594. if( it != cArr->end() ){
  3595. strncpy( tVal->name, it->second.c_str(), 64 );
  3596. };
  3597. it = cArr->find("status");
  3598. if( it != cArr->end() ){
  3599. tVal->status = atoi( it->second.c_str() );
  3600. };
  3601. it = cArr->find("session");
  3602. if( it != cArr->end() ){
  3603. strncpy( tVal->session, it->second.c_str(), 32 );
  3604. };
  3605. it = cArr->find("protocol");
  3606. if( it != cArr->end() ){
  3607. tVal->protocol = atoi( it->second.c_str() );
  3608. };
  3609. it = cArr->find("protocol_aux");
  3610. if( it != cArr->end() ){
  3611. tVal->protocol_aux = atoi( it->second.c_str() );
  3612. };
  3613. it = cArr->find("uart_id");
  3614. if( it != cArr->end() ){
  3615. tVal->uart_id = atoi( it->second.c_str() );
  3616. };
  3617. it = cArr->find("x");
  3618. if( it != cArr->end() ){
  3619. tVal->x = atof( it->second.c_str() );
  3620. };
  3621. it = cArr->find("y");
  3622. if( it != cArr->end() ){
  3623. tVal->y = atof( it->second.c_str() );
  3624. };
  3625. it = cArr->find("z");
  3626. if( it != cArr->end() ){
  3627. tVal->z = atof( it->second.c_str() );
  3628. };
  3629. it = cArr->find("x_rot");
  3630. if( it != cArr->end() ){
  3631. tVal->x_rot = atof( it->second.c_str() );
  3632. };
  3633. it = cArr->find("y_rot");
  3634. if( it != cArr->end() ){
  3635. tVal->y_rot = atof( it->second.c_str() );
  3636. };
  3637. it = cArr->find("z_rot");
  3638. if( it != cArr->end() ){
  3639. tVal->z_rot = atof( it->second.c_str() );
  3640. };
  3641. it = cArr->find("imager_type");
  3642. if( it != cArr->end() ){
  3643. tVal->imager_type = atoi( it->second.c_str() );
  3644. };
  3645. it = cArr->find("lens_f");
  3646. if( it != cArr->end() ){
  3647. tVal->lens_f = atoi( it->second.c_str() );
  3648. };
  3649. it = cArr->find("ptz_speed_step");
  3650. if( it != cArr->end() ){
  3651. tVal->ptz_speed_step = atof( it->second.c_str() );
  3652. };
  3653. it = cArr->find("pan_model");
  3654. if( it != cArr->end() ){
  3655. tVal->pan_model = atoi( it->second.c_str() );
  3656. };
  3657. it = cArr->find("pan_angles");
  3658. if( it != cArr->end() ){
  3659. tVal->pan_angles = atoi( it->second.c_str() );
  3660. };
  3661. it = cArr->find("tilt_model");
  3662. if( it != cArr->end() ){
  3663. tVal->tilt_model = atoi( it->second.c_str() );
  3664. };
  3665. it = cArr->find("tilt_angles");
  3666. if( it != cArr->end() ){
  3667. tVal->tilt_angles = atoi( it->second.c_str() );
  3668. };
  3669. it = cArr->find("zoom_model");
  3670. if( it != cArr->end() ){
  3671. tVal->zoom_model = atoi( it->second.c_str() );
  3672. };
  3673. it = cArr->find("zoom");
  3674. if( it != cArr->end() ){
  3675. tVal->zoom = atof( it->second.c_str() );
  3676. };
  3677. it = cArr->find("max_range");
  3678. if( it != cArr->end() ){
  3679. tVal->max_range = atof( it->second.c_str() );
  3680. };
  3681. it = cArr->find("access_login");
  3682. if( it != cArr->end() ){
  3683. strncpy( tVal->username, it->second.c_str(), 16 );
  3684. };
  3685. it = cArr->find("access_pass");
  3686. if( it != cArr->end() ){
  3687. strncpy( tVal->password, it->second.c_str(), 16 );
  3688. };
  3689. it = cArr->find("uuid");
  3690. if( it != cArr->end() ){
  3691. strncpy( tVal->uuid, it->second.c_str(), 64 );
  3692. };
  3693. it = cArr->find("mac");
  3694. if( it != cArr->end() ){
  3695. strncpy( tVal->mac, it->second.c_str(), 16 );
  3696. };
  3697. it = cArr->find("ip");
  3698. if( it != cArr->end() ){
  3699. strncpy( tVal->ip, it->second.c_str(), 16 );
  3700. };
  3701. it = cArr->find("port");
  3702. if( it != cArr->end() ){
  3703. tVal->port = atoi( it->second.c_str() );
  3704. };
  3705. it = cArr->find("onvif_access");
  3706. if( it != cArr->end() ){
  3707. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  3708. };
  3709. it = cArr->find("onvif_ctrl");
  3710. if( it != cArr->end() ){
  3711. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  3712. };
  3713. it = cArr->find("ptz_token");
  3714. if( it != cArr->end() ){
  3715. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  3716. };
  3717. it = cArr->find("trigger_mode");
  3718. if( it != cArr->end() ){
  3719. tVal->triggering_mode = atoi( it->second.c_str() );
  3720. };
  3721. it = cArr->find("auth_mode");
  3722. if( it != cArr->end() ){
  3723. tVal->auth_mode = atoi( it->second.c_str() );
  3724. };
  3725. it = cArr->find("ptz_axel");
  3726. if( it != cArr->end() ){
  3727. tVal->ptz_axel = atoi( it->second.c_str() );
  3728. };
  3729. it = cArr->find("manual");
  3730. if( it != cArr->end() ){
  3731. tVal->manual = (bool) atoi( it->second.c_str() );
  3732. };
  3733. it = cArr->find("homing_enable");
  3734. if( it != cArr->end() ){
  3735. tVal->homing_enable = (bool) atoi( it->second.c_str() );
  3736. };
  3737. it = cArr->find("homing_to");
  3738. if( it != cArr->end() ){
  3739. tVal->homing_to = atoi( it->second.c_str() );
  3740. };
  3741. it = cArr->find("home_token");
  3742. if( it != cArr->end() ){
  3743. strncpy( tVal->home_token, it->second.c_str(), 32 );
  3744. };
  3745. };
  3746. };
  3747. };
  3748. deleteResult( result );
  3749. };
  3750. return tVal;
  3751. };
  3752. uint32_t sqliteDBC::getCamId( CSTR UUID ){
  3753. uint32_t RetVal = 0;
  3754. if( m_bStoIsConnected ){
  3755. char tQBuf[CTEXT];
  3756. AAssoc *result;
  3757. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID );
  3758. #ifdef DEBUG_ALL
  3759. LOG(tQBuf);
  3760. #endif
  3761. result = m_pDataStorage->Query( tQBuf );
  3762. if( result!= NULL ) {
  3763. if( result->size() > 0 ) {
  3764. Assoc *cArr;
  3765. Assoc::iterator it;
  3766. cArr = result->at(0);
  3767. it = cArr->find("id");
  3768. if( it != cArr->end() ){
  3769. RetVal = atoi( it->second.c_str() );
  3770. };
  3771. };
  3772. };
  3773. deleteResult( result );
  3774. };
  3775. return RetVal;
  3776. };
  3777. CSTR sqliteDBC::getDataParam( uint8_t pid ){
  3778. switch ( pid ) {
  3779. case 0: return strdup( m_pDbFile ); break;
  3780. case 1: return strdup( web_access_user ); break;
  3781. case 2: return strdup( web_access_group ); break;
  3782. };
  3783. return NULL;
  3784. };
  3785. bool sqliteDBC::updateIPCam( uint32_t CamID, CSTR Addr ){
  3786. if( m_bStoIsConnected ) {
  3787. char tQBuf[MAX_PATH_LEN];
  3788. char tIpBuf[CTEXT];
  3789. char *tPos;
  3790. char *tPos2;
  3791. uint16_t tPort = 0;
  3792. tPos = strstr(Addr,"//");
  3793. if( tPos != NULL ) {
  3794. tPos2 = strstr( tPos + 2,":");
  3795. if( tPos2 != NULL ) {
  3796. LOG("IP Port Defined!");
  3797. int len, len2, len3;
  3798. len = tPos2 - tPos - 2;
  3799. strncpy( tIpBuf, tPos + 2, len);
  3800. tIpBuf[len] = (char)0;
  3801. tPort = atoi( tPos2 + 1 );
  3802. //strncpy( tPath, tPos + 2 + len, CTEXT );
  3803. } else {
  3804. tPos2 = strstr( tPos + 2,"/");
  3805. if( tPos2 != NULL ) {
  3806. int len, len2, len3;
  3807. len = tPos2 - tPos - 2;
  3808. strncpy( tIpBuf, tPos + 2, len);
  3809. tIpBuf[len] = (char)0;
  3810. //strncpy( tPath, tPos + 2 + len, CTEXT );
  3811. } else {
  3812. tPos2 = strstr( tPos + 2,"/");
  3813. LOG_ERROR("Pos 2 - err");
  3814. tIpBuf[0] = (char)0;
  3815. };
  3816. };
  3817. } else {
  3818. LOG_ERROR("Pos 0 - err");
  3819. tIpBuf[0] = (char)0;
  3820. };
  3821. if ( tPort ) {
  3822. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d",
  3823. Addr,
  3824. tIpBuf,
  3825. tPort,
  3826. CamID
  3827. );
  3828. } else {
  3829. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d",
  3830. Addr,
  3831. tIpBuf,
  3832. CamID
  3833. );
  3834. }
  3835. #ifdef DEBUG
  3836. LOG(tQBuf);
  3837. #endif
  3838. m_pDataStorage->SilentQuery( tQBuf );
  3839. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  3840. #ifdef DEBUG_ALL
  3841. LOG(tQBuf);
  3842. #endif
  3843. m_pDataStorage->SilentQuery( tQBuf );
  3844. return true;
  3845. };
  3846. return false;
  3847. };
  3848. bool sqliteDBC::updateCamRange( uint32_t camID, float range ){
  3849. if( m_bStoIsConnected ) {
  3850. char tQBuf[MAX_PATH_LEN];
  3851. sprintf(tQBuf,"UPDATE `cameras` SET `max_range`=%.3f WHERE `id`=%d",
  3852. range,
  3853. camID
  3854. );
  3855. #ifdef DEBUG_EXTRA
  3856. LOG(tQBuf);
  3857. #endif
  3858. m_pDataStorage->SilentQuery( tQBuf );
  3859. return true;
  3860. };
  3861. return false;
  3862. };
  3863. bool sqliteDBC::updateCamOnlineStatus( uint32_t camID, bool online ){
  3864. if( m_bStoIsConnected ) {
  3865. char tQBuf[MAX_PATH_LEN];
  3866. if ( online ) {
  3867. sprintf( tQBuf, "UPDATE `cameras` SET `online`=1 WHERE `id`=%d", camID );
  3868. } else {
  3869. sprintf( tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `id`=%d", camID );
  3870. };
  3871. m_pDataStorage->SilentQuery(tQBuf);
  3872. if ( online ) {
  3873. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", camID);
  3874. } else {
  3875. sprintf( tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id`=%d", camID );
  3876. };
  3877. m_pDataStorage->SilentQuery(tQBuf);
  3878. return true;
  3879. };
  3880. return false;
  3881. };
  3882. bool sqliteDBC::updateIPCam( CSTR uuid, CSTR Addr ){
  3883. if( m_bStoIsConnected ) {
  3884. uint32_t cID = getCamId( uuid );
  3885. if ( cID ) {
  3886. return updateIPCam( cID, Addr );
  3887. };
  3888. };
  3889. return false;
  3890. };
  3891. bool sqliteDBC::updateLocalCam( CSTR uuid ){
  3892. if( m_bStoIsConnected ) {
  3893. uint32_t cID = getCamId( uuid );
  3894. if ( cID ) {
  3895. return updateLocalCam( cID );
  3896. };
  3897. };
  3898. return false;
  3899. };
  3900. bool sqliteDBC::updateLocalCam( uint32_t CamID ){
  3901. if( m_bStoIsConnected ) {
  3902. char tQBuf[MAX_PATH_LEN];
  3903. sprintf(tQBuf,"UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID );
  3904. #ifdef DEBUG
  3905. LOG(tQBuf);
  3906. #endif
  3907. m_pDataStorage->SilentQuery( tQBuf );
  3908. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  3909. #ifdef DEBUG
  3910. LOG(tQBuf);
  3911. #endif
  3912. m_pDataStorage->SilentQuery( tQBuf );
  3913. return true;
  3914. };
  3915. return false;
  3916. };
  3917. bool sqliteDBC::updateLRFComStat( uint32_t lrfID, uint32_t ErrorPerMinute ){
  3918. if( m_bStoIsConnected ) {
  3919. char tQBuf[MAX_PATH_LEN];
  3920. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `com_epm`=%d WHERE `id`=%d", ErrorPerMinute, lrfID );
  3921. #ifdef DEBUG_ALL
  3922. LOG(tQBuf);
  3923. #endif
  3924. m_pDataStorage->SilentQuery( tQBuf );
  3925. return true;
  3926. };
  3927. return false;
  3928. };
  3929. bool sqliteDBC::updateLRFFrozenStatus( uint32_t lrfID, bool FFT, bool SFT ){
  3930. if( m_bStoIsConnected ) {
  3931. char tQBuf[MAX_PATH_LEN];
  3932. sprintf(tQBuf, "UPDATE `lrf_ini_settings` SET `FREEZ_FIRST_TH`=%d, `SAVE_FIRST_TH`=%d WHERE `sensor_id`=%d", FFT, SFT, lrfID );
  3933. #ifdef DEBUG
  3934. LOG(tQBuf);
  3935. #endif
  3936. m_pDataStorage->SilentQuery( tQBuf );
  3937. return true;
  3938. };
  3939. return false;
  3940. };
  3941. bool sqliteDBC::updateLRFDetectionProfile( uint32_t lrfID, uint8_t profileID, bool master, uint16_t nOrder, uint16_t nOffset, uint16_t nDelta, uint16_t nWidth, uint16_t nFlip, uint16_t nSun, uint8_t bSST, uint32_t GP, uint32_t spikes ){
  3942. if( m_bStoIsConnected ) {
  3943. char tQBuf[MAX_PATH_LEN];
  3944. if( master ) {
  3945. sprintf( tQBuf, "INSERT INTO `lrf_detection_profiles`(`aux_id`,`profile_id`,`master`,`order`,`offset`,`delta`,`flip`,`width`,`sst`,`sun`,`gp`,`spikes`) VALUES(%d,%d,1,%d,%d,%d,%d,%d,%d,%d,%d,%d)ON CONFLICT(`aux_id`,`lrf_id`,`master`) DO UPDATE SET `order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
  3946. lrfID,
  3947. profileID,
  3948. nOrder,
  3949. nOffset,
  3950. nDelta,
  3951. nFlip,
  3952. nWidth,
  3953. bSST,
  3954. nSun,
  3955. GP,
  3956. spikes,
  3957. nOrder,
  3958. nOffset,
  3959. nDelta,
  3960. nFlip,
  3961. nWidth,
  3962. bSST,
  3963. nSun,
  3964. GP,
  3965. spikes );
  3966. } else {
  3967. sprintf( tQBuf, "INSERT INTO `lrf_detection_profiles`(`aux_id`,`profile_id`,`master`,`order`,`offset`,`delta`,`flip`,`width`,`sst`,`sun`,`gp`,`spikes`) VALUES(%d,%d,0,%d,%d,%d,%d,%d,%d,%d,%d,%d)ON CONFLICT(`aux_id`,`lrf_id`,`master`) DO UPDATE SET `order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
  3968. lrfID,
  3969. profileID,
  3970. nOrder,
  3971. nOffset,
  3972. nDelta,
  3973. nFlip,
  3974. nWidth,
  3975. bSST,
  3976. nSun,
  3977. GP,
  3978. spikes,
  3979. nOrder,
  3980. nOffset,
  3981. nDelta,
  3982. nFlip,
  3983. nWidth,
  3984. bSST,
  3985. nSun,
  3986. GP,
  3987. spikes );
  3988. };
  3989. #ifdef DEBUG_ALL
  3990. LOG(tQBuf);
  3991. #endif
  3992. m_pDataStorage->SilentQuery( tQBuf );
  3993. return true;
  3994. };
  3995. return false;
  3996. };
  3997. bool sqliteDBC::updateLRFDryContact( uint32_t lrfID, bool bAuto, bool bNO, uint16_t wWindow ){
  3998. if( m_bStoIsConnected ) {
  3999. char tQBuf[MAX_PATH_LEN];
  4000. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `dc_auto`=%d, `dc_no`=%d, `dc_window`=%d WHERE `id`=%d", bAuto, bNO, wWindow, lrfID );
  4001. #ifdef DEBUG_ALL
  4002. LOG(tQBuf);
  4003. #endif
  4004. m_pDataStorage->SilentQuery( tQBuf );
  4005. return true;
  4006. };
  4007. return false;
  4008. };
  4009. bool sqliteDBC::updateLRFSerial( uint32_t lrfID, CSTR Serial ){
  4010. if( m_bStoIsConnected ) {
  4011. char tQBuf[MAX_PATH_LEN];
  4012. CSTR tES = m_pDataStorage->escapeString( Serial );
  4013. if( tES ) {
  4014. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID );
  4015. free( tES );
  4016. #ifdef DEBUG_ALL
  4017. LOG(tQBuf);
  4018. #endif
  4019. m_pDataStorage->SilentQuery( tQBuf );
  4020. return true;
  4021. };
  4022. };
  4023. return false;
  4024. };
  4025. bool sqliteDBC::updateLRFUpload( uint32_t id, uint8_t status ){
  4026. if( m_bStoIsConnected ) {
  4027. char tQBuf[MAX_PATH_LEN];
  4028. sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=%d WHERE `id`=%d", status, id );
  4029. #ifdef DEBUG_ALL
  4030. LOG(tQBuf);
  4031. #endif
  4032. m_pDataStorage->SilentQuery( tQBuf );
  4033. return true;
  4034. };
  4035. return false;
  4036. };
  4037. bool sqliteDBC::updateLRFUnfinishedUploads( uint32_t deviceID ){
  4038. if( m_bStoIsConnected ) {
  4039. char tQBuf[MAX_PATH_LEN];
  4040. sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=-1 WHERE `sensor_id`=%d AND `status` < 101 ", deviceID );
  4041. #ifdef DEBUG_ALL
  4042. LOG(tQBuf);
  4043. #endif
  4044. m_pDataStorage->SilentQuery( tQBuf );
  4045. return true;
  4046. };
  4047. return false;
  4048. };
  4049. bool sqliteDBC::updateLRFVerison( uint32_t lrfID, CSTR version ){
  4050. if( m_bStoIsConnected ) {
  4051. char tQBuf[MAX_PATH_LEN];
  4052. CSTR tES = m_pDataStorage->escapeString( version );
  4053. if( tES ) {
  4054. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID );
  4055. free( tES );
  4056. #ifdef DEBUG_ALL
  4057. LOG(tQBuf);
  4058. #endif
  4059. m_pDataStorage->SilentQuery( tQBuf );
  4060. return true;
  4061. };
  4062. };
  4063. return false;
  4064. };
  4065. bool sqliteDBC::updateServerIdentity( uint32_t m_nServerID, CSTR m_sIdentity ){
  4066. if( m_bStoIsConnected ) {
  4067. char tQBuf[MAX_PATH_LEN];
  4068. CSTR tES = m_pDataStorage->escapeString( m_sIdentity );
  4069. if( tES ) {
  4070. sprintf( tQBuf, "UPDATE `servers` SET `identity`='%s' WHERE `id`=%d", tES, m_nServerID );
  4071. free( tES );
  4072. #ifdef DEBUG_ALL
  4073. LOG(tQBuf);
  4074. #endif
  4075. m_pDataStorage->SilentQuery( tQBuf );
  4076. return true;
  4077. };
  4078. };
  4079. return false;
  4080. };
  4081. #undef LOG
  4082. #undef LOG_ERROR