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.

4194 lines
158 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
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_OP_ACK_MASK:
  726. case EVT_OP_NACK_MASK:
  727. case EVT_OP_ON_MASK:
  728. case EVT_OP_OFF_MASK:
  729. case EVT_CONNECTED:
  730. case EVT_DISCONNECTED:
  731. case EVT_COMMUNICATION_JAMED:
  732. case EVT_STUDY:
  733. case EVT_BUSY:
  734. case EVT_FLIP:
  735. case EVT_READY:
  736. case EVT_ERR_LRF_NO_IRQ:
  737. case EVT_ERR_LRF_NO_ZERO:
  738. case EVT_ERR_LRF_ANGLE:
  739. case EVT_ERR_LRF_VOLTAGE:
  740. case EVT_ERR_LRF_BROKEN:
  741. case EVT_LRF_VIS_CHANGED:
  742. case EVT_ERR_OVERFLOW:
  743. case EVT_SST_OVERFLOW:
  744. case EVT_VIDEO_FPS_DEGRADED:
  745. case EVT_VIDEO_FPS_RAISED:{
  746. updateStats();
  747. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)",
  748. ptr->node_id,
  749. ptr->uuid,
  750. ptr->ts_s,
  751. ptr->ts_ms,
  752. ptr->type
  753. );
  754. break;
  755. };
  756. case EVT_ROI_ACTIVE:
  757. case EVT_TARGET_3D_WHITE:
  758. case EVT_TARGET_3D_BLACK:{
  759. updateStats();
  760. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`lrf_id`,`range_digital`,`azimuth`,`elevation`,`range`,`area`,`lat`,`lon`,`alt`)" \
  761. " VALUES(%d,'%s',%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  762. ptr->node_id,
  763. ptr->uuid,
  764. ptr->ts_s,
  765. ptr->ts_ms,
  766. ptr->type,
  767. ptr->lrf_id,
  768. ptr->range_digital,
  769. ptr->az,
  770. ptr->el,
  771. ptr->ra,
  772. ptr->area,
  773. ptr->lat,
  774. ptr->lon,
  775. ptr->alt
  776. );
  777. break;
  778. };
  779. case EVT_TARGET_2D_WHITE:
  780. case EVT_TARGET_2D_BLACK:{
  781. updateStats();
  782. sprintf( tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`lrf_id`,`range_digital`,`azimuth`,`range`,`area`,`lat`,`lon`,`alt`)" \
  783. " VALUES(%d,'%s',%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  784. ptr->node_id,
  785. ptr->uuid,
  786. ptr->ts_s,
  787. ptr->ts_ms,
  788. ptr->type,
  789. ptr->lrf_id,
  790. ptr->range_digital,
  791. ptr->az,
  792. ptr->ra,
  793. ptr->area,
  794. ptr->lat,
  795. ptr->lon,
  796. ptr->alt
  797. );
  798. break;
  799. };
  800. default: return 0;
  801. };
  802. #ifdef DEBUG_EXTRA
  803. LOG( tQBuf );
  804. #endif
  805. if ( m_bRepIsConnected ) {
  806. return ptr->id = m_pDataReports->QueryID(tQBuf);
  807. } else {
  808. LOG_ERROR("Report DB Droped!");
  809. }
  810. };
  811. return 0;
  812. };
  813. bool sqliteDBC::addIPCam( OBSERVER_RESULT *tPtr ){
  814. if(m_bStoIsConnected) {
  815. char tQBuf[1024];
  816. char tIpBuf[32];
  817. char tProto[32];
  818. char tPath[CTEXT];
  819. char *tPos;
  820. char *tPos2;
  821. uint16_t tPort = 0;
  822. memcpy( tProto, tPtr->XAddrs, 5 );
  823. tProto[5] = (char)0;
  824. if( tProto[4] == ':') tProto[4] = (char)0;
  825. memset( tPath, 0, CTEXT );
  826. tPos = strstr(tPtr->XAddrs,"//");
  827. if( tPos != NULL ) {
  828. tPos2 = strstr( tPos + 2,":");
  829. if( tPos2 != NULL ) {
  830. LOG("IP Port Defined!");
  831. int len, len2, len3;
  832. len = tPos2 - tPos - 2;
  833. strncpy( tIpBuf, tPos + 2, len);
  834. tIpBuf[len] = (char)0;
  835. tPort = atoi( tPos2 + 1 );
  836. strncpy( tPath, tPos + 2 + len, CTEXT );
  837. } else {
  838. tPos2 = strstr( tPos + 2,"/");
  839. if( tPos2 != NULL ) {
  840. int len, len2, len3;
  841. len = tPos2 - tPos - 2;
  842. strncpy( tIpBuf, tPos + 2, len);
  843. tIpBuf[len] = (char)0;
  844. strncpy( tPath, tPos + 2 + len, CTEXT );
  845. } else {
  846. tPos2 = strstr( tPos + 2,"/");
  847. LOG_ERROR("Pos 2 - err");
  848. tIpBuf[0] = (char)0;
  849. };
  850. };
  851. } else {
  852. LOG_ERROR("Pos 0 - err");
  853. tIpBuf[0] = (char)0;
  854. };
  855. if ( tPort ) {
  856. 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",
  857. tPtr->RefID,
  858. //tPtr->XAddrs,
  859. tPath,
  860. tIpBuf,
  861. tPort,
  862. tPtr->MAC,
  863. tPtr->Hardware,
  864. tPtr->Name,
  865. tProto,
  866. //tPtr->XAddrs,
  867. tIpBuf,
  868. tPort
  869. );
  870. } else {
  871. 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'",
  872. tPtr->RefID,
  873. //tPtr->XAddrs,
  874. tPath,
  875. tIpBuf,
  876. tPtr->MAC,
  877. tPtr->Hardware,
  878. tPtr->Name,
  879. tProto,
  880. //tPtr->XAddrs,
  881. tIpBuf
  882. );
  883. }
  884. #ifdef DEBUG
  885. LOG(tQBuf);
  886. #endif
  887. m_pDataStorage->SilentQuery( tQBuf );
  888. return true;
  889. };
  890. return false;
  891. };
  892. uint32_t sqliteDBC::addLocalCam( CSTR name, CSTR hardware, CSTR path, uint32_t width, uint32_t height, uint16_t fps, uint16_t den ){
  893. uint32_t RetVal = 0;
  894. if( m_bStoIsConnected ) {
  895. char tQBuf[1024];
  896. char tM5[33];
  897. CSTR tMD5 = MD5String( path );
  898. if( tMD5 != NULL ) {
  899. memcpy( tM5, tMD5, 33 );
  900. tM5[32]=(char)0;
  901. free( tMD5 );
  902. 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",
  903. tM5,
  904. hardware,
  905. name,
  906. path
  907. );
  908. #ifdef DEBUG_ALL
  909. LOG(tQBuf);
  910. #endif
  911. uint32_t camID = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  912. if ( camID == 0 ) {
  913. camID = getCamId( tM5 );
  914. };
  915. 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",
  916. camID, name, tM5, width, height, path, fps, width, height, fps );
  917. #ifdef DEBUG_EXTRA
  918. LOG(tQBuf);
  919. #endif
  920. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  921. if( RetVal == 0 ){
  922. RetVal = getMediaStreamID( camID, tM5 );
  923. };
  924. };
  925. };
  926. return RetVal;
  927. };
  928. uint32_t sqliteDBC::addLocalNetwork( uint32_t NIF_ID, NETWORK_TYPE net_type, CSTR address, CSTR mask, CSTR gateway, CSTR broadcast ){
  929. uint32_t RetVal = 0;
  930. if( m_bStoIsConnected ) {
  931. char tQBuf[MAX_PATH_LEN];
  932. 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'",
  933. NIF_ID,
  934. net_type,
  935. address,
  936. gateway,
  937. mask,
  938. broadcast,
  939. address,
  940. gateway,
  941. mask,
  942. broadcast
  943. );
  944. #ifdef DEBUG_ALL
  945. LOG(tQBuf);
  946. #endif
  947. RetVal = m_pDataStorage->QueryID( tQBuf );
  948. // On Update IF data no need to return ID
  949. //if( RetVal == 0 ) {
  950. // RetVal = getNetworkInterfaceID( tKey );
  951. //};
  952. };
  953. return RetVal;
  954. };
  955. uint32_t sqliteDBC::addLocalNetworkInfrastructure( uint32_t NIF_ID, uint32_t OUTBOUND_NIF, bool ena_dhcp, CSTR BRANG, CSTR ERANG, CSTR MASK, CSTR LEASE_TIME ){
  956. uint32_t RetVal = 0;
  957. if( m_bStoIsConnected ) {
  958. char tQBuf[MAX_PATH_LEN];
  959. bool ena_routing = true;
  960. if( OUTBOUND_NIF == 0 ){
  961. ena_routing = false;
  962. };
  963. if( ena_dhcp ){
  964. if ( BRANG == NULL || ERANG == NULL || MASK == NULL || LEASE_TIME == NULL ) {
  965. LOG_ERROR("Wrong DHCP parameters to create Network Infrastructure!");
  966. return 0;
  967. };
  968. 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'",
  969. NIF_ID,
  970. ena_routing,
  971. OUTBOUND_NIF,
  972. BRANG,
  973. ERANG,
  974. MASK,
  975. LEASE_TIME,
  976. ena_routing,
  977. OUTBOUND_NIF,
  978. BRANG,
  979. ERANG,
  980. MASK,
  981. LEASE_TIME
  982. );
  983. } else {
  984. if( ena_routing ){
  985. 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",
  986. NIF_ID,
  987. OUTBOUND_NIF,
  988. OUTBOUND_NIF
  989. );
  990. } else {
  991. LOG_ERROR("No Special Service Defined To Network Infrastructure!");
  992. return 0;
  993. };
  994. };
  995. #ifdef DEBUG_ALL
  996. LOG(tQBuf);
  997. #endif
  998. RetVal = m_pDataStorage->QueryID( tQBuf );
  999. // On Update IF data no need to return ID
  1000. if( RetVal == 0 ) {
  1001. RetVal = getNetworkInfrastructureID( NIF_ID );
  1002. };
  1003. };
  1004. return RetVal;
  1005. };
  1006. uint32_t sqliteDBC::addLocalNIF( CSTR device, CSTR mac, NIF_TYPE dtype, NIF_MODE dmode, uint32_t aux_id ){
  1007. uint32_t RetVal = 0;
  1008. if( m_bStoIsConnected ) {
  1009. char tQBuf[MAX_PATH_LEN];
  1010. char * tKey = MD5String( device );
  1011. if( tKey != NULL ) {
  1012. 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'",
  1013. dtype,
  1014. tKey,
  1015. device,
  1016. mac,
  1017. dmode,
  1018. aux_id,
  1019. dmode,
  1020. aux_id,
  1021. mac
  1022. );
  1023. #ifdef DEBUG_ALL
  1024. LOG(tQBuf);
  1025. #endif
  1026. RetVal = m_pDataStorage->QueryID( tQBuf );
  1027. if( RetVal == 0 ) {
  1028. RetVal = getNIF_ID( tKey );
  1029. };
  1030. free( tKey );
  1031. };
  1032. };
  1033. return RetVal;
  1034. };
  1035. 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 ){
  1036. uint32_t RetVal = 0;
  1037. if( m_bStoIsConnected ) {
  1038. char tQBuf[MAX_PATH_LEN];
  1039. 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`",
  1040. m_nServerNodeID,
  1041. utype,
  1042. speed,
  1043. bit,
  1044. parity,
  1045. stop_bit,
  1046. mode,
  1047. port,
  1048. PATH
  1049. );
  1050. #ifdef DEBUG_ALL
  1051. LOG(tQBuf);
  1052. #endif
  1053. RetVal = m_pDataStorage->QueryID( tQBuf );
  1054. if( RetVal == 0 ) {
  1055. RetVal = getUartID( m_nServerNodeID, utype, PATH );
  1056. };
  1057. };
  1058. return RetVal;
  1059. };
  1060. uint32_t sqliteDBC::addLocalWiFi( uint32_t NIF_ID, WIFI_MODE mode, CSTR SSID, CSTR password, WIFI_ENCRIPTION encryption, uint32_t router_id ){
  1061. uint32_t RetVal = 0;
  1062. if( m_bStoIsConnected ) {
  1063. char tQBuf[MAX_PATH_LEN];
  1064. 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",
  1065. NIF_ID,
  1066. mode,
  1067. router_id,
  1068. SSID,
  1069. password,
  1070. encryption,
  1071. mode,
  1072. router_id,
  1073. SSID,
  1074. password,
  1075. encryption
  1076. );
  1077. #ifdef DEBUG_ALL
  1078. LOG(tQBuf);
  1079. #endif
  1080. RetVal = m_pDataStorage->QueryID( tQBuf );
  1081. if( RetVal == 0 ) {
  1082. RetVal = getWiFi_ID( NIF_ID );
  1083. };
  1084. };
  1085. return RetVal;
  1086. };
  1087. bool sqliteDBC::cleanLRFMask( uint32_t maskID, bool deleteMask ){
  1088. if( m_bStoIsConnected ) {
  1089. char tQBuf[MAX_PATH_LEN];
  1090. //sprintf( tQBuf, "DELETE FROM `lrf_mask_trace_zones` WHERE `zone_id` IN ( SELECT `id` FROM `lrf_mask_traces` WHERE `mask_id`=%d )", maskID );
  1091. #ifdef DEBUG_EXTRA
  1092. //LOG(tQBuf);
  1093. #endif
  1094. //m_pDataStorage->SilentQuery( tQBuf );
  1095. //sprintf( tQBuf, "DELETE FROM `lrf_mask_traces` WHERE `mask_id`=%d", maskID );
  1096. #ifdef DEBUG_EXTRA
  1097. //LOG(tQBuf);
  1098. #endif
  1099. //m_pDataStorage->SilentQuery( tQBuf );
  1100. sprintf( tQBuf, "DELETE FROM `lrf_mask_vertex_points` WHERE `vertex_id` IN ( SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d )", maskID );
  1101. #ifdef DEBUG
  1102. LOG(tQBuf);
  1103. #endif
  1104. m_pDataStorage->SilentQuery( tQBuf );
  1105. sprintf( tQBuf, "DELETE FROM `lrf_mask_vertexes` WHERE `mask_id`=%d", maskID );
  1106. #ifdef DEBUG
  1107. LOG(tQBuf);
  1108. #endif
  1109. m_pDataStorage->SilentQuery( tQBuf );
  1110. //Delete Mask Itself!
  1111. if ( deleteMask ) {
  1112. sprintf( tQBuf, "DELETE FROM `lrf_masks` WHERE `id`=%d", maskID );
  1113. #ifdef DEBUG_EXTRA
  1114. LOG(tQBuf);
  1115. #endif
  1116. m_pDataStorage->SilentQuery( tQBuf );
  1117. };
  1118. return true;
  1119. };
  1120. return false;
  1121. };
  1122. bool sqliteDBC::cleanLRFWidthTable( uint32_t sensorID ){
  1123. if( m_bStoIsConnected ) {
  1124. char tQBuf[MAX_PATH_LEN];
  1125. sprintf( tQBuf, "DELETE FROM `lrf_detection_width_tables` WHERE `aux_id`=%d", sensorID );
  1126. #ifdef DEBUG_EXTRA
  1127. LOG(tQBuf);
  1128. #endif
  1129. m_pDataStorage->SilentQuery( tQBuf );
  1130. return true;
  1131. };
  1132. return false;
  1133. };
  1134. bool sqliteDBC::cleanResources( bool all ){
  1135. if( m_bStoIsConnected ) {
  1136. char tQBuf[MAX_PATH_LEN];
  1137. if( all ) {
  1138. sprintf( tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id`=%d", m_nServerNodeID );
  1139. } else {
  1140. 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 );
  1141. };
  1142. #ifdef DEBUG_ALL
  1143. LOG(tQBuf);
  1144. #endif
  1145. m_pDataStorage->SilentQuery( tQBuf );
  1146. return true;
  1147. };
  1148. return false;
  1149. };
  1150. bool sqliteDBC::wipeServerResources(){
  1151. if( m_bStoIsConnected ) {
  1152. char tQBuf[MAX_PATH_LEN];
  1153. sprintf( tQBuf, "DELETE FROM `resources` WHERE `node_id`=%d", m_nServerNodeID );
  1154. #ifdef DEBUG_ALL
  1155. LOG(tQBuf);
  1156. #endif
  1157. m_pDataStorage->SilentQuery( tQBuf );
  1158. return true;
  1159. };
  1160. return false;
  1161. };
  1162. uint32_t sqliteDBC::addLRFMask( uint32_t devID, uint8_t lrfID, uint8_t layerID ){
  1163. uint32_t RetVal = 0;
  1164. if( m_bStoIsConnected ) {
  1165. char tQBuf[MAX_PATH_LEN];
  1166. 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",
  1167. devID,
  1168. lrfID,
  1169. layerID,
  1170. layerID
  1171. );
  1172. #ifdef DEBUG
  1173. LOG(tQBuf);
  1174. #endif
  1175. RetVal = m_pDataStorage->QueryID( tQBuf );
  1176. if( RetVal == 0 ) {
  1177. RetVal = getLRFMaskID( devID, lrfID, layerID );
  1178. };
  1179. };
  1180. return RetVal;
  1181. };
  1182. uint32_t sqliteDBC::addLRFMaskPolygone( uint32_t maskID, uint8_t vertexID, uint8_t r, uint8_t g, uint8_t b ){
  1183. uint32_t RetVal = 0;
  1184. if( m_bStoIsConnected ) {
  1185. char tQBuf[MAX_PATH_LEN];
  1186. sprintf( tQBuf, "INSERT INTO `lrf_mask_vertexes`(`mask_id`,`vertex_id`,`r`,`g`,`b`) VALUES(%d,%d,%d,%d,%d)",
  1187. maskID,
  1188. vertexID,
  1189. r,
  1190. g,
  1191. b
  1192. );
  1193. #ifdef DEBUG
  1194. LOG(tQBuf);
  1195. #endif
  1196. RetVal = m_pDataStorage->QueryID( tQBuf );
  1197. if( RetVal == 0 ) {
  1198. RetVal = getLRFMaskPolygoneID( maskID, vertexID );
  1199. };
  1200. };
  1201. return RetVal;
  1202. };
  1203. bool sqliteDBC::addLRFMaskPolygonePoint( uint32_t polyID, int16_t x, int16_t y ){
  1204. if( m_bStoIsConnected ) {
  1205. char tQBuf[MAX_PATH_LEN];
  1206. sprintf( tQBuf, "INSERT INTO `lrf_mask_vertex_points`(`vertex_id`,`x`,`y`) VALUES(%d,%d,%d)",
  1207. polyID,
  1208. x,
  1209. y
  1210. );
  1211. #ifdef DEBUG
  1212. LOG(tQBuf);
  1213. #endif
  1214. m_pDataStorage->SilentQuery( tQBuf );
  1215. return true;
  1216. };
  1217. return false;
  1218. };
  1219. bool sqliteDBC::addLRFWidthTable( uint32_t sensorID, PWIDTH_TABLE ptr ){
  1220. if( m_bStoIsConnected ) {
  1221. char tQBuf[MAX_PATH_LEN];
  1222. float kK = 100.0f / ptr->nCount;
  1223. for( int i = 0; i < ptr->nCount; i++ ) {
  1224. uint16_t CC = kK * i;
  1225. sprintf(tQBuf, "INSERT INTO `lrf_detection_width_tables`(`aux_id`,`width_k`,`range_k`) VALUES(%d,%d,%d)",
  1226. sensorID,
  1227. ptr->pTable[i],
  1228. CC
  1229. );
  1230. #ifdef DEBUG
  1231. LOG(tQBuf);
  1232. #endif
  1233. m_pDataStorage->SilentQuery( tQBuf );
  1234. };
  1235. return true;
  1236. };
  1237. return false;
  1238. };
  1239. uint32_t sqliteDBC::addResource( uint8_t type, CSTR devName, uint32_t auxID ){
  1240. uint32_t RetVal = 0;
  1241. if( m_bStoIsConnected ){
  1242. char tQBuf[2048];
  1243. sprintf( tQBuf, "%d:%d:%s", m_nServerNodeID, type, devName );
  1244. CSTR tMD5 = MD5String( tQBuf );
  1245. if( tMD5 ) {
  1246. 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",
  1247. m_nServerNodeID,
  1248. type,
  1249. tMD5,
  1250. auxID,
  1251. devName,
  1252. auxID
  1253. );
  1254. free( tMD5 );
  1255. #ifdef DEBUG_ALL
  1256. LOG(tQBuf);
  1257. #endif
  1258. RetVal = (uint32_t) m_pDataStorage->QueryID( tQBuf );
  1259. if( RetVal == 0 ) {
  1260. RetVal = getResourceID( m_nServerNodeID, type, auxID );
  1261. };
  1262. };
  1263. };
  1264. return RetVal;
  1265. };
  1266. bool sqliteDBC::addVideoEvent( uint32_t evtID, uint32_t videoID, uint32_t frame, uint32_t tss, uint32_t tms ){
  1267. if( m_bRepIsConnected ){
  1268. char tQBuf[2048];
  1269. sprintf( tQBuf, "INSERT INTO `events_2_videos`(`evt_id`,`video_id`,`frame`,`tss`,`tms`) VALUES(%d,%d,%d,%d,%d)",
  1270. evtID,
  1271. videoID,
  1272. frame,
  1273. tss,
  1274. tms
  1275. );
  1276. #ifdef DEBUG_EXTRA
  1277. LOG(tQBuf);
  1278. #endif
  1279. m_pDataReports->SilentQuery( tQBuf );
  1280. };
  1281. return false;
  1282. };
  1283. uint32_t sqliteDBC::addVideoRecord( uint32_t streamID, CSTR md5, CSTR ext, uint32_t tss, uint32_t tms, uint32_t fps ){
  1284. uint32_t RetVal = 0;
  1285. if( m_bRepIsConnected ){
  1286. updateStats( false, true );
  1287. char tQBuf[2048];
  1288. sprintf( tQBuf, "INSERT INTO `videos`(`media_stream_id`,`md5`,`uts`,`uts_ms`,`ext`,`fps`) VALUES(%d,'%s',%d,%d,'%s',%d)",
  1289. streamID,
  1290. md5,
  1291. tss,
  1292. tms,
  1293. ext,
  1294. fps
  1295. );
  1296. #ifdef DEBUG_EXTRA
  1297. LOG(tQBuf);
  1298. #endif
  1299. RetVal = (uint32_t) m_pDataReports->QueryID( tQBuf );
  1300. };
  1301. return RetVal;
  1302. };
  1303. DBC_MODULE_EMBED_VIDEO_DATA *sqliteDBC::getEmbedVideoData( uint32_t ID ){
  1304. DBC_MODULE_EMBED_VIDEO_DATA *tPtr = NULL;
  1305. if( m_bStoIsConnected ) {
  1306. char tQBuf[MAX_PATH_LEN];
  1307. AAssoc *result;
  1308. sprintf( tQBuf, "SELECT * FROM `module_eve` WHERE `id`=%d", ID );
  1309. #ifdef DEBUG_ALL
  1310. LOG(tQBuf);
  1311. #endif
  1312. result = m_pDataStorage->Query( tQBuf );
  1313. if( result!= NULL ) {
  1314. if( result->size() > 0 ) {
  1315. Assoc *cArr;
  1316. tPtr = (DBC_MODULE_EMBED_VIDEO_DATA*)malloc(sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  1317. if( tPtr!= NULL ) {
  1318. Assoc::iterator it;
  1319. memset(tPtr,0,sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  1320. cArr = result->at(0);
  1321. it = cArr->find("output_mode");
  1322. if( it != cArr->end() ){
  1323. tPtr->output_mode = atoi( it->second.c_str() );
  1324. };
  1325. it = cArr->find("web_port");
  1326. if( it != cArr->end() ){
  1327. tPtr->port = atoi( it->second.c_str() );
  1328. };
  1329. it = cArr->find("width");
  1330. if( it != cArr->end() ){
  1331. tPtr->width = atoi( it->second.c_str() );
  1332. };
  1333. it = cArr->find("height");
  1334. if( it != cArr->end() ){
  1335. tPtr->height = atoi( it->second.c_str() );
  1336. };
  1337. it = cArr->find("compression_quality");
  1338. if( it != cArr->end() ){
  1339. tPtr->compression_quality = atoi( it->second.c_str() );
  1340. };
  1341. it = cArr->find("show_ts");
  1342. if( it != cArr->end() ){
  1343. tPtr->show_ts = (bool)atoi( it->second.c_str() );
  1344. };
  1345. it = cArr->find("ts_x");
  1346. if( it != cArr->end() ){
  1347. tPtr->ts_x = atoi( it->second.c_str() );
  1348. };
  1349. it = cArr->find("ts_y");
  1350. if( it != cArr->end() ){
  1351. tPtr->ts_y = atoi( it->second.c_str() );
  1352. };
  1353. it = cArr->find("ts_font_face");
  1354. if( it != cArr->end() ){
  1355. tPtr->ts_font_face = atoi( it->second.c_str() );
  1356. };
  1357. it = cArr->find("ts_font_scale");
  1358. if( it != cArr->end() ){
  1359. tPtr->ts_font_scale = atof( it->second.c_str() );
  1360. };
  1361. it = cArr->find("ts_font_thickness");
  1362. if( it != cArr->end() ){
  1363. tPtr->ts_font_thickness = atoi( it->second.c_str() );
  1364. };
  1365. it = cArr->find("ts_line_type");
  1366. if( it != cArr->end() ){
  1367. tPtr->ts_line_type = atoi( it->second.c_str() );
  1368. };
  1369. it = cArr->find("ts_transperant");
  1370. if( it != cArr->end() ){
  1371. tPtr->ts_transperant = (bool)atoi( it->second.c_str() );
  1372. };
  1373. it = cArr->find("show_label");
  1374. if( it != cArr->end() ){
  1375. tPtr->show_label = (bool)atoi( it->second.c_str() );
  1376. };
  1377. it = cArr->find("label_x");
  1378. if( it != cArr->end() ){
  1379. tPtr->label_x = atoi( it->second.c_str() );
  1380. };
  1381. it = cArr->find("label_y");
  1382. if( it != cArr->end() ){
  1383. tPtr->label_y = atoi( it->second.c_str() );
  1384. };
  1385. it = cArr->find("label_font_face");
  1386. if( it != cArr->end() ){
  1387. tPtr->label_font_face = atoi( it->second.c_str() );
  1388. };
  1389. it = cArr->find("label_font_scale");
  1390. if( it != cArr->end() ){
  1391. tPtr->label_font_scale = atof( it->second.c_str() );
  1392. };
  1393. it = cArr->find("label_font_thickness");
  1394. if( it != cArr->end() ){
  1395. tPtr->label_font_thickness = atoi( it->second.c_str() );
  1396. };
  1397. it = cArr->find("label_line_type");
  1398. if( it != cArr->end() ){
  1399. tPtr->label_line_type = atoi( it->second.c_str() );
  1400. };
  1401. it = cArr->find("label_transperant");
  1402. if( it != cArr->end() ){
  1403. tPtr->label_transperant = (bool) atoi( it->second.c_str() );
  1404. };
  1405. it = cArr->find("show_logo");
  1406. if( it != cArr->end() ){
  1407. tPtr->show_logo = (bool) atoi( it->second.c_str() );
  1408. };
  1409. it = cArr->find("logo_w");
  1410. if( it != cArr->end() ){
  1411. tPtr->logo_w = atoi( it->second.c_str() );
  1412. };
  1413. it = cArr->find("logo_h");
  1414. if( it != cArr->end() ){
  1415. tPtr->logo_h = atoi( it->second.c_str() );
  1416. };
  1417. it = cArr->find("logo_x");
  1418. if( it != cArr->end() ){
  1419. tPtr->logo_x = atoi( it->second.c_str() );
  1420. };
  1421. it = cArr->find("logo_y");
  1422. if( it != cArr->end() ){
  1423. tPtr->logo_y = atoi( it->second.c_str() );
  1424. };
  1425. it = cArr->find("logo_md5");
  1426. if( it != cArr->end() ){
  1427. strncpy( tPtr->logo_md5, it->second.c_str(),32);
  1428. tPtr->logo_md5[32]=(char)0;
  1429. };
  1430. };
  1431. };
  1432. };
  1433. deleteResult( result );
  1434. };
  1435. return tPtr;
  1436. };
  1437. DBC_MEDIA_STREAM_DATA *sqliteDBC::getMediaStreamData(uint32_t ID){
  1438. DBC_MEDIA_STREAM_DATA *tVal = NULL;
  1439. if( m_bStoIsConnected ){
  1440. char tQBuf[CTEXT];
  1441. AAssoc *result;
  1442. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID );
  1443. #ifdef DEBUG_ALL
  1444. LOG(tQBuf);
  1445. #endif
  1446. result = m_pDataStorage->Query( tQBuf );
  1447. if( result!= NULL ) {
  1448. if( result->size() > 0 ) {
  1449. Assoc *cArr;
  1450. tVal = (DBC_MEDIA_STREAM_DATA*) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  1451. if( tVal != NULL ) {
  1452. memset( tVal, 0, sizeof(DBC_MEDIA_STREAM_DATA) );
  1453. Assoc::iterator it;
  1454. cArr = result->at(0);
  1455. tVal->id = ID;
  1456. it = cArr->find("source_id");
  1457. if( it != cArr->end() ){
  1458. tVal->source_id = atoi( it->second.c_str() );
  1459. };
  1460. it = cArr->find("source_type");
  1461. if( it != cArr->end() ){
  1462. sprintf( tVal->source_type, it->second.c_str() );
  1463. };
  1464. it = cArr->find("valid");
  1465. if( it != cArr->end() ){
  1466. tVal->valid = (bool) atoi( it->second.c_str() );
  1467. };
  1468. it = cArr->find("token");
  1469. if( it != cArr->end() ){
  1470. sprintf( tVal->token, it->second.c_str() );
  1471. };
  1472. it = cArr->find("img_width");
  1473. if( it != cArr->end() ){
  1474. tVal->width = atoi( it->second.c_str() );
  1475. };
  1476. it = cArr->find("img_height");
  1477. if( it != cArr->end() ){
  1478. tVal->height = atoi( it->second.c_str() );
  1479. };
  1480. it = cArr->find("fps");
  1481. if( it != cArr->end() ){
  1482. tVal->fps = atoi( it->second.c_str() );
  1483. };
  1484. it = cArr->find("compression_type");
  1485. if( it != cArr->end() ){
  1486. sprintf(tVal->c_type, it->second.c_str());
  1487. };
  1488. it = cArr->find("mrl_proto");
  1489. if( it != cArr->end() ){
  1490. sprintf( tVal->mrl_proto, it->second.c_str() );
  1491. };
  1492. it = cArr->find("mrl_port");
  1493. if( it != cArr->end() ){
  1494. tVal->mrl_port = atoi( it->second.c_str() );
  1495. };
  1496. it = cArr->find("mrl");
  1497. if( it != cArr->end() ){
  1498. sprintf( tVal->mrl, it->second.c_str() );
  1499. };
  1500. };
  1501. };
  1502. };
  1503. deleteResult( result );
  1504. };
  1505. return tVal;
  1506. };
  1507. uint32_t sqliteDBC::getMediaStreamID( uint32_t source_id, CSTR token ){
  1508. uint32_t RetVal = 0;
  1509. if( m_bStoIsConnected ){
  1510. char tQBuf[CTEXT];
  1511. AAssoc *result;
  1512. sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token );
  1513. #ifdef DEBUG_ALL
  1514. LOG(tQBuf);
  1515. #endif
  1516. result = m_pDataStorage->Query( tQBuf );
  1517. if( result!= NULL ) {
  1518. if( result->size() > 0 ) {
  1519. Assoc *cArr;
  1520. Assoc::iterator it;
  1521. cArr = result->at(0);
  1522. it = cArr->find("id");
  1523. if( it != cArr->end() ){
  1524. RetVal = atoi( it->second.c_str() );
  1525. };
  1526. };
  1527. };
  1528. deleteResult( result );
  1529. };
  1530. return RetVal;
  1531. };
  1532. DBC_MODULE_DATA *sqliteDBC::getModuleData( uint32_t ID ){
  1533. DBC_MODULE_DATA *tVal = NULL;
  1534. if( m_bStoIsConnected ){
  1535. char tQBuf[CTEXT];
  1536. AAssoc *result;
  1537. sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID );
  1538. #ifdef DEBUG_ALL
  1539. LOG(tQBuf);
  1540. #endif
  1541. result = m_pDataStorage->Query( tQBuf );
  1542. if( result!= NULL ) {
  1543. if( result->size() > 0 ) {
  1544. Assoc *cArr;
  1545. tVal = (DBC_MODULE_DATA*) malloc(sizeof(DBC_MODULE_DATA));
  1546. if( tVal != NULL ) {
  1547. Assoc::iterator it;
  1548. cArr = result->at(0);
  1549. it = cArr->find("id");
  1550. if( it != cArr->end() ){
  1551. tVal->module_id = atoi( it->second.c_str() );
  1552. };
  1553. it = cArr->find("aux_id");
  1554. if( it != cArr->end() ){
  1555. tVal->aux_id = atoi( it->second.c_str() );
  1556. };
  1557. it = cArr->find("type");
  1558. if( it != cArr->end() ){
  1559. tVal->type = atoi( it->second.c_str() );
  1560. };
  1561. it = cArr->find("enabled");
  1562. if( it != cArr->end() ){
  1563. tVal->enabled = (bool)atoi( it->second.c_str() );
  1564. };
  1565. };
  1566. };
  1567. };
  1568. deleteResult( result );
  1569. };
  1570. return tVal;
  1571. };
  1572. DBC_LRF_INI *sqliteDBC::getINIData( uint32_t lrfID ){
  1573. DBC_LRF_INI * tVal = NULL;
  1574. if( m_bStoIsConnected ){
  1575. char tQBuf[CTEXT];
  1576. AAssoc *result;
  1577. sprintf(tQBuf, "SELECT * FROM `lrf_ini_settings` WHERE `sensor_id`=%d", lrfID );
  1578. #ifdef DEBUG
  1579. LOG(tQBuf);
  1580. #endif
  1581. result = m_pDataStorage->Query( tQBuf );
  1582. if( result!= NULL ) {
  1583. if( result->size() > 0 ) {
  1584. Assoc *cArr;
  1585. tVal = (DBC_LRF_INI*) malloc(sizeof(DBC_LRF_INI));
  1586. if( tVal != NULL ) {
  1587. memset( tVal, 0 , sizeof(DBC_LRF_INI));
  1588. Assoc::iterator it;
  1589. cArr = result->at(0);
  1590. it = cArr->find("AUTORESET_ENABLE");
  1591. if( it != cArr->end() ){
  1592. tVal->_AUTO_RESET_ENABLE = (bool) atoi( it->second.c_str() );
  1593. };
  1594. it = cArr->find("AUTORESET_INTERLEAVE");
  1595. if( it != cArr->end() ){
  1596. tVal->_AUTO_RESET_INTERLEAVE = atoi( it->second.c_str() );
  1597. };
  1598. it = cArr->find("AUTORESET_TIMEOUT");
  1599. if( it != cArr->end() ){
  1600. tVal->_AUTO_RESET_TIMEOUT = atoi( it->second.c_str() );
  1601. };
  1602. it = cArr->find("BOARD_TEMPERATURE");
  1603. if( it != cArr->end() ){
  1604. tVal->_BOARD_TEMPERATURE = atoi( it->second.c_str() );
  1605. };
  1606. it = cArr->find("BOARD_ZEROSHIFT");
  1607. if( it != cArr->end() ){
  1608. tVal->_BOARD_ZEROSHIFT = atoi( it->second.c_str() );
  1609. };
  1610. it = cArr->find("BOARD_SPEED");
  1611. if( it != cArr->end() ){
  1612. tVal->_BOARD_SPEED = atoi( it->second.c_str() );
  1613. };
  1614. it = cArr->find("RUN_DIR");
  1615. if( it != cArr->end() ){
  1616. tVal->_RUN_DIR = atoi( it->second.c_str() );
  1617. };
  1618. it = cArr->find("JUST_MOVING_TARGETS");
  1619. if( it != cArr->end() ){
  1620. tVal->_JUST_MOVING_TARGETS = (bool) atoi( it->second.c_str() );
  1621. };
  1622. it = cArr->find("JUST_MOVING_RADIAL");
  1623. if( it != cArr->end() ){
  1624. tVal->_JUST_MOVING_RADIAL = (bool) atoi( it->second.c_str() );
  1625. };
  1626. it = cArr->find("JUST_MOVING_DISTANCE");
  1627. if( it != cArr->end() ){
  1628. tVal->_JUST_MOVING_DISTANCE = (bool) atoi( it->second.c_str() );
  1629. };
  1630. it = cArr->find("FREEZ_FIRST_TH");
  1631. if( it != cArr->end() ){
  1632. tVal->_FREEZ_FIRST_TH = (bool) atoi( it->second.c_str() );
  1633. };
  1634. it = cArr->find("SAVE_FIRST_TH");
  1635. if( it != cArr->end() ){
  1636. tVal->_SAVE_FIRST_TH = (bool) atoi( it->second.c_str() );
  1637. };
  1638. };
  1639. };
  1640. };
  1641. deleteResult( result );
  1642. };
  1643. return tVal;
  1644. };
  1645. bool sqliteDBC::getEventSessionStatus( uint32_t nodeID ){
  1646. bool rv = false;
  1647. if( m_bRepIsConnected ){
  1648. char tQBuf[CTEXT];
  1649. AAssoc *result;
  1650. //Since Unimodule -> no need to check NodeID
  1651. //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 );
  1652. 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 );
  1653. #ifdef DEBUG_ALL
  1654. LOG(tQBuf);
  1655. #endif
  1656. result = m_pDataReports->Query( tQBuf );
  1657. if( result!= NULL ) {
  1658. if( result->size() > 0 ) {
  1659. Assoc *cArr;
  1660. Assoc::iterator it;
  1661. cArr = result->at(0);
  1662. it = cArr->find("evt_type");
  1663. if( it != cArr->end() ){
  1664. if( atoi( it->second.c_str() ) == EVT_LRF_START_SESSION ) rv = true;
  1665. };
  1666. };
  1667. };
  1668. deleteResult( result );
  1669. };
  1670. return rv;
  1671. };
  1672. /*
  1673. * The Last TS is from the current report (as latest)
  1674. */
  1675. uint32_t sqliteDBC::getLastEventTS(){
  1676. uint32_t rv = 0;
  1677. if( m_bRepIsConnected ){
  1678. char tQBuf[CTEXT];
  1679. AAssoc *result;
  1680. sprintf( tQBuf, "SELECT `ts` FROM `events` ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1" );
  1681. #ifdef DEBUG_ALL
  1682. LOG(tQBuf);
  1683. #endif
  1684. result = m_pDataReports->Query( tQBuf );
  1685. if( result!= NULL ) {
  1686. if( result->size() > 0 ) {
  1687. Assoc *cArr;
  1688. Assoc::iterator it;
  1689. cArr = result->at(0);
  1690. it = cArr->find("ts");
  1691. if( it != cArr->end() ){
  1692. rv = atoi( it->second.c_str() );
  1693. };
  1694. };
  1695. };
  1696. deleteResult( result );
  1697. };
  1698. return rv;
  1699. };
  1700. /*
  1701. * The first TS is generaly must show a DAY of first registered event so TS fetched
  1702. * from general statistics
  1703. */
  1704. uint32_t sqliteDBC::getFirstEventTS(){
  1705. uint32_t rv = 0;
  1706. if( m_bStaIsConnected ){
  1707. char tQBuf[CTEXT];
  1708. AAssoc *result;
  1709. sprintf( tQBuf, "SELECT `year`,`month`,`day` FROM `days` ORDER BY `year`,`month`,`day` LIMIT 1" );
  1710. #ifdef DEBUG_ALL
  1711. LOG(tQBuf);
  1712. #endif
  1713. result = m_pDataStatistics->Query( tQBuf );
  1714. if( result!= NULL ) {
  1715. if( result->size() > 0 ) {
  1716. REPORT_DIRINFO RDF = { 0 };
  1717. Assoc *cArr;
  1718. Assoc::iterator it;
  1719. cArr = result->at(0);
  1720. it = cArr->find("year");
  1721. if( it != cArr->end() ){
  1722. strncpy( RDF.YEAR, it->second.c_str(), 4 );
  1723. };
  1724. it = cArr->find("month");
  1725. if( it != cArr->end() ){
  1726. strncpy( RDF.MONTH, it->second.c_str(), 2 );
  1727. };
  1728. it = cArr->find("day");
  1729. if( it != cArr->end() ){
  1730. strncpy( RDF.DAY, it->second.c_str(), 2 );
  1731. };
  1732. rv = getTsFromRDF( RDF );
  1733. };
  1734. };
  1735. deleteResult( result );
  1736. };
  1737. return rv;
  1738. };
  1739. bool sqliteDBC::deleteReportDate( uint32_t ts ){
  1740. if( ts ){
  1741. REPORT_DIRINFO tRDF = {0};
  1742. fillRDF( ts, tRDF );
  1743. LOG("Delete RDF Dir");
  1744. if( deleteRDFDir( m_pDbReportBase, tRDF ) ){
  1745. LOG("Dir Deleted OK");
  1746. return deleteReportStat( tRDF );
  1747. };
  1748. };
  1749. return false;
  1750. };
  1751. bool sqliteDBC::deleteRDFDir( CSTR Base, REPORT_DIRINFO &RDF ){
  1752. char tBuff[MAX_PATH];
  1753. sprintf( tBuff, "%s/%s/%s/%s", Base, RDF.YEAR, RDF.MONTH, RDF.DAY );
  1754. // No Checking if dir is empty - dir contents all we need to delete!
  1755. if( fileExist( tBuff ) ){
  1756. sprintf( tBuff, "rm -f -R \"%s/%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH, RDF.DAY );
  1757. system( tBuff );
  1758. };
  1759. sprintf( tBuff, "%s/%s/%s", Base, RDF.YEAR, RDF.MONTH );
  1760. if( fileExist( tBuff ) ){
  1761. if( dirEmpty( tBuff ) ){
  1762. sprintf( tBuff, "rm -f -R \"%s/%s/%s\"", Base, RDF.YEAR, RDF.MONTH );
  1763. system( tBuff );
  1764. };
  1765. };
  1766. sprintf( tBuff, "%s/%s", Base, RDF.YEAR );
  1767. if( fileExist( tBuff ) ){
  1768. if( dirEmpty( tBuff ) ){
  1769. sprintf( tBuff, "rm -f -R \"%s/%s\"", Base, RDF.YEAR );
  1770. system( tBuff );
  1771. };
  1772. };
  1773. return true;
  1774. };
  1775. DBC_LRF_DATA *sqliteDBC::getLRFData( uint32_t ID ){
  1776. DBC_LRF_DATA * tVal = NULL;
  1777. if( m_bStoIsConnected ){
  1778. char tQBuf[CTEXT];
  1779. AAssoc *result;
  1780. sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID );
  1781. #ifdef DEBUG_ALL
  1782. LOG(tQBuf);
  1783. #endif
  1784. result = m_pDataStorage->Query( tQBuf );
  1785. if( result!= NULL ) {
  1786. if( result->size() > 0 ) {
  1787. Assoc *cArr;
  1788. tVal = (DBC_LRF_DATA*) malloc(sizeof(DBC_LRF_DATA));
  1789. if( tVal != NULL ) {
  1790. memset( tVal, 0 , sizeof(DBC_LRF_DATA));
  1791. Assoc::iterator it;
  1792. tVal->id = ID;
  1793. cArr = result->at(0);
  1794. it = cArr->find("data_status");
  1795. if( it != cArr->end() ){
  1796. tVal->data_status = atoi( it->second.c_str() );
  1797. };
  1798. it = cArr->find("serial");
  1799. if( it != cArr->end() ){
  1800. strncpy( tVal->serial, it->second.c_str(), sizeof(tVal->serial)-1 );
  1801. tVal->serial[sizeof(tVal->serial)-1]=(char)0;
  1802. };
  1803. it = cArr->find("version");
  1804. if( it != cArr->end() ){
  1805. strncpy( tVal->version, it->second.c_str(), sizeof(tVal->version)-1 );
  1806. tVal->version[sizeof(tVal->version)-1]=(char)0;
  1807. };
  1808. it = cArr->find("x");
  1809. if( it != cArr->end() ){
  1810. tVal->x = atof( it->second.c_str() );
  1811. };
  1812. it = cArr->find("y");
  1813. if( it != cArr->end() ){
  1814. tVal->y = atof( it->second.c_str() );
  1815. };
  1816. it = cArr->find("z");
  1817. if( it != cArr->end() ){
  1818. tVal->z = atof( it->second.c_str() );
  1819. };
  1820. it = cArr->find("x_rot");
  1821. if( it != cArr->end() ){
  1822. tVal->x_rot = atof( it->second.c_str() );
  1823. };
  1824. it = cArr->find("y_rot");
  1825. if( it != cArr->end() ){
  1826. tVal->y_rot = atof( it->second.c_str() );
  1827. };
  1828. it = cArr->find("z_rot");
  1829. if( it != cArr->end() ){
  1830. tVal->z_rot = atof( it->second.c_str() );
  1831. };
  1832. it = cArr->find("uart_id");
  1833. if( it != cArr->end() ){
  1834. tVal->uart_id = atoi( it->second.c_str() );
  1835. };
  1836. it = cArr->find("device_id");
  1837. if( it != cArr->end() ){
  1838. tVal->device_id = atoi( it->second.c_str() );
  1839. };
  1840. it = cArr->find("ip");
  1841. if( it != cArr->end() ){
  1842. sprintf( tVal->master_host, it->second.c_str() );
  1843. };
  1844. it = cArr->find("aux_ip");
  1845. if( it != cArr->end() ){
  1846. sprintf( tVal->slave_host, it->second.c_str() );
  1847. };
  1848. it = cArr->find("port");
  1849. if( it != cArr->end() ){
  1850. tVal->master_port = atoi( it->second.c_str() );
  1851. };
  1852. it = cArr->find("aux_port");
  1853. if( it != cArr->end() ){
  1854. tVal->slave_port = atoi( it->second.c_str() );
  1855. };
  1856. it = cArr->find("auto_draw");
  1857. if( it != cArr->end() ){
  1858. tVal->auto_draw = atoi( it->second.c_str() );
  1859. };
  1860. it = cArr->find("filter_id");
  1861. if( it != cArr->end() ){
  1862. tVal->filter_id = atoi( it->second.c_str() );
  1863. };
  1864. it = cArr->find("enabled_lrf");
  1865. if( it != cArr->end() ){
  1866. tVal->enabled_lrf = atoi( it->second.c_str() );
  1867. };
  1868. it = cArr->find("max_range");
  1869. if( it != cArr->end() ){
  1870. tVal->max_range = atof( it->second.c_str() );
  1871. };
  1872. it = cArr->find("base_shift");
  1873. if( it != cArr->end() ){
  1874. tVal->base_shift = atoi( it->second.c_str() );
  1875. };
  1876. it = cArr->find("mirror_shift");
  1877. if( it != cArr->end() ){
  1878. tVal->mirror_shift = atoi( it->second.c_str() );
  1879. };
  1880. it = cArr->find("detection_profile_id");
  1881. if( it != cArr->end() ){
  1882. tVal->detection_profile_id = atoi( it->second.c_str() );
  1883. };
  1884. it = cArr->find("draw_lrf_id");
  1885. if( it != cArr->end() ){
  1886. tVal->draw_lrf_id = atoi( it->second.c_str() );
  1887. };
  1888. it = cArr->find("width_table_id");
  1889. if( it != cArr->end() ){
  1890. tVal->width_table_id = atoi( it->second.c_str() );
  1891. };
  1892. it = cArr->find("zoom_table_id");
  1893. if( it != cArr->end() ){
  1894. tVal->zoom_table_id = atoi( it->second.c_str() );
  1895. };
  1896. it = cArr->find("embd_cam");
  1897. if( it != cArr->end() ){
  1898. tVal->embd_cam = atoi( it->second.c_str() );
  1899. };
  1900. it = cArr->find("embd_cam_auto");
  1901. if( it != cArr->end() ){
  1902. tVal->embd_cam_auto = (bool)atoi( it->second.c_str() );
  1903. };
  1904. it = cArr->find("embd_dc");
  1905. if( it != cArr->end() ){
  1906. tVal->embd_dc = atoi( it->second.c_str() );
  1907. };
  1908. it = cArr->find("autoreset");
  1909. if( it != cArr->end() ){
  1910. tVal->autoreset = (bool)atoi( it->second.c_str() );
  1911. };
  1912. it = cArr->find("force_settings");
  1913. if( it != cArr->end() ){
  1914. tVal->force_settings = (bool)atoi( it->second.c_str() );
  1915. };
  1916. };
  1917. };
  1918. };
  1919. deleteResult( result );
  1920. };
  1921. return tVal;
  1922. };
  1923. PLGS_2D_PARAMETERS sqliteDBC::getLRFDetectionProfile( uint32_t lrfID, uint8_t profileID, bool master ){
  1924. PLGS_2D_PARAMETERS retVal = NULL;
  1925. if( m_bStoIsConnected ){
  1926. char tQBuf[CTEXT];
  1927. AAssoc *result;
  1928. sprintf(tQBuf, "SELECT * FROM `lrf_detection_profiles` WHERE `aux_id`=%d AND `profile_id`=%d AND `master`=%d", lrfID, profileID,((master)?1:0) );
  1929. #ifdef DEBUG
  1930. LOG(tQBuf);
  1931. #endif
  1932. result = m_pDataStorage->Query( tQBuf );
  1933. if( result!= NULL ) {
  1934. if( result->size() > 0 ) {
  1935. retVal = (PLGS_2D_PARAMETERS) malloc(sizeof(LGS_2D_PARAMETERS));
  1936. memset( retVal, 0, sizeof(LGS_2D_PARAMETERS));
  1937. Assoc *cArr;
  1938. Assoc::iterator it;
  1939. cArr = result->at(0);
  1940. it = cArr->find("order");
  1941. if( it != cArr->end() ){
  1942. retVal->nOrder = atoi(it->second.c_str());
  1943. };
  1944. it = cArr->find("offset");
  1945. if( it != cArr->end() ){
  1946. retVal->nOffset = atoi(it->second.c_str());
  1947. };
  1948. it = cArr->find("delta");
  1949. if( it != cArr->end() ){
  1950. retVal->nDelta = atoi(it->second.c_str());
  1951. };
  1952. it = cArr->find("flip");
  1953. if( it != cArr->end() ){
  1954. retVal->nFlip = atoi(it->second.c_str());
  1955. };
  1956. it = cArr->find("width");
  1957. if( it != cArr->end() ){
  1958. retVal->nWidth = atoi(it->second.c_str());
  1959. };
  1960. it = cArr->find("sst");
  1961. if( it != cArr->end() ){
  1962. retVal->bSST = atoi(it->second.c_str());
  1963. };
  1964. it = cArr->find("sun");
  1965. if( it != cArr->end() ){
  1966. retVal->nSun = atoi(it->second.c_str());
  1967. };
  1968. };
  1969. };
  1970. deleteResult( result );
  1971. };
  1972. return retVal;
  1973. };
  1974. RELAY_PARAMETERS *sqliteDBC::getLRFDryContact( uint32_t lrfID ){
  1975. RELAY_PARAMETERS *retVal = NULL;
  1976. if( m_bStoIsConnected ){
  1977. char tQBuf[CTEXT];
  1978. AAssoc *result;
  1979. sprintf(tQBuf, "SELECT `dc_auto`, `dc_no`, `dc_window` FROM `lrf_sensors` WHERE `id`=%d", lrfID );
  1980. #ifdef DEBUG
  1981. LOG(tQBuf);
  1982. #endif
  1983. result = m_pDataStorage->Query( tQBuf );
  1984. if( result!= NULL ) {
  1985. if( result->size() > 0 ) {
  1986. retVal = (RELAY_PARAMETERS*) malloc(sizeof(RELAY_PARAMETERS));
  1987. if( retVal ) {
  1988. Assoc *cArr;
  1989. Assoc::iterator it;
  1990. cArr = result->at(0);
  1991. it = cArr->find("dc_auto");
  1992. if( it != cArr->end() ){
  1993. retVal->bAuto = atoi( it->second.c_str() );
  1994. };
  1995. it = cArr->find("dc_no");
  1996. if( it != cArr->end() ){
  1997. retVal->bNormallyClose = ( atoi( it->second.c_str() ) == 0 );
  1998. };
  1999. it = cArr->find("dc_window");
  2000. if( it != cArr->end() ){
  2001. retVal->nAutoDelay = atoi( it->second.c_str() );
  2002. };
  2003. };
  2004. };
  2005. };
  2006. deleteResult( result );
  2007. };
  2008. return retVal;
  2009. };
  2010. uint8_t sqliteDBC::getLRFDataStatus( uint32_t ID ){
  2011. uint8_t RetVal = 0;
  2012. if( m_bStoIsConnected ){
  2013. char tQBuf[CTEXT];
  2014. AAssoc *result;
  2015. sprintf(tQBuf, "SELECT `data_status` FROM `lrf_sensors` WHERE `id`=%d", ID );
  2016. #ifdef DEBUG
  2017. LOG(tQBuf);
  2018. #endif
  2019. result = m_pDataStorage->Query( tQBuf );
  2020. if( result!= NULL ) {
  2021. if( result->size() > 0 ) {
  2022. Assoc *cArr;
  2023. Assoc::iterator it;
  2024. cArr = result->at(0);
  2025. it = cArr->find("data_status");
  2026. if( it != cArr->end() ){
  2027. RetVal = atoi( it->second.c_str() );
  2028. };
  2029. };
  2030. };
  2031. deleteResult( result );
  2032. };
  2033. return RetVal;
  2034. };
  2035. uint32_t sqliteDBC::getLRFMaskID( uint32_t deviceID, uint8_t lrfID, uint8_t layer ){
  2036. uint32_t RetVal = 0;
  2037. if( m_bStoIsConnected ){
  2038. char tQBuf[CTEXT];
  2039. AAssoc *result;
  2040. sprintf(tQBuf, "SELECT `id` FROM `lrf_masks` WHERE `sensor_id`=%d AND `lrf_id`=%d AND `layer`=%d ", deviceID, lrfID, layer );
  2041. #ifdef DEBUG
  2042. LOG(tQBuf);
  2043. #endif
  2044. result = m_pDataStorage->Query( tQBuf );
  2045. if( result!= NULL ) {
  2046. if( result->size() > 0 ) {
  2047. Assoc *cArr;
  2048. Assoc::iterator it;
  2049. cArr = result->at(0);
  2050. it = cArr->find("id");
  2051. if( it != cArr->end() ){
  2052. RetVal = atoi( it->second.c_str() );
  2053. };
  2054. };
  2055. };
  2056. deleteResult( result );
  2057. };
  2058. return RetVal;
  2059. };
  2060. uint32_t sqliteDBC::getLRFMaskPolygoneID( uint32_t maskID, uint8_t vertecID ){
  2061. uint32_t RetVal = 0;
  2062. if( m_bStoIsConnected ){
  2063. char tQBuf[CTEXT];
  2064. AAssoc *result;
  2065. sprintf(tQBuf, "SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d AND `vertex_id`=%d", maskID, vertecID );
  2066. #ifdef DEBUG
  2067. LOG(tQBuf);
  2068. #endif
  2069. result = m_pDataStorage->Query( tQBuf );
  2070. if( result!= NULL ) {
  2071. if( result->size() > 0 ) {
  2072. Assoc *cArr;
  2073. Assoc::iterator it;
  2074. cArr = result->at(0);
  2075. it = cArr->find("id");
  2076. if( it != cArr->end() ){
  2077. RetVal = atoi( it->second.c_str() );
  2078. };
  2079. };
  2080. };
  2081. deleteResult( result );
  2082. };
  2083. return RetVal;
  2084. };
  2085. uint8_t sqliteDBC::getLRFMasksStatus( uint32_t deviceID ){
  2086. uint8_t RetVal = 0;
  2087. if( m_bStoIsConnected ){
  2088. char tQBuf[CTEXT];
  2089. AAssoc *result;
  2090. sprintf(tQBuf, "SELECT `layer` FROM `lrf_masks` WHERE `sensor_id`=%d AND `status`='E'", deviceID );
  2091. #ifdef DEBUG
  2092. LOG(tQBuf);
  2093. #endif
  2094. result = m_pDataStorage->Query( tQBuf );
  2095. if( result!= NULL ) {
  2096. if( result->size() > 0 ) {
  2097. Assoc *cArr;
  2098. Assoc::iterator it;
  2099. for(int i=0;i<result->size();i++) {
  2100. cArr = result->at(i);
  2101. it = cArr->find("layer");
  2102. if( it != cArr->end() ){
  2103. RetVal |= ( 1<< atoi( it->second.c_str() ));
  2104. };
  2105. };
  2106. };
  2107. };
  2108. deleteResult( result );
  2109. };
  2110. return RetVal;
  2111. };
  2112. DBC_LRF_UPLOAD_DATA *sqliteDBC::getLRFUploadData( uint32_t upID ){
  2113. DBC_LRF_UPLOAD_DATA *RetVal = NULL;
  2114. if( m_bStoIsConnected ){
  2115. char tQBuf[CTEXT];
  2116. AAssoc *result;
  2117. sprintf(tQBuf, "SELECT * FROM `lrf_uploads` WHERE `id`=%d", upID );
  2118. #ifdef DEBUG
  2119. LOG(tQBuf);
  2120. #endif
  2121. result = m_pDataStorage->Query( tQBuf );
  2122. if( result->size() > 0 ) {
  2123. Assoc *cArr;
  2124. RetVal = (DBC_LRF_UPLOAD_DATA*) malloc(sizeof(DBC_LRF_UPLOAD_DATA));
  2125. if( RetVal != NULL ) {
  2126. memset( RetVal, 0, sizeof(DBC_LRF_UPLOAD_DATA));
  2127. Assoc::iterator it;
  2128. RetVal->id = upID;
  2129. cArr = result->at(0);
  2130. it = cArr->find("md5");
  2131. if( it != cArr->end() ){
  2132. strncpy( RetVal->md5, it->second.c_str(), 32 );
  2133. };
  2134. it = cArr->find("file");
  2135. if( it != cArr->end() ){
  2136. strncpy( RetVal->fname, it->second.c_str(), 16 );
  2137. };
  2138. it = cArr->find("issue_reset");
  2139. if( it != cArr->end() ){
  2140. RetVal->transfer_mode = atoi( it->second.c_str() );
  2141. };
  2142. };
  2143. };
  2144. deleteResult( result );
  2145. };
  2146. return RetVal;
  2147. };
  2148. DBC_WIDTH_TABLE *sqliteDBC::getLRFWidthTable( uint32_t lrfID ){
  2149. DBC_WIDTH_TABLE *retVal = NULL;
  2150. if( m_bStoIsConnected ){
  2151. retVal = (DBC_WIDTH_TABLE *)malloc(sizeof(DBC_WIDTH_TABLE));
  2152. if ( retVal ) {
  2153. char tQBuf[CTEXT];
  2154. AAssoc *result;
  2155. sprintf(tQBuf, "SELECT `range_k`, `width_k` FROM `lrf_detection_width_tables` WHERE `aux_id`=%d ORDER BY `range_k`", lrfID );
  2156. #ifdef DEBUG_EXTRA
  2157. LOG(tQBuf);
  2158. #endif
  2159. memset( retVal, 0 , sizeof( DBC_WIDTH_TABLE ));
  2160. result = m_pDataStorage->Query( tQBuf );
  2161. if( result!= NULL ) {
  2162. if( result->size() > 0 ) {
  2163. retVal->count = result->size();
  2164. retVal->wt_records = (DBC_WT_RECORD*) calloc( retVal->count, sizeof(DBC_WT_RECORD));
  2165. if ( retVal->wt_records ) {
  2166. Assoc *cArr;
  2167. Assoc::iterator it;
  2168. for( int i = 0; i < retVal->count; i++ ) {
  2169. cArr = result->at(i);
  2170. it = cArr->find("range_k");
  2171. if( it != cArr->end() ){
  2172. retVal->wt_records[i].rangePercent = atoi( it->second.c_str() );
  2173. };
  2174. it = cArr->find("width_k");
  2175. if( it != cArr->end() ){
  2176. retVal->wt_records[i].widthPercent = atoi( it->second.c_str() );
  2177. };
  2178. };
  2179. } else {
  2180. //retVal->count = 0;
  2181. LOG_ERROR("No Memory To create WT Record!");
  2182. free( retVal );
  2183. retVal = NULL;
  2184. };
  2185. };
  2186. };
  2187. deleteResult( result );
  2188. } else {
  2189. LOG_ERROR("No Memory To create WT!");
  2190. };
  2191. };
  2192. return retVal;
  2193. };
  2194. uint32_t sqliteDBC::getNodeAuxParam( uint32_t NodeID ){
  2195. uint32_t retVal = 0;
  2196. if( m_bStoIsConnected ){
  2197. char tQBuf[CTEXT];
  2198. AAssoc *result;
  2199. sprintf(tQBuf, "SELECT `aux_parameter` FROM `nodes` WHERE `id`=%d", NodeID );
  2200. #ifdef DEBUG_EXTRA
  2201. LOG(tQBuf);
  2202. #endif
  2203. result = m_pDataStorage->Query( tQBuf );
  2204. if( result!= NULL ) {
  2205. if( result->size() > 0 ) {
  2206. Assoc *cArr;
  2207. Assoc::iterator it;
  2208. cArr = result->at(0);
  2209. it = cArr->find("aux_parameter");
  2210. if( it != cArr->end() ){
  2211. retVal = atoi( it->second.c_str() );
  2212. };
  2213. };
  2214. };
  2215. deleteResult( result );
  2216. };
  2217. return retVal;
  2218. };
  2219. DBC_NODE_DATA *sqliteDBC::getNodeData( uint32_t ID ){
  2220. DBC_NODE_DATA * tVal = NULL;
  2221. if( m_bStoIsConnected ){
  2222. char tQBuf[CTEXT];
  2223. AAssoc *result;
  2224. sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID );
  2225. #ifdef DEBUG_EXTRA
  2226. LOG(tQBuf);
  2227. #endif
  2228. result = m_pDataStorage->Query( tQBuf );
  2229. if( result!= NULL ) {
  2230. if( result->size() > 0 ) {
  2231. Assoc *cArr;
  2232. tVal = (DBC_NODE_DATA*) malloc(sizeof(DBC_NODE_DATA));
  2233. if( tVal != NULL ) {
  2234. memset( tVal, 0, sizeof(DBC_NODE_DATA));
  2235. Assoc::iterator it;
  2236. tVal->node_id = ID;
  2237. cArr = result->at(0);
  2238. it = cArr->find("type");
  2239. if( it != cArr->end() ){
  2240. tVal->type = /*(NODE_TYPE)*/ atoi( it->second.c_str() );
  2241. };
  2242. it = cArr->find("aux_id");
  2243. if( it != cArr->end() ){
  2244. tVal->aux_id = atoi( it->second.c_str() );
  2245. };
  2246. it = cArr->find("aux_parameter");
  2247. if( it != cArr->end() ){
  2248. tVal->aux_parameter = atoi( it->second.c_str() );
  2249. };
  2250. it = cArr->find("online");
  2251. if( it != cArr->end() ){
  2252. tVal->online = (bool)atoi( it->second.c_str() );
  2253. };
  2254. it = cArr->find("enable");
  2255. if( it != cArr->end() ){
  2256. tVal->enabled = (bool)atoi( it->second.c_str() );
  2257. };
  2258. it = cArr->find("name");
  2259. if( it != cArr->end() ){
  2260. sprintf( tVal->name, it->second.c_str() );
  2261. };
  2262. };
  2263. };
  2264. };
  2265. deleteResult( result );
  2266. };
  2267. return tVal;
  2268. };
  2269. uint32_t sqliteDBC::getNodeIdByTypeAndAux( uint8_t type, uint32_t aux_id ){
  2270. uint32_t retVal = 0;
  2271. if( m_bStoIsConnected ){
  2272. char tQBuf[CTEXT];
  2273. AAssoc *result;
  2274. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type`=%d AND `aux_id`=%d", type, aux_id);
  2275. #ifdef DEBUG_EXTRA
  2276. LOG(tQBuf);
  2277. #endif
  2278. result = m_pDataStorage->Query( tQBuf );
  2279. if( result!= NULL ) {
  2280. if( result->size() > 0 ) {
  2281. Assoc *cArr;
  2282. Assoc::iterator it;
  2283. cArr = result->at(0);
  2284. it = cArr->find("id");
  2285. if( it != cArr->end() ){
  2286. retVal = atoi( it->second.c_str() );
  2287. };
  2288. };
  2289. };
  2290. deleteResult( result );
  2291. };
  2292. return retVal;
  2293. };
  2294. CSTR sqliteDBC::getNodeName( uint32_t NodeID ){
  2295. CSTR retVal = NULL;
  2296. if( m_bStoIsConnected ){
  2297. char tQBuf[CTEXT];
  2298. AAssoc *result;
  2299. sprintf(tQBuf, "SELECT `name` FROM `nodes` WHERE `id`=%d", NodeID );
  2300. #ifdef DEBUG_EXTRA
  2301. LOG(tQBuf);
  2302. #endif
  2303. result = m_pDataStorage->Query( tQBuf );
  2304. if( result!= NULL ) {
  2305. if( result->size() > 0 ) {
  2306. Assoc *cArr;
  2307. Assoc::iterator it;
  2308. cArr = result->at(0);
  2309. it = cArr->find("name");
  2310. if( it != cArr->end() ){
  2311. retVal = strdup( it->second.c_str() );
  2312. };
  2313. };
  2314. };
  2315. deleteResult( result );
  2316. };
  2317. return retVal;
  2318. };
  2319. DBC_NET_IF_DATA *sqliteDBC::getNIF_Data( uint32_t nif_id ){
  2320. DBC_NET_IF_DATA * tVal = NULL;
  2321. if( m_bStoIsConnected ){
  2322. char tQBuf[CTEXT];
  2323. AAssoc *result;
  2324. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id );
  2325. #ifdef DEBUG_EXTRA
  2326. LOG(tQBuf);
  2327. #endif
  2328. result = m_pDataStorage->Query( tQBuf );
  2329. if( result!= NULL ) {
  2330. if( result->size() > 0 ) {
  2331. Assoc *cArr;
  2332. tVal = (DBC_NET_IF_DATA*) malloc(sizeof(DBC_NET_IF_DATA));
  2333. if( tVal != NULL ) {
  2334. Assoc::iterator it;
  2335. tVal->id = nif_id;
  2336. cArr = result->at(0);
  2337. it = cArr->find("type");
  2338. if( it != cArr->end() ){
  2339. tVal->type = atoi(it->second.c_str());
  2340. };
  2341. it = cArr->find("mode");
  2342. if( it != cArr->end() ){
  2343. tVal->mode = atoi(it->second.c_str());
  2344. };
  2345. it = cArr->find("device_name");
  2346. if( it != cArr->end() ){
  2347. strncpy( tVal->device_name, it->second.c_str(),11);
  2348. tVal->device_name[10]=(char)0;
  2349. };
  2350. it = cArr->find("mac");
  2351. if( it != cArr->end() ){
  2352. strncpy( tVal->mac, it->second.c_str(), 33 );
  2353. tVal->mac[32]=(char)0;
  2354. };
  2355. it = cArr->find("aux_id");
  2356. if( it != cArr->end() ){
  2357. tVal->aux_id = atoi(it->second.c_str());
  2358. };
  2359. };
  2360. };
  2361. };
  2362. deleteResult( result );
  2363. };
  2364. return tVal;
  2365. };
  2366. uint32_t sqliteDBC::getNIF_ID( CSTR key ){
  2367. uint32_t tVal = 0;
  2368. if( m_bStoIsConnected ){
  2369. char tQBuf[CTEXT];
  2370. AAssoc *result;
  2371. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key );
  2372. #ifdef DEBUG_ALL
  2373. LOG(tQBuf);
  2374. #endif
  2375. result = m_pDataStorage->Query( tQBuf );
  2376. if( result!= NULL ) {
  2377. if( result->size() > 0 ) {
  2378. Assoc *cArr;
  2379. Assoc::iterator it;
  2380. cArr = result->at(0);
  2381. it = cArr->find("id");
  2382. if( it != cArr->end() ){
  2383. tVal = atoi( it->second.c_str() );
  2384. };
  2385. };
  2386. };
  2387. deleteResult( result );
  2388. };
  2389. return tVal;
  2390. };
  2391. uint32_t sqliteDBC::getNIF_IDbyDevice( CSTR device ){
  2392. uint32_t tVal = 0;
  2393. if( m_bStoIsConnected ){
  2394. char tQBuf[CTEXT];
  2395. AAssoc *result;
  2396. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device );
  2397. #ifdef DEBUG_ALL
  2398. LOG(tQBuf);
  2399. #endif
  2400. result = m_pDataStorage->Query( tQBuf );
  2401. if( result!= NULL ) {
  2402. if( result->size() > 0 ) {
  2403. Assoc *cArr;
  2404. Assoc::iterator it;
  2405. cArr = result->at(0);
  2406. it = cArr->find("id");
  2407. if( it != cArr->end() ){
  2408. tVal = atoi( it->second.c_str() );
  2409. };
  2410. };
  2411. };
  2412. deleteResult( result );
  2413. };
  2414. return tVal;
  2415. };
  2416. uint32_t sqliteDBC::getNetworkInfrastructureID( uint32_t nif_id ){
  2417. uint32_t tVal = 0;
  2418. if( m_bStoIsConnected ){
  2419. char tQBuf[CTEXT];
  2420. AAssoc *result;
  2421. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id );
  2422. #ifdef DEBUG_ALL
  2423. LOG(tQBuf);
  2424. #endif
  2425. result = m_pDataStorage->Query( tQBuf );
  2426. if( result!= NULL ) {
  2427. if( result->size() > 0 ) {
  2428. Assoc *cArr;
  2429. Assoc::iterator it;
  2430. cArr = result->at(0);
  2431. it = cArr->find("id");
  2432. if( it != cArr->end() ){
  2433. tVal = atoi( it->second.c_str() );
  2434. };
  2435. };
  2436. };
  2437. deleteResult( result );
  2438. };
  2439. return tVal;
  2440. };
  2441. REPORT_DIRINFO *sqliteDBC::getReportDirInfo( uint32_t id ){
  2442. REPORT_DIRINFO *tVal = NULL;
  2443. if( m_bStaIsCreated ){
  2444. char tQBuf[CTEXT];
  2445. AAssoc *result;
  2446. sprintf(tQBuf, "SELECT * FROM `days` WHERE `ROWID`=%d", id );
  2447. #ifdef DEBUG_ALL
  2448. LOG(tQBuf);
  2449. #endif
  2450. result = m_pDataStorage->Query( tQBuf );
  2451. if( result!= NULL ) {
  2452. if( result->size() > 0 ) {
  2453. tVal = (REPORT_DIRINFO *) malloc( sizeof(REPORT_DIRINFO));
  2454. if ( tVal ) {
  2455. Assoc *cArr;
  2456. Assoc::iterator it;
  2457. memset( tVal, 0 , sizeof( REPORT_DIRINFO));
  2458. cArr = result->at(0);
  2459. it = cArr->find("year");
  2460. if( it != cArr->end() ){
  2461. strncpy( tVal->YEAR, it->second.c_str(), 5 );
  2462. };
  2463. it = cArr->find("month");
  2464. if( it != cArr->end() ){
  2465. strncpy( tVal->MONTH, it->second.c_str(), 3 );
  2466. };
  2467. it = cArr->find("day");
  2468. if( it != cArr->end() ){
  2469. strncpy( tVal->DAY, it->second.c_str(), 3 );
  2470. };
  2471. };
  2472. };
  2473. };
  2474. deleteResult( result );
  2475. };
  2476. return tVal;
  2477. };
  2478. DBC_RESOURCE_DATA *sqliteDBC::getResourceData( uint32_t ResourceID ){
  2479. DBC_RESOURCE_DATA *tVal = NULL;
  2480. if( m_bStoIsConnected ){
  2481. char tQBuf[CTEXT];
  2482. AAssoc *result;
  2483. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID );
  2484. #ifdef DEBUG_ALL
  2485. LOG(tQBuf);
  2486. #endif
  2487. result = m_pDataStorage->Query( tQBuf );
  2488. if( result!= NULL ) {
  2489. if( result->size() > 0 ) {
  2490. Assoc *cArr;
  2491. tVal = (DBC_RESOURCE_DATA*) malloc(sizeof(DBC_RESOURCE_DATA));
  2492. if( tVal != NULL ) {
  2493. Assoc::iterator it;
  2494. cArr = result->at(0);
  2495. tVal->id = ResourceID;
  2496. it = cArr->find("node_id");
  2497. if( it != cArr->end() ){
  2498. tVal->node_id = atoi( it->second.c_str() );
  2499. };
  2500. it = cArr->find("type");
  2501. if( it != cArr->end() ){
  2502. tVal->type = atoi( it->second.c_str() );
  2503. };
  2504. it = cArr->find("aux_id");
  2505. if( it != cArr->end() ){
  2506. tVal->aux_id = atoi( it->second.c_str() );
  2507. };
  2508. it = cArr->find("device");
  2509. if( it != cArr->end() ){
  2510. strncpy( tVal->device, it->second.c_str(), 64 );
  2511. tVal->device[63] = (char)0;
  2512. };
  2513. };
  2514. };
  2515. };
  2516. deleteResult( result );
  2517. };
  2518. return tVal;
  2519. };
  2520. uint32_t sqliteDBC::getResourceID( uint32_t NodeID, uint8_t type, uint32_t auxID ){
  2521. uint32_t RetVal = 0;
  2522. if( m_bStoIsConnected ){
  2523. char tQBuf[CTEXT];
  2524. sprintf(tQBuf,"SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID );
  2525. #ifdef DEBUG_ALL
  2526. LOG(tQBuf);
  2527. #endif
  2528. AAssoc *result;
  2529. result = m_pDataStorage->Query( tQBuf );
  2530. if( result!= NULL ) {
  2531. if( result->size() > 0 ) {
  2532. Assoc * cArr;
  2533. Assoc::iterator it;
  2534. cArr = result->at(0);
  2535. it = cArr->find("id");
  2536. if( it != cArr->end() ){
  2537. RetVal = atoi( it->second.c_str() );
  2538. };
  2539. };
  2540. };
  2541. deleteResult( result );
  2542. };
  2543. return RetVal;
  2544. };
  2545. DBC_SITE_DATA *sqliteDBC::getSiteData( uint32_t ID ){
  2546. DBC_SITE_DATA * tVal = NULL;
  2547. if( m_bStoIsConnected ){
  2548. char tQBuf[CTEXT];
  2549. AAssoc *result;
  2550. sprintf(tQBuf, "SELECT * FROM `system_props` WHERE `id`=%d", ID);
  2551. #ifdef DEBUG_ALL
  2552. LOG(tQBuf);
  2553. #endif
  2554. result = m_pDataStorage->Query( tQBuf );
  2555. if( result!= NULL ) {
  2556. if( result->size() > 0 ) {
  2557. Assoc *cArr;
  2558. tVal = (DBC_SITE_DATA*) malloc(sizeof(DBC_SITE_DATA));
  2559. if( tVal != NULL ) {
  2560. memset( tVal, 0 , sizeof(DBC_SITE_DATA));
  2561. Assoc::iterator it;
  2562. cArr = result->at(0);
  2563. it = cArr->find("type");
  2564. if( it != cArr->end() ){
  2565. tVal->account_id = atoi( it->second.c_str() );
  2566. };
  2567. it = cArr->find("name");
  2568. if( it != cArr->end() ){
  2569. sprintf( tVal->name, it->second.c_str() );
  2570. };
  2571. //it = cArr->find("server_ident");
  2572. //if( it != cArr->end() ){
  2573. // sprintf( tVal->ident, it->second.c_str() );
  2574. //};
  2575. it = cArr->find("anchor_lat");
  2576. if( it != cArr->end() ){
  2577. tVal->lat = atof( it->second.c_str() );
  2578. };
  2579. it = cArr->find("anchor_lon");
  2580. if( it != cArr->end() ){
  2581. tVal->lon = atof( it->second.c_str() );
  2582. };
  2583. it = cArr->find("anchor_alt");
  2584. if( it != cArr->end() ){
  2585. tVal->alt = atof( it->second.c_str() );
  2586. };
  2587. it = cArr->find("hemisphere");
  2588. if( it != cArr->end() ){
  2589. tVal->SH = ( it->second.c_str()[0] == 'S' );
  2590. };
  2591. it = cArr->find("map_id");
  2592. if( it != cArr->end() ){
  2593. tVal->map_id = atoi( it->second.c_str() );
  2594. };
  2595. it = cArr->find("local_binding");
  2596. if( it != cArr->end() ){
  2597. tVal->local_binding = (bool) atoi(it->second.c_str());
  2598. };
  2599. it = cArr->find("work_mode");
  2600. if( it != cArr->end() ){
  2601. tVal->work_mode = atoi(it->second.c_str());
  2602. };
  2603. it = cArr->find("report_cleanup");
  2604. if( it != cArr->end() ){
  2605. tVal->report_cleanup = atoi(it->second.c_str());
  2606. };
  2607. it = cArr->find("preserve_empty");
  2608. if( it != cArr->end() ){
  2609. tVal->preserve_empty = atoi(it->second.c_str());
  2610. };
  2611. };
  2612. };
  2613. };
  2614. deleteResult( result );
  2615. };
  2616. return tVal;
  2617. };
  2618. uint32_t sqliteDBC::getServerID( CSTR identity ){
  2619. uint32_t RetVal = 0;
  2620. if( m_bStoIsConnected ){
  2621. char tQBuf[CTEXT];
  2622. sprintf(tQBuf,"SELECT `id` FROM `servers` WHERE `identity`='%s'", identity );
  2623. #ifdef DEBUG_ALL
  2624. LOG(tQBuf);
  2625. #endif
  2626. AAssoc *result;
  2627. result = m_pDataStorage->Query( tQBuf );
  2628. if( result!= NULL ) {
  2629. if( result->size() > 0 ) {
  2630. Assoc * cArr;
  2631. Assoc::iterator it;
  2632. cArr = result->at(0);
  2633. it = cArr->find("id");
  2634. if( it != cArr->end() ){
  2635. RetVal = atoi( it->second.c_str() );
  2636. };
  2637. };
  2638. };
  2639. deleteResult( result );
  2640. };
  2641. return RetVal;
  2642. };
  2643. uint32_t sqliteDBC::getServerNode( uint32_t SID ){
  2644. uint32_t RetVal = 0;
  2645. if( m_bStoIsConnected ){
  2646. char tQBuf[CTEXT];
  2647. sprintf(tQBuf,"SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID );
  2648. #ifdef DEBUG_ALL
  2649. LOG(tQBuf);
  2650. #endif
  2651. AAssoc *result;
  2652. result = m_pDataStorage->Query( tQBuf );
  2653. if( result!= NULL ) {
  2654. if( result->size() > 0 ) {
  2655. Assoc * cArr;
  2656. Assoc::iterator it;
  2657. cArr = result->at(0);
  2658. it = cArr->find("id");
  2659. if( it != cArr->end() ){
  2660. RetVal = atoi( it->second.c_str() );
  2661. };
  2662. };
  2663. };
  2664. deleteResult( result );
  2665. };
  2666. return RetVal;
  2667. }
  2668. DBC_TARGET_FLITER_DATA *sqliteDBC::getTargetFilterData( uint32_t ID ){
  2669. DBC_TARGET_FLITER_DATA * tVal = NULL;
  2670. if( m_bStoIsConnected ){
  2671. char tQBuf[CTEXT];
  2672. AAssoc *result;
  2673. sprintf(tQBuf, "SELECT * FROM `target_filters` WHERE `id`=%d", ID );
  2674. #ifdef DEBUG
  2675. LOG(tQBuf);
  2676. #endif
  2677. result = m_pDataStorage->Query( tQBuf );
  2678. if( result!= NULL ) {
  2679. if( result->size() > 0 ) {
  2680. Assoc *cArr;
  2681. tVal = (DBC_TARGET_FLITER_DATA*) malloc(sizeof(DBC_TARGET_FLITER_DATA));
  2682. if( tVal != NULL ) {
  2683. memset( tVal, 0 , sizeof(DBC_TARGET_FLITER_DATA));
  2684. Assoc::iterator it;
  2685. tVal->id = ID;
  2686. cArr = result->at(0);
  2687. it = cArr->find("type");
  2688. if( it != cArr->end() ){
  2689. tVal->type = (TARGET_FILTER_TYPE) atoi( it->second.c_str() );
  2690. };
  2691. it = cArr->find("aux_id");
  2692. if( it != cArr->end() ){
  2693. tVal->aux_id = atoi( it->second.c_str() );
  2694. };
  2695. it = cArr->find("enabled");
  2696. if( it != cArr->end() ){
  2697. tVal->enabled = (bool) atoi( it->second.c_str() );
  2698. };
  2699. };
  2700. };
  2701. };
  2702. deleteResult( result );
  2703. };
  2704. return tVal;
  2705. };
  2706. TRAIN_FILTER_DATA *sqliteDBC::getTrainFilterData( uint32_t ID ){
  2707. TRAIN_FILTER_DATA * tVal = NULL;
  2708. if( m_bStoIsConnected ){
  2709. char tQBuf[CTEXT];
  2710. AAssoc *result;
  2711. sprintf(tQBuf, "SELECT * FROM `target_filter_train` WHERE `id`=%d", ID );
  2712. #ifdef DEBUG_EXTRA
  2713. LOG(tQBuf);
  2714. #endif
  2715. result = m_pDataStorage->Query( tQBuf );
  2716. if( result!= NULL ) {
  2717. if( result->size() > 0 ) {
  2718. Assoc *cArr;
  2719. tVal = (TRAIN_FILTER_DATA*) malloc(sizeof(TRAIN_FILTER_DATA));
  2720. if( tVal != NULL ) {
  2721. memset( tVal, 0 , sizeof(TRAIN_FILTER_DATA));
  2722. Assoc::iterator it;
  2723. cArr = result->at(0);
  2724. it = cArr->find("UPPER_SENSOR");
  2725. if( it != cArr->end() ){
  2726. tVal->nUpperSensorID = atoi( it->second.c_str() );
  2727. };
  2728. it = cArr->find("LOWER_SENSOR");
  2729. if( it != cArr->end() ){
  2730. tVal->nBottomSensorID = atoi( it->second.c_str() );
  2731. };
  2732. it = cArr->find("TRAIN_DETECT");
  2733. if( it != cArr->end() ){
  2734. tVal->bDetectTrain = (bool)atoi( it->second.c_str() );
  2735. };
  2736. it = cArr->find("TRAIN_WIDTH");
  2737. if( it != cArr->end() ){
  2738. tVal->nTrainWidth = atoi( it->second.c_str() );
  2739. };
  2740. it = cArr->find("DECISION_TIME");
  2741. if( it != cArr->end() ){
  2742. tVal->nDecisionTime = 1000 * atoi( it->second.c_str() );
  2743. };
  2744. it = cArr->find("SUSPECT_TIME");
  2745. if( it != cArr->end() ){
  2746. tVal->nSuspectTime = 1000 * atoi( it->second.c_str() );
  2747. };
  2748. it = cArr->find("TRACK_TRAIN");
  2749. if( it != cArr->end() ){
  2750. tVal->bTrackTrain = (bool)atoi( it->second.c_str() );
  2751. };
  2752. it = cArr->find("DEBUG");
  2753. if( it != cArr->end() ){
  2754. tVal->bDebugMode = (bool)atoi( it->second.c_str() );
  2755. };
  2756. };
  2757. };
  2758. };
  2759. deleteResult( result );
  2760. };
  2761. return tVal;
  2762. };
  2763. DBC_UART_DATA *sqliteDBC::getUartData( uint32_t UartID ){
  2764. DBC_UART_DATA *tVal = NULL;
  2765. if( m_bStoIsConnected ) {
  2766. char tQBuf[MAX_PATH_LEN];
  2767. AAssoc *result;
  2768. sprintf( tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID );
  2769. #ifdef DEBUG_ALL
  2770. LOG(tQBuf);
  2771. #endif
  2772. result = m_pDataStorage->Query( tQBuf );
  2773. if( result!= NULL ) {
  2774. if( result->size() > 0 ) {
  2775. Assoc *cArr;
  2776. tVal = (DBC_UART_DATA*) malloc(sizeof(DBC_UART_DATA));
  2777. if( tVal != NULL ) {
  2778. Assoc::iterator it;
  2779. cArr = result->at(0);
  2780. it = cArr->find("type");
  2781. if( it != cArr->end() ){
  2782. tVal->type = atoi( it->second.c_str() );
  2783. };
  2784. it = cArr->find("speed");
  2785. if( it != cArr->end() ){
  2786. tVal->speed = atoi( it->second.c_str() );
  2787. };
  2788. it = cArr->find("mode");
  2789. if( it != cArr->end() ){
  2790. tVal->mode = atoi( it->second.c_str() );
  2791. };
  2792. it = cArr->find("port");
  2793. if( it != cArr->end() ){
  2794. tVal->port = atoi( it->second.c_str() );
  2795. };
  2796. it = cArr->find("path");
  2797. if( it != cArr->end() ){
  2798. strncpy( tVal->path, it->second.c_str(), 128 );
  2799. };
  2800. };
  2801. };
  2802. };
  2803. deleteResult( result );
  2804. };
  2805. return tVal;
  2806. };
  2807. uint32_t sqliteDBC::getUartID( uint32_t NodeID, uint8_t Type, CSTR path ){
  2808. uint32_t RetVal = 0;
  2809. if( m_bStoIsConnected ){
  2810. char tQBuf[CTEXT];
  2811. sprintf(tQBuf,"SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path );
  2812. #ifdef DEBUG_ALL
  2813. LOG(tQBuf);
  2814. #endif
  2815. AAssoc *result;
  2816. result = m_pDataStorage->Query( tQBuf );
  2817. if( result!= NULL ) {
  2818. if( result->size() > 0 ) {
  2819. Assoc * cArr;
  2820. Assoc::iterator it;
  2821. cArr = result->at(0);
  2822. it = cArr->find("id");
  2823. if( it != cArr->end() ){
  2824. RetVal = atoi( it->second.c_str() );
  2825. };
  2826. };
  2827. };
  2828. deleteResult( result );
  2829. };
  2830. return RetVal;
  2831. };
  2832. uint32_t sqliteDBC::getWiFi_ID( uint32_t nif_id ){
  2833. uint32_t tVal = 0;
  2834. if( m_bStoIsConnected ){
  2835. char tQBuf[CTEXT];
  2836. AAssoc *result;
  2837. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id );
  2838. #ifdef DEBUG_ALL
  2839. LOG(tQBuf);
  2840. #endif
  2841. result = m_pDataStorage->Query( tQBuf );
  2842. if( result!= NULL ) {
  2843. if( result->size() > 0 ) {
  2844. Assoc *cArr;
  2845. Assoc::iterator it;
  2846. cArr = result->at(0);
  2847. it = cArr->find("id");
  2848. if( it != cArr->end() ){
  2849. tVal = atoi( it->second.c_str() );
  2850. };
  2851. };
  2852. };
  2853. deleteResult( result );
  2854. };
  2855. return tVal;
  2856. };
  2857. DBC_INT_LIST *sqliteDBC::listFilters( uint32_t ServerID ){
  2858. DBC_INT_LIST * tVal = NULL;
  2859. if( m_bStoIsConnected ){
  2860. char tQBuf[CTEXT];
  2861. AAssoc *result;
  2862. sprintf(tQBuf, "SELECT `id` FROM `target_filters` WHERE `enabled`=1" );
  2863. #ifdef DEBUG
  2864. LOG(tQBuf);
  2865. #endif
  2866. result = m_pDataStorage->Query( tQBuf );
  2867. if( result!= NULL ) {
  2868. if( result->size() > 0 ) {
  2869. Assoc *cArr;
  2870. tVal = new DBC_INT_LIST();
  2871. if( tVal != NULL ) {
  2872. Assoc::iterator it;
  2873. for(int i=0;i<result->size();i++) {
  2874. cArr = result->at(i);
  2875. it = cArr->find("id");
  2876. if( it != cArr->end() ){
  2877. tVal->push_back( atoi( it->second.c_str() ));
  2878. };
  2879. };
  2880. };
  2881. };
  2882. };
  2883. deleteResult( result );
  2884. };
  2885. return tVal;
  2886. };
  2887. DBC_INT_LIST *sqliteDBC::listLRFUploads( uint32_t deviceID ){
  2888. DBC_INT_LIST * tVal = NULL;
  2889. if( m_bStoIsConnected ){
  2890. char tQBuf[CTEXT];
  2891. AAssoc *result;
  2892. sprintf(tQBuf, "SELECT `id` FROM `lrf_uploads` WHERE `status`=-1 AND `sensor_id`=%d", deviceID );
  2893. #ifdef DEBUG
  2894. LOG(tQBuf);
  2895. #endif
  2896. result = m_pDataStorage->Query( tQBuf );
  2897. if( result!= NULL ) {
  2898. if( result->size() > 0 ) {
  2899. Assoc *cArr;
  2900. tVal = new DBC_INT_LIST();
  2901. if( tVal != NULL ) {
  2902. Assoc::iterator it;
  2903. for(int i=0;i<result->size();i++) {
  2904. cArr = result->at(i);
  2905. it = cArr->find("id");
  2906. if( it != cArr->end() ){
  2907. tVal->push_back( atoi( it->second.c_str() ));
  2908. };
  2909. };
  2910. };
  2911. };
  2912. };
  2913. deleteResult( result );
  2914. };
  2915. return tVal;
  2916. };
  2917. DBC_INT_LIST *sqliteDBC::listMediaStreams( uint32_t SiteId, uint8_t listMode ){
  2918. DBC_INT_LIST * tVal = NULL;
  2919. if( m_bStoIsConnected ){
  2920. char tQBuf[CTEXT];
  2921. AAssoc *result;
  2922. switch( listMode ){
  2923. case LIST_VALID_RECORDS:
  2924. 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" );
  2925. break;
  2926. case LIST_INVALID_RECORDS:
  2927. 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" );
  2928. break;
  2929. default:
  2930. 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" );
  2931. };
  2932. #ifdef DEBUG_ALL
  2933. LOG(tQBuf);
  2934. #endif
  2935. result = m_pDataStorage->Query( tQBuf );
  2936. if( result!= NULL ) {
  2937. if( result->size() > 0 ) {
  2938. Assoc *cArr;
  2939. tVal = new DBC_INT_LIST();
  2940. if( tVal != NULL ) {
  2941. Assoc::iterator it;
  2942. for(int i=0;i<result->size();i++) {
  2943. cArr = result->at(i);
  2944. it = cArr->find("id");
  2945. if( it != cArr->end() ){
  2946. tVal->push_back( atoi( it->second.c_str() ));
  2947. };
  2948. };
  2949. };
  2950. };
  2951. };
  2952. deleteResult( result );
  2953. };
  2954. return tVal;
  2955. };
  2956. DBC_INT_LIST *sqliteDBC::listModules(){
  2957. DBC_INT_LIST * tVal = NULL;
  2958. if( m_bStoIsConnected ){
  2959. AAssoc *result;
  2960. result = m_pDataStorage->Query( "SELECT `id` FROM `modules` WHERE `enabled`=1" );
  2961. if( result!= NULL ) {
  2962. if( result->size() > 0 ) {
  2963. Assoc *cArr;
  2964. tVal = new DBC_INT_LIST();
  2965. if( tVal != NULL ) {
  2966. Assoc::iterator it;
  2967. for(int i=0;i<result->size();i++) {
  2968. cArr = result->at(i);
  2969. it = cArr->find("id");
  2970. if( it != cArr->end() ){
  2971. tVal->push_back( atoi( it->second.c_str() ));
  2972. };
  2973. };
  2974. };
  2975. };
  2976. };
  2977. deleteResult( result );
  2978. };
  2979. return tVal;
  2980. };
  2981. DBC_INT_LIST *sqliteDBC::listNewModules( DBC_INT_LIST * old_ids ){
  2982. DBC_INT_LIST * tVal = NULL;
  2983. if( m_bStoIsConnected ){
  2984. char tParam[CTEXT];
  2985. //memset( tParam, 0, CTEXT );
  2986. sprintf(tParam,"0");
  2987. if( old_ids != NULL ) {
  2988. char tBuffer[CTEXT];
  2989. for(int i = 0; i< old_ids->size(); i++ ) {
  2990. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  2991. strncpy( tParam, tBuffer, CTEXT );
  2992. };
  2993. };
  2994. char tQBuf[CTEXT];
  2995. AAssoc *result;
  2996. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam );
  2997. #ifdef DEBUG_ALL
  2998. LOG(tQBuf);
  2999. #endif
  3000. result = m_pDataStorage->Query( tQBuf );
  3001. if( result!= NULL ) {
  3002. if( result->size() > 0 ) {
  3003. Assoc *cArr;
  3004. tVal = new DBC_INT_LIST();
  3005. if( tVal != NULL ) {
  3006. Assoc::iterator it;
  3007. for(int i=0;i<result->size();i++) {
  3008. cArr = result->at(i);
  3009. it = cArr->find("id");
  3010. if( it != cArr->end() ){
  3011. tVal->push_back( atoi( it->second.c_str() ));
  3012. };
  3013. };
  3014. };
  3015. };
  3016. };
  3017. deleteResult( result );
  3018. };
  3019. return tVal;
  3020. };
  3021. DBC_INT_LIST *sqliteDBC::listNewNodes( DBC_INT_LIST * old_ids ){
  3022. DBC_INT_LIST * tVal = NULL;
  3023. if( m_bStoIsConnected ){
  3024. char tParam[CTEXT];
  3025. //memset( tParam, 0, CTEXT );
  3026. sprintf(tParam,"0");
  3027. if( old_ids != NULL ) {
  3028. char tBuffer[CTEXT];
  3029. for(int i = 0; i< old_ids->size(); i++ ) {
  3030. sprintf( tBuffer, "%s,%d", tParam, old_ids->at(i) );
  3031. strncpy( tParam, tBuffer, CTEXT );
  3032. };
  3033. };
  3034. char tQBuf[CTEXT];
  3035. AAssoc *result;
  3036. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `enable`=1 AND `id` NOT IN(%s)", tParam );
  3037. #ifdef DEBUG_EXTRA
  3038. LOG(tQBuf);
  3039. #endif
  3040. result = m_pDataStorage->Query( tQBuf );
  3041. if( result!= NULL ) {
  3042. if( result->size() > 0 ) {
  3043. Assoc *cArr;
  3044. tVal = new DBC_INT_LIST();
  3045. if( tVal != NULL ) {
  3046. Assoc::iterator it;
  3047. for(int i=0;i<result->size();i++) {
  3048. cArr = result->at(i);
  3049. it = cArr->find("id");
  3050. if( it != cArr->end() ){
  3051. tVal->push_back( atoi( it->second.c_str() ));
  3052. };
  3053. };
  3054. };
  3055. };
  3056. };
  3057. deleteResult( result );
  3058. };
  3059. return tVal;
  3060. };
  3061. DBC_INT_LIST *sqliteDBC::listNodes( bool all ){
  3062. DBC_INT_LIST * tVal = NULL;
  3063. if( m_bStoIsConnected ){
  3064. char tQBuf[1024];
  3065. AAssoc *result;
  3066. if( all ) {
  3067. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  3068. } else {
  3069. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0 AND `enable`=1" );
  3070. };
  3071. #ifdef DEBUG_EXTRA
  3072. LOG(tQBuf);
  3073. #endif
  3074. result = m_pDataStorage->Query( tQBuf );
  3075. if( result!= NULL ) {
  3076. if( result->size() > 0 ) {
  3077. Assoc *cArr;
  3078. tVal = new DBC_INT_LIST();
  3079. if( tVal != NULL ) {
  3080. Assoc::iterator it;
  3081. for(int i=0; i <result->size();i++) {
  3082. cArr = result->at(i);
  3083. it = cArr->find("id");
  3084. if( it != cArr->end() ){
  3085. tVal->push_back( atoi( it->second.c_str() ));
  3086. };
  3087. };
  3088. };
  3089. };
  3090. };
  3091. deleteResult( result );
  3092. };
  3093. return tVal;
  3094. };
  3095. DBC_INT_LIST *sqliteDBC::listOlderReportStat( CSTR Y, CSTR M, CSTR D ){
  3096. DBC_INT_LIST * tVal = NULL;
  3097. if ( m_bStaIsConnected ) {
  3098. char tQBuf[MAX_PATH_LEN];
  3099. AAssoc *result;
  3100. 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 );
  3101. result = m_pDataStatistics->Query( tQBuf );
  3102. if( result!= NULL ) {
  3103. if( result->size() > 0 ) {
  3104. Assoc *cArr;
  3105. tVal = new DBC_INT_LIST();
  3106. if( tVal != NULL ) {
  3107. Assoc::iterator it;
  3108. for(int i=0;i<result->size();i++) {
  3109. cArr = result->at(i);
  3110. it = cArr->find("ROWID");
  3111. if( it != cArr->end() ){
  3112. tVal->push_back( atoi( it->second.c_str() ));
  3113. };
  3114. };
  3115. };
  3116. };
  3117. };
  3118. deleteResult( result );
  3119. };
  3120. return tVal;
  3121. };
  3122. DBC_INT_LIST *sqliteDBC::listOlderReportStat( uint32_t TS ){
  3123. DBC_INT_LIST * tVal = NULL;
  3124. if ( m_bStaIsConnected ) {
  3125. char tQBuf[MAX_PATH_LEN];
  3126. AAssoc *result;
  3127. sprintf(tQBuf, "SELECT `ROWID` FROM `days` WHERE `ts`< %d", TS );
  3128. result = m_pDataStatistics->Query( tQBuf );
  3129. if( result!= NULL ) {
  3130. if( result->size() > 0 ) {
  3131. Assoc *cArr;
  3132. tVal = new DBC_INT_LIST();
  3133. if( tVal != NULL ) {
  3134. Assoc::iterator it;
  3135. for(int i=0;i<result->size();i++) {
  3136. cArr = result->at(i);
  3137. it = cArr->find("ROWID");
  3138. if( it != cArr->end() ){
  3139. tVal->push_back( atoi( it->second.c_str() ));
  3140. };
  3141. };
  3142. };
  3143. };
  3144. };
  3145. deleteResult( result );
  3146. };
  3147. return tVal;
  3148. };
  3149. DBC_INT_LIST *sqliteDBC::listResources( uint32_t NodeID ){
  3150. DBC_INT_LIST * tVal = NULL;
  3151. if( m_bStoIsConnected ){
  3152. char tQBuf[CTEXT];
  3153. AAssoc *result;
  3154. if ( NodeID == 0 ) NodeID = m_nServerNodeID;
  3155. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID );
  3156. #ifdef DEBUG
  3157. LOG(tQBuf);
  3158. #endif
  3159. result = m_pDataStorage->Query( tQBuf );
  3160. if( result!= NULL ) {
  3161. if( result->size() > 0 ) {
  3162. Assoc *cArr;
  3163. tVal = new DBC_INT_LIST();
  3164. if( tVal != NULL ) {
  3165. Assoc::iterator it;
  3166. for(int i=0;i<result->size();i++) {
  3167. cArr = result->at(i);
  3168. it = cArr->find("id");
  3169. if( it != cArr->end() ){
  3170. tVal->push_back( atoi( it->second.c_str() ));
  3171. };
  3172. };
  3173. };
  3174. };
  3175. };
  3176. deleteResult( result );
  3177. };
  3178. return tVal;
  3179. };
  3180. void sqliteDBC::registerServer( uint32_t SiteID, CSTR identity, int PID, uint32_t *ServID, uint32_t *ServNode ){
  3181. if( m_bStoIsConnected ){
  3182. m_nSiteID = SiteID;
  3183. char tQBuf[MAX_PATH_LEN];
  3184. #ifdef DEFAULT_SERVER_ID
  3185. 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'",
  3186. DEFAULT_SERVER_ID, SiteID, identity, _BUILD_STRING_, identity, _BUILD_STRING_ );
  3187. #else
  3188. sprintf( tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'",
  3189. SiteID, identity, _BUILD_STRING_, _BUILD_STRING_ );
  3190. #endif
  3191. #ifdef DEBUG_ALL
  3192. LOG(tQBuf);
  3193. #endif
  3194. m_pDataStorage->SilentQuery( tQBuf );
  3195. #ifdef DEFAULT_SERVER_ID
  3196. m_nServerID = DEFAULT_SERVER_ID;
  3197. #else
  3198. m_nServerID = getServerID( identity );
  3199. #endif
  3200. *ServID = m_nServerID;
  3201. if( m_nServerID ) {
  3202. //if ( bRecreateMemStructures )
  3203. initMemDB( m_nServerID );
  3204. sprintf( tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID );
  3205. #ifdef DEBUG_ALL
  3206. LOG(tQBuf);
  3207. #endif
  3208. m_pDataMemory->SilentQuery( tQBuf );
  3209. 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 );
  3210. #ifdef DEBUG_ALL
  3211. LOG(tQBuf);
  3212. #endif
  3213. m_nServerNodeID = ( uint32_t ) m_pDataStorage->QueryID( tQBuf );
  3214. if( m_nServerNodeID== 0 ){
  3215. m_nServerNodeID = getServerNode( m_nServerID );
  3216. };
  3217. *ServNode = m_nServerNodeID;
  3218. // Clenup Nodes and services ...
  3219. sprintf(tQBuf,"UPDATE `nodes` SET `online`=0" );
  3220. #ifdef DEBUG_ALL
  3221. LOG(tQBuf);
  3222. #endif
  3223. m_pDataStorage->SilentQuery( tQBuf );
  3224. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0 WHERE `manual`=0" );
  3225. #ifdef DEBUG_ALL
  3226. LOG(tQBuf);
  3227. #endif
  3228. m_pDataStorage->SilentQuery( tQBuf );
  3229. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `manual`=0 )" );
  3230. #ifdef DEBUG_ALL
  3231. LOG(tQBuf);
  3232. #endif
  3233. m_pDataStorage->SilentQuery( tQBuf );
  3234. //sprintf(tQBuf,"UPDATE `cameras` SET `online`=1 WHERE `manual`=1" );
  3235. #ifdef DEBUG_ALL
  3236. //LOG(tQBuf);
  3237. #endif
  3238. //m_pDataStorage->SilentQuery( tQBuf );
  3239. //sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `online`=1 )" );
  3240. #ifdef DEBUG_ALL
  3241. //LOG(tQBuf);
  3242. #endif
  3243. //m_pDataStorage->SilentQuery( tQBuf );
  3244. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  3245. #ifdef DEBUG_ALL
  3246. // LOG(tQBuf);
  3247. #endif
  3248. // m_pDataStorage->SilentQuery( tQBuf );
  3249. //cleanResources( tServerNodeID );
  3250. setNodeOnline( m_nServerNodeID );
  3251. };
  3252. };
  3253. };
  3254. bool sqliteDBC::setLRFDataStatus( uint32_t lrfID, uint8_t ST ){
  3255. if( m_bStoIsConnected ) {
  3256. char tQBuf[MAX_PATH_LEN];
  3257. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `data_status`=%d WHERE `id`=%d", ST, lrfID );
  3258. #ifdef DEBUG
  3259. LOG(tQBuf);
  3260. #endif
  3261. m_pDataStorage->SilentQuery( tQBuf );
  3262. return true;
  3263. };
  3264. return false;
  3265. };
  3266. bool sqliteDBC::setLRFDetectionProfile( uint32_t lrfID, uint8_t profileID ){
  3267. if( m_bStoIsConnected ) {
  3268. char tQBuf[MAX_PATH_LEN];
  3269. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `detection_profile_id`=%d WHERE `id`=%d", profileID, lrfID );
  3270. #ifdef DEBUG_ALL
  3271. LOG(tQBuf);
  3272. #endif
  3273. m_pDataStorage->SilentQuery( tQBuf );
  3274. return true;
  3275. };
  3276. return false;
  3277. };
  3278. bool sqliteDBC::setLRFMaskStatus( uint32_t maskID, bool status ){
  3279. if( m_bStoIsConnected ) {
  3280. char tQBuf[MAX_PATH_LEN];
  3281. if( status ) {
  3282. sprintf( tQBuf, "UPDATE `lrf_masks` SET `status`='E' WHERE `id`=%d", maskID );
  3283. } else {
  3284. sprintf( tQBuf, "UPDATE `lrf_masks` SET `status`='D' WHERE `id`=%d", maskID );
  3285. };
  3286. #ifdef DEBUG_ALL
  3287. LOG(tQBuf);
  3288. #endif
  3289. m_pDataStorage->SilentQuery( tQBuf );
  3290. return true;
  3291. };
  3292. return false;
  3293. };
  3294. bool sqliteDBC::setLRFAltSerial( uint32_t lrfID, CSTR Serial ){
  3295. if( m_bStoIsConnected ) {
  3296. char tQBuf[MAX_PATH_LEN];
  3297. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_serial`='%s' WHERE `id`=%d", Serial, lrfID );
  3298. #ifdef DEBUG_ALL
  3299. LOG(tQBuf);
  3300. #endif
  3301. m_pDataStorage->SilentQuery( tQBuf );
  3302. return true;
  3303. };
  3304. return false;
  3305. };
  3306. bool sqliteDBC::setLRFAltVerison( uint32_t lrfID, CSTR version ){
  3307. if( m_bStoIsConnected ) {
  3308. char tQBuf[MAX_PATH_LEN];
  3309. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `alt_version`='%s' WHERE `id`=%d", version, lrfID );
  3310. #ifdef DEBUG_ALL
  3311. LOG(tQBuf);
  3312. #endif
  3313. m_pDataStorage->SilentQuery( tQBuf );
  3314. return true;
  3315. };
  3316. return false;
  3317. };
  3318. bool sqliteDBC::setNodeOffline( uint32_t NodeID ){
  3319. if( m_bStoIsConnected ){
  3320. char tQBuf[CTEXT];
  3321. if( NodeID ) {
  3322. sprintf(tQBuf,"UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID );
  3323. #ifdef DEBUG_ALL
  3324. LOG(tQBuf);
  3325. #endif
  3326. m_pDataStorage->SilentQuery( tQBuf );
  3327. sprintf(tQBuf,"UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID );
  3328. #ifdef DEBUG_ALL
  3329. LOG(tQBuf);
  3330. #endif
  3331. m_pDataStorage->SilentQuery( tQBuf );
  3332. return true;
  3333. };
  3334. };
  3335. return false;
  3336. };
  3337. bool sqliteDBC::setNodeOnline( uint32_t NodeID, uint8_t status ){
  3338. if( m_bStoIsConnected ){
  3339. char tQBuf[CTEXT];
  3340. if( NodeID ) {
  3341. sprintf(tQBuf,"UPDATE `nodes` SET `online`=%d WHERE `id`=%d", status, NodeID );
  3342. #ifdef DEBUG
  3343. LOG(tQBuf);
  3344. #endif
  3345. m_pDataStorage->SilentQuery( tQBuf );
  3346. sprintf(tQBuf,"UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID );
  3347. #ifdef DEBUG
  3348. LOG(tQBuf);
  3349. #endif
  3350. m_pDataStorage->SilentQuery( tQBuf );
  3351. return true;
  3352. };
  3353. };
  3354. return false;
  3355. };
  3356. bool sqliteDBC::setRessourceActive( uint32_t RessourceID, bool Status ){
  3357. if( m_bStoIsConnected ){
  3358. char tQBuf[CTEXT];
  3359. if( RessourceID ) {
  3360. sprintf(tQBuf,"UPDATE `resources` SET `active`=%d WHERE `id`=%d", Status, RessourceID );
  3361. #ifdef DEBUG
  3362. LOG(tQBuf);
  3363. #endif
  3364. m_pDataStorage->SilentQuery( tQBuf );
  3365. return true;
  3366. };
  3367. };
  3368. return false;
  3369. };
  3370. bool sqliteDBC::setStreamValid( uint32_t streamID, bool status ){
  3371. if( m_bStoIsConnected ) {
  3372. char tQBuf[MAX_PATH_LEN];
  3373. if ( status ) {
  3374. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  3375. } else {
  3376. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  3377. };
  3378. #ifdef DEBUG
  3379. LOG(tQBuf);
  3380. #endif
  3381. m_pDataStorage->SilentQuery( tQBuf );
  3382. return true;
  3383. };
  3384. return false;
  3385. };
  3386. void sqliteDBC::unregisterServer( void ){
  3387. if( m_bStoIsConnected ) {
  3388. char tQBuf[MAX_PATH_LEN];
  3389. sprintf(tQBuf,"UPDATE `cameras` SET `online`=0" );
  3390. #ifdef DEBUG_ALL
  3391. LOG(tQBuf);
  3392. #endif
  3393. m_pDataStorage->SilentQuery( tQBuf );
  3394. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=0" );
  3395. #ifdef DEBUG_ALL
  3396. LOG(tQBuf);
  3397. #endif
  3398. m_pDataStorage->SilentQuery( tQBuf );
  3399. };
  3400. setNodeOffline( m_nServerNodeID );
  3401. };
  3402. bool sqliteDBC::updateSystemInfo( DBC_SYS_INFO *ptr ){
  3403. if( m_bStoIsConnected ) {
  3404. char tQBuf[MAX_PATH_LEN];
  3405. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d",
  3406. ptr->OS_INFO,
  3407. ptr->HW_INFO,
  3408. m_nSiteID
  3409. );
  3410. #ifdef DEBUG_ALL
  3411. LOG(tQBuf);
  3412. #endif
  3413. m_pDataStorage->SilentQuery( tQBuf );
  3414. return true;
  3415. };
  3416. return false;
  3417. };
  3418. bool sqliteDBC::fillShadow2D( uint32_t ID, SHADOW & SH ){
  3419. if( m_bStoIsConnected ){
  3420. char tQBuf[CTEXT];
  3421. AAssoc *result;
  3422. 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 );
  3423. LOG(tQBuf);
  3424. result = m_pDataStorage->Query( tQBuf );
  3425. if( result!= NULL ) {
  3426. if( result->size() > 0 ) {
  3427. SH.m_nEnabled = 0xFFFFFFFF;
  3428. SH.m_nPolygonCount = result->size();
  3429. SH.pPolygones = (SHADOW_POLY *) calloc( SH.m_nPolygonCount, sizeof( SHADOW_POLY ) );
  3430. if ( SH.pPolygones ) {
  3431. Assoc * cArr;
  3432. for ( uint8_t idx = 0; idx < result->size(); idx++ ) {
  3433. Assoc::iterator it;
  3434. cArr = result->at(idx);
  3435. it = cArr->find("id");
  3436. if( it != cArr->end() ){
  3437. SH.pPolygones[idx].m_nDBID = atoi(it->second.c_str());
  3438. };
  3439. it = cArr->find("layer");
  3440. if( it != cArr->end() ){
  3441. SH.pPolygones[idx].bType = atoi(it->second.c_str());
  3442. };
  3443. it = cArr->find("status");
  3444. if( it != cArr->end() ){
  3445. SH.m_nEnabled &= ( it->second.c_str()[0] == 'E' )?0xFFFFFFFF:~(1<<SH.pPolygones[idx].bType);
  3446. };
  3447. sprintf(tQBuf, "SELECT * FROM `lrf_mask_vertex_points` WHERE `vertex_id`=%d ORDER BY `id`", SH.pPolygones[idx].m_nDBID );
  3448. LOG(tQBuf);
  3449. AAssoc *v_result = m_pDataStorage->Query( tQBuf );
  3450. if( v_result!= NULL ) {
  3451. if( v_result->size() > 0 ) {
  3452. SH.pPolygones[idx].wCount = v_result->size();
  3453. SH.pPolygones[idx].pPoint = (HZ_POINT *) calloc( SH.pPolygones[idx].wCount, sizeof( HZ_POINT ) );
  3454. if(SH.pPolygones[idx].pPoint ) {
  3455. for ( uint32_t zdx =0; zdx < SH.pPolygones[idx].wCount; zdx++ ) {
  3456. cArr = v_result->at(zdx);
  3457. it = cArr->find("x");
  3458. if( it != cArr->end() ){
  3459. SH.pPolygones[idx].pPoint[zdx].wX = atoi(it->second.c_str());
  3460. };
  3461. it = cArr->find("y");
  3462. if( it != cArr->end() ){
  3463. SH.pPolygones[idx].pPoint[zdx].wY = atoi(it->second.c_str());
  3464. };
  3465. LOG("Point Added!");
  3466. };
  3467. } else {
  3468. LOG("No mem for Points!");
  3469. };
  3470. } else {
  3471. LOG("No Points!");
  3472. SH.pPolygones[idx].wCount = 0;
  3473. SH.pPolygones[idx].pPoint = NULL;
  3474. };
  3475. deleteResult( v_result );
  3476. };
  3477. };
  3478. };
  3479. };
  3480. deleteResult( result );
  3481. };
  3482. return true;
  3483. };
  3484. return false;
  3485. };
  3486. bool sqliteDBC::getAppDebugMode(){
  3487. bool rVal = false;
  3488. if( m_bStoIsConnected ){
  3489. char tQBuf[CTEXT];
  3490. AAssoc *result;
  3491. sprintf(tQBuf, "SELECT `work_mode` FROM `system_props` WHERE `id`=1" );
  3492. #ifdef DEBUG_ALL
  3493. LOG(tQBuf);
  3494. #endif
  3495. result = m_pDataStorage->Query( tQBuf );
  3496. if( result!= NULL ) {
  3497. if( result->size() > 0 ) {
  3498. Assoc *cArr;
  3499. Assoc::iterator it;
  3500. cArr = result->at(0);
  3501. it = cArr->find("work_mode");
  3502. if( it != cArr->end() ){
  3503. rVal = (bool) atoi( it->second.c_str() );
  3504. };
  3505. };
  3506. };
  3507. deleteResult( result );
  3508. };
  3509. return rVal;
  3510. };
  3511. DBC_CAM_DATA *sqliteDBC::getCamData( uint32_t ID ){
  3512. DBC_CAM_DATA *tVal = NULL;
  3513. if( m_bStoIsConnected ){
  3514. char tQBuf[CTEXT];
  3515. AAssoc *result;
  3516. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID );
  3517. #ifdef DEBUG_ALL
  3518. LOG(tQBuf);
  3519. #endif
  3520. result = m_pDataStorage->Query( tQBuf );
  3521. if( result!= NULL ) {
  3522. if( result->size() > 0 ) {
  3523. Assoc *cArr;
  3524. tVal = (DBC_CAM_DATA*) malloc(sizeof(DBC_CAM_DATA));
  3525. if( tVal != NULL ) {
  3526. memset( tVal, 0 , sizeof(DBC_CAM_DATA));
  3527. tVal->id = ID;
  3528. Assoc::iterator it;
  3529. cArr = result->at(0);
  3530. it = cArr->find("type");
  3531. if( it != cArr->end() ){
  3532. tVal->type = atoi( it->second.c_str() );
  3533. };
  3534. it = cArr->find("online");
  3535. if( it != cArr->end() ){
  3536. tVal->online = (bool) atoi( it->second.c_str() );
  3537. };
  3538. it = cArr->find("stream_id");
  3539. if( it != cArr->end() ){
  3540. tVal->stream_id = atoi( it->second.c_str() );
  3541. };
  3542. it = cArr->find("hardware");
  3543. if( it != cArr->end() ){
  3544. strncpy( tVal->hardware, it->second.c_str(), 32 );
  3545. };
  3546. it = cArr->find("name");
  3547. if( it != cArr->end() ){
  3548. strncpy( tVal->name, it->second.c_str(), 64 );
  3549. };
  3550. it = cArr->find("status");
  3551. if( it != cArr->end() ){
  3552. tVal->status = atoi( it->second.c_str() );
  3553. };
  3554. it = cArr->find("session");
  3555. if( it != cArr->end() ){
  3556. strncpy( tVal->session, it->second.c_str(), 32 );
  3557. };
  3558. it = cArr->find("protocol");
  3559. if( it != cArr->end() ){
  3560. tVal->protocol = atoi( it->second.c_str() );
  3561. };
  3562. it = cArr->find("protocol_aux");
  3563. if( it != cArr->end() ){
  3564. tVal->protocol_aux = atoi( it->second.c_str() );
  3565. };
  3566. it = cArr->find("uart_id");
  3567. if( it != cArr->end() ){
  3568. tVal->uart_id = atoi( it->second.c_str() );
  3569. };
  3570. it = cArr->find("x");
  3571. if( it != cArr->end() ){
  3572. tVal->x = atof( it->second.c_str() );
  3573. };
  3574. it = cArr->find("y");
  3575. if( it != cArr->end() ){
  3576. tVal->y = atof( it->second.c_str() );
  3577. };
  3578. it = cArr->find("z");
  3579. if( it != cArr->end() ){
  3580. tVal->z = atof( it->second.c_str() );
  3581. };
  3582. it = cArr->find("x_rot");
  3583. if( it != cArr->end() ){
  3584. tVal->x_rot = atof( it->second.c_str() );
  3585. };
  3586. it = cArr->find("y_rot");
  3587. if( it != cArr->end() ){
  3588. tVal->y_rot = atof( it->second.c_str() );
  3589. };
  3590. it = cArr->find("z_rot");
  3591. if( it != cArr->end() ){
  3592. tVal->z_rot = atof( it->second.c_str() );
  3593. };
  3594. it = cArr->find("imager_type");
  3595. if( it != cArr->end() ){
  3596. tVal->imager_type = atoi( it->second.c_str() );
  3597. };
  3598. it = cArr->find("lens_f");
  3599. if( it != cArr->end() ){
  3600. tVal->lens_f = atoi( it->second.c_str() );
  3601. };
  3602. it = cArr->find("ptz_speed_step");
  3603. if( it != cArr->end() ){
  3604. tVal->ptz_speed_step = atof( it->second.c_str() );
  3605. };
  3606. it = cArr->find("pan_model");
  3607. if( it != cArr->end() ){
  3608. tVal->pan_model = atoi( it->second.c_str() );
  3609. };
  3610. it = cArr->find("pan_angles");
  3611. if( it != cArr->end() ){
  3612. tVal->pan_angles = atoi( it->second.c_str() );
  3613. };
  3614. it = cArr->find("tilt_model");
  3615. if( it != cArr->end() ){
  3616. tVal->tilt_model = atoi( it->second.c_str() );
  3617. };
  3618. it = cArr->find("tilt_angles");
  3619. if( it != cArr->end() ){
  3620. tVal->tilt_angles = atoi( it->second.c_str() );
  3621. };
  3622. it = cArr->find("zoom_model");
  3623. if( it != cArr->end() ){
  3624. tVal->zoom_model = atoi( it->second.c_str() );
  3625. };
  3626. it = cArr->find("zoom");
  3627. if( it != cArr->end() ){
  3628. tVal->zoom = atof( it->second.c_str() );
  3629. };
  3630. it = cArr->find("max_range");
  3631. if( it != cArr->end() ){
  3632. tVal->max_range = atof( it->second.c_str() );
  3633. };
  3634. it = cArr->find("access_login");
  3635. if( it != cArr->end() ){
  3636. strncpy( tVal->username, it->second.c_str(), 16 );
  3637. };
  3638. it = cArr->find("access_pass");
  3639. if( it != cArr->end() ){
  3640. strncpy( tVal->password, it->second.c_str(), 16 );
  3641. };
  3642. it = cArr->find("uuid");
  3643. if( it != cArr->end() ){
  3644. strncpy( tVal->uuid, it->second.c_str(), 64 );
  3645. };
  3646. it = cArr->find("mac");
  3647. if( it != cArr->end() ){
  3648. strncpy( tVal->mac, it->second.c_str(), 16 );
  3649. };
  3650. it = cArr->find("ip");
  3651. if( it != cArr->end() ){
  3652. strncpy( tVal->ip, it->second.c_str(), 16 );
  3653. };
  3654. it = cArr->find("port");
  3655. if( it != cArr->end() ){
  3656. tVal->port = atoi( it->second.c_str() );
  3657. };
  3658. it = cArr->find("onvif_access");
  3659. if( it != cArr->end() ){
  3660. strncpy( tVal->onvif_access, it->second.c_str(), 5);
  3661. };
  3662. it = cArr->find("onvif_ctrl");
  3663. if( it != cArr->end() ){
  3664. strncpy( tVal->onvif_ctrl, it->second.c_str(), 250);
  3665. };
  3666. it = cArr->find("ptz_token");
  3667. if( it != cArr->end() ){
  3668. strncpy( tVal->ptz_token, it->second.c_str(), 64);
  3669. };
  3670. it = cArr->find("trigger_mode");
  3671. if( it != cArr->end() ){
  3672. tVal->triggering_mode = atoi( it->second.c_str() );
  3673. };
  3674. it = cArr->find("auth_mode");
  3675. if( it != cArr->end() ){
  3676. tVal->auth_mode = atoi( it->second.c_str() );
  3677. };
  3678. it = cArr->find("ptz_axel");
  3679. if( it != cArr->end() ){
  3680. tVal->ptz_axel = atoi( it->second.c_str() );
  3681. };
  3682. it = cArr->find("manual");
  3683. if( it != cArr->end() ){
  3684. tVal->manual = (bool) atoi( it->second.c_str() );
  3685. };
  3686. it = cArr->find("homing_enable");
  3687. if( it != cArr->end() ){
  3688. tVal->homing_enable = (bool) atoi( it->second.c_str() );
  3689. };
  3690. it = cArr->find("homing_to");
  3691. if( it != cArr->end() ){
  3692. tVal->homing_to = atoi( it->second.c_str() );
  3693. };
  3694. it = cArr->find("home_token");
  3695. if( it != cArr->end() ){
  3696. strncpy( tVal->home_token, it->second.c_str(), 32 );
  3697. };
  3698. };
  3699. };
  3700. };
  3701. deleteResult( result );
  3702. };
  3703. return tVal;
  3704. };
  3705. uint32_t sqliteDBC::getCamId( CSTR UUID ){
  3706. uint32_t RetVal = 0;
  3707. if( m_bStoIsConnected ){
  3708. char tQBuf[CTEXT];
  3709. AAssoc *result;
  3710. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID );
  3711. #ifdef DEBUG_ALL
  3712. LOG(tQBuf);
  3713. #endif
  3714. result = m_pDataStorage->Query( tQBuf );
  3715. if( result!= NULL ) {
  3716. if( result->size() > 0 ) {
  3717. Assoc *cArr;
  3718. Assoc::iterator it;
  3719. cArr = result->at(0);
  3720. it = cArr->find("id");
  3721. if( it != cArr->end() ){
  3722. RetVal = atoi( it->second.c_str() );
  3723. };
  3724. };
  3725. };
  3726. deleteResult( result );
  3727. };
  3728. return RetVal;
  3729. };
  3730. CSTR sqliteDBC::getDataParam( uint8_t pid ){
  3731. switch ( pid ) {
  3732. case 0: return strdup( m_pDbFile ); break;
  3733. case 1: return strdup( web_access_user ); break;
  3734. case 2: return strdup( web_access_group ); break;
  3735. };
  3736. return NULL;
  3737. };
  3738. bool sqliteDBC::updateIPCam( uint32_t CamID, CSTR Addr ){
  3739. if( m_bStoIsConnected ) {
  3740. char tQBuf[MAX_PATH_LEN];
  3741. char tIpBuf[CTEXT];
  3742. char *tPos;
  3743. char *tPos2;
  3744. uint16_t tPort = 0;
  3745. tPos = strstr(Addr,"//");
  3746. if( tPos != NULL ) {
  3747. tPos2 = strstr( tPos + 2,":");
  3748. if( tPos2 != NULL ) {
  3749. LOG("IP Port Defined!");
  3750. int len, len2, len3;
  3751. len = tPos2 - tPos - 2;
  3752. strncpy( tIpBuf, tPos + 2, len);
  3753. tIpBuf[len] = (char)0;
  3754. tPort = atoi( tPos2 + 1 );
  3755. //strncpy( tPath, tPos + 2 + len, CTEXT );
  3756. } else {
  3757. tPos2 = strstr( tPos + 2,"/");
  3758. if( tPos2 != NULL ) {
  3759. int len, len2, len3;
  3760. len = tPos2 - tPos - 2;
  3761. strncpy( tIpBuf, tPos + 2, len);
  3762. tIpBuf[len] = (char)0;
  3763. //strncpy( tPath, tPos + 2 + len, CTEXT );
  3764. } else {
  3765. tPos2 = strstr( tPos + 2,"/");
  3766. LOG_ERROR("Pos 2 - err");
  3767. tIpBuf[0] = (char)0;
  3768. };
  3769. };
  3770. } else {
  3771. LOG_ERROR("Pos 0 - err");
  3772. tIpBuf[0] = (char)0;
  3773. };
  3774. if ( tPort ) {
  3775. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d",
  3776. Addr,
  3777. tIpBuf,
  3778. tPort,
  3779. CamID
  3780. );
  3781. } else {
  3782. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d",
  3783. Addr,
  3784. tIpBuf,
  3785. CamID
  3786. );
  3787. }
  3788. #ifdef DEBUG
  3789. LOG(tQBuf);
  3790. #endif
  3791. m_pDataStorage->SilentQuery( tQBuf );
  3792. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  3793. #ifdef DEBUG_ALL
  3794. LOG(tQBuf);
  3795. #endif
  3796. m_pDataStorage->SilentQuery( tQBuf );
  3797. return true;
  3798. };
  3799. return false;
  3800. };
  3801. bool sqliteDBC::updateCamRange( uint32_t camID, float range ){
  3802. if( m_bStoIsConnected ) {
  3803. char tQBuf[MAX_PATH_LEN];
  3804. sprintf(tQBuf,"UPDATE `cameras` SET `max_range`=%.3f WHERE `id`=%d",
  3805. range,
  3806. camID
  3807. );
  3808. #ifdef DEBUG_EXTRA
  3809. LOG(tQBuf);
  3810. #endif
  3811. m_pDataStorage->SilentQuery( tQBuf );
  3812. return true;
  3813. };
  3814. return false;
  3815. };
  3816. bool sqliteDBC::updateCamOnlineStatus( uint32_t camID, bool online ){
  3817. if( m_bStoIsConnected ) {
  3818. char tQBuf[MAX_PATH_LEN];
  3819. if ( online ) {
  3820. sprintf( tQBuf, "UPDATE `cameras` SET `online`=1 WHERE `id`=%d", camID );
  3821. } else {
  3822. sprintf( tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `id`=%d", camID );
  3823. };
  3824. m_pDataStorage->SilentQuery(tQBuf);
  3825. if ( online ) {
  3826. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", camID);
  3827. } else {
  3828. sprintf( tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id`=%d", camID );
  3829. };
  3830. m_pDataStorage->SilentQuery(tQBuf);
  3831. return true;
  3832. };
  3833. return false;
  3834. };
  3835. bool sqliteDBC::updateIPCam( CSTR uuid, CSTR Addr ){
  3836. if( m_bStoIsConnected ) {
  3837. uint32_t cID = getCamId( uuid );
  3838. if ( cID ) {
  3839. return updateIPCam( cID, Addr );
  3840. };
  3841. };
  3842. return false;
  3843. };
  3844. bool sqliteDBC::updateLocalCam( CSTR uuid ){
  3845. if( m_bStoIsConnected ) {
  3846. uint32_t cID = getCamId( uuid );
  3847. if ( cID ) {
  3848. return updateLocalCam( cID );
  3849. };
  3850. };
  3851. return false;
  3852. };
  3853. bool sqliteDBC::updateLocalCam( uint32_t CamID ){
  3854. if( m_bStoIsConnected ) {
  3855. char tQBuf[MAX_PATH_LEN];
  3856. sprintf(tQBuf,"UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID );
  3857. #ifdef DEBUG
  3858. LOG(tQBuf);
  3859. #endif
  3860. m_pDataStorage->SilentQuery( tQBuf );
  3861. sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID );
  3862. #ifdef DEBUG
  3863. LOG(tQBuf);
  3864. #endif
  3865. m_pDataStorage->SilentQuery( tQBuf );
  3866. return true;
  3867. };
  3868. return false;
  3869. };
  3870. bool sqliteDBC::updateLRFComStat( uint32_t lrfID, uint32_t ErrorPerMinute ){
  3871. if( m_bStoIsConnected ) {
  3872. char tQBuf[MAX_PATH_LEN];
  3873. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `com_epm`=%d WHERE `id`=%d", ErrorPerMinute, lrfID );
  3874. #ifdef DEBUG_ALL
  3875. LOG(tQBuf);
  3876. #endif
  3877. m_pDataStorage->SilentQuery( tQBuf );
  3878. return true;
  3879. };
  3880. return false;
  3881. };
  3882. bool sqliteDBC::updateLRFFrozenStatus( uint32_t lrfID, bool FFT, bool SFT ){
  3883. if( m_bStoIsConnected ) {
  3884. char tQBuf[MAX_PATH_LEN];
  3885. sprintf(tQBuf, "UPDATE `lrf_ini_settings` SET `FREEZ_FIRST_TH`=%d, `SAVE_FIRST_TH`=%d WHERE `sensor_id`=%d", FFT, SFT, lrfID );
  3886. #ifdef DEBUG
  3887. LOG(tQBuf);
  3888. #endif
  3889. m_pDataStorage->SilentQuery( tQBuf );
  3890. return true;
  3891. };
  3892. return false;
  3893. };
  3894. 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 ){
  3895. if( m_bStoIsConnected ) {
  3896. char tQBuf[MAX_PATH_LEN];
  3897. if( master ) {
  3898. 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",
  3899. lrfID,
  3900. profileID,
  3901. nOrder,
  3902. nOffset,
  3903. nDelta,
  3904. nFlip,
  3905. nWidth,
  3906. bSST,
  3907. nSun,
  3908. GP,
  3909. spikes,
  3910. nOrder,
  3911. nOffset,
  3912. nDelta,
  3913. nFlip,
  3914. nWidth,
  3915. bSST,
  3916. nSun,
  3917. GP,
  3918. spikes );
  3919. } else {
  3920. 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",
  3921. lrfID,
  3922. profileID,
  3923. nOrder,
  3924. nOffset,
  3925. nDelta,
  3926. nFlip,
  3927. nWidth,
  3928. bSST,
  3929. nSun,
  3930. GP,
  3931. spikes,
  3932. nOrder,
  3933. nOffset,
  3934. nDelta,
  3935. nFlip,
  3936. nWidth,
  3937. bSST,
  3938. nSun,
  3939. GP,
  3940. spikes );
  3941. };
  3942. #ifdef DEBUG_ALL
  3943. LOG(tQBuf);
  3944. #endif
  3945. m_pDataStorage->SilentQuery( tQBuf );
  3946. return true;
  3947. };
  3948. return false;
  3949. };
  3950. bool sqliteDBC::updateLRFDryContact( uint32_t lrfID, bool bAuto, bool bNO, uint16_t wWindow ){
  3951. if( m_bStoIsConnected ) {
  3952. char tQBuf[MAX_PATH_LEN];
  3953. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `dc_auto`=%d, `dc_no`=%d, `dc_window`=%d WHERE `id`=%d", bAuto, bNO, wWindow, lrfID );
  3954. #ifdef DEBUG_ALL
  3955. LOG(tQBuf);
  3956. #endif
  3957. m_pDataStorage->SilentQuery( tQBuf );
  3958. return true;
  3959. };
  3960. return false;
  3961. };
  3962. bool sqliteDBC::updateLRFSerial( uint32_t lrfID, CSTR Serial ){
  3963. if( m_bStoIsConnected ) {
  3964. char tQBuf[MAX_PATH_LEN];
  3965. CSTR tES = m_pDataStorage->escapeString( Serial );
  3966. if( tES ) {
  3967. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID );
  3968. free( tES );
  3969. #ifdef DEBUG_ALL
  3970. LOG(tQBuf);
  3971. #endif
  3972. m_pDataStorage->SilentQuery( tQBuf );
  3973. return true;
  3974. };
  3975. };
  3976. return false;
  3977. };
  3978. bool sqliteDBC::updateLRFUpload( uint32_t id, uint8_t status ){
  3979. if( m_bStoIsConnected ) {
  3980. char tQBuf[MAX_PATH_LEN];
  3981. sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=%d WHERE `id`=%d", status, id );
  3982. #ifdef DEBUG_ALL
  3983. LOG(tQBuf);
  3984. #endif
  3985. m_pDataStorage->SilentQuery( tQBuf );
  3986. return true;
  3987. };
  3988. return false;
  3989. };
  3990. bool sqliteDBC::updateLRFUnfinishedUploads( uint32_t deviceID ){
  3991. if( m_bStoIsConnected ) {
  3992. char tQBuf[MAX_PATH_LEN];
  3993. sprintf( tQBuf, "UPDATE `lrf_uploads` SET `status`=-1 WHERE `sensor_id`=%d AND `status` < 101 ", deviceID );
  3994. #ifdef DEBUG_ALL
  3995. LOG(tQBuf);
  3996. #endif
  3997. m_pDataStorage->SilentQuery( tQBuf );
  3998. return true;
  3999. };
  4000. return false;
  4001. };
  4002. bool sqliteDBC::updateLRFVerison( uint32_t lrfID, CSTR version ){
  4003. if( m_bStoIsConnected ) {
  4004. char tQBuf[MAX_PATH_LEN];
  4005. CSTR tES = m_pDataStorage->escapeString( version );
  4006. if( tES ) {
  4007. sprintf( tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID );
  4008. free( tES );
  4009. #ifdef DEBUG_ALL
  4010. LOG(tQBuf);
  4011. #endif
  4012. m_pDataStorage->SilentQuery( tQBuf );
  4013. return true;
  4014. };
  4015. };
  4016. return false;
  4017. };
  4018. bool sqliteDBC::updateServerIdentity( uint32_t m_nServerID, CSTR m_sIdentity ){
  4019. if( m_bStoIsConnected ) {
  4020. char tQBuf[MAX_PATH_LEN];
  4021. CSTR tES = m_pDataStorage->escapeString( m_sIdentity );
  4022. if( tES ) {
  4023. sprintf( tQBuf, "UPDATE `servers` SET `identity`='%s' WHERE `id`=%d", tES, m_nServerID );
  4024. free( tES );
  4025. #ifdef DEBUG_ALL
  4026. LOG(tQBuf);
  4027. #endif
  4028. m_pDataStorage->SilentQuery( tQBuf );
  4029. return true;
  4030. };
  4031. };
  4032. return false;
  4033. };
  4034. #undef LOG
  4035. #undef LOG_ERROR