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.

4238 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
​
​
​
​
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("JUST_MOVING_TARGETS");
  1654. if( it != cArr->end() ){
  1655. tVal->_JUST_MOVING_TARGETS = (bool) atoi( it->second.c_str() );
  1656. };
  1657. it = cArr->find("JUST_MOVING_RADIAL");
  1658. if( it != cArr->end() ){
  1659. tVal->_JUST_MOVING_RADIAL = (bool) atoi( it->second.c_str() );
  1660. };
  1661. it = cArr->find("JUST_MOVING_DISTANCE");
  1662. if( it != cArr->end() ){
  1663. tVal->_JUST_MOVING_DISTANCE = (bool) atoi( it->second.c_str() );
  1664. };
  1665. it = cArr->find("FREEZ_FIRST_TH");
  1666. if( it != cArr->end() ){
  1667. tVal->_FREEZ_FIRST_TH = (bool) atoi( it->second.c_str() );
  1668. };
  1669. it = cArr->find("SAVE_FIRST_TH");
  1670. if( it != cArr->end() ){
  1671. tVal->_SAVE_FIRST_TH = (bool) atoi( it->second.c_str() );
  1672. };
  1673. };
  1674. };
  1675. };
  1676. deleteResult( result );
  1677. };
  1678. return tVal;
  1679. };
  1680. bool sqliteDBC::getEventSessionStatus( uint32_t nodeID ){
  1681. bool rv = false;
  1682. if( m_bRepIsConnected ){
  1683. char tQBuf[CTEXT];
  1684. AAssoc *result;
  1685. //Since Unimodule -> no need to check NodeID
  1686. //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 );
  1687. 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 );
  1688. #ifdef DEBUG_ALL
  1689. LOG(tQBuf);
  1690. #endif
  1691. result = m_pDataReports->Query( tQBuf );
  1692. if( result!= NULL ) {
  1693. if( result->size() > 0 ) {
  1694. Assoc *cArr;
  1695. Assoc::iterator it;
  1696. cArr = result->at(0);
  1697. it = cArr->find("evt_type");
  1698. if( it != cArr->end() ){
  1699. if( atoi( it->second.c_str() ) == EVT_LRF_START_SESSION ) rv = true;
  1700. };
  1701. };
  1702. };
  1703. deleteResult( result );
  1704. };
  1705. return rv;
  1706. };
  1707. /*
  1708. * The Last TS is from the current report (as latest)
  1709. */
  1710. uint32_t sqliteDBC::getLastEventTS(){
  1711. uint32_t rv = 0;
  1712. if( m_bRepIsConnected ){
  1713. char tQBuf[CTEXT];
  1714. AAssoc *result;
  1715. sprintf( tQBuf, "SELECT `ts` FROM `events` ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1" );
  1716. #ifdef DEBUG_ALL
  1717. LOG(tQBuf);
  1718. #endif
  1719. result = m_pDataReports->Query( tQBuf );
  1720. if( result!= NULL ) {
  1721. if( result->size() > 0 ) {
  1722. Assoc *cArr;
  1723. Assoc::iterator it;
  1724. cArr = result->at(0);
  1725. it = cArr->find("ts");
  1726. if( it != cArr->end() ){
  1727. rv = atoi( it->second.c_str() );
  1728. };
  1729. };
  1730. };
  1731. deleteResult( result );
  1732. };
  1733. return rv;
  1734. };
  1735. /*
  1736. * The first TS is generaly must show a DAY of first registered event so TS fetched
  1737. * from general statistics
  1738. */
  1739. uint32_t sqliteDBC::getFirstEventTS(){
  1740. uint32_t rv = 0;
  1741. if( m_bStaIsConnected ){
  1742. char tQBuf[CTEXT];
  1743. AAssoc *result;
  1744. sprintf( tQBuf, "SELECT `year`,`month`,`day` FROM `days` ORDER BY `year`,`month`,`day` LIMIT 1" );
  1745. #ifdef DEBUG_ALL
  1746. LOG(tQBuf);
  1747. #endif
  1748. result = m_pDataStatistics->Query( tQBuf );
  1749. if( result!= NULL ) {
  1750. if( result->size() > 0 ) {
  1751. REPORT_DIRINFO RDF = { 0 };
  1752. Assoc *cArr;
  1753. Assoc::iterator it;
  1754. cArr = result->at(0);
  1755. it = cArr->find("year");
  1756. if( it != cArr->end() ){
  1757. strncpy( RDF.YEAR, it->second.c_str(), 4 );
  1758. };
  1759. it = cArr->find("month");
  1760. if( it != cArr->end() ){
  1761. strncpy( RDF.MONTH, it->second.c_str(), 2 );
  1762. };
  1763. it = cArr->find("day");
  1764. if( it != cArr->end() ){
  1765. strncpy( RDF.DAY, it->second.c_str(), 2 );
  1766. };
  1767. rv = getTsFromRDF( RDF );
  1768. };
  1769. };
  1770. deleteResult( result );
  1771. };
  1772. return rv;
  1773. };
  1774. bool sqliteDBC::deleteReportDate( uint32_t ts ){
  1775. if( ts ){
  1776. REPORT_DIRINFO tRDF = {0};
  1777. fillRDF( ts, tRDF );
  1778. LOG("Delete RDF Dir");
  1779. if( deleteRDFDir( m_pDbReportBase, tRDF ) ){
  1780. LOG("Dir Deleted OK");
  1781. return deleteReportStat( tRDF );
  1782. };
  1783. };
  1784. return false;
  1785. };
  1786. bool sqliteDBC::deleteRDFDir( CSTR Base, REPORT_DIRINFO &RDF ){
  1787. char tBuff[MAX_PATH];
  1788. sprintf( tBuff, "%s/%s/%s/%s", Base, RDF.YEAR, RDF.MONTH, RDF.DAY );
  1789. // No Checking if dir is empty - dir contents all we need to delete!
  1790. if( fileExist( tBuff ) ){
  1791. sprintf( tBuff, "rm -f -R \"%s/%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH, RDF.DAY );
  1792. system( tBuff );
  1793. };
  1794. sprintf( tBuff, "%s/%s/%s", Base, RDF.YEAR, RDF.MONTH );
  1795. if( fileExist( tBuff ) ){
  1796. if( dirEmpty( tBuff ) ){
  1797. sprintf( tBuff, "rm -f -R \"%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH );
  1798. system( tBuff );
  1799. };
  1800. };
  1801. sprintf( tBuff, "%s/%s", Base, RDF.YEAR );
  1802. if( fileExist( tBuff ) ){
  1803. if( dirEmpty( tBuff ) ){
  1804. sprintf( tBuff, "rm -f -R \"%s/%s\"", Base, RDF.YEAR );
  1805. system( tBuff );
  1806. };
  1807. };
  1808. return true;
  1809. };
  1810. DBC_LRF_DATA *sqliteDBC::getLRFData( uint32_t ID ){
  1811. DBC_LRF_DATA * tVal = NULL;
  1812. if( m_bStoIsConnected ){
  1813. char tQBuf[CTEXT];
  1814. AAssoc *result;
  1815. sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID );
  1816. #ifdef DEBUG_ALL
  1817. LOG(tQBuf);
  1818. #endif
  1819. result = m_pDataStorage->Query( tQBuf );
  1820. if( result!= NULL ) {
  1821. if( result->size() > 0 ) {
  1822. Assoc *cArr;
  1823. tVal = (DBC_LRF_DATA*) malloc(sizeof(DBC_LRF_DATA));
  1824. if( tVal != NULL ) {
  1825. memset( tVal, 0 , sizeof(DBC_LRF_DATA));
  1826. Assoc::iterator it;
  1827. tVal->id = ID;
  1828. cArr = result->at(0);
  1829. it = cArr->find("data_status");
  1830. if( it != cArr->end() ){
  1831. tVal->data_status = atoi( it->second.c_str() );
  1832. };
  1833. it = cArr->find("serial");
  1834. if( it != cArr->end() ){
  1835. strncpy( tVal->serial, it->second.c_str(), sizeof(tVal->serial)-1 );
  1836. tVal->serial[sizeof(tVal->serial)-1]=(char)0;
  1837. };
  1838. it = cArr->find("version");
  1839. if( it != cArr->end() ){
  1840. strncpy( tVal->version, it->second.c_str(), sizeof(tVal->version)-1 );
  1841. tVal->version[sizeof(tVal->version)-1]=(char)0;
  1842. };
  1843. it = cArr->find("x");
  1844. if( it != cArr->end() ){
  1845. tVal->x = atof( it->second.c_str() );
  1846. };
  1847. it = cArr->find("y");
  1848. if( it != cArr->end() ){
  1849. tVal->y = atof( it->second.c_str() );
  1850. };
  1851. it = cArr->find("z");
  1852. if( it != cArr->end() ){
  1853. tVal->z = atof( it->second.c_str() );
  1854. };
  1855. it = cArr->find("x_rot");
  1856. if( it != cArr->end() ){
  1857. tVal->x_rot = atof( it->second.c_str() );
  1858. };
  1859. it = cArr->find("y_rot");
  1860. if( it != cArr->end() ){
  1861. tVal->y_rot = atof( it->second.c_str() );
  1862. };
  1863. it = cArr->find("z_rot");
  1864. if( it != cArr->end() ){
  1865. tVal->z_rot = atof( it->second.c_str() );
  1866. };
  1867. it = cArr->find("uart_id");
  1868. if( it != cArr->end() ){
  1869. tVal->uart_id = atoi( it->second.c_str() );
  1870. };
  1871. it = cArr->find("device_id");
  1872. if( it != cArr->end() ){
  1873. tVal->device_id = atoi( it->second.c_str() );
  1874. };
  1875. it = cArr->find("ip");
  1876. if( it != cArr->end() ){
  1877. sprintf( tVal->master_host, it->second.c_str() );
  1878. };
  1879. it = cArr->find("aux_ip");
  1880. if( it != cArr->end() ){
  1881. sprintf( tVal->slave_host, it->second.c_str() );
  1882. };
  1883. it = cArr->find("port");
  1884. if( it != cArr->end() ){
  1885. tVal->master_port = atoi( it->second.c_str() );
  1886. };
  1887. it = cArr->find("aux_port");
  1888. if( it != cArr->end() ){
  1889. tVal->slave_port = atoi( it->second.c_str() );
  1890. };
  1891. it = cArr->find("auto_draw");
  1892. if( it != cArr->end() ){
  1893. tVal->auto_draw = atoi( it->second.c_str() );
  1894. };
  1895. it = cArr->find("filter_id");
  1896. if( it != cArr->end() ){
  1897. tVal->filter_id = atoi( it->second.c_str() );
  1898. };
  1899. it = cArr->find("enabled_lrf");
  1900. if( it != cArr->end() ){
  1901. tVal->enabled_lrf = atoi( it->second.c_str() );
  1902. };
  1903. it = cArr->find("max_range");
  1904. if( it != cArr->end() ){
  1905. tVal->max_range = atof( it->second.c_str() );
  1906. };
  1907. it = cArr->find("base_shift");
  1908. if( it != cArr->end() ){
  1909. tVal->base_shift = atoi( it->second.c_str() );
  1910. };
  1911. it = cArr->find("mirror_shift");
  1912. if( it != cArr->end() ){
  1913. tVal->mirror_shift = atoi( it->second.c_str() );
  1914. };
  1915. it = cArr->find("detection_profile_id");
  1916. if( it != cArr->end() ){
  1917. tVal->detection_profile_id = atoi( it->second.c_str() );
  1918. };
  1919. it = cArr->find("draw_lrf_id");
  1920. if( it != cArr->end() ){
  1921. tVal->draw_lrf_id = atoi( it->second.c_str() );
  1922. };
  1923. it = cArr->find("width_table_id");
  1924. if( it != cArr->end() ){
  1925. tVal->width_table_id = atoi( it->second.c_str() );
  1926. };
  1927. it = cArr->find("zoom_table_id");
  1928. if( it != cArr->end() ){
  1929. tVal->zoom_table_id = atoi( it->second.c_str() );
  1930. };
  1931. it = cArr->find("embd_cam");
  1932. if( it != cArr->end() ){
  1933. tVal->embd_cam = atoi( it->second.c_str() );
  1934. };
  1935. it = cArr->find("embd_cam_auto");
  1936. if( it != cArr->end() ){
  1937. tVal->embd_cam_auto = (bool)atoi( it->second.c_str() );
  1938. };
  1939. it = cArr->find("embd_dc");
  1940. if( it != cArr->end() ){
  1941. tVal->embd_dc = atoi( it->second.c_str() );
  1942. };
  1943. it = cArr->find("autoreset");
  1944. if( it != cArr->end() ){
  1945. tVal->autoreset = (bool)atoi( it->second.c_str() );
  1946. };
  1947. it = cArr->find("force_settings");
  1948. if( it != cArr->end() ){
  1949. tVal->force_settings = (bool)atoi( it->second.c_str() );
  1950. };
  1951. };
  1952. };
  1953. };
  1954. deleteResult( result );
  1955. };
  1956. return tVal;
  1957. };
  1958. PLGS_2D_PARAMETERS sqliteDBC::getLRFDetectionProfile( uint32_t lrfID, uint8_t profileID, bool master ){
  1959. PLGS_2D_PARAMETERS retVal = NULL;
  1960. if( m_bStoIsConnected ){
  1961. char tQBuf[CTEXT];
  1962. AAssoc *result;
  1963. sprintf(tQBuf, "SELECT * FROM `lrf_detection_profiles` WHERE `aux_id`=%d AND `profile_id`=%d AND `master`=%d", lrfID, profileID,((master)?1:0) );
  1964. #ifdef DEBUG
  1965. LOG(tQBuf);
  1966. #endif
  1967. result = m_pDataStorage->Query( tQBuf );
  1968. if( result!= NULL ) {
  1969. if( result->size() > 0 ) {
  1970. retVal = (PLGS_2D_PARAMETERS) malloc(sizeof(LGS_2D_PARAMETERS));
  1971. memset( retVal, 0, sizeof(LGS_2D_PARAMETERS));
  1972. Assoc *cArr;
  1973. Assoc::iterator it;
  1974. cArr = result->at(0);
  1975. it = cArr->find("order");
  1976. if( it != cArr->end() ){
  1977. retVal->nOrder = atoi(it->second.c_str());
  1978. };
  1979. it = cArr->find("offset");
  1980. if( it != cArr->end() ){
  1981. retVal->nOffset = atoi(it->second.c_str());
  1982. };
  1983. it = cArr->find("delta");
  1984. if( it != cArr->end() ){
  1985. retVal->nDelta = atoi(it->second.c_str());
  1986. };
  1987. it = cArr->find("flip");
  1988. if( it != cArr->end() ){
  1989. retVal->nFlip = atoi(it->second.c_str());
  1990. };
  1991. it = cArr->find("width");
  1992. if( it != cArr->end() ){
  1993. retVal->nWidth = atoi(it->second.c_str());
  1994. };
  1995. it = cArr->find("sst");
  1996. if( it != cArr->end() ){
  1997. retVal->bSST = atoi(it->second.c_str());
  1998. };
  1999. it = cArr->find("sun");
  2000. if( it != cArr->end() ){
  2001. retVal->nSun = atoi(it->second.c_str());
  2002. };
  2003. };
  2004. };
  2005. deleteResult( result );
  2006. };
  2007. return retVal;
  2008. };
  2009. RELAY_PARAMETERS *sqliteDBC::getLRFDryContact( uint32_t lrfID ){
  2010. RELAY_PARAMETERS *retVal = NULL;
  2011. if( m_bStoIsConnected ){
  2012. char tQBuf[CTEXT];
  2013. AAssoc *result;
  2014. sprintf(tQBuf, "SELECT `dc_auto`, `dc_no`, `dc_window` FROM `lrf_sensors` WHERE `id`=%d", lrfID );
  2015. #ifdef DEBUG
  2016. LOG(tQBuf);
  2017. #endif
  2018. result = m_pDataStorage->Query( tQBuf );
  2019. if( result!= NULL ) {
  2020. if( result->size() > 0 ) {
  2021. retVal = (RELAY_PARAMETERS*) malloc(sizeof(RELAY_PARAMETERS));
  2022. if( retVal ) {
  2023. Assoc *cArr;
  2024. Assoc::iterator it;
  2025. cArr = result->at(0);
  2026. it = cArr->find("dc_auto");
  2027. if( it != cArr->end() ){
  2028. retVal->bAuto = atoi( it->second.c_str() );
  2029. };
  2030. it = cArr->find("dc_no");
  2031. if( it != cArr->end() ){
  2032. retVal->bNormallyClose = ( atoi( it->second.c_str() ) == 0 );
  2033. };
  2034. it = cArr->find("dc_window");
  2035. if( it != cArr->end() ){
  2036. retVal->nAutoDelay = atoi( it->second.c_str() );
  2037. };
  2038. };
  2039. };
  2040. };
  2041. deleteResult( result );
  2042. };
  2043. return retVal;
  2044. };
  2045. uint8_t sqliteDBC::getLRFDataStatus( uint32_t ID ){
  2046. uint8_t RetVal = 0;
  2047. if( m_bStoIsConnected ){
  2048. char tQBuf[CTEXT];
  2049. AAssoc *result;
  2050. sprintf(tQBuf, "SELECT `data_status` FROM `lrf_sensors` WHERE `id`=%d", ID );
  2051. #ifdef DEBUG
  2052. LOG(tQBuf);
  2053. #endif
  2054. result = m_pDataStorage->Query( tQBuf );
  2055. if( result!= NULL ) {
  2056. if( result->size() > 0 ) {
  2057. Assoc *cArr;
  2058. Assoc::iterator it;
  2059. cArr = result->at(0);
  2060. it = cArr->find("data_status");
  2061. if( it != cArr->end() ){
  2062. RetVal = atoi( it->second.c_str() );
  2063. };
  2064. };
  2065. };
  2066. deleteResult( result );
  2067. };
  2068. return RetVal;
  2069. };
  2070. uint32_t sqliteDBC::getLRFMaskID( uint32_t deviceID, uint8_t lrfID, uint8_t layer ){
  2071. uint32_t RetVal = 0;
  2072. if( m_bStoIsConnected ){
  2073. char tQBuf[CTEXT];
  2074. AAssoc *result;
  2075. sprintf(tQBuf, "SELECT `id` FROM `lrf_masks` WHERE `sensor_id`=%d AND `lrf_id`=%d AND `layer`=%d ", deviceID, lrfID, layer );
  2076. #ifdef DEBUG
  2077. LOG(tQBuf);
  2078. #endif
  2079. result = m_pDataStorage->Query( tQBuf );
  2080. if( result!= NULL ) {
  2081. if( result->size() > 0 ) {
  2082. Assoc *cArr;
  2083. Assoc::iterator it;
  2084. cArr = result->at(0);
  2085. it = cArr->find("id");
  2086. if( it != cArr->end() ){
  2087. RetVal = atoi( it->second.c_str() );
  2088. };
  2089. };
  2090. };
  2091. deleteResult( result );
  2092. };
  2093. return RetVal;
  2094. };
  2095. uint32_t sqliteDBC::getLRFMaskPolygoneID( uint32_t maskID, uint8_t vertecID ){
  2096. uint32_t RetVal = 0;
  2097. if( m_bStoIsConnected ){
  2098. char tQBuf[CTEXT];
  2099. AAssoc *result;
  2100. sprintf(tQBuf, "SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d AND `vertex_id`=%d", maskID, vertecID );
  2101. #ifdef DEBUG
  2102. LOG(tQBuf);
  2103. #endif
  2104. result = m_pDataStorage->Query( tQBuf );
  2105. if( result!= NULL ) {
  2106. if( result->size() > 0 ) {
  2107. Assoc *cArr;
  2108. Assoc::iterator it;
  2109. cArr = result->at(0);
  2110. it = cArr->find("id");
  2111. if( it != cArr->end() ){
  2112. RetVal = atoi( it->second.c_str() );
  2113. };
  2114. };
  2115. };
  2116. deleteResult( result );
  2117. };
  2118. return RetVal;
  2119. };
  2120. uint8_t sqliteDBC::getLRFMasksStatus( uint32_t deviceID ){
  2121. uint8_t RetVal = 0;
  2122. if( m_bStoIsConnected ){
  2123. char tQBuf[CTEXT];
  2124. AAssoc *result;
  2125. sprintf(tQBuf, "SELECT `layer` FROM `lrf_masks` WHERE `sensor_id`=%d AND `status`='E'", deviceID );
  2126. #ifdef DEBUG
  2127. LOG(tQBuf);
  2128. #endif
  2129. result = m_pDataStorage->Query( tQBuf );
  2130. if( result!= NULL ) {
  2131. if( result->size() > 0 ) {
  2132. Assoc *cArr;
  2133. Assoc::iterator it;
  2134. for(int i=0;i<result->size();i++) {
  2135. cArr = result->at(i);
  2136. it = cArr->find("layer");
  2137. if( it != cArr->end() ){
  2138. RetVal |= ( 1<< atoi( it->second.c_str() ));
  2139. };
  2140. };
  2141. };
  2142. };
  2143. deleteResult( result );
  2144. };
  2145. return RetVal;
  2146. };
  2147. DBC_LRF_UPLOAD_DATA *sqliteDBC::getLRFUploadData( uint32_t upID ){
  2148. DBC_LRF_UPLOAD_DATA *RetVal = NULL;
  2149. if( m_bStoIsConnected ){
  2150. char tQBuf[CTEXT];
  2151. AAssoc *result;
  2152. sprintf(tQBuf, "SELECT * FROM `lrf_uploads` WHERE `id`=%d", upID );
  2153. #ifdef DEBUG
  2154. LOG(tQBuf);
  2155. #endif
  2156. result = m_pDataStorage->Query( tQBuf );
  2157. if( result->size() > 0 ) {
  2158. Assoc *cArr;
  2159. RetVal = (DBC_LRF_UPLOAD_DATA*) malloc(sizeof(DBC_LRF_UPLOAD_DATA));
  2160. if( RetVal != NULL ) {
  2161. memset( RetVal, 0, sizeof(DBC_LRF_UPLOAD_DATA));
  2162. Assoc::iterator it;
  2163. RetVal->id = upID;
  2164. cArr = result->at(0);
  2165. it = cArr->find("md5");
  2166. if( it != cArr->end() ){
  2167. strncpy( RetVal->md5, it->second.c_str(), 32 );
  2168. };
  2169. it = cArr->find("file");
  2170. if( it != cArr->end() ){
  2171. strncpy( RetVal->fname, it->second.c_str(), 16 );
  2172. };
  2173. it = cArr->find("issue_reset");
  2174. if( it != cArr->end() ){
  2175. RetVal->transfer_mode = atoi( it->second.c_str() );
  2176. };
  2177. };
  2178. };
  2179. deleteResult( result );
  2180. };
  2181. return RetVal;
  2182. };
  2183. DBC_WIDTH_TABLE *sqliteDBC::getLRFWidthTable( uint32_t lrfID ){
  2184. DBC_WIDTH_TABLE *retVal = NULL;
  2185. if( m_bStoIsConnected ){
  2186. retVal = (DBC_WIDTH_TABLE *)malloc(sizeof(DBC_WIDTH_TABLE));
  2187. if ( retVal ) {
  2188. char tQBuf[CTEXT];
  2189. AAssoc *result;
  2190. sprintf(tQBuf, "SELECT `range_k`, `width_k` FROM `lrf_detection_width_tables` WHERE `aux_id`=%d ORDER BY `range_k`", lrfID );
  2191. #ifdef DEBUG_EXTRA
  2192. LOG(tQBuf);
  2193. #endif
  2194. memset( retVal, 0 , sizeof( DBC_WIDTH_TABLE ));
  2195. result = m_pDataStorage->Query( tQBuf );
  2196. if( result!= NULL ) {
  2197. if( result->size() > 0 ) {
  2198. retVal->count = result->size();
  2199. retVal->wt_records = (DBC_WT_RECORD*) calloc( retVal->count, sizeof(DBC_WT_RECORD));
  2200. if ( retVal->wt_records ) {
  2201. Assoc *cArr;
  2202. Assoc::iterator it;
  2203. for( int i = 0; i < retVal->count; i++ ) {
  2204. cArr = result->at(i);
  2205. it = cArr->find("range_k");
  2206. if( it != cArr->end() ){
  2207. retVal->wt_records[i].rangePercent = atoi( it->second.c_str() );
  2208. };
  2209. it = cArr->find("width_k");
  2210. if( it != cArr->end() ){
  2211. retVal->wt_records[i].widthPercent = atoi( it->second.c_str() );
  2212. };
  2213. };
  2214. } else {
  2215. //retVal->count = 0;
  2216. LOG_ERROR("No Memory To create WT Record!");
  2217. free( retVal );
  2218. retVal = NULL;
  2219. };
  2220. };
  2221. };
  2222. deleteResult( result );
  2223. } else {
  2224. LOG_ERROR("No Memory To create WT!");
  2225. };
  2226. };
  2227. return retVal;
  2228. };
  2229. uint32_t sqliteDBC::getNodeAuxParam( uint32_t NodeID ){
  2230. uint32_t retVal = 0;
  2231. if( m_bStoIsConnected ){
  2232. char tQBuf[CTEXT];
  2233. AAssoc *result;
  2234. sprintf(tQBuf, "SELECT `aux_parameter` FROM `nodes` WHERE `id`=%d", NodeID );
  2235. #ifdef DEBUG_EXTRA
  2236. LOG(tQBuf);
  2237. #endif
  2238. result = m_pDataStorage->Query( tQBuf );
  2239. if( result!= NULL ) {
  2240. if( result->size() > 0 ) {
  2241. Assoc *cArr;
  2242. Assoc::iterator it;
  2243. cArr = result->at(0);
  2244. it = cArr->find("aux_parameter");
  2245. if( it != cArr->end() ){
  2246. retVal = atoi( it->second.c_str() );
  2247. };
  2248. };
  2249. };
  2250. deleteResult( result );
  2251. };
  2252. return retVal;
  2253. };
  2254. DBC_NODE_DATA *sqliteDBC::getNodeData( uint32_t ID ){
  2255. DBC_NODE_DATA * tVal = NULL;
  2256. if( m_bStoIsConnected ){
  2257. char tQBuf[CTEXT];
  2258. AAssoc *result;
  2259. sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID );
  2260. #ifdef DEBUG_EXTRA
  2261. LOG(tQBuf);
  2262. #endif
  2263. result = m_pDataStorage->Query( tQBuf );
  2264. if( result!= NULL ) {
  2265. if( result->size() > 0 ) {
  2266. Assoc *cArr;
  2267. tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
  2268. if( tVal != NULL ) {
  2269. memset( tVal, 0, sizeof(DBC_NODE_DATA));
  2270. Assoc::iterator it;
  2271. tVal->node_id = ID;
  2272. cArr = result->at(0);
  2273. it = cArr->find("type");
  2274. if( it != cArr->end() ){
  2275. tVal->type = /*(NODE_TYPE)*/ atoi( it->second.c_str() );
  2276. };
  2277. it = cArr->find("aux_id");
  2278. if( it != cArr->end() ){
  2279. tVal->aux_id = atoi( it->second.c_str() );
  2280. };
  2281. it = cArr->find("aux_parameter");
  2282. if( it != cArr->end() ){
  2283. tVal->aux_parameter = atoi( it->second.c_str() );
  2284. };
  2285. it = cArr->find("online");
  2286. if( it != cArr->end() ){
  2287. tVal->online = (bool)atoi( it->second.c_str() );
  2288. };
  2289. it = cArr->find("enable");
  2290. if( it != cArr->end() ){
  2291. tVal->enabled = (bool)atoi( it->second.c_str() );
  2292. };
  2293. it = cArr->find("name");
  2294. if( it != cArr->end() ){
  2295. sprintf( tVal->name, it->second.c_str() );
  2296. };
  2297. };
  2298. };
  2299. };
  2300. deleteResult( result );
  2301. };
  2302. return tVal;
  2303. };
  2304. uint32_t sqliteDBC::getNodeIdByTypeAndAux( uint8_t type, uint32_t aux_id ){
  2305. uint32_t retVal = 0;
  2306. if( m_bStoIsConnected ){
  2307. char tQBuf[CTEXT];
  2308. AAssoc *result;
  2309. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type`=%d AND `aux_id`=%d", type, aux_id);
  2310. #ifdef DEBUG_EXTRA
  2311. LOG(tQBuf);
  2312. #endif
  2313. result = m_pDataStorage->Query( tQBuf );
  2314. if( result!= NULL ) {
  2315. if( result->size() > 0 ) {
  2316. Assoc *cArr;
  2317. Assoc::iterator it;
  2318. cArr = result->at(0);
  2319. it = cArr->find("id");
  2320. if( it != cArr->end() ){
  2321. retVal = atoi( it->second.c_str() );
  2322. };
  2323. };
  2324. };
  2325. deleteResult( result );
  2326. };
  2327. return retVal;
  2328. };
  2329. CSTR sqliteDBC::getNodeName( uint32_t NodeID ){
  2330. CSTR retVal = NULL;
  2331. if( m_bStoIsConnected ){
  2332. char tQBuf[CTEXT];
  2333. AAssoc *result;
  2334. sprintf(tQBuf, "SELECT `name` FROM `nodes` WHERE `id`=%d", NodeID );
  2335. #ifdef DEBUG_EXTRA
  2336. LOG(tQBuf);
  2337. #endif
  2338. result = m_pDataStorage->Query( tQBuf );
  2339. if( result!= NULL ) {
  2340. if( result->size() > 0 ) {
  2341. Assoc *cArr;
  2342. Assoc::iterator it;
  2343. cArr = result->at(0);
  2344. it = cArr->find("name");
  2345. if( it != cArr->end() ){
  2346. retVal = strdup( it->second.c_str() );
  2347. };
  2348. };
  2349. };
  2350. deleteResult( result );
  2351. };
  2352. return retVal;
  2353. };
  2354. DBC_NET_IF_DATA *sqliteDBC::getNIF_Data( uint32_t nif_id ){
  2355. DBC_NET_IF_DATA * tVal = NULL;
  2356. if( m_bStoIsConnected ){
  2357. char tQBuf[CTEXT];
  2358. AAssoc *result;
  2359. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id );
  2360. #ifdef DEBUG_EXTRA
  2361. LOG(tQBuf);
  2362. #endif
  2363. result = m_pDataStorage->Query( tQBuf );
  2364. if( result!= NULL ) {
  2365. if( result->size() > 0 ) {
  2366. Assoc *cArr;
  2367. tVal = (DBC_NET_IF_DATA*) malloc(sizeof(DBC_NET_IF_DATA));
  2368. if( tVal != NULL ) {
  2369. Assoc::iterator it;
  2370. tVal->id = nif_id;
  2371. cArr = result->at(0);
  2372. it = cArr->find("type");
  2373. if( it != cArr->end() ){
  2374. tVal->type = atoi(it->second.c_str());
  2375. };
  2376. it = cArr->find("mode");
  2377. if( it != cArr->end() ){
  2378. tVal->mode = atoi(it->second.c_str());
  2379. };
  2380. it = cArr->find("device_name");
  2381. if( it != cArr->end() ){
  2382. strncpy( tVal->device_name, it->second.c_str(),11);
  2383. tVal->device_name[10]=(char)0;
  2384. };
  2385. it = cArr->find("mac");
  2386. if( it != cArr->end() ){
  2387. strncpy( tVal->mac, it->second.c_str(), 33 );
  2388. tVal->mac[32]=(char)0;
  2389. };
  2390. it = cArr->find("aux_id");
  2391. if( it != cArr->end() ){
  2392. tVal->aux_id = atoi(it->second.c_str());
  2393. };
  2394. };
  2395. };
  2396. };
  2397. deleteResult( result );
  2398. };
  2399. return tVal;
  2400. };
  2401. uint32_t sqliteDBC::getNIF_ID( CSTR key ){
  2402. uint32_t tVal = 0;
  2403. if( m_bStoIsConnected ){
  2404. char tQBuf[CTEXT];
  2405. AAssoc *result;
  2406. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key );
  2407. #ifdef DEBUG_ALL
  2408. LOG(tQBuf);
  2409. #endif
  2410. result = m_pDataStorage->Query( tQBuf );
  2411. if( result!= NULL ) {
  2412. if( result->size() > 0 ) {
  2413. Assoc *cArr;
  2414. Assoc::iterator it;
  2415. cArr = result->at(0);
  2416. it = cArr->find("id");
  2417. if( it != cArr->end() ){
  2418. tVal = atoi( it->second.c_str() );
  2419. };
  2420. };
  2421. };
  2422. deleteResult( result );
  2423. };
  2424. return tVal;
  2425. };
  2426. uint32_t sqliteDBC::getNIF_IDbyDevice( CSTR device ){
  2427. uint32_t tVal = 0;
  2428. if( m_bStoIsConnected ){
  2429. char tQBuf[CTEXT];
  2430. AAssoc *result;
  2431. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device );
  2432. #ifdef DEBUG_ALL
  2433. LOG(tQBuf);
  2434. #endif
  2435. result = m_pDataStorage->Query( tQBuf );
  2436. if( result!= NULL ) {
  2437. if( result->size() > 0 ) {
  2438. Assoc *cArr;
  2439. Assoc::iterator it;
  2440. cArr = result->at(0);
  2441. it = cArr->find("id");
  2442. if( it != cArr->end() ){
  2443. tVal = atoi( it->second.c_str() );
  2444. };
  2445. };
  2446. };
  2447. deleteResult( result );
  2448. };
  2449. return tVal;
  2450. };
  2451. uint32_t sqliteDBC::getNetworkInfrastructureID( uint32_t nif_id ){
  2452. uint32_t tVal = 0;
  2453. if( m_bStoIsConnected ){
  2454. char tQBuf[CTEXT];
  2455. AAssoc *result;
  2456. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id );
  2457. #ifdef DEBUG_ALL
  2458. LOG(tQBuf);
  2459. #endif
  2460. result = m_pDataStorage->Query( tQBuf );
  2461. if( result!= NULL ) {
  2462. if( result->size() > 0 ) {
  2463. Assoc *cArr;
  2464. Assoc::iterator it;
  2465. cArr = result->at(0);
  2466. it = cArr->find("id");
  2467. if( it != cArr->end() ){
  2468. tVal = atoi( it->second.c_str() );
  2469. };
  2470. };
  2471. };
  2472. deleteResult( result );
  2473. };
  2474. return tVal;
  2475. };
  2476. REPORT_DIRINFO *sqliteDBC::getReportDirInfo( uint32_t id ){
  2477. REPORT_DIRINFO *tVal = NULL;
  2478. if( m_bStaIsCreated ){
  2479. char tQBuf[CTEXT];
  2480. AAssoc *result;
  2481. sprintf(tQBuf, "SELECT * FROM `days` WHERE `ROWID`=%d", id );
  2482. #ifdef DEBUG_ALL
  2483. LOG(tQBuf);
  2484. #endif
  2485. result = m_pDataStorage->Query( tQBuf );
  2486. if( result!= NULL ) {
  2487. if( result->size() > 0 ) {
  2488. tVal = (REPORT_DIRINFO *) malloc( sizeof(REPORT_DIRINFO));
  2489. if ( tVal ) {
  2490. Assoc *cArr;
  2491. Assoc::iterator it;
  2492. memset( tVal, 0 , sizeof( REPORT_DIRINFO));
  2493. cArr = result->at(0);
  2494. it = cArr->find("year");
  2495. if( it != cArr->end() ){
  2496. strncpy( tVal->YEAR, it->second.c_str(), 5 );
  2497. };
  2498. it = cArr->find("month");
  2499. if( it != cArr->end() ){
  2500. strncpy( tVal->MONTH, it->second.c_str(), 3 );
  2501. };
  2502. it = cArr->find("day");
  2503. if( it != cArr->end() ){
  2504. strncpy( tVal->DAY, it->second.c_str(), 3 );
  2505. };
  2506. };
  2507. };
  2508. };
  2509. deleteResult( result );
  2510. };
  2511. return tVal;
  2512. };
  2513. DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){
  2514. DBC_RESOURCE_DATA *tVal = NULL;
  2515. if( m_bStoIsConnected ){
  2516. char tQBuf[CTEXT];
  2517. AAssoc *result;
  2518. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID );
  2519. #ifdef DEBUG_ALL
  2520. LOG(tQBuf);
  2521. #endif
  2522. result = m_pDataStorage->Query( tQBuf );
  2523. if( result!= NULL ) {
  2524. if( result->size() > 0 ) {
  2525. Assoc *cArr;
  2526. tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA));
  2527. if( tVal != NULL ) {
  2528. Assoc::iterator it;
  2529. cArr = result->at(0);
  2530. tVal->id = ResourceID;
  2531. it = cArr->find("node_id");
  2532. if( it != cArr->end() ){
  2533. tVal->node_id = atoi( it->second.c_str() );
  2534. };
  2535. it = cArr->find("type");
  2536. if( it != cArr->end() ){
  2537. tVal->type = atoi( it->second.c_str() );
  2538. };
  2539. it = cArr->find("aux_id");
  2540. if( it != cArr->end() ){
  2541. tVal->aux_id = atoi( it->second.c_str() );
  2542. };
  2543. it = cArr->find("device");
  2544. if( it != cArr->end() ){
  2545. strncpy( tVal->device, it->second.c_str(), 64 );
  2546. tVal->device[63] = (char)0;
  2547. };
  2548. };
  2549. };
  2550. };
  2551. deleteResult( result );
  2552. };
  2553. return tVal;
  2554. };
  2555. uint32_t sqliteDBC::getResourceID( uint32_t NodeID, uint8_t type, uint32_t auxID ){
  2556. uint32_t RetVal = 0;
  2557. if( m_bStoIsConnected ){
  2558. char tQBuf[CTEXT];
  2559. sprintf(tQBuf,"SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID );
  2560. #ifdef DEBUG_ALL
  2561. LOG(tQBuf);
  2562. #endif
  2563. AAssoc *result;
  2564. result = m_pDataStorage->Query( tQBuf );
  2565. if( result!= NULL ) {
  2566. if( result->size() > 0 ) {
  2567. Assoc * cArr;
  2568. Assoc::iterator it;
  2569. cArr = result->at(0);
  2570. it = cArr->find("id");
  2571. if( it != cArr->end() ){
  2572. RetVal = atoi( it->second.c_str() );
  2573. };
  2574. };
  2575. };
  2576. deleteResult( result );
  2577. };
  2578. return RetVal;
  2579. };
  2580. DBC_SITE_DATA *sqliteDBC::getSiteData( uint32_t ID ){
  2581. DBC_SITE_DATA * tVal = NULL;
  2582. if( m_bStoIsConnected ){
  2583. char tQBuf[CTEXT];
  2584. AAssoc *result;
  2585. sprintf(tQBuf, "SELECT * FROM `system_props` WHERE `id`=%d", ID);
  2586. #ifdef DEBUG_ALL
  2587. LOG(tQBuf);
  2588. #endif
  2589. result = m_pDataStorage->Query( tQBuf );
  2590. if( result!= NULL ) {
  2591. if( result->size() > 0 ) {
  2592. Assoc *cArr;
  2593. tVal = (DBC_SITE_DATA*) malloc(sizeof(DBC_SITE_DATA));
  2594. if( tVal != NULL ) {
  2595. memset( tVal, 0 , sizeof(DBC_SITE_DATA));
  2596. Assoc::iterator it;
  2597. cArr = result->at(0);
  2598. it = cArr->find("type");
  2599. if( it != cArr->end() ){
  2600. tVal->account_id = atoi( it->second.c_str() );
  2601. };
  2602. it = cArr->find("name");
  2603. if( it != cArr->end() ){
  2604. sprintf( tVal->name, it->second.c_str() );
  2605. };
  2606. //it = cArr->find("server_ident");
  2607. //if( it != cArr->end() ){
  2608. // sprintf( tVal->ident, it->second.c_str() );
  2609. //};
  2610. it = cArr->find("anchor_lat");
  2611. if( it != cArr->end() ){
  2612. tVal->lat = atof( it->second.c_str() );
  2613. };
  2614. it = cArr->find("anchor_lon");
  2615. if( it != cArr->end() ){
  2616. tVal->lon = atof( it->second.c_str() );
  2617. };
  2618. it = cArr->find("anchor_alt");
  2619. if( it != cArr->end() ){
  2620. tVal->alt = atof( it->second.c_str() );
  2621. };
  2622. it = cArr->find("hemisphere");
  2623. if( it != cArr->end() ){
  2624. tVal->SH = ( it->second.c_str()[0] == 'S' );
  2625. };
  2626. it = cArr->find("map_id");
  2627. if( it != cArr->end() ){
  2628. tVal->map_id = atoi( it->second.c_str() );
  2629. };
  2630. it = cArr->find("local_binding");
  2631. if( it != cArr->end() ){
  2632. tVal->local_binding = (bool) atoi(it->second.c_str());
  2633. };
  2634. it = cArr->find("work_mode");
  2635. if( it != cArr->end() ){
  2636. tVal->work_mode = atoi(it->second.c_str());
  2637. };
  2638. it = cArr->find("report_cleanup");
  2639. if( it != cArr->end() ){
  2640. tVal->report_cleanup = atoi(it->second.c_str());
  2641. };
  2642. it = cArr->find("preserve_empty");
  2643. if( it != cArr->end() ){
  2644. tVal->preserve_empty = atoi(it->second.c_str());
  2645. };
  2646. };
  2647. };
  2648. };
  2649. deleteResult( result );
  2650. };
  2651. return tVal;
  2652. };
  2653. uint32_t sqliteDBC::getServerID( CSTR identity ){
  2654. uint32_t RetVal = 0;
  2655. if( m_bStoIsConnected ){
  2656. char tQBuf[CTEXT];
  2657. sprintf(tQBuf,"SELECT `id` FROM `servers` WHERE `identity`='%s'", identity );
  2658. #ifdef DEBUG_ALL
  2659. LOG(tQBuf);
  2660. #endif
  2661. AAssoc *result;
  2662. result = m_pDataStorage->Query( tQBuf );
  2663. if( result!= NULL ) {
  2664. if( result->size() > 0 ) {
  2665. Assoc * cArr;
  2666. Assoc::iterator it;
  2667. cArr = result->at(0);
  2668. it = cArr->find("id");
  2669. if( it != cArr->end() ){
  2670. RetVal = atoi( it->second.c_str() );
  2671. };
  2672. };
  2673. };
  2674. deleteResult( result );
  2675. };
  2676. return RetVal;
  2677. };
  2678. uint32_t sqliteDBC::getServerNode( uint32_t SID ){
  2679. uint32_t RetVal = 0;
  2680. if( m_bStoIsConnected ){
  2681. char tQBuf[CTEXT];
  2682. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  2683. #ifdef DEBUG_ALL
  2684. LOG(tQBuf);
  2685. #endif
  2686. AAssoc *result;
  2687. result = m_pDataStorage->Query( tQBuf );
  2688. if( result!= NULL ) {
  2689. if( result->size() > 0 ) {
  2690. Assoc * cArr;
  2691. Assoc::iterator it;
  2692. cArr = result->at(0);
  2693. it = cArr->find("id");
  2694. if( it != cArr->end() ){
  2695. RetVal = atoi( it->second.c_str() );
  2696. };
  2697. };
  2698. };
  2699. deleteResult( result );
  2700. };
  2701. return RetVal;
  2702. }
  2703. DBC_TARGET_FLITER_DATA *sqliteDBC::getTargetFilterData( uint32_t ID ){
  2704. DBC_TARGET_FLITER_DATA * tVal = NULL;
  2705. if( m_bStoIsConnected ){
  2706. char tQBuf[CTEXT];
  2707. AAssoc *result;
  2708. sprintf(tQBuf, "SELECT * FROM `target_filters` WHERE `id`=%d", ID );
  2709. #ifdef DEBUG
  2710. LOG(tQBuf);
  2711. #endif
  2712. result = m_pDataStorage->Query( tQBuf );
  2713. if( result!= NULL ) {
  2714. if( result->size() > 0 ) {
  2715. Assoc *cArr;
  2716. tVal = (DBC_TARGET_FLITER_DATA*) malloc(sizeof(DBC_TARGET_FLITER_DATA));
  2717. if( tVal != NULL ) {
  2718. memset( tVal, 0 , sizeof(DBC_TARGET_FLITER_DATA));
  2719. Assoc::iterator it;
  2720. tVal->id = ID;
  2721. cArr = result->at(0);
  2722. it = cArr->find("type");
  2723. if( it != cArr->end() ){
  2724. tVal->type = (TARGET_FILTER_TYPE) atoi( it->second.c_str() );
  2725. };
  2726. it = cArr->find("aux_id");
  2727. if( it != cArr->end() ){
  2728. tVal->aux_id = atoi( it->second.c_str() );
  2729. };
  2730. it = cArr->find("enabled");
  2731. if( it != cArr->end() ){
  2732. tVal->enabled = (bool) atoi( it->second.c_str() );
  2733. };
  2734. };
  2735. };
  2736. };
  2737. deleteResult( result );
  2738. };
  2739. return tVal;
  2740. };
  2741. TRAIN_FILTER_DATA *sqliteDBC::getTrainFilterData( uint32_t ID ){
  2742. TRAIN_FILTER_DATA * tVal = NULL;
  2743. if( m_bStoIsConnected ){
  2744. char tQBuf[CTEXT];
  2745. AAssoc *result;
  2746. sprintf(tQBuf, "SELECT * FROM `target_filter_train` WHERE `id`=%d", ID );
  2747. #ifdef DEBUG_EXTRA
  2748. LOG(tQBuf);
  2749. #endif
  2750. result = m_pDataStorage->Query( tQBuf );
  2751. if( result!= NULL ) {
  2752. if( result->size() > 0 ) {
  2753. Assoc *cArr;
  2754. tVal = (TRAIN_FILTER_DATA*) malloc(sizeof(TRAIN_FILTER_DATA));
  2755. if( tVal != NULL ) {
  2756. memset( tVal, 0 , sizeof(TRAIN_FILTER_DATA));
  2757. Assoc::iterator it;
  2758. cArr = result->at(0);
  2759. it = cArr->find("UPPER_SENSOR");
  2760. if( it != cArr->end() ){
  2761. tVal->nUpperSensorID = atoi( it->second.c_str() );
  2762. };
  2763. it = cArr->find("LOWER_SENSOR");
  2764. if( it != cArr->end() ){
  2765. tVal->nBottomSensorID = atoi( it->second.c_str() );
  2766. };
  2767. it = cArr->find("TRAIN_DETECT");
  2768. if( it != cArr->end() ){
  2769. tVal->bDetectTrain = (bool)atoi( it->second.c_str() );
  2770. };
  2771. it = cArr->find("TRAIN_WIDTH");
  2772. if( it != cArr->end() ){
  2773. tVal->nTrainWidth = atoi( it->second.c_str() );
  2774. };
  2775. it = cArr->find("DECISION_TIME");
  2776. if( it != cArr->end() ){
  2777. tVal->nDecisionTime = 1000 * atoi( it->second.c_str() );
  2778. };
  2779. it = cArr->find("SUSPECT_TIME");
  2780. if( it != cArr->end() ){
  2781. tVal->nSuspectTime = 1000 * atoi( it->second.c_str() );
  2782. };
  2783. it = cArr->find("TRACK_TRAIN");
  2784. if( it != cArr->end() ){
  2785. tVal->bTrackTrain = (bool)atoi( it->second.c_str() );
  2786. };
  2787. it = cArr->find("AUX_IP");
  2788. if( it != cArr->end() ){
  2789. strncpy( tVal->aux_ip, it->second.c_str(), 15 );
  2790. };
  2791. //it = cArr->find("AUX_PORT");
  2792. //if( it != cArr->end() ){
  2793. // tVal->aux_port = atoi( it->second.c_str() );
  2794. //};
  2795. it = cArr->find("DEBUG");
  2796. if( it != cArr->end() ){
  2797. tVal->bDebugMode = (bool)atoi( it->second.c_str() );
  2798. };
  2799. };
  2800. };
  2801. };
  2802. deleteResult( result );
  2803. };
  2804. return tVal;
  2805. };
  2806. DBC_UART_DATA *sqliteDBC::getUartData( uint32_t UartID ){
  2807. DBC_UART_DATA *tVal = NULL;
  2808. if( m_bStoIsConnected ) {
  2809. char tQBuf[MAX_PATH_LEN];
  2810. AAssoc *result;
  2811. sprintf( tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID );
  2812. #ifdef DEBUG_ALL
  2813. LOG(tQBuf);
  2814. #endif
  2815. result = m_pDataStorage->Query( tQBuf );
  2816. if( result!= NULL ) {
  2817. if( result->size() > 0 ) {
  2818. Assoc *cArr;
  2819. tVal = (DBC_UART_DATA*) malloc(sizeof(DBC_UART_DATA));
  2820. if( tVal != NULL ) {
  2821. Assoc::iterator it;
  2822. cArr = result->at(0);
  2823. it = cArr->find("type");
  2824. if( it != cArr->end() ){
  2825. tVal->type = atoi( it->second.c_str() );
  2826. };
  2827. it = cArr->find("speed");
  2828. if( it != cArr->end() ){
  2829. tVal->speed = atoi( it->second.c_str() );
  2830. };
  2831. it = cArr->find("mode");
  2832. if( it != cArr->end() ){
  2833. tVal->mode = atoi( it->second.c_str() );
  2834. };
  2835. it = cArr->find("port");
  2836. if( it != cArr->end() ){
  2837. tVal->port = atoi( it->second.c_str() );
  2838. };
  2839. it = cArr->find("path");
  2840. if( it != cArr->end() ){
  2841. strncpy( tVal->path, it->second.c_str(), 128 );
  2842. };
  2843. };
  2844. };
  2845. };
  2846. deleteResult( result );
  2847. };
  2848. return tVal;
  2849. };
  2850. uint32_t sqliteDBC::getUartID( uint32_t NodeID, uint8_t Type, CSTR path ){
  2851. uint32_t RetVal = 0;
  2852. if( m_bStoIsConnected ){
  2853. char tQBuf[CTEXT];
  2854. sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path );
  2855. #ifdef DEBUG_ALL
  2856. LOG(tQBuf);
  2857. #endif
  2858. AAssoc *result;
  2859. result = m_pDataStorage->Query( tQBuf );
  2860. if( result!= NULL ) {
  2861. if( result->size() > 0 ) {
  2862. Assoc * cArr;
  2863. Assoc::iterator it;
  2864. cArr = result->at(0);
  2865. it = cArr->find("id");
  2866. if( it != cArr->end() ){
  2867. RetVal = atoi( it->second.c_str() );
  2868. };
  2869. };
  2870. };
  2871. deleteResult( result );
  2872. };
  2873. return RetVal;
  2874. };
  2875. uint32_t sqliteDBC::getWiFi_ID( uint32_t nif_id ){
  2876. uint32_t tVal = 0;
  2877. if( m_bStoIsConnected ){
  2878. char tQBuf[CTEXT];
  2879. AAssoc *result;
  2880. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id );
  2881. #ifdef DEBUG_ALL
  2882. LOG(tQBuf);
  2883. #endif
  2884. result = m_pDataStorage->Query( tQBuf );
  2885. if( result!= NULL ) {
  2886. if( result->size() > 0 ) {
  2887. Assoc *cArr;
  2888. Assoc::iterator it;
  2889. cArr = result->at(0);
  2890. it = cArr->find("id");
  2891. if( it != cArr->end() ){
  2892. tVal = atoi( it->second.c_str() );
  2893. };
  2894. };
  2895. };
  2896. deleteResult( result );
  2897. };
  2898. return tVal;
  2899. };
  2900. DBC_INT_LIST *sqliteDBC::listFilters( uint32_t ServerID ){
  2901. DBC_INT_LIST * tVal = NULL;
  2902. if( m_bStoIsConnected ){
  2903. char tQBuf[CTEXT];
  2904. AAssoc *result;
  2905. sprintf(tQBuf, "SELECT `id` FROM `target_filters` WHERE `enabled`=1" );
  2906. #ifdef DEBUG
  2907. LOG(tQBuf);
  2908. #endif
  2909. result = m_pDataStorage->Query( tQBuf );
  2910. if( result!= NULL ) {
  2911. if( result->size() > 0 ) {
  2912. Assoc *cArr;
  2913. tVal = new DBC_INT_LIST();
  2914. if( tVal != NULL ) {
  2915. Assoc::iterator it;
  2916. for(int i=0;i<result->size();i++) {
  2917. cArr = result->at(i);
  2918. it = cArr->find("id");
  2919. if( it != cArr->end() ){
  2920. tVal->push_back( atoi( it->second.c_str() ));
  2921. };
  2922. };
  2923. };
  2924. };
  2925. };
  2926. deleteResult( result );
  2927. };
  2928. return tVal;
  2929. };
  2930. DBC_INT_LIST *sqliteDBC::listLRFUploads( uint32_t deviceID ){
  2931. DBC_INT_LIST * tVal = NULL;
  2932. if( m_bStoIsConnected ){
  2933. char tQBuf[CTEXT];
  2934. AAssoc *result;
  2935. sprintf(tQBuf, "SELECT `id` FROM `lrf_uploads` WHERE `status`=-1 AND `sensor_id`=%d", deviceID );
  2936. #ifdef DEBUG
  2937. LOG(tQBuf);
  2938. #endif
  2939. result = m_pDataStorage->Query( tQBuf );
  2940. if( result!= NULL ) {
  2941. if( result->size() > 0 ) {
  2942. Assoc *cArr;
  2943. tVal = new DBC_INT_LIST();
  2944. if( tVal != NULL ) {
  2945. Assoc::iterator it;
  2946. for(int i=0;i<result->size();i++) {
  2947. cArr = result->at(i);
  2948. it = cArr->find("id");
  2949. if( it != cArr->end() ){
  2950. tVal->push_back( atoi( it->second.c_str() ));
  2951. };
  2952. };
  2953. };
  2954. };
  2955. };
  2956. deleteResult( result );
  2957. };
  2958. return tVal;
  2959. };
  2960. DBC_INT_LIST *sqliteDBC::listMediaStreams( uint32_t SiteId, uint8_t listMode ){
  2961. DBC_INT_LIST * tVal = NULL;
  2962. if( m_bStoIsConnected ){
  2963. char tQBuf[CTEXT];
  2964. AAssoc *result;
  2965. switch( listMode ){
  2966. case LIST_VALID_RECORDS:
  2967. 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" );
  2968. break;
  2969. case LIST_INVALID_RECORDS:
  2970. 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" );
  2971. break;
  2972. default:
  2973. 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" );
  2974. };
  2975. #ifdef DEBUG_ALL
  2976. LOG(tQBuf);
  2977. #endif
  2978. result = m_pDataStorage->Query( tQBuf );
  2979. if( result!= NULL ) {
  2980. if( result->size() > 0 ) {
  2981. Assoc *cArr;
  2982. tVal = new DBC_INT_LIST();
  2983. if( tVal != NULL ) {
  2984. Assoc::iterator it;
  2985. for(int i=0;i<result->size();i++) {
  2986. cArr = result->at(i);
  2987. it = cArr->find("id");
  2988. if( it != cArr->end() ){
  2989. tVal->push_back( atoi( it->second.c_str() ));
  2990. };
  2991. };
  2992. };
  2993. };
  2994. };
  2995. deleteResult( result );
  2996. };
  2997. return tVal;
  2998. };
  2999. DBC_INT_LIST *sqliteDBC::listModules(){
  3000. DBC_INT_LIST * tVal = NULL;
  3001. if( m_bStoIsConnected ){
  3002. AAssoc *result;
  3003. result = m_pDataStorage->Query( "SELECT `id` FROM `modules` WHERE `enabled`=1" );
  3004. if( result!= NULL ) {
  3005. if( result->size() > 0 ) {
  3006. Assoc *cArr;
  3007. tVal = new DBC_INT_LIST();
  3008. if( tVal != NULL ) {
  3009. Assoc::iterator it;
  3010. for(int i=0;i<result->size();i++) {
  3011. cArr = result->at(i);
  3012. it = cArr->find("id");
  3013. if( it != cArr->end() ){
  3014. tVal->push_back( atoi( it->second.c_str() ));
  3015. };
  3016. };
  3017. };
  3018. };
  3019. };
  3020. deleteResult( result );
  3021. };
  3022. return tVal;
  3023. };
  3024. DBC_INT_LIST *sqliteDBC::listNewModules( DBC_INT_LIST * old_ids ){
  3025. DBC_INT_LIST * tVal = NULL;
  3026. if( m_bStoIsConnected ){
  3027. char tParam[CTEXT];
  3028. //memset( tParam, 0, CTEXT );
  3029. sprintf(tParam,"0");
  3030. if( old_ids != NULL ) {
  3031. char tBuffer[CTEXT];
  3032. for(int i = 0; i< old_ids->size(); i++ ) {
  3033. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  3034. strncpy( tParam, tBuffer, CTEXT );
  3035. };
  3036. };
  3037. char tQBuf[CTEXT];
  3038. AAssoc *result;
  3039. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam );
  3040. #ifdef DEBUG_ALL
  3041. LOG(tQBuf);
  3042. #endif
  3043. result = m_pDataStorage->Query( tQBuf );
  3044. if( result!= NULL ) {
  3045. if( result->size() > 0 ) {
  3046. Assoc *cArr;
  3047. tVal = new DBC_INT_LIST();
  3048. if( tVal != NULL ) {
  3049. Assoc::iterator it;
  3050. for(int i=0;i<result->size();i++) {
  3051. cArr = result->at(i);
  3052. it = cArr->find("id");
  3053. if( it != cArr->end() ){
  3054. tVal->push_back( atoi( it->second.c_str() ));
  3055. };
  3056. };
  3057. };
  3058. };
  3059. };
  3060. deleteResult( result );
  3061. };
  3062. return tVal;
  3063. };
  3064. DBC_INT_LIST *sqliteDBC::listNewNodes( DBC_INT_LIST * old_ids ){
  3065. DBC_INT_LIST * tVal = NULL;
  3066. if( m_bStoIsConnected ){
  3067. char tParam[CTEXT];
  3068. //memset( tParam, 0, CTEXT );
  3069. sprintf(tParam,"0");
  3070. if( old_ids != NULL ) {
  3071. char tBuffer[CTEXT];
  3072. for(int i = 0; i< old_ids->size(); i++ ) {
  3073. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  3074. strncpy( tParam, tBuffer, CTEXT );
  3075. };
  3076. };
  3077. char tQBuf[CTEXT];
  3078. AAssoc *result;
  3079. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `enable`=1 AND `id` NOT IN(%s)", tParam );
  3080. #ifdef DEBUG_EXTRA
  3081. LOG(tQBuf);
  3082. #endif
  3083. result = m_pDataStorage->Query( tQBuf );
  3084. if( result!= NULL ) {
  3085. if( result->size() > 0 ) {
  3086. Assoc *cArr;
  3087. tVal = new DBC_INT_LIST();
  3088. if( tVal != NULL ) {
  3089. Assoc::iterator it;
  3090. for(int i=0;i<result->size();i++) {
  3091. cArr = result->at(i);
  3092. it = cArr->find("id");
  3093. if( it != cArr->end() ){
  3094. tVal->push_back( atoi( it->second.c_str() ));
  3095. };
  3096. };
  3097. };
  3098. };
  3099. };
  3100. deleteResult( result );
  3101. };
  3102. return tVal;
  3103. };
  3104. DBC_INT_LIST *sqliteDBC::listNodes( bool all ){
  3105. DBC_INT_LIST * tVal = NULL;
  3106. if( m_bStoIsConnected ){
  3107. char tQBuf[1024];
  3108. AAssoc *result;
  3109. if( all ) {
  3110. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  3111. } else {
  3112. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0 AND `enable`=1" );
  3113. };
  3114. #ifdef DEBUG_EXTRA
  3115. LOG(tQBuf);
  3116. #endif
  3117. result = m_pDataStorage->Query( tQBuf );
  3118. if( result!= NULL ) {
  3119. if( result->size() > 0 ) {
  3120. Assoc *cArr;
  3121. tVal = new DBC_INT_LIST();
  3122. if( tVal != NULL ) {
  3123. Assoc::iterator it;
  3124. for(int i=0; i <result->size();i++) {
  3125. cArr = result->at(i);
  3126. it = cArr->find("id");
  3127. if( it != cArr->end() ){
  3128. tVal->push_back( atoi( it->second.c_str() ));
  3129. };
  3130. };
  3131. };
  3132. };
  3133. };
  3134. deleteResult( result );
  3135. };
  3136. return tVal;
  3137. };
  3138. DBC_INT_LIST *sqliteDBC::listOlderReportStat( CSTR Y, CSTR M, CSTR D ){
  3139. DBC_INT_LIST * tVal = NULL;
  3140. if ( m_bStaIsConnected ) {
  3141. char tQBuf[MAX_PATH_LEN];
  3142. AAssoc *result;
  3143. 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 );
  3144. result = m_pDataStatistics->Query( tQBuf );
  3145. if( result!= NULL ) {
  3146. if( result->size() > 0 ) {
  3147. Assoc *cArr;
  3148. tVal = new DBC_INT_LIST();
  3149. if( tVal != NULL ) {
  3150. Assoc::iterator it;
  3151. for(int i=0;i<result->size();i++) {
  3152. cArr = result->at(i);
  3153. it = cArr->find("ROWID");
  3154. if( it != cArr->end() ){
  3155. tVal->push_back( atoi( it->second.c_str() ));
  3156. };
  3157. };
  3158. };
  3159. };
  3160. };
  3161. deleteResult( result );
  3162. };
  3163. return tVal;
  3164. };
  3165. DBC_INT_LIST *sqliteDBC::listOlderReportStat( uint32_t TS ){
  3166. DBC_INT_LIST * tVal = NULL;
  3167. if ( m_bStaIsConnected ) {
  3168. char tQBuf[MAX_PATH_LEN];
  3169. AAssoc *result;
  3170. sprintf(tQBuf, "SELECT `ROWID` FROM `days` WHERE `ts`< %d", TS );
  3171. result = m_pDataStatistics->Query( tQBuf );
  3172. if( result!= NULL ) {
  3173. if( result->size() > 0 ) {
  3174. Assoc *cArr;
  3175. tVal = new DBC_INT_LIST();
  3176. if( tVal != NULL ) {
  3177. Assoc::iterator it;
  3178. for(int i=0;i<result->size();i++) {
  3179. cArr = result->at(i);
  3180. it = cArr->find("ROWID");
  3181. if( it != cArr->end() ){
  3182. tVal->push_back( atoi( it->second.c_str() ));
  3183. };
  3184. };
  3185. };
  3186. };
  3187. };
  3188. deleteResult( result );
  3189. };
  3190. return tVal;
  3191. };
  3192. DBC_INT_LIST *sqliteDBC::listResources( uint32_t NodeID ){
  3193. DBC_INT_LIST * tVal = NULL;
  3194. if( m_bStoIsConnected ){
  3195. char tQBuf[CTEXT];
  3196. AAssoc *result;
  3197. if ( NodeID == 0 ) NodeID = m_nServerNodeID;
  3198. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID );
  3199. #ifdef DEBUG
  3200. LOG(tQBuf);
  3201. #endif
  3202. result = m_pDataStorage->Query( tQBuf );
  3203. if( result!= NULL ) {
  3204. if( result->size() > 0 ) {
  3205. Assoc *cArr;
  3206. tVal = new DBC_INT_LIST();
  3207. if( tVal != NULL ) {
  3208. Assoc::iterator it;
  3209. for(int i=0;i<result->size();i++) {
  3210. cArr = result->at(i);
  3211. it = cArr->find("id");
  3212. if( it != cArr->end() ){
  3213. tVal->push_back( atoi( it->second.c_str() ));
  3214. };
  3215. };
  3216. };
  3217. };
  3218. };
  3219. deleteResult( result );
  3220. };
  3221. return tVal;
  3222. };
  3223. void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
  3224. if( m_bStoIsConnected ){
  3225. m_nSiteID = SiteID;
  3226. char tQBuf[MAX_PATH_LEN];
  3227. #ifdef DEFAULT_SERVER_ID
  3228. 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'",
  3229. DEFAULT_SERVER_ID, SiteID, identity, _BUILD_STRING_, identity, _BUILD_STRING_ );
  3230. #else
  3231. sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'",
  3232. SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ );
  3233. #endif
  3234. #ifdef DEBUG_ALL
  3235. LOG(tQBuf);
  3236. #endif
  3237. m_pDataStorage->SilentQuery( tQBuf );
  3238. #ifdef DEFAULT_SERVER_ID
  3239. m_nServerID = DEFAULT_SERVER_ID;
  3240. #else
  3241. m_nServerID = getServerID( identity );
  3242. #endif
  3243. *ServID = m_nServerID;
  3244. if( m_nServerID ) {
  3245. //if ( bRecreateMemStructures )
  3246. initMemDB( m_nServerID );
  3247. sprintf( tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID );
  3248. #ifdef DEBUG_ALL
  3249. LOG(tQBuf);
  3250. #endif
  3251. m_pDataMemory->SilentQuery( tQBuf );
  3252. 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 );
  3253. #ifdef DEBUG_ALL
  3254. LOG(tQBuf);
  3255. #endif
  3256. m_nServerNodeID = ( uint32_t ) m_pDataStorage->QueryID( tQBuf );
  3257. if( m_nServerNodeID== 0 ){
  3258. m_nServerNodeID = getServerNode( m_nServerID );
  3259. };
  3260. *ServNode = m_nServerNodeID;
  3261. // Clenup Nodes and services ...
  3262. sprintf(tQBuf,"UPDATE `nodes` SET `online`=0" );
  3263. #ifdef DEBUG_ALL
  3264. LOG(tQBuf);
  3265. #endif
  3266. m_pDataStorage->SilentQuery( tQBuf );
  3267. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0 WHERE `manual`=0" );
  3268. #ifdef DEBUG_ALL
  3269. LOG(tQBuf);
  3270. #endif
  3271. m_pDataStorage->SilentQuery( tQBuf );
  3272. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `manual`=0 )" );
  3273. #ifdef DEBUG_ALL
  3274. LOG(tQBuf);
  3275. #endif
  3276. m_pDataStorage->SilentQuery( tQBuf );
  3277. //sprintf(tQBuf,"UPDATE `cameras` SET `online`=1 WHERE `manual`=1" );
  3278. #ifdef DEBUG_ALL
  3279. //LOG(tQBuf);
  3280. #endif
  3281. //m_pDataStorage->SilentQuery( tQBuf );
  3282. //sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `online`=1 )" );
  3283. #ifdef DEBUG_ALL
  3284. //LOG(tQBuf);
  3285. #endif
  3286. //m_pDataStorage->SilentQuery( tQBuf );
  3287. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  3288. #ifdef DEBUG_ALL
  3289. // LOG(tQBuf);
  3290. #endif
  3291. // m_pDataStorage->SilentQuery( tQBuf );
  3292. //cleanResources( tServerNodeID );
  3293. setNodeOnline( m_nServerNodeID );
  3294. };
  3295. };
  3296. };
  3297. bool sqliteDBC::setLRFDataStatus( uint32_t lrfID, uint8_t ST ){
  3298. if( m_bStoIsConnected ) {
  3299. char tQBuf[MAX_PATH_LEN];
  3300. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `data_status`=%d WHERE `id`=%d", ST, lrfID );
  3301. #ifdef DEBUG
  3302. LOG(tQBuf);
  3303. #endif
  3304. m_pDataStorage->SilentQuery( tQBuf );
  3305. return true;
  3306. };
  3307. return false;
  3308. };
  3309. bool sqliteDBC::setLRFDetectionProfile( uint32_t lrfID, uint8_t profileID ){
  3310. if( m_bStoIsConnected ) {
  3311. char tQBuf[MAX_PATH_LEN];
  3312. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `detection_profile_id`=%d WHERE `id`=%d", profileID, lrfID );
  3313. #ifdef DEBUG_ALL
  3314. LOG(tQBuf);
  3315. #endif
  3316. m_pDataStorage->SilentQuery( tQBuf );
  3317. return true;
  3318. };
  3319. return false;
  3320. };
  3321. bool sqliteDBC::setLRFMaskStatus( uint32_t maskID, bool status ){
  3322. if( m_bStoIsConnected ) {
  3323. char tQBuf[MAX_PATH_LEN];
  3324. if( status ) {
  3325. sprintf( tQBuf, "UPDATE `lrf_masks` SET `status`='E' WHERE `id`=%d", maskID );
  3326. } else {
  3327. sprintf( tQBuf, "UPDATE `lrf_masks` SET `status`='D' WHERE `id`=%d", maskID );
  3328. };
  3329. #ifdef DEBUG_ALL
  3330. LOG(tQBuf);
  3331. #endif
  3332. m_pDataStorage->SilentQuery( tQBuf );
  3333. return true;
  3334. };
  3335. return false;
  3336. };
  3337. bool sqliteDBC::setLRFAltSerial( uint32_t lrfID, CSTR Serial ){
  3338. if( m_bStoIsConnected ) {
  3339. char tQBuf[MAX_PATH_LEN];
  3340. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_serial`='%s' WHERE `id`=%d", Serial, lrfID );
  3341. #ifdef DEBUG_ALL
  3342. LOG(tQBuf);
  3343. #endif
  3344. m_pDataStorage->SilentQuery( tQBuf );
  3345. return true;
  3346. };
  3347. return false;
  3348. };
  3349. bool sqliteDBC::setLRFAltVerison( uint32_t lrfID, CSTR version ){
  3350. if( m_bStoIsConnected ) {
  3351. char tQBuf[MAX_PATH_LEN];
  3352. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_version`='%s' WHERE `id`=%d", version, lrfID );
  3353. #ifdef DEBUG_ALL
  3354. LOG(tQBuf);
  3355. #endif
  3356. m_pDataStorage->SilentQuery( tQBuf );
  3357. return true;
  3358. };
  3359. return false;
  3360. };
  3361. bool sqliteDBC::setNodeOffline( uint32_t NodeID ){
  3362. if( m_bStoIsConnected ){
  3363. char tQBuf[CTEXT];
  3364. if( NodeID ) {
  3365. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  3366. #ifdef DEBUG_ALL
  3367. LOG(tQBuf);
  3368. #endif
  3369. m_pDataStorage->SilentQuery( tQBuf );
  3370. sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
  3371. #ifdef DEBUG_ALL
  3372. LOG(tQBuf);
  3373. #endif
  3374. m_pDataStorage->SilentQuery( tQBuf );
  3375. return true;
  3376. };
  3377. };
  3378. return false;
  3379. };
  3380. bool sqliteDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
  3381. if( m_bStoIsConnected ){
  3382. char tQBuf[CTEXT];
  3383. if( NodeID ) {
  3384. sprintf(tQBuf,"UPDATE `nodes` SET `online`=%d WHERE `id`=%d", status, NodeID );
  3385. #ifdef DEBUG
  3386. LOG(tQBuf);
  3387. #endif
  3388. m_pDataStorage->SilentQuery( tQBuf );
  3389. sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
  3390. #ifdef DEBUG
  3391. LOG(tQBuf);
  3392. #endif
  3393. m_pDataStorage->SilentQuery( tQBuf );
  3394. return true;
  3395. };
  3396. };
  3397. return false;
  3398. };
  3399. bool sqliteDBC::setRessourceActive( uint32_t RessourceID, bool Status ){
  3400. if( m_bStoIsConnected ){
  3401. char tQBuf[CTEXT];
  3402. if( RessourceID ) {
  3403. sprintf(tQBuf,"UPDATE `resources` SET `active`=%d WHERE `id`=%d", Status, RessourceID );
  3404. #ifdef DEBUG
  3405. LOG(tQBuf);
  3406. #endif
  3407. m_pDataStorage->SilentQuery( tQBuf );
  3408. return true;
  3409. };
  3410. };
  3411. return false;
  3412. };
  3413. bool sqliteDBC::setStreamValid( uint32_t streamID, bool status ){
  3414. if( m_bStoIsConnected ) {
  3415. char tQBuf[MAX_PATH_LEN];
  3416. if ( status ) {
  3417. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  3418. } else {
  3419. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  3420. };
  3421. #ifdef DEBUG
  3422. LOG(tQBuf);
  3423. #endif
  3424. m_pDataStorage->SilentQuery( tQBuf );
  3425. return true;
  3426. };
  3427. return false;
  3428. };
  3429. void sqliteDBC::unregisterServer( void ){
  3430. if( m_bStoIsConnected ) {
  3431. char tQBuf[MAX_PATH_LEN];
  3432. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0" );
  3433. #ifdef DEBUG_ALL
  3434. LOG(tQBuf);
  3435. #endif
  3436. m_pDataStorage->SilentQuery( tQBuf );
  3437. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0" );
  3438. #ifdef DEBUG_ALL
  3439. LOG(tQBuf);
  3440. #endif
  3441. m_pDataStorage->SilentQuery( tQBuf );
  3442. };
  3443. setNodeOffline( m_nServerNodeID );
  3444. };
  3445. bool sqliteDBC::updateSystemInfo( DBC_SYS_INFO *ptr ){
  3446. if( m_bStoIsConnected ) {
  3447. char tQBuf[MAX_PATH_LEN];
  3448. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d",
  3449. ptr->OS_INFO,
  3450. ptr->HW_INFO,
  3451. m_nSiteID
  3452. );
  3453. #ifdef DEBUG_ALL
  3454. LOG(tQBuf);
  3455. #endif
  3456. m_pDataStorage->SilentQuery( tQBuf );
  3457. return true;
  3458. };
  3459. return false;
  3460. };
  3461. bool sqliteDBC::fillShadow2D( uint32_t ID, SHADOW & SH ){
  3462. if( m_bStoIsConnected ){
  3463. char tQBuf[CTEXT];
  3464. AAssoc *result;
  3465. 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 );
  3466. LOG(tQBuf);
  3467. result = m_pDataStorage->Query( tQBuf );
  3468. if( result!= NULL ) {
  3469. if( result->size() > 0 ) {
  3470. SH.m_nEnabled = 0xFFFFFFFF;
  3471. SH.m_nPolygonCount = result->size();
  3472. SH.pPolygones = (SHADOW_POLY *) calloc( SH.m_nPolygonCount, sizeof( SHADOW_POLY ) );
  3473. if ( SH.pPolygones ) {
  3474. Assoc * cArr;
  3475. for ( uint8_t idx = 0; idx < result->size(); idx++ ) {
  3476. Assoc::iterator it;
  3477. cArr = result->at(idx);
  3478. it = cArr->find("id");
  3479. if( it != cArr->end() ){
  3480. SH.pPolygones[idx].m_nDBID = atoi(it->second.c_str());
  3481. };
  3482. it = cArr->find("layer");
  3483. if( it != cArr->end() ){
  3484. SH.pPolygones[idx].bType = atoi(it->second.c_str());
  3485. };
  3486. it = cArr->find("status");
  3487. if( it != cArr->end() ){
  3488. SH.m_nEnabled &= ( it->second.c_str()[0] == 'E' )?0xFFFFFFFF:~(1<<SH.pPolygones[idx].bType);
  3489. };
  3490. sprintf(tQBuf, "SELECT * FROM `lrf_mask_vertex_points` WHERE `vertex_id`=%d ORDER BY `id`", SH.pPolygones[idx].m_nDBID );
  3491. LOG(tQBuf);
  3492. AAssoc *v_result = m_pDataStorage->Query( tQBuf );
  3493. if( v_result!= NULL ) {
  3494. if( v_result->size() > 0 ) {
  3495. SH.pPolygones[idx].wCount = v_result->size();
  3496. SH.pPolygones[idx].pPoint = (HZ_POINT *) calloc( SH.pPolygones[idx].wCount, sizeof( HZ_POINT ) );
  3497. if(SH.pPolygones[idx].pPoint ) {
  3498. for ( uint32_t zdx =0; zdx < SH.pPolygones[idx].wCount; zdx++ ) {
  3499. cArr = v_result->at(zdx);
  3500. it = cArr->find("x");
  3501. if( it != cArr->end() ){
  3502. SH.pPolygones[idx].pPoint[zdx].wX = atoi(it->second.c_str());
  3503. };
  3504. it = cArr->find("y");
  3505. if( it != cArr->end() ){
  3506. SH.pPolygones[idx].pPoint[zdx].wY = atoi(it->second.c_str());
  3507. };
  3508. LOG("Point Added!");
  3509. };
  3510. } else {
  3511. LOG("No mem for Points!");
  3512. };
  3513. } else {
  3514. LOG("No Points!");
  3515. SH.pPolygones[idx].wCount = 0;
  3516. SH.pPolygones[idx].pPoint = NULL;
  3517. };
  3518. deleteResult( v_result );
  3519. };
  3520. };
  3521. };
  3522. };
  3523. deleteResult( result );
  3524. };
  3525. return true;
  3526. };
  3527. return false;
  3528. };
  3529. bool sqliteDBC::getAppDebugMode(){
  3530. bool rVal = false;
  3531. if( m_bStoIsConnected ){
  3532. char tQBuf[CTEXT];
  3533. AAssoc *result;
  3534. sprintf(tQBuf, "SELECT `work_mode` FROM `system_props` WHERE `id`=1" );
  3535. #ifdef DEBUG_ALL
  3536. LOG(tQBuf);
  3537. #endif
  3538. result = m_pDataStorage->Query( tQBuf );
  3539. if( result!= NULL ) {
  3540. if( result->size() > 0 ) {
  3541. Assoc *cArr;
  3542. Assoc::iterator it;
  3543. cArr = result->at(0);
  3544. it = cArr->find("work_mode");
  3545. if( it != cArr->end() ){
  3546. rVal = (bool) atoi( it->second.c_str() );
  3547. };
  3548. };
  3549. };
  3550. deleteResult( result );
  3551. };
  3552. return rVal;
  3553. };
  3554. DBC_CAM_DATA *sqliteDBC::getCamData( uint32_t ID ){
  3555. DBC_CAM_DATA *tVal = NULL;
  3556. if( m_bStoIsConnected ){
  3557. char tQBuf[CTEXT];
  3558. AAssoc *result;
  3559. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  3560. #ifdef DEBUG_ALL
  3561. LOG(tQBuf);
  3562. #endif
  3563. result = m_pDataStorage->Query( tQBuf );
  3564. if( result!= NULL ) {
  3565. if( result->size() > 0 ) {
  3566. Assoc *cArr;
  3567. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  3568. if( tVal != NULL ) {
  3569. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  3570. tVal->id = ID;
  3571. Assoc::iterator it;
  3572. cArr = result->at(0);
  3573. it = cArr->find("type");
  3574. if( it != cArr->end() ){
  3575. tVal->type = atoi( it->second.c_str() );
  3576. };
  3577. it = cArr->find("online");
  3578. if( it != cArr->end() ){
  3579. tVal->online = (bool) atoi( it->second.c_str() );
  3580. };
  3581. it = cArr->find("stream_id");
  3582. if( it != cArr->end() ){
  3583. tVal->stream_id = atoi( it->second.c_str() );
  3584. };
  3585. it = cArr->find("hardware");
  3586. if( it != cArr->end() ){
  3587. strncpy( tVal->hardware, it->second.c_str(), 32 );
  3588. };
  3589. it = cArr->find("name");
  3590. if( it != cArr->end() ){
  3591. strncpy( tVal->name, it->second.c_str(), 64 );
  3592. };
  3593. it = cArr->find("status");
  3594. if( it != cArr->end() ){
  3595. tVal->status = atoi( it->second.c_str() );
  3596. };
  3597. it = cArr->find("session");
  3598. if( it != cArr->end() ){
  3599. strncpy( tVal->session, it->second.c_str(), 32 );
  3600. };
  3601. it = cArr->find("protocol");
  3602. if( it != cArr->end() ){
  3603. tVal->protocol = atoi( it->second.c_str() );
  3604. };
  3605. it = cArr->find("protocol_aux");
  3606. if( it != cArr->end() ){
  3607. tVal->protocol_aux = atoi( it->second.c_str() );
  3608. };
  3609. it = cArr->find("uart_id");
  3610. if( it != cArr->end() ){
  3611. tVal->uart_id = atoi( it->second.c_str() );
  3612. };
  3613. it = cArr->find("x");
  3614. if( it != cArr->end() ){
  3615. tVal->x = atof( it->second.c_str() );
  3616. };
  3617. it = cArr->find("y");
  3618. if( it != cArr->end() ){
  3619. tVal->y = atof( it->second.c_str() );
  3620. };
  3621. it = cArr->find("z");
  3622. if( it != cArr->end() ){
  3623. tVal->z = atof( it->second.c_str() );
  3624. };
  3625. it = cArr->find("x_rot");
  3626. if( it != cArr->end() ){
  3627. tVal->x_rot = atof( it->second.c_str() );
  3628. };
  3629. it = cArr->find("y_rot");
  3630. if( it != cArr->end() ){
  3631. tVal->y_rot = atof( it->second.c_str() );
  3632. };
  3633. it = cArr->find("z_rot");
  3634. if( it != cArr->end() ){
  3635. tVal->z_rot = atof( it->second.c_str() );
  3636. };
  3637. it = cArr->find("imager_type");
  3638. if( it != cArr->end() ){
  3639. tVal->imager_type = atoi( it->second.c_str() );
  3640. };
  3641. it = cArr->find("lens_f");
  3642. if( it != cArr->end() ){
  3643. tVal->lens_f = atoi( it->second.c_str() );
  3644. };
  3645. it = cArr->find("ptz_speed_step");
  3646. if( it != cArr->end() ){
  3647. tVal->ptz_speed_step = atof( it->second.c_str() );
  3648. };
  3649. it = cArr->find("pan_model");
  3650. if( it != cArr->end() ){
  3651. tVal->pan_model = atoi( it->second.c_str() );
  3652. };
  3653. it = cArr->find("pan_angles");
  3654. if( it != cArr->end() ){
  3655. tVal->pan_angles = atoi( it->second.c_str() );
  3656. };
  3657. it = cArr->find("tilt_model");
  3658. if( it != cArr->end() ){
  3659. tVal->tilt_model = atoi( it->second.c_str() );
  3660. };
  3661. it = cArr->find("tilt_angles");
  3662. if( it != cArr->end() ){
  3663. tVal->tilt_angles = atoi( it->second.c_str() );
  3664. };
  3665. it = cArr->find("zoom_model");
  3666. if( it != cArr->end() ){
  3667. tVal->zoom_model = atoi( it->second.c_str() );
  3668. };
  3669. it = cArr->find("zoom");
  3670. if( it != cArr->end() ){
  3671. tVal->zoom = atof( it->second.c_str() );
  3672. };
  3673. it = cArr->find("max_range");
  3674. if( it != cArr->end() ){
  3675. tVal->max_range = atof( it->second.c_str() );
  3676. };
  3677. it = cArr->find("access_login");
  3678. if( it != cArr->end() ){
  3679. strncpy( tVal->username, it->second.c_str(), 16 );
  3680. };
  3681. it = cArr->find("access_pass");
  3682. if( it != cArr->end() ){
  3683. strncpy( tVal->password, it->second.c_str(), 16 );
  3684. };
  3685. it = cArr->find("uuid");
  3686. if( it != cArr->end() ){
  3687. strncpy( tVal->uuid, it->second.c_str(), 64 );
  3688. };
  3689. it = cArr->find("mac");
  3690. if( it != cArr->end() ){
  3691. strncpy( tVal->mac, it->second.c_str(), 16 );
  3692. };
  3693. it = cArr->find("ip");
  3694. if( it != cArr->end() ){
  3695. strncpy( tVal->ip, it->second.c_str(), 16 );
  3696. };
  3697. it = cArr->find("port");
  3698. if( it != cArr->end() ){
  3699. tVal->port = atoi( it->second.c_str() );
  3700. };
  3701. it = cArr->find("onvif_access");
  3702. if( it != cArr->end() ){
  3703. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  3704. };
  3705. it = cArr->find("onvif_ctrl");
  3706. if( it != cArr->end() ){
  3707. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  3708. };
  3709. it = cArr->find("ptz_token");
  3710. if( it != cArr->end() ){
  3711. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  3712. };
  3713. it = cArr->find("trigger_mode");
  3714. if( it != cArr->end() ){
  3715. tVal->triggering_mode = atoi( it->second.c_str() );
  3716. };
  3717. it = cArr->find("auth_mode");
  3718. if( it != cArr->end() ){
  3719. tVal->auth_mode = atoi( it->second.c_str() );
  3720. };
  3721. it = cArr->find("ptz_axel");
  3722. if( it != cArr->end() ){
  3723. tVal->ptz_axel = atoi( it->second.c_str() );
  3724. };
  3725. it = cArr->find("manual");
  3726. if( it != cArr->end() ){
  3727. tVal->manual = (bool) atoi( it->second.c_str() );
  3728. };
  3729. it = cArr->find("homing_enable");
  3730. if( it != cArr->end() ){
  3731. tVal->homing_enable = (bool) atoi( it->second.c_str() );
  3732. };
  3733. it = cArr->find("homing_to");
  3734. if( it != cArr->end() ){
  3735. tVal->homing_to = atoi( it->second.c_str() );
  3736. };
  3737. it = cArr->find("home_token");
  3738. if( it != cArr->end() ){
  3739. strncpy( tVal->home_token, it->second.c_str(), 32 );
  3740. };
  3741. };
  3742. };
  3743. };
  3744. deleteResult( result );
  3745. };
  3746. return tVal;
  3747. };
  3748. uint32_t sqliteDBC::getCamId( CSTR UUID ){
  3749. uint32_t RetVal = 0;
  3750. if( m_bStoIsConnected ){
  3751. char tQBuf[CTEXT];
  3752. AAssoc *result;
  3753. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID );
  3754. #ifdef DEBUG_ALL
  3755. LOG(tQBuf);
  3756. #endif
  3757. result = m_pDataStorage->Query( tQBuf );
  3758. if( result!= NULL ) {
  3759. if( result->size() > 0 ) {
  3760. Assoc *cArr;
  3761. Assoc::iterator it;
  3762. cArr = result->at(0);
  3763. it = cArr->find("id");
  3764. if( it != cArr->end() ){
  3765. RetVal = atoi( it->second.c_str() );
  3766. };
  3767. };
  3768. };
  3769. deleteResult( result );
  3770. };
  3771. return RetVal;
  3772. };
  3773. CSTR sqliteDBC::getDataParam( uint8_t pid ){
  3774. switch ( pid ) {
  3775. case 0: return strdup( m_pDbFile ); break;
  3776. case 1: return strdup( web_access_user ); break;
  3777. case 2: return strdup( web_access_group ); break;
  3778. };
  3779. return NULL;
  3780. };
  3781. bool sqliteDBC::updateIPCam( uint32_t CamID, CSTR Addr ){
  3782. if( m_bStoIsConnected ) {
  3783. char tQBuf[MAX_PATH_LEN];
  3784. char tIpBuf[CTEXT];
  3785. char *tPos;
  3786. char *tPos2;
  3787. uint16_t tPort = 0;
  3788. tPos = strstr(Addr,"//");
  3789. if( tPos != NULL ) {
  3790. tPos2 = strstr( tPos + 2,":");
  3791. if( tPos2 != NULL ) {
  3792. LOG("IP Port Defined!");
  3793. int len, len2, len3;
  3794. len = tPos2 - tPos - 2;
  3795. strncpy( tIpBuf, tPos + 2, len);
  3796. tIpBuf[len] = (char)0;
  3797. tPort = atoi( tPos2 + 1 );
  3798. //strncpy( tPath, tPos + 2 + len, CTEXT );
  3799. } else {
  3800. tPos2 = strstr( tPos + 2,"/");
  3801. if( tPos2 != NULL ) {
  3802. int len, len2, len3;
  3803. len = tPos2 - tPos - 2;
  3804. strncpy( tIpBuf, tPos + 2, len);
  3805. tIpBuf[len] = (char)0;
  3806. //strncpy( tPath, tPos + 2 + len, CTEXT );
  3807. } else {
  3808. tPos2 = strstr( tPos + 2,"/");
  3809. LOG_ERROR("Pos 2 - err");
  3810. tIpBuf[0] = (char)0;
  3811. };
  3812. };
  3813. } else {
  3814. LOG_ERROR("Pos 0 - err");
  3815. tIpBuf[0] = (char)0;
  3816. };
  3817. if ( tPort ) {
  3818. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d",
  3819. Addr,
  3820. tIpBuf,
  3821. tPort,
  3822. CamID
  3823. );
  3824. } else {
  3825. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d",
  3826. Addr,
  3827. tIpBuf,
  3828. CamID
  3829. );
  3830. }
  3831. #ifdef DEBUG
  3832. LOG(tQBuf);
  3833. #endif
  3834. m_pDataStorage->SilentQuery( tQBuf );
  3835. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  3836. #ifdef DEBUG_ALL
  3837. LOG(tQBuf);
  3838. #endif
  3839. m_pDataStorage->SilentQuery( tQBuf );
  3840. return true;
  3841. };
  3842. return false;
  3843. };
  3844. bool sqliteDBC::updateCamRange( uint32_t camID, float range ){
  3845. if( m_bStoIsConnected ) {
  3846. char tQBuf[MAX_PATH_LEN];
  3847. sprintf(tQBuf,"UPDATE `cameras` SET `max_range`=%.3f WHERE `id`=%d",
  3848. range,
  3849. camID
  3850. );
  3851. #ifdef DEBUG_EXTRA
  3852. LOG(tQBuf);
  3853. #endif
  3854. m_pDataStorage->SilentQuery( tQBuf );
  3855. return true;
  3856. };
  3857. return false;
  3858. };
  3859. bool sqliteDBC::updateCamOnlineStatus( uint32_t camID, bool online ){
  3860. if( m_bStoIsConnected ) {
  3861. char tQBuf[MAX_PATH_LEN];
  3862. if ( online ) {
  3863. sprintf( tQBuf, "UPDATE `cameras` SET `online`=1 WHERE `id`=%d", camID );
  3864. } else {
  3865. sprintf( tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `id`=%d", camID );
  3866. };
  3867. m_pDataStorage->SilentQuery(tQBuf);
  3868. if ( online ) {
  3869. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", camID);
  3870. } else {
  3871. sprintf( tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id`=%d", camID );
  3872. };
  3873. m_pDataStorage->SilentQuery(tQBuf);
  3874. return true;
  3875. };
  3876. return false;
  3877. };
  3878. bool sqliteDBC::updateIPCam( CSTR uuid, CSTR Addr ){
  3879. if( m_bStoIsConnected ) {
  3880. uint32_t cID = getCamId( uuid );
  3881. if ( cID ) {
  3882. return updateIPCam( cID, Addr );
  3883. };
  3884. };
  3885. return false;
  3886. };
  3887. bool sqliteDBC::updateLocalCam( CSTR uuid ){
  3888. if( m_bStoIsConnected ) {
  3889. uint32_t cID = getCamId( uuid );
  3890. if ( cID ) {
  3891. return updateLocalCam( cID );
  3892. };
  3893. };
  3894. return false;
  3895. };
  3896. bool sqliteDBC::updateLocalCam( uint32_t CamID ){
  3897. if( m_bStoIsConnected ) {
  3898. char tQBuf[MAX_PATH_LEN];
  3899. sprintf(tQBuf,"UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID );
  3900. #ifdef DEBUG
  3901. LOG(tQBuf);
  3902. #endif
  3903. m_pDataStorage->SilentQuery( tQBuf );
  3904. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  3905. #ifdef DEBUG
  3906. LOG(tQBuf);
  3907. #endif
  3908. m_pDataStorage->SilentQuery( tQBuf );
  3909. return true;
  3910. };
  3911. return false;
  3912. };
  3913. bool sqliteDBC::updateLRFComStat( uint32_t lrfID, uint32_t ErrorPerMinute ){
  3914. if( m_bStoIsConnected ) {
  3915. char tQBuf[MAX_PATH_LEN];
  3916. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `com_epm`=%d WHERE `id`=%d", ErrorPerMinute, lrfID );
  3917. #ifdef DEBUG_ALL
  3918. LOG(tQBuf);
  3919. #endif
  3920. m_pDataStorage->SilentQuery( tQBuf );
  3921. return true;
  3922. };
  3923. return false;
  3924. };
  3925. bool sqliteDBC::updateLRFFrozenStatus( uint32_t lrfID, bool FFT, bool SFT ){
  3926. if( m_bStoIsConnected ) {
  3927. char tQBuf[MAX_PATH_LEN];
  3928. sprintf(tQBuf, "UPDATE `lrf_ini_settings` SET `FREEZ_FIRST_TH`=%d, `SAVE_FIRST_TH`=%d WHERE `sensor_id`=%d", FFT, SFT, lrfID );
  3929. #ifdef DEBUG
  3930. LOG(tQBuf);
  3931. #endif
  3932. m_pDataStorage->SilentQuery( tQBuf );
  3933. return true;
  3934. };
  3935. return false;
  3936. };
  3937. 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 ){
  3938. if( m_bStoIsConnected ) {
  3939. char tQBuf[MAX_PATH_LEN];
  3940. if( master ) {
  3941. 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",
  3942. lrfID,
  3943. profileID,
  3944. nOrder,
  3945. nOffset,
  3946. nDelta,
  3947. nFlip,
  3948. nWidth,
  3949. bSST,
  3950. nSun,
  3951. GP,
  3952. spikes,
  3953. nOrder,
  3954. nOffset,
  3955. nDelta,
  3956. nFlip,
  3957. nWidth,
  3958. bSST,
  3959. nSun,
  3960. GP,
  3961. spikes );
  3962. } else {
  3963. 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",
  3964. lrfID,
  3965. profileID,
  3966. nOrder,
  3967. nOffset,
  3968. nDelta,
  3969. nFlip,
  3970. nWidth,
  3971. bSST,
  3972. nSun,
  3973. GP,
  3974. spikes,
  3975. nOrder,
  3976. nOffset,
  3977. nDelta,
  3978. nFlip,
  3979. nWidth,
  3980. bSST,
  3981. nSun,
  3982. GP,
  3983. spikes );
  3984. };
  3985. #ifdef DEBUG_ALL
  3986. LOG(tQBuf);
  3987. #endif
  3988. m_pDataStorage->SilentQuery( tQBuf );
  3989. return true;
  3990. };
  3991. return false;
  3992. };
  3993. bool sqliteDBC::updateLRFDryContact( uint32_t lrfID, bool bAuto, bool bNO, uint16_t wWindow ){
  3994. if( m_bStoIsConnected ) {
  3995. char tQBuf[MAX_PATH_LEN];
  3996. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `dc_auto`=%d, `dc_no`=%d, `dc_window`=%d WHERE `id`=%d", bAuto, bNO, wWindow, lrfID );
  3997. #ifdef DEBUG_ALL
  3998. LOG(tQBuf);
  3999. #endif
  4000. m_pDataStorage->SilentQuery( tQBuf );
  4001. return true;
  4002. };
  4003. return false;
  4004. };
  4005. bool sqliteDBC::updateLRFSerial( uint32_t lrfID, CSTR Serial ){
  4006. if( m_bStoIsConnected ) {
  4007. char tQBuf[MAX_PATH_LEN];
  4008. CSTR tES = m_pDataStorage->escapeString( Serial );
  4009. if( tES ) {
  4010. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID );
  4011. free( tES );
  4012. #ifdef DEBUG_ALL
  4013. LOG(tQBuf);
  4014. #endif
  4015. m_pDataStorage->SilentQuery( tQBuf );
  4016. return true;
  4017. };
  4018. };
  4019. return false;
  4020. };
  4021. bool sqliteDBC::updateLRFUpload( uint32_t id, uint8_t status ){
  4022. if( m_bStoIsConnected ) {
  4023. char tQBuf[MAX_PATH_LEN];
  4024. sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=%d WHERE `id`=%d", status, id );
  4025. #ifdef DEBUG_ALL
  4026. LOG(tQBuf);
  4027. #endif
  4028. m_pDataStorage->SilentQuery( tQBuf );
  4029. return true;
  4030. };
  4031. return false;
  4032. };
  4033. bool sqliteDBC::updateLRFUnfinishedUploads( uint32_t deviceID ){
  4034. if( m_bStoIsConnected ) {
  4035. char tQBuf[MAX_PATH_LEN];
  4036. sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=-1 WHERE `sensor_id`=%d AND `status` < 101 ", deviceID );
  4037. #ifdef DEBUG_ALL
  4038. LOG(tQBuf);
  4039. #endif
  4040. m_pDataStorage->SilentQuery( tQBuf );
  4041. return true;
  4042. };
  4043. return false;
  4044. };
  4045. bool sqliteDBC::updateLRFVerison( uint32_t lrfID, CSTR version ){
  4046. if( m_bStoIsConnected ) {
  4047. char tQBuf[MAX_PATH_LEN];
  4048. CSTR tES = m_pDataStorage->escapeString( version );
  4049. if( tES ) {
  4050. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID );
  4051. free( tES );
  4052. #ifdef DEBUG_ALL
  4053. LOG(tQBuf);
  4054. #endif
  4055. m_pDataStorage->SilentQuery( tQBuf );
  4056. return true;
  4057. };
  4058. };
  4059. return false;
  4060. };
  4061. bool sqliteDBC::updateServerIdentity( uint32_t m_nServerID, CSTR m_sIdentity ){
  4062. if( m_bStoIsConnected ) {
  4063. char tQBuf[MAX_PATH_LEN];
  4064. CSTR tES = m_pDataStorage->escapeString( m_sIdentity );
  4065. if( tES ) {
  4066. sprintf( tQBuf, "UPDATE `servers` SET `identity`='%s' WHERE `id`=%d", tES, m_nServerID );
  4067. free( tES );
  4068. #ifdef DEBUG_ALL
  4069. LOG(tQBuf);
  4070. #endif
  4071. m_pDataStorage->SilentQuery( tQBuf );
  4072. return true;
  4073. };
  4074. };
  4075. return false;
  4076. };
  4077. #undef LOG
  4078. #undef LOG_ERROR