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.

5934 lines
145 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. { \
  7. char _tBfrErrMSG2w34[256]; \
  8. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  9. m_pLog->Log("SDBC", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  10. }
  11. #else
  12. #define LOG_ERROR(X) m_pLog->Log("SDBC", this, _LOG_ERROR, X);
  13. #endif
  14. sqliteDBC * sqliteDBC::_instance = NULL;
  15. /**
  16. * Returns the single instance of the object.
  17. */
  18. sqliteDBC * sqliteDBC::getInstance(config_setting_t * Settings)
  19. {
  20. // check if the instance has been created yet
  21. if (0 == _instance)
  22. {
  23. // if not, then create it
  24. _instance = new sqliteDBC(Settings);
  25. };
  26. // return the single instance
  27. return _instance;
  28. }
  29. /*
  30. uint32_t sqliteDBC::getTsFromRDF( REPORT_DIRINFO &RDF ){
  31. struct tm ltm;
  32. memset( &ltm, 0, sizeof( struct tm ));
  33. ltm.tm_mday = atoi( RDF.DAY );
  34. ltm.tm_mon = atoi( RDF.MONTH ) - 1;
  35. ltm.tm_year = atoi( RDF.YEAR ) - 1900;
  36. return (uint32_t) timelocal( &ltm );
  37. };
  38. bool sqliteDBC::createAccessibleDir( CSTR path ){
  39. if( mkdir( path, STORAGE_DIR_MODE ) != 0 ) return false;
  40. chown( path, web_access_user, web_access_group );
  41. return true;
  42. };
  43. bool sqliteDBC::checkRDFDir( CSTR basePath, REPORT_DIRINFO &RDF, bool recreate ){
  44. char tBuff[MAX_PATH];
  45. sprintf( tBuff, "%s/%s", basePath, RDF.YEAR );
  46. #ifdef DEBUG
  47. LOG( tBuff );
  48. #endif
  49. if( fileExist( tBuff ) == 0 ){
  50. if ( recreate ) {
  51. if (!createAccessibleDir(tBuff)) return false;
  52. } else return false;
  53. };
  54. sprintf( tBuff, "%s/%s/%s", basePath, RDF.YEAR, RDF.MONTH );
  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/%s", basePath, RDF.YEAR, RDF.MONTH, RDF.DAY );
  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. return true;
  73. };
  74. */
  75. sqliteDBC::sqliteDBC(config_setting_t * Settings) :
  76. dataBinder()
  77. {
  78. m_pLog = basicLogCollection::getInstance();
  79. m_pSettings = basicSettingsProvider::getInstance()->getSubNode(Settings, "DATABASE");
  80. m_bStoIsConnected = false;
  81. m_bMemIsConnected = false;
  82. m_bRepIsConnected = false;
  83. m_bStaIsConnected = false;
  84. m_bLastEventWasClosed = false;
  85. m_pDataStorage = NULL;
  86. m_pDataMemory = NULL;
  87. m_pDataReports = NULL;
  88. m_pDataStatistics = NULL;
  89. m_nServerID = 1;
  90. }
  91. sqliteDBC::~sqliteDBC()
  92. {
  93. if (m_pDataStorage)
  94. {
  95. delete(m_pDataStorage);
  96. }
  97. if (m_pDataMemory)
  98. {
  99. delete(m_pDataMemory);
  100. }
  101. if (m_pDataReports)
  102. {
  103. delete(m_pDataReports);
  104. }
  105. if (m_pDataStatistics)
  106. {
  107. delete(m_pDataStatistics);
  108. }
  109. m_bStoIsConnected = false;
  110. m_bMemIsConnected = false;
  111. m_bRepIsConnected = false;
  112. m_bStaIsConnected = false;
  113. _instance = NULL;
  114. }
  115. bool sqliteDBC::connect(void)
  116. {
  117. if (m_pSettings == NULL)
  118. {
  119. return false;
  120. }
  121. bool bRecreateMemStructures = false;
  122. config_setting_t * pSetting;
  123. const char * tBuff;
  124. // Disconnect if Connected
  125. m_bStoIsConnected = false;
  126. m_bMemIsConnected = false;
  127. m_bRepIsConnected = false;
  128. m_bStaIsConnected = false;
  129. m_bLastEventWasClosed = false;
  130. // Apply New Config
  131. #ifdef DEBUG
  132. LOG("Reading DB Settings ... ");
  133. #endif
  134. if (! config_setting_lookup_string(m_pSettings, "DATA_FILE_NAME", (const char **) &tBuff))
  135. {
  136. pSetting = config_setting_add(m_pSettings, "DATA_FILE_NAME", CONFIG_TYPE_STRING);
  137. sprintf(m_pDbFile, "data/mysqlitedb.db");
  138. config_setting_set_string(pSetting, m_pDbFile);
  139. basicSettingsProvider::getInstance()->compromise();
  140. LOG("Data Storage not set - using default");
  141. }
  142. else
  143. {
  144. sprintf(m_pDbFile, tBuff);
  145. };
  146. if (! config_setting_lookup_string(m_pSettings, "REPORTS_DIR_NAME", (const char **) &tBuff))
  147. {
  148. pSetting = config_setting_add(m_pSettings, "REPORTS_DIR_NAME", CONFIG_TYPE_STRING);
  149. sprintf(m_pDbReportBase, "data/reports");
  150. config_setting_set_string(pSetting, m_pDbReportBase);
  151. basicSettingsProvider::getInstance()->compromise();
  152. LOG("Reports Storage not set - using default");
  153. }
  154. else
  155. {
  156. sprintf(m_pDbReportBase, tBuff);
  157. };
  158. if (! config_setting_lookup_string(m_pSettings, "WEB_ACCESS_USER", (const char **) &tBuff))
  159. {
  160. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_USER", CONFIG_TYPE_STRING);
  161. sprintf(web_access_user, "www-data");
  162. config_setting_set_string(pSetting, web_access_user);
  163. basicSettingsProvider::getInstance()->compromise();
  164. LOG("Web Access User not set - using default");
  165. }
  166. else
  167. {
  168. sprintf(web_access_user, tBuff);
  169. };
  170. if (! config_setting_lookup_string(m_pSettings, "WEB_ACCESS_GROUP", (const char **) &tBuff))
  171. {
  172. pSetting = config_setting_add(m_pSettings, "WEB_ACCESS_GROUP", CONFIG_TYPE_STRING);
  173. sprintf(web_access_group, "www-data");
  174. config_setting_set_string(pSetting, web_access_group);
  175. basicSettingsProvider::getInstance()->compromise();
  176. LOG("Web Access Group not set - using default");
  177. }
  178. else
  179. {
  180. sprintf(web_access_group, tBuff);
  181. };
  182. if (! fileExist(DB_SHMEM_POINT))
  183. {
  184. bRecreateMemStructures = true;
  185. mkdir(DB_SHMEM_POINT, STORAGE_DIR_MODE); // S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  186. chown(DB_SHMEM_POINT, getUserUid(web_access_user), getUserGid(web_access_group)); // To make Accessed from PHP
  187. chmod(DB_SHMEM_POINT, STORAGE_DIR_MODE);
  188. };
  189. char tB[128];
  190. if (! config_setting_lookup_string(m_pSettings, "DATA_MEMORY_NAME", (const char **) &tBuff))
  191. {
  192. pSetting = config_setting_add(m_pSettings, "DATA_MEMORY_NAME", CONFIG_TYPE_STRING);
  193. tBuff = tB;
  194. sprintf(tB, "mem_db.db");
  195. config_setting_set_string(pSetting, tB);
  196. basicSettingsProvider::getInstance()->compromise();
  197. LOG("Memory Storage not set - using default");
  198. };
  199. sprintf(m_pDbMemory, "%s/%s", DB_SHMEM_POINT, tBuff);
  200. basicSettingsProvider::getInstance()->validate();
  201. // Kill Connection if any
  202. if (m_pDataStorage)
  203. {
  204. delete(m_pDataStorage);
  205. }
  206. if (m_pDataMemory)
  207. {
  208. delete(m_pDataMemory);
  209. }
  210. if (m_pDataReports)
  211. {
  212. delete(m_pDataReports);
  213. }
  214. if (m_pDataStatistics)
  215. {
  216. delete(m_pDataStatistics);
  217. }
  218. m_pDataMemory = NULL;
  219. m_pDataStorage = NULL;
  220. m_pDataReports = NULL;
  221. m_pDataStatistics = NULL;
  222. #ifdef DEBUG
  223. LOG(m_pDbFile);
  224. #endif
  225. // Create New Connections
  226. m_pDataStorage = new SQLite_Connector();
  227. if (m_pDataStorage)
  228. {
  229. if (m_pDataStorage->Connect(m_pDbFile, 0, true))
  230. {
  231. #ifdef DEBUG
  232. LOG("DB File Opened");
  233. #endif
  234. chown(m_pDbFile, getUserUid(web_access_user), getUserGid(web_access_group));
  235. chmod(m_pDbFile, DB_FILE_MODE);
  236. m_bStoIsConnected = true;
  237. }
  238. else
  239. {
  240. LOG_ERROR("Cannot Connect to DB File");
  241. delete(m_pDataStorage);
  242. m_pDataStorage = NULL;
  243. LOG("Storage Deleted!");
  244. return false;
  245. };
  246. };
  247. #ifdef DEBUG
  248. LOG(m_pDbMemory);
  249. #endif
  250. if (! fileExist(m_pDbMemory))
  251. {
  252. bRecreateMemStructures = true;
  253. // Always recreate
  254. unlink(m_pDbMemory);
  255. };
  256. m_pDataMemory = new SQLite_Connector();
  257. if (m_pDataMemory)
  258. {
  259. if (m_pDataMemory->Connect(m_pDbMemory, 0, true))
  260. {
  261. #ifdef DEBUG
  262. LOG("DB Memory Opened");
  263. #endif
  264. chown(m_pDbMemory, getUserUid(web_access_user), getUserGid(web_access_group));
  265. chmod(m_pDbMemory, DB_FILE_MODE);
  266. m_pDataMemory->SilentQuery("PRAGMA journal_mode=WAL");
  267. // Recreate at Register Server
  268. m_bMemIsConnected = true;
  269. }
  270. else
  271. {
  272. LOG_ERROR("Cannot Connect to DB Memory");
  273. delete(m_pDataStorage);
  274. delete(m_pDataMemory);
  275. m_bStoIsConnected = false;
  276. m_pDataStorage = NULL;
  277. m_pDataMemory = NULL;
  278. return false;
  279. };
  280. };
  281. sprintf(m_pDbStat, "%s/stat.db", m_pDbReportBase);
  282. m_pDataStatistics = new SQLite_Connector();
  283. if (m_pDataStatistics)
  284. {
  285. if (fileExist(m_pDbStat))
  286. {
  287. m_bStaIsCreated = false;
  288. if (m_pDataStatistics->Connect(m_pDbStat, 0, true))
  289. {
  290. #ifdef DEBUG
  291. LOG(m_pDbStat);
  292. LOG("DB Statistics Opened");
  293. #endif
  294. m_pDataStatistics->SilentQuery("PRAGMA journal_mode=WAL");
  295. m_bStaIsConnected = true;
  296. }
  297. else
  298. {
  299. LOG_ERROR("Cannot Connect to DB Statistics");
  300. delete(m_pDataStorage);
  301. delete(m_pDataMemory);
  302. delete(m_pDataStatistics);
  303. m_bMemIsConnected = false;
  304. m_bStoIsConnected = false;
  305. m_pDataStorage = NULL;
  306. m_pDataMemory = NULL;
  307. m_pDataStatistics = NULL;
  308. return false;
  309. };
  310. }
  311. else
  312. {
  313. m_bStaIsCreated = true;
  314. if (m_pDataStatistics->Connect(m_pDbStat, 0, true))
  315. {
  316. #ifdef DEBUG
  317. LOG("NEW DB Statistics Creating");
  318. #endif
  319. chown(m_pDbStat, getUserUid(web_access_user), getUserGid(web_access_group));
  320. chmod(m_pDbStat, DB_FILE_MODE);
  321. m_pDataStatistics->SilentQuery("PRAGMA journal_mode=WAL");
  322. m_pDataStatistics->SilentQuery(
  323. "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 "
  324. "DEFAULT 0, CONSTRAINT \"date_uni\" UNIQUE(\"year\",\"month\",\"day\") );");
  325. m_bStaIsConnected = true;
  326. }
  327. else
  328. {
  329. LOG_ERROR("Cannot Connect to DB Statistics");
  330. delete(m_pDataStorage);
  331. delete(m_pDataMemory);
  332. delete(m_pDataStatistics);
  333. m_bMemIsConnected = false;
  334. m_bStoIsConnected = false;
  335. m_pDataStorage = NULL;
  336. m_pDataMemory = NULL;
  337. m_pDataStatistics = NULL;
  338. return false;
  339. };
  340. };
  341. };
  342. bool create_tables = false;
  343. m_pDataReports = new SQLite_Connector();
  344. if (m_pDataReports)
  345. {
  346. memset(&m_sPRepInfo, 0, sizeof(REPORT_DIRINFO));
  347. if (! m_bStaIsCreated)
  348. {
  349. #ifdef DEBUG
  350. LOG("OLD Statistics PRESENT Report Created!");
  351. #endif
  352. AAssoc * result = m_pDataStatistics->Query("SELECT * FROM `days` ORDER BY `year` DESC, `month` DESC, `day` DESC LIMIT 1");
  353. if (result)
  354. {
  355. if (result->size() > 0)
  356. {
  357. Assoc * cArr;
  358. Assoc::iterator it;
  359. cArr = result->at(0);
  360. it = cArr->find("year");
  361. if (it != cArr->end())
  362. {
  363. strncpy(m_sPRepInfo.YEAR, it->second.c_str(), 4);
  364. };
  365. it = cArr->find("month");
  366. if (it != cArr->end())
  367. {
  368. sprintf(m_sPRepInfo.MONTH, "%02d", atoi(it->second.c_str()));
  369. };
  370. it = cArr->find("day");
  371. if (it != cArr->end())
  372. {
  373. sprintf(m_sPRepInfo.DAY, "%02d", atoi(it->second.c_str()));
  374. };
  375. m_sPRepInfo.ts = getTsFromRDF(m_sPRepInfo);
  376. }
  377. else
  378. {
  379. create_tables = true;
  380. #ifdef DEBUG
  381. LOG("No Rows Fetched from STAT!");
  382. #endif
  383. };
  384. };
  385. m_pDataMemory->deleteResult(result);
  386. };
  387. if (m_sPRepInfo.ts != 0)
  388. {
  389. #ifdef DEBUG
  390. LOG("Have Old Reports!");
  391. #endif
  392. if (checkRDFDir(m_pDbReportBase, m_sPRepInfo, false) == 0)
  393. {
  394. LOG_ERROR("Error Connecting to Prev Report!");
  395. m_bLastEventWasClosed = true;
  396. create_tables = true;
  397. }
  398. else
  399. {
  400. #ifdef DEBUG
  401. LOG("Connect to old Report Database");
  402. #endif
  403. sprintf(m_pDbReport, "%s/%s/%s/%s/report.db", m_pDbReportBase, m_sPRepInfo.YEAR, m_sPRepInfo.MONTH, m_sPRepInfo.DAY);
  404. LOG(m_pDbReport);
  405. if (fileExist(m_pDbReport))
  406. {
  407. SQLite_Connector * tDBC = new SQLite_Connector();
  408. if (tDBC)
  409. {
  410. if (tDBC->Connect(m_pDbReport, 0, true))
  411. {
  412. char tQBuf[CTEXT];
  413. AAssoc * result;
  414. 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);
  415. #ifdef DEBUG_ALL
  416. LOG(tQBuf);
  417. #endif
  418. result = tDBC->Query(tQBuf);
  419. if (result != NULL)
  420. {
  421. if (result->size() > 0)
  422. {
  423. Assoc * cArr;
  424. Assoc::iterator it;
  425. cArr = result->at(0);
  426. it = cArr->find("evt_type");
  427. if (it != cArr->end())
  428. {
  429. if (atoi(it->second.c_str()) != EVT_LRF_START_SESSION)
  430. {
  431. m_bLastEventWasClosed = true;
  432. }
  433. };
  434. }
  435. else
  436. {
  437. m_bLastEventWasClosed = true;
  438. }
  439. };
  440. deleteResult(result);
  441. }
  442. else
  443. {
  444. LOG_ERROR("Error Connecting to Prev Report!");
  445. m_bLastEventWasClosed = true;
  446. create_tables = true;
  447. };
  448. delete(tDBC);
  449. }
  450. else
  451. {
  452. LOG_ERROR("Error Create DB Connection Object!");
  453. m_bLastEventWasClosed = true;
  454. create_tables = true;
  455. };
  456. }
  457. else
  458. {
  459. LOG_ERROR("Error No Prev Report Database!");
  460. m_bLastEventWasClosed = true;
  461. create_tables = true;
  462. };
  463. };
  464. }
  465. else
  466. {
  467. m_bLastEventWasClosed = true;
  468. create_tables = true;
  469. };
  470. if (m_bLastEventWasClosed)
  471. {
  472. #ifdef DEBUG
  473. LOG("Last Session is Closed!");
  474. #endif
  475. fillRDF(time(NULL), m_sCRepInfo);
  476. }
  477. else
  478. {
  479. #ifdef DEBUG
  480. LOG("Last Session is NOT Closed!");
  481. #endif
  482. memcpy(&m_sCRepInfo, &m_sPRepInfo, sizeof(REPORT_DIRINFO));
  483. };
  484. #ifdef DEBUG
  485. LOG(m_pDbReportBase);
  486. #endif
  487. if (! checkRDFDir(m_pDbReportBase, m_sCRepInfo, true, getUserUid(web_access_user), getUserGid(web_access_group)))
  488. {
  489. LOG_ERROR("Cannot Open Report!!");
  490. return false;
  491. };
  492. sprintf(m_pDbReport, "%s/%s/%s/%s/report.db", m_pDbReportBase, m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  493. #ifdef DEBUG
  494. LOG(m_pDbReport);
  495. #endif
  496. if (m_pDataReports->Connect(m_pDbReport, 0, true))
  497. {
  498. #ifdef DEBUG
  499. LOG("Report File Opened");
  500. #endif
  501. chown(m_pDbReport, getUserUid(web_access_user), getUserGid(web_access_group));
  502. chmod(m_pDbReport, DB_FILE_MODE);
  503. // Recreate Table in any case
  504. // if ( m_sCRepInfo.ts != m_sPRepInfo.ts || create_tables ) {
  505. // Created New Report ??
  506. m_pDataReports->SilentQuery(
  507. "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\" "
  508. "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\" "
  509. "AUTOINCREMENT) )");
  510. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events_2_pictures\" ( \"evt_id\" INTEGER NOT NULL, \"picture_id\" INTEGER NOT NULL )");
  511. 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 )");
  512. m_pDataReports->SilentQuery(
  513. "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, "
  514. "\"data\" BLOB COLLATE BINARY, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  515. m_pDataReports->SilentQuery(
  516. "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\" "
  517. "INTEGER NOT NULL, \"flength\" INTEGER, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  518. //};
  519. m_bRepIsConnected = true;
  520. }
  521. else
  522. {
  523. LOG_ERROR("Cannot Connect to DB File");
  524. delete(m_pDataReports);
  525. m_pDataReports = NULL;
  526. LOG("Storage Deleted!");
  527. return false;
  528. };
  529. };
  530. return (m_bRepIsConnected && m_bMemIsConnected && m_bStoIsConnected && m_bStaIsConnected);
  531. }
  532. bool sqliteDBC::isDatabaseNew()
  533. {
  534. bool RetVal = true;
  535. if (m_bStoIsConnected)
  536. {
  537. char tQBuf[CTEXT];
  538. sprintf(tQBuf, "SELECT `os_info` FROM `system_props` LIMIT 1");
  539. #ifdef DEBUG_ALL
  540. LOG(tQBuf);
  541. #endif
  542. AAssoc * result;
  543. result = m_pDataStorage->Query(tQBuf);
  544. if (result != NULL)
  545. {
  546. if (result->size() > 0)
  547. {
  548. Assoc * cArr;
  549. Assoc::iterator it;
  550. cArr = result->at(0);
  551. it = cArr->find("os_info");
  552. if (it != cArr->end())
  553. {
  554. RetVal = (strlen(it->second.c_str()) < 5);
  555. };
  556. };
  557. };
  558. deleteResult(result);
  559. };
  560. return RetVal;
  561. }
  562. void sqliteDBC::initMemDB(uint32_t sid)
  563. {
  564. m_pDataMemory->SilentQuery("DROP TABLE \"nodes_qcq\"");
  565. m_pDataMemory->SilentQuery(
  566. "CREATE TABLE \"nodes_qcq\" ( \
  567. \"id\" INTEGER NOT NULL,\
  568. \"node_id\" INTEGER NOT NULL,\
  569. \"cmd\" INTEGER NOT NULL,\
  570. \"status\" INTEGER DEFAULT 0,\
  571. \"parameter\" TEXT,\
  572. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  573. );");
  574. m_pDataMemory->SilentQuery("DROP TABLE \"servers\"");
  575. m_pDataMemory->SilentQuery(
  576. "CREATE TABLE \"servers\" (\
  577. \"id\" INTEGER NOT NULL,\
  578. \"ts\" INTEGER DEFAULT 0,\
  579. \"pid\" INTEGER DEFAULT 0,\
  580. \"hw_info\" TEXT,\
  581. \"os_info\" TEXT,\
  582. \"cpu_cores\" INTEGER DEFAULT 1,\
  583. \"stat_cpu1\" REAL DEFAULT 0.0,\
  584. \"stat_cpu5\" REAL DEFAULT 0.0,\
  585. \"stat_cpu15\" REAL DEFAULT 0.0,\
  586. \"stat_mem\" REAL DEFAULT 0.0,\
  587. \"stat_disk\" REAL DEFAULT 0.0,\
  588. \"val_mem_tottal\" INTEGER DEFAULT 0,\
  589. \"val_mem_current\" INTEGER DEFAULT 0,\
  590. \"val_disk_tottal\" INTEGER DEFAULT 0,\
  591. \"val_disk_current\" INTEGER DEFAULT 0,\
  592. \"temp0\" REAL DEFAULT 0.0,\
  593. PRIMARY KEY(\"id\" AUTOINCREMENT)\
  594. );");
  595. char tB[128];
  596. sprintf(tB, "INSERT INTO `servers` (`id`,`ts`) VALUES (%d, strftime('%%s') )", sid);
  597. m_pDataMemory->SilentQuery(tB);
  598. }
  599. CSTR sqliteDBC::getCurrentReportDir(void)
  600. {
  601. CSTR tV = NULL;
  602. if (m_sCRepInfo.ts)
  603. {
  604. char tBuf[256];
  605. sprintf(tBuf, "%s/%s/%s", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  606. tV = strdup(tBuf);
  607. };
  608. return tV;
  609. }
  610. DBC_PUSHED_CMD * sqliteDBC::getNodePushedCMD(uint32_t NodeID)
  611. {
  612. DBC_PUSHED_CMD * RetVal = NULL;
  613. if (m_bMemIsConnected)
  614. {
  615. char QS[MAX_PATH];
  616. AAssoc * result;
  617. sprintf(QS, "select * FROM `nodes_qcq` WHERE `node_id`=%d AND `status`=0 ORDER BY `id` LIMIT 1", NodeID);
  618. #ifdef DEBUG_ALL
  619. LOG(QS);
  620. #endif
  621. result = m_pDataMemory->Query(QS);
  622. if (result)
  623. {
  624. if (result->size() > 0)
  625. {
  626. RetVal = (DBC_PUSHED_CMD *) malloc(sizeof(DBC_PUSHED_CMD));
  627. if (RetVal)
  628. {
  629. Assoc * cArr;
  630. Assoc::iterator it;
  631. memset(RetVal, 0, sizeof(DBC_PUSHED_CMD));
  632. cArr = result->at(0);
  633. it = cArr->find("id");
  634. if (it != cArr->end())
  635. {
  636. RetVal->id = atoi(it->second.c_str());
  637. };
  638. it = cArr->find("cmd");
  639. if (it != cArr->end())
  640. {
  641. RetVal->CMD = atoi(it->second.c_str());
  642. };
  643. it = cArr->find("parameter");
  644. if (it != cArr->end())
  645. {
  646. strncpy(RetVal->PARAM, it->second.c_str(), 256);
  647. RetVal->PARAM[255] = (char) 0;
  648. };
  649. };
  650. #ifdef DEBUG_ALL
  651. }
  652. else
  653. {
  654. LOG("No Rows Fetched!");
  655. #endif
  656. };
  657. };
  658. m_pDataMemory->deleteResult(result);
  659. };
  660. return RetVal;
  661. }
  662. bool sqliteDBC::updateModemManagerActiveCall(uint32_t MMID, int32_t callID)
  663. {
  664. if (m_bMemIsConnected)
  665. {
  666. char QS[MAX_PATH];
  667. sprintf(QS, "UPDATE `module_modem` SET `active_call`=%d WHERE `id`=%d ", callID, MMID);
  668. return m_pDataStorage->SilentQuery(QS);
  669. };
  670. return false;
  671. }
  672. bool sqliteDBC::updateModemManagerLTS(uint32_t MMID, uint32_t ts)
  673. {
  674. if (m_bMemIsConnected)
  675. {
  676. char QS[MAX_PATH];
  677. sprintf(QS, "UPDATE `module_modem` SET `last_trigger_ts`=%d WHERE `id`=%d ", ts, MMID);
  678. return m_pDataStorage->SilentQuery(QS);
  679. };
  680. return false;
  681. }
  682. bool sqliteDBC::updateModemManagerActivePhoneBookID(uint32_t MMID, uint32_t pbID)
  683. {
  684. if (m_bMemIsConnected)
  685. {
  686. char QS[MAX_PATH];
  687. sprintf(QS, "UPDATE `module_modem` SET `active_ph_id`=%d WHERE `id`=%d ", pbID, MMID);
  688. return m_pDataStorage->SilentQuery(QS);
  689. };
  690. return false;
  691. }
  692. bool sqliteDBC::updateModemManagerActiveSMS(uint32_t MMID, int32_t smsID)
  693. {
  694. if (m_bMemIsConnected)
  695. {
  696. char QS[MAX_PATH];
  697. sprintf(QS, "UPDATE `module_modem` SET `active_sms`=%d WHERE `id`=%d ", smsID, MMID);
  698. return m_pDataStorage->SilentQuery(QS);
  699. };
  700. return false;
  701. }
  702. bool sqliteDBC::updateNodePushedCMD(uint32_t CmdID, uint8_t status)
  703. {
  704. if (m_bMemIsConnected)
  705. {
  706. char QS[MAX_PATH];
  707. sprintf(QS, "UPDATE `nodes_qcq` SET `status`=%d WHERE `id`=%d ", status, CmdID);
  708. return m_pDataMemory->SilentQuery(QS);
  709. };
  710. return false;
  711. }
  712. bool sqliteDBC::cleanNodePushedCMD(void)
  713. {
  714. if (m_bMemIsConnected)
  715. {
  716. return m_pDataMemory->SilentQuery("DELETE FROM `nodes_qcq` WHERE `status` > 200");
  717. };
  718. return false;
  719. }
  720. void sqliteDBC::deleteNIF(uint32_t NifID)
  721. {
  722. if (m_bStoIsConnected)
  723. {
  724. char tQBuf[MAX_PATH_LEN];
  725. sprintf(tQBuf, "", NifID);
  726. #ifdef DEBUG_ALL
  727. LOG(tQBuf);
  728. #endif
  729. m_pDataStorage->SilentQuery(tQBuf);
  730. };
  731. }
  732. void sqliteDBC::deleteNIS(uint32_t NisID) {}
  733. void sqliteDBC::deleteNetwork(uint32_t NetID) {}
  734. bool sqliteDBC::deleteReportStat(REPORT_DIRINFO & RDF)
  735. {
  736. if (m_bStaIsConnected)
  737. {
  738. char tQBuf[MAX_PATH_LEN];
  739. sprintf(tQBuf, "DELETE FROM `days` WHERE `year`=%s AND `month`=%s AND `day`=%s", RDF.YEAR, RDF.MONTH, RDF.DAY);
  740. #ifdef DEBUG_ALL
  741. LOG(tQBuf);
  742. #endif
  743. m_pDataStatistics->SilentQuery(tQBuf);
  744. return true;
  745. };
  746. return false;
  747. }
  748. void sqliteDBC::deleteResource(uint32_t ResourceID) {}
  749. void sqliteDBC::deleteSensor(uint32_t SensorID) {}
  750. void sqliteDBC::deleteUART(uint32_t UARTID) {}
  751. bool sqliteDBC::updateServerUtilisation(DBC_SYS_LOAD_INFO * ptr)
  752. {
  753. if (m_bMemIsConnected)
  754. {
  755. char tQBuf[MAX_PATH_LEN];
  756. sprintf(tQBuf,
  757. "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
  758. ", `val_disk_tottal`=%" PRIu64 ", `val_disk_current`=%" PRIu64 " WHERE `id`=%d",
  759. ptr->CPU_CORES, ptr->cpu1, ptr->cpu5, ptr->cpu15, ptr->mem, ptr->disk, ptr->temperature, ptr->T_DISK, ptr->C_DISK, ptr->T_MEM, ptr->C_MEM, m_nServerID);
  760. #ifdef DEBUG_ALL
  761. LOG(tQBuf);
  762. #endif
  763. m_pDataMemory->SilentQuery(tQBuf);
  764. return true;
  765. };
  766. return false;
  767. }
  768. /********************************************************************
  769. * *
  770. * Slow DB Functions *
  771. * *
  772. ********************************************************************/
  773. bool sqliteDBC::addLRFDraw(uint32_t node_id, CSTR uniq_id, uint8_t lrf_id, uint8_t type)
  774. {
  775. if (m_bStoIsConnected)
  776. {
  777. char tQBuf[1024];
  778. sprintf(tQBuf, "INSERT INTO `lrf_draw`(`node_id`,`lrf_id`,`type`,`ts`,`data`) VALUES(%d,%d,%d,STRFTIME('%%s'),'%s')", node_id, lrf_id, type, uniq_id);
  779. #ifdef DEBUG_ALL
  780. LOG(tQBuf);
  781. #endif
  782. m_pDataStorage->SilentQuery(tQBuf);
  783. return true;
  784. };
  785. return false;
  786. }
  787. bool sqliteDBC::addDeviceDraw(uint32_t DBID, uint8_t type, uint8_t lrf, void * th_data, uint32_t th_size)
  788. {
  789. if (m_bStoIsConnected)
  790. {
  791. if (type == 0)
  792. {
  793. char tQBuf[1024];
  794. CSTR uid = getUniqID_MD5();
  795. if (uid == NULL)
  796. {
  797. return false;
  798. }
  799. sprintf(tQBuf, "data/device_draws/%s.raw", uid);
  800. FILE * fd = fopen(tQBuf, "wb+");
  801. if (fd != NULL)
  802. {
  803. uint32_t sisi = th_size >> 2;
  804. fwrite(&sisi, 4, 1, fd);
  805. fwrite(th_data, 4, sisi, fd);
  806. fclose(fd);
  807. bool rV = addLRFDraw(DBID, uid, lrf, type);
  808. free(uid);
  809. return rV;
  810. };
  811. free(uid);
  812. }
  813. else
  814. {
  815. return addLRFDraw(DBID, (CSTR) th_data, lrf, type);
  816. };
  817. };
  818. return false;
  819. }
  820. void sqliteDBC::updateStats(bool evt, bool video, uint8_t ses_status)
  821. {
  822. if (m_bStaIsConnected)
  823. {
  824. char tQBuf[1024];
  825. switch (ses_status)
  826. {
  827. case SESSION_CONTINUE:
  828. {
  829. if (video)
  830. {
  831. 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", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  832. }
  833. else
  834. {
  835. 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", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  836. };
  837. #ifdef DEBUG_ALL
  838. LOG(tQBuf);
  839. #endif
  840. m_pDataStatistics->SilentQuery(tQBuf);
  841. break;
  842. };
  843. case SESSION_START:
  844. {
  845. // Check New Day Reports
  846. REPORT_DIRINFO tRR = {0};
  847. fillRDF(time(NULL), tRR);
  848. if (tRR.ts > m_sCRepInfo.ts)
  849. {
  850. // New Day Report
  851. if (m_bRepIsConnected)
  852. {
  853. m_bRepIsConnected = false;
  854. LOG("Disconnect Report!");
  855. m_pDataReports->Disconnect();
  856. };
  857. memcpy(&m_sPRepInfo, &m_sCRepInfo, sizeof(REPORT_DIRINFO));
  858. memcpy(&m_sCRepInfo, &tRR, sizeof(REPORT_DIRINFO));
  859. if (checkRDFDir(m_pDbReportBase, m_sCRepInfo, true, getUserUid(web_access_user), getUserGid(web_access_group)))
  860. {
  861. sprintf(m_pDbReport, "%s/%s/%s/%s/report.db", m_pDbReportBase, m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  862. LOG("Connect Report!");
  863. LOG(m_pDbReport);
  864. if (m_pDataReports->Connect(m_pDbReport, 0, true))
  865. {
  866. chown(m_pDbReport, getUserUid(web_access_user), getUserGid(web_access_group));
  867. chmod(m_pDbReport, DB_FILE_MODE);
  868. m_pDataReports->SilentQuery(
  869. "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 "
  870. "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\" "
  871. "TEXT, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  872. m_pDataReports->SilentQuery("CREATE TABLE IF NOT EXISTS \"events_2_pictures\" ( \"evt_id\" INTEGER NOT NULL, \"picture_id\" INTEGER NOT NULL )");
  873. 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 )");
  874. m_pDataReports->SilentQuery(
  875. "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 "
  876. "NOT NULL, \"data\" BLOB COLLATE BINARY, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  877. m_pDataReports->SilentQuery(
  878. "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 "
  879. "NULL, \"fps\" INTEGER NOT NULL, \"flength\" INTEGER, PRIMARY KEY(\"id\" AUTOINCREMENT) )");
  880. m_bRepIsConnected = true;
  881. LOG("Report Connected!");
  882. // Create New Day Record
  883. sprintf(tQBuf, "INSERT INTO `days`(`year`,`month`,`day`,`session_count`,`session_closed`) VALUES(%s,%s,%s,0,0) ON CONFLICT(`year`,`month`,`day`) DO UPDATE SET `session_count`=`session_count`+1, `session_closed`=0",
  884. m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  885. LOG(tQBuf);
  886. m_pDataStatistics->SilentQuery(tQBuf);
  887. };
  888. };
  889. };
  890. // Create New record
  891. 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", m_sCRepInfo.YEAR,
  892. m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  893. #ifdef DEBUG_ALL
  894. LOG(tQBuf);
  895. #endif
  896. m_pDataStatistics->SilentQuery(tQBuf);
  897. break;
  898. };
  899. case SESSION_END:
  900. {
  901. 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", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  902. #ifdef DEBUG_ALL
  903. LOG(tQBuf);
  904. #endif
  905. m_pDataStatistics->SilentQuery(tQBuf);
  906. break;
  907. };
  908. case SESSION_DROP:
  909. {
  910. // sprintf( tQBuf, "UPDATE `days` SET `session_count`=`session_count`-1, `session_closed`=1 WHERE `year`='%s' AND `month`='%s' AND `day`='%s'",
  911. sprintf(tQBuf, "UPDATE `days` SET `session_closed`=1 WHERE `year`='%s' AND `month`='%s' AND `day`='%s'", m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY);
  912. #ifdef DEBUG_ALL
  913. LOG(tQBuf);
  914. #endif
  915. m_pDataStatistics->SilentQuery(tQBuf);
  916. break;
  917. };
  918. };
  919. };
  920. }
  921. bool sqliteDBC::dropSession()
  922. {
  923. if (m_bRepIsConnected)
  924. {
  925. char tQBuf[1024];
  926. AAssoc * result;
  927. #ifdef REMOVE_ONLY_SESSION_MARKS
  928. sprintf(tQBuf, "SELECT `ts`, `ts_ms` FROM `events` WHERE `evt_type`=%d ORDER BY `ts` DESC,`ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION);
  929. #else
  930. sprintf(tQBuf, "SELECT `ts`, `ts_ms` FROM `events` WHERE `evt_type`=%d ORDER BY `ts` DESC,`ts_ms` DESC LIMIT 1", EVT_LRF_START_SESSION);
  931. #endif
  932. m_pDataReports->Query(tQBuf);
  933. #ifdef DEBUG_ALL
  934. LOG(tQBuf);
  935. #endif
  936. result = m_pDataReports->Query(tQBuf);
  937. if (result != NULL)
  938. {
  939. uint32_t ts = 0;
  940. uint32_t ms = 0;
  941. uint64_t r = 0;
  942. if (result->size() > 0)
  943. {
  944. Assoc * cArr;
  945. Assoc::iterator it;
  946. cArr = result->at(0);
  947. it = cArr->find("ts");
  948. if (it != cArr->end())
  949. {
  950. ts = atoi(it->second.c_str());
  951. };
  952. it = cArr->find("ts_ms");
  953. if (it != cArr->end())
  954. {
  955. ms = atoi(it->second.c_str());
  956. };
  957. r = ts;
  958. r = (r * 1000) + ms;
  959. };
  960. deleteResult(result);
  961. if (r)
  962. {
  963. #ifdef REMOVE_ONLY_SESSION_MARKS
  964. sprintf(tQBuf, "DELETE FROM `events` WHERE ((`ts`*1000+`ts_ms`) >= %" PRIu64 ") AND ( `evt_type`=%d OR `evt_type`=%d OR `evt_type`=%d )", r, EVT_LRF_START_SESSION, EVT_LRF_END_SESSION_TO, EVT_LRF_END_SESSION_MA);
  965. #else
  966. sprintf(tQBuf, "DELETE FROM `events` WHERE (`ts`*1000+`ts_ms`) >= %" PRIu64, r);
  967. #endif
  968. #ifdef DEBUG_ALL
  969. LOG(tQBuf);
  970. #endif
  971. m_pDataReports->SilentQuery(tQBuf);
  972. };
  973. };
  974. };
  975. updateStats(false, false, SESSION_DROP);
  976. return true;
  977. }
  978. uint32_t sqliteDBC::addEvent(DBC_EVT_RECORD * ptr)
  979. {
  980. if (ptr == NULL)
  981. {
  982. return 0;
  983. }
  984. if (m_bRepIsConnected)
  985. {
  986. char tQBuf[1024];
  987. switch (ptr->type)
  988. {
  989. case EVT_LRF_END_SESSION_TO:
  990. case EVT_LRF_END_SESSION_MA:
  991. updateStats(false, false, SESSION_END);
  992. sprintf(tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)", ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type);
  993. break;
  994. case EVT_LRF_START_SESSION:
  995. updateStats(false, false, SESSION_START);
  996. sprintf(tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)", ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type);
  997. break;
  998. case EVT_FLT_LATF_CHK:
  999. case EVT_FLT_LATF_UP_DETEC:
  1000. case EVT_FLT_LATF_BT_DETEC:
  1001. case EVT_FLT_LATF_TRP_DET:
  1002. case EVT_FLT_LATF_TRP_UPDT:
  1003. case EVT_FLT_LATF_SUSP_START:
  1004. case EVT_FLT_LATF_REGULAR:
  1005. case EVT_FLT_LATF_DISABLED:
  1006. case EVT_FLT_LATF_SUSP_DET:
  1007. case EVT_FLT_LATF_DIFF_DET:
  1008. case EVT_FLT_BUBBLE_POPED:
  1009. case EVT_FLT_BUBBLE_CREATED:
  1010. case EVT_FLT_BUBBLE_BLOCK_T:
  1011. case EVT_FLT_BUBBLE_BLOCK_P:
  1012. {
  1013. updateStats();
  1014. sprintf(tQBuf,
  1015. "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`lrf_id`,`range_digital`,`azimuth`,`range`,`area`,`lat`,`lon`,`alt`)"
  1016. " VALUES(%d,'%s',%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  1017. ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type, ptr->lrf_id, ptr->range_digital, ptr->az, ptr->ra, ptr->area, ptr->lat, ptr->lon, ptr->alt);
  1018. break;
  1019. };
  1020. case EVT_FLT_LATF_OP_DISABLED:
  1021. case EVT_FLT_LATF_OP_TD:
  1022. case EVT_FLT_LATF_OP_TG:
  1023. case EVT_FLT_LATF_OP_EXP:
  1024. case EVT_FLT_LATF_TRP_EXP:
  1025. case EVT_FLT_LATF_SUSP_NAK:
  1026. case EVT_FLT_LATF_SUSP_EXP:
  1027. {
  1028. updateStats();
  1029. sprintf(tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)", ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type);
  1030. break;
  1031. };
  1032. case EVT_OP_ACK_MASK:
  1033. case EVT_OP_NACK_MASK:
  1034. case EVT_OP_ON_MASK:
  1035. case EVT_OP_OFF_MASK:
  1036. case EVT_CONNECTED:
  1037. case EVT_DISCONNECTED:
  1038. case EVT_COMMUNICATION_JAMED:
  1039. case EVT_STUDY:
  1040. case EVT_BUSY:
  1041. case EVT_FLIP:
  1042. case EVT_READY:
  1043. case EVT_ERR_LRF_NO_IRQ:
  1044. case EVT_ERR_LRF_NO_ZERO:
  1045. case EVT_ERR_LRF_ANGLE:
  1046. case EVT_ERR_LRF_VOLTAGE:
  1047. case EVT_ERR_LRF_BROKEN:
  1048. case EVT_LRF_VIS_CHANGED:
  1049. case EVT_ERR_OVERFLOW:
  1050. case EVT_SST_OVERFLOW:
  1051. case EVT_VIDEO_FPS_DEGRADED:
  1052. case EVT_VIDEO_FPS_RAISED:
  1053. {
  1054. updateStats();
  1055. sprintf(tQBuf, "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`) VALUES(%d,'%s',%d,%d,%d)", ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type);
  1056. break;
  1057. };
  1058. case EVT_ROI_ACTIVE:
  1059. case EVT_TARGET_3D_WHITE:
  1060. case EVT_TARGET_3D_BLACK:
  1061. {
  1062. updateStats();
  1063. sprintf(tQBuf,
  1064. "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`lrf_id`,`range_digital`,`azimuth`,`elevation`,`range`,`area`,`lat`,`lon`,`alt`)"
  1065. " VALUES(%d,'%s',%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  1066. ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type, ptr->lrf_id, ptr->range_digital, ptr->az, ptr->el, ptr->ra, ptr->area, ptr->lat, ptr->lon, ptr->alt);
  1067. break;
  1068. };
  1069. case EVT_TARGET_2D_WHITE:
  1070. case EVT_TARGET_2D_BLACK:
  1071. {
  1072. updateStats();
  1073. sprintf(tQBuf,
  1074. "INSERT INTO `events`(`node_id`,`uuid`,`ts`,`ts_ms`,`evt_type`,`lrf_id`,`range_digital`,`azimuth`,`range`,`area`,`lat`,`lon`,`alt`)"
  1075. " VALUES(%d,'%s',%d,%d,%d,%d,%d,%0.2f,%0.2f,%0.2f,%0.7f,%0.7f,%0.2f)",
  1076. ptr->node_id, ptr->uuid, ptr->ts_s, ptr->ts_ms, ptr->type, ptr->lrf_id, ptr->range_digital, ptr->az, ptr->ra, ptr->area, ptr->lat, ptr->lon, ptr->alt);
  1077. break;
  1078. };
  1079. default: return 0;
  1080. };
  1081. #ifdef DEBUG_ALL
  1082. LOG(tQBuf);
  1083. #endif
  1084. if (m_bRepIsConnected)
  1085. {
  1086. return ptr->id = m_pDataReports->QueryID(tQBuf);
  1087. }
  1088. else
  1089. {
  1090. LOG_ERROR("Report DB Droped!");
  1091. }
  1092. };
  1093. return 0;
  1094. }
  1095. bool sqliteDBC::addIPCam(OBSERVER_RESULT * tPtr)
  1096. {
  1097. if (m_bStoIsConnected)
  1098. {
  1099. char tQBuf[1024];
  1100. char tIpBuf[32];
  1101. char tProto[32];
  1102. char tPath[CTEXT];
  1103. char * tPos;
  1104. char * tPos2;
  1105. uint16_t tPort = 0;
  1106. memcpy(tProto, tPtr->XAddrs, 5);
  1107. tProto[5] = (char) 0;
  1108. if (tProto[4] == ':')
  1109. {
  1110. tProto[4] = (char) 0;
  1111. }
  1112. memset(tPath, 0, CTEXT);
  1113. tPos = strstr(tPtr->XAddrs, "//");
  1114. if (tPos != NULL)
  1115. {
  1116. tPos2 = strstr(tPos + 2, ":");
  1117. if (tPos2 != NULL)
  1118. {
  1119. LOG("IP Port Defined!");
  1120. int len, len2, len3;
  1121. len = tPos2 - tPos - 2;
  1122. strncpy(tIpBuf, tPos + 2, len);
  1123. tIpBuf[len] = (char) 0;
  1124. tPort = atoi(tPos2 + 1);
  1125. strncpy(tPath, tPos + 2 + len, CTEXT);
  1126. }
  1127. else
  1128. {
  1129. tPos2 = strstr(tPos + 2, "/");
  1130. if (tPos2 != NULL)
  1131. {
  1132. int len, len2, len3;
  1133. len = tPos2 - tPos - 2;
  1134. strncpy(tIpBuf, tPos + 2, len);
  1135. tIpBuf[len] = (char) 0;
  1136. strncpy(tPath, tPos + 2 + len, CTEXT);
  1137. }
  1138. else
  1139. {
  1140. tPos2 = strstr(tPos + 2, "/");
  1141. LOG_ERROR("Pos 2 - err");
  1142. tIpBuf[0] = (char) 0;
  1143. };
  1144. };
  1145. }
  1146. else
  1147. {
  1148. LOG_ERROR("Pos 0 - err");
  1149. tIpBuf[0] = (char) 0;
  1150. };
  1151. if (tPort)
  1152. {
  1153. sprintf(tQBuf,
  1154. "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', "
  1155. "`port`=%d",
  1156. tPtr->RefID,
  1157. // tPtr->XAddrs,
  1158. tPath, tIpBuf, tPort, tPtr->MAC, tPtr->Hardware, tPtr->Name, tProto,
  1159. // tPtr->XAddrs,
  1160. tIpBuf, tPort);
  1161. }
  1162. else
  1163. {
  1164. 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'",
  1165. tPtr->RefID,
  1166. // tPtr->XAddrs,
  1167. tPath, tIpBuf, tPtr->MAC, tPtr->Hardware, tPtr->Name, tProto,
  1168. // tPtr->XAddrs,
  1169. tIpBuf);
  1170. }
  1171. #ifdef DEBUG_ALL
  1172. LOG(tQBuf);
  1173. #endif
  1174. m_pDataStorage->SilentQuery(tQBuf);
  1175. return true;
  1176. };
  1177. return false;
  1178. }
  1179. uint32_t sqliteDBC::addLocalCam(CSTR name, CSTR hardware, CSTR path, uint32_t width, uint32_t height, uint16_t fps, uint16_t den)
  1180. {
  1181. uint32_t RetVal = 0;
  1182. if (m_bStoIsConnected)
  1183. {
  1184. char tQBuf[1024];
  1185. char tM5[33];
  1186. CSTR tMD5 = MD5String(path);
  1187. if (tMD5 != NULL)
  1188. {
  1189. memcpy(tM5, tMD5, 33);
  1190. tM5[32] = (char) 0;
  1191. free(tMD5);
  1192. 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", tM5, hardware, name, path);
  1193. #ifdef DEBUG_ALL
  1194. LOG(tQBuf);
  1195. #endif
  1196. uint32_t camID = (uint32_t) m_pDataStorage->QueryID(tQBuf);
  1197. if (camID == 0)
  1198. {
  1199. camID = getCamId(tM5);
  1200. };
  1201. sprintf(tQBuf,
  1202. "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`) "
  1203. "DO UPDATE SET `valid`=1, img_width=%d, img_height=%d, `fps`=%d",
  1204. camID, name, tM5, width, height, path, fps, width, height, fps);
  1205. #ifdef DEBUG_ALL
  1206. LOG(tQBuf);
  1207. #endif
  1208. RetVal = (uint32_t) m_pDataStorage->QueryID(tQBuf);
  1209. if (RetVal == 0)
  1210. {
  1211. RetVal = getMediaStreamID(camID, tM5);
  1212. };
  1213. };
  1214. };
  1215. return RetVal;
  1216. }
  1217. uint32_t sqliteDBC::addLocalNetwork(uint32_t NIF_ID, NETWORK_TYPE net_type, CSTR address, CSTR mask, CSTR gateway, CSTR broadcast)
  1218. {
  1219. uint32_t RetVal = 0;
  1220. if (m_bStoIsConnected)
  1221. {
  1222. char tQBuf[MAX_PATH_LEN];
  1223. 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'",
  1224. NIF_ID, net_type, address, gateway, mask, broadcast, address, gateway, mask, broadcast);
  1225. #ifdef DEBUG_ALL
  1226. LOG(tQBuf);
  1227. #endif
  1228. RetVal = m_pDataStorage->QueryID(tQBuf);
  1229. // On Update IF data no need to return ID
  1230. // if( RetVal == 0 ) {
  1231. // RetVal = getNetworkInterfaceID( tKey );
  1232. //};
  1233. };
  1234. return RetVal;
  1235. }
  1236. uint32_t sqliteDBC::addLocalNetworkInfrastructure(uint32_t NIF_ID, uint32_t OUTBOUND_NIF, bool ena_dhcp, CSTR BRANG, CSTR ERANG, CSTR MASK, CSTR LEASE_TIME)
  1237. {
  1238. uint32_t RetVal = 0;
  1239. if (m_bStoIsConnected)
  1240. {
  1241. char tQBuf[MAX_PATH_LEN];
  1242. bool ena_routing = true;
  1243. if (OUTBOUND_NIF == 0)
  1244. {
  1245. ena_routing = false;
  1246. };
  1247. if (ena_dhcp)
  1248. {
  1249. if (BRANG == NULL || ERANG == NULL || MASK == NULL || LEASE_TIME == NULL)
  1250. {
  1251. LOG_ERROR("Wrong DHCP parameters to create Network Infrastructure!");
  1252. return 0;
  1253. };
  1254. sprintf(tQBuf,
  1255. "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 "
  1256. "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'",
  1257. NIF_ID, ena_routing, OUTBOUND_NIF, BRANG, ERANG, MASK, LEASE_TIME, ena_routing, OUTBOUND_NIF, BRANG, ERANG, MASK, LEASE_TIME);
  1258. }
  1259. else
  1260. {
  1261. if (ena_routing)
  1262. {
  1263. 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", NIF_ID, OUTBOUND_NIF, OUTBOUND_NIF);
  1264. }
  1265. else
  1266. {
  1267. LOG_ERROR("No Special Service Defined To Network Infrastructure!");
  1268. return 0;
  1269. };
  1270. };
  1271. #ifdef DEBUG_ALL
  1272. LOG(tQBuf);
  1273. #endif
  1274. RetVal = m_pDataStorage->QueryID(tQBuf);
  1275. // On Update IF data no need to return ID
  1276. if (RetVal == 0)
  1277. {
  1278. RetVal = getNetworkInfrastructureID(NIF_ID);
  1279. };
  1280. };
  1281. return RetVal;
  1282. }
  1283. uint32_t sqliteDBC::addLocalNIF(CSTR device, CSTR mac, NIF_TYPE dtype, NIF_MODE dmode, uint32_t aux_id)
  1284. {
  1285. uint32_t RetVal = 0;
  1286. if (m_bStoIsConnected)
  1287. {
  1288. char tQBuf[MAX_PATH_LEN];
  1289. char * tKey = MD5String(device);
  1290. if (tKey != NULL)
  1291. {
  1292. 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'", dtype, tKey, device, mac, dmode,
  1293. aux_id, dmode, aux_id, mac);
  1294. #ifdef DEBUG_ALL
  1295. LOG(tQBuf);
  1296. #endif
  1297. RetVal = m_pDataStorage->QueryID(tQBuf);
  1298. if (RetVal == 0)
  1299. {
  1300. RetVal = getNIF_ID(tKey);
  1301. };
  1302. free(tKey);
  1303. };
  1304. };
  1305. return RetVal;
  1306. }
  1307. 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)
  1308. {
  1309. uint32_t RetVal = 0;
  1310. if (m_bStoIsConnected)
  1311. {
  1312. char tQBuf[MAX_PATH_LEN];
  1313. 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`", m_nServerNodeID,
  1314. utype, speed, bit, parity, stop_bit, mode, port, PATH);
  1315. #ifdef DEBUG_ALL
  1316. LOG(tQBuf);
  1317. #endif
  1318. RetVal = m_pDataStorage->QueryID(tQBuf);
  1319. if (RetVal == 0)
  1320. {
  1321. RetVal = getUartID(m_nServerNodeID, utype, PATH);
  1322. };
  1323. };
  1324. return RetVal;
  1325. }
  1326. uint32_t sqliteDBC::addLocalWiFi(uint32_t NIF_ID, WIFI_MODE mode, CSTR SSID, CSTR password, WIFI_ENCRIPTION encryption, uint32_t router_id)
  1327. {
  1328. uint32_t RetVal = 0;
  1329. if (m_bStoIsConnected)
  1330. {
  1331. char tQBuf[MAX_PATH_LEN];
  1332. sprintf(tQBuf,
  1333. "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", NIF_ID,
  1334. mode, router_id, SSID, password, encryption, mode, router_id, SSID, password, encryption);
  1335. #ifdef DEBUG_ALL
  1336. LOG(tQBuf);
  1337. #endif
  1338. RetVal = m_pDataStorage->QueryID(tQBuf);
  1339. if (RetVal == 0)
  1340. {
  1341. RetVal = getWiFi_ID(NIF_ID);
  1342. };
  1343. };
  1344. return RetVal;
  1345. }
  1346. bool sqliteDBC::cleanLRFMask(uint32_t maskID, bool deleteMask)
  1347. {
  1348. if (m_bStoIsConnected)
  1349. {
  1350. char tQBuf[MAX_PATH_LEN];
  1351. // sprintf( tQBuf, "DELETE FROM `lrf_mask_trace_zones` WHERE `zone_id` IN ( SELECT `id` FROM `lrf_mask_traces` WHERE `mask_id`=%d )", maskID );
  1352. #ifdef DEBUG_ALL
  1353. // LOG(tQBuf);
  1354. #endif
  1355. // m_pDataStorage->SilentQuery( tQBuf );
  1356. // sprintf( tQBuf, "DELETE FROM `lrf_mask_traces` WHERE `mask_id`=%d", maskID );
  1357. #ifdef DEBUG_ALL
  1358. // LOG(tQBuf);
  1359. #endif
  1360. // m_pDataStorage->SilentQuery( tQBuf );
  1361. sprintf(tQBuf, "DELETE FROM `lrf_mask_vertex_points` WHERE `vertex_id` IN ( SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d )", maskID);
  1362. #ifdef DEBUG
  1363. LOG(tQBuf);
  1364. #endif
  1365. m_pDataStorage->SilentQuery(tQBuf);
  1366. sprintf(tQBuf, "DELETE FROM `lrf_mask_vertexes` WHERE `mask_id`=%d", maskID);
  1367. #ifdef DEBUG_ALL
  1368. LOG(tQBuf);
  1369. #endif
  1370. m_pDataStorage->SilentQuery(tQBuf);
  1371. // Delete Mask Itself!
  1372. if (deleteMask)
  1373. {
  1374. sprintf(tQBuf, "DELETE FROM `lrf_masks` WHERE `id`=%d", maskID);
  1375. #ifdef DEBUG_ALL
  1376. LOG(tQBuf);
  1377. #endif
  1378. m_pDataStorage->SilentQuery(tQBuf);
  1379. };
  1380. return true;
  1381. };
  1382. return false;
  1383. }
  1384. bool sqliteDBC::cleanLRFWidthTable(uint32_t sensorID)
  1385. {
  1386. if (m_bStoIsConnected)
  1387. {
  1388. char tQBuf[MAX_PATH_LEN];
  1389. sprintf(tQBuf, "DELETE FROM `lrf_detection_width_tables` WHERE `aux_id`=%d", sensorID);
  1390. #ifdef DEBUG_ALL
  1391. LOG(tQBuf);
  1392. #endif
  1393. m_pDataStorage->SilentQuery(tQBuf);
  1394. return true;
  1395. };
  1396. return false;
  1397. }
  1398. bool sqliteDBC::cleanResources(bool all)
  1399. {
  1400. if (m_bStoIsConnected)
  1401. {
  1402. char tQBuf[MAX_PATH_LEN];
  1403. if (all)
  1404. {
  1405. sprintf(tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id`=%d", m_nServerNodeID);
  1406. }
  1407. else
  1408. {
  1409. 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);
  1410. };
  1411. #ifdef DEBUG_ALL
  1412. LOG(tQBuf);
  1413. #endif
  1414. m_pDataStorage->SilentQuery(tQBuf);
  1415. return true;
  1416. };
  1417. return false;
  1418. }
  1419. bool sqliteDBC::wipeServerResources()
  1420. {
  1421. if (m_bStoIsConnected)
  1422. {
  1423. char tQBuf[MAX_PATH_LEN];
  1424. sprintf(tQBuf, "DELETE FROM `resources` WHERE `node_id`=%d", m_nServerNodeID);
  1425. #ifdef DEBUG_ALL
  1426. LOG(tQBuf);
  1427. #endif
  1428. m_pDataStorage->SilentQuery(tQBuf);
  1429. return true;
  1430. };
  1431. return false;
  1432. }
  1433. uint32_t sqliteDBC::addLRFMask(uint32_t devID, uint8_t lrfID, uint8_t layerID)
  1434. {
  1435. uint32_t RetVal = 0;
  1436. if (m_bStoIsConnected)
  1437. {
  1438. char tQBuf[MAX_PATH_LEN];
  1439. 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", devID, lrfID, layerID, layerID);
  1440. #ifdef DEBUG
  1441. LOG(tQBuf);
  1442. #endif
  1443. RetVal = m_pDataStorage->QueryID(tQBuf);
  1444. if (RetVal == 0)
  1445. {
  1446. RetVal = getLRFMaskID(devID, lrfID, layerID);
  1447. };
  1448. };
  1449. return RetVal;
  1450. }
  1451. uint32_t sqliteDBC::addLRFMaskPolygone(uint32_t maskID, uint8_t vertexID, uint8_t r, uint8_t g, uint8_t b)
  1452. {
  1453. uint32_t RetVal = 0;
  1454. if (m_bStoIsConnected)
  1455. {
  1456. char tQBuf[MAX_PATH_LEN];
  1457. sprintf(tQBuf, "INSERT INTO `lrf_mask_vertexes`(`mask_id`,`vertex_id`,`r`,`g`,`b`) VALUES(%d,%d,%d,%d,%d)", maskID, vertexID, r, g, b);
  1458. #ifdef DEBUG_ALL
  1459. LOG(tQBuf);
  1460. #endif
  1461. RetVal = m_pDataStorage->QueryID(tQBuf);
  1462. if (RetVal == 0)
  1463. {
  1464. RetVal = getLRFMaskPolygoneID(maskID, vertexID);
  1465. };
  1466. };
  1467. return RetVal;
  1468. }
  1469. bool sqliteDBC::addLRFMaskPolygonePoint(uint32_t polyID, int16_t x, int16_t y)
  1470. {
  1471. if (m_bStoIsConnected)
  1472. {
  1473. char tQBuf[MAX_PATH_LEN];
  1474. sprintf(tQBuf, "INSERT INTO `lrf_mask_vertex_points`(`vertex_id`,`x`,`y`) VALUES(%d,%d,%d)", polyID, x, y);
  1475. #ifdef DEBUG_ALL
  1476. LOG(tQBuf);
  1477. #endif
  1478. m_pDataStorage->SilentQuery(tQBuf);
  1479. return true;
  1480. };
  1481. return false;
  1482. }
  1483. bool sqliteDBC::addLRFWidthTable(uint32_t sensorID, PWIDTH_TABLE ptr)
  1484. {
  1485. if (m_bStoIsConnected)
  1486. {
  1487. char tQBuf[MAX_PATH_LEN];
  1488. float kK = 100.0f / ptr->nCount;
  1489. for (int i = 0; i < ptr->nCount; i++)
  1490. {
  1491. uint16_t CC = kK * i;
  1492. sprintf(tQBuf, "INSERT INTO `lrf_detection_width_tables`(`aux_id`,`width_k`,`range_k`) VALUES(%d,%d,%d)", sensorID, ptr->pTable[i], CC);
  1493. #ifdef DEBUG_ALL
  1494. LOG(tQBuf);
  1495. #endif
  1496. m_pDataStorage->SilentQuery(tQBuf);
  1497. };
  1498. return true;
  1499. };
  1500. return false;
  1501. }
  1502. bool sqliteDBC::addPicture(DBC_SNAPSHOT_DATA * ptr, CSTR ext)
  1503. {
  1504. bool rVal = false;
  1505. if (ptr != NULL)
  1506. {
  1507. if (m_bRepIsConnected)
  1508. {
  1509. char tQBuf[MAX_PATH_LEN];
  1510. uint32_t pic_id;
  1511. sprintf(tQBuf, "%s/%s/%s/%s/%s.%s", m_pDbReportBase, m_sCRepInfo.YEAR, m_sCRepInfo.MONTH, m_sCRepInfo.DAY, ptr->md5, ext);
  1512. #ifdef DEBUG_ALL
  1513. LOG(tQBuf);
  1514. #endif
  1515. FILE * fd = fopen(tQBuf, "wb+");
  1516. if (fd)
  1517. {
  1518. fwrite(ptr->data, 1, ptr->data_size, fd);
  1519. fclose(fd);
  1520. chown(tQBuf, getUserUid(web_access_user), getUserGid(web_access_group));
  1521. }
  1522. else
  1523. {
  1524. LOG_ERROR("Error Saving Captured picture!");
  1525. return rVal;
  1526. };
  1527. sprintf(tQBuf, "INSERT INTO pictures(`media_stream_id`,`uts`,`uts_ms`,`md5`,`ext`) VALUES(%d,%d,0,'%s','%s')", ptr->stream_id, ptr->ts, ptr->md5, ext);
  1528. pic_id = (uint32_t) m_pDataReports->QueryID(tQBuf);
  1529. if (pic_id)
  1530. {
  1531. for (DBC_INT_LIST::iterator idx = ptr->assoc_event_ids.begin(); idx < ptr->assoc_event_ids.end(); idx++)
  1532. {
  1533. sprintf(tQBuf, "INSERT INTO `events_2_pictures`(`evt_id`,`picture_id`) VALUES(%d,%d)", *idx, pic_id);
  1534. m_pDataReports->SilentQuery(tQBuf);
  1535. };
  1536. rVal = true;
  1537. };
  1538. };
  1539. };
  1540. return rVal;
  1541. }
  1542. uint32_t sqliteDBC::addResource(uint8_t type, CSTR devName, uint32_t auxID)
  1543. {
  1544. uint32_t RetVal = 0;
  1545. if (m_bStoIsConnected)
  1546. {
  1547. char tQBuf[2048];
  1548. sprintf(tQBuf, "%d:%d:%s", m_nServerNodeID, type, devName);
  1549. CSTR tMD5 = MD5String(tQBuf);
  1550. if (tMD5)
  1551. {
  1552. 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", m_nServerNodeID, type, tMD5, auxID, devName, auxID);
  1553. free(tMD5);
  1554. #ifdef DEBUG_ALL
  1555. LOG(tQBuf);
  1556. #endif
  1557. RetVal = (uint32_t) m_pDataStorage->QueryID(tQBuf);
  1558. if (RetVal == 0)
  1559. {
  1560. RetVal = getResourceID(m_nServerNodeID, type, auxID);
  1561. };
  1562. };
  1563. };
  1564. return RetVal;
  1565. }
  1566. bool sqliteDBC::addVideoEvent(uint32_t evtID, uint32_t videoID, uint32_t frame, uint32_t tss, uint32_t tms)
  1567. {
  1568. if (m_bRepIsConnected)
  1569. {
  1570. char tQBuf[2048];
  1571. sprintf(tQBuf, "INSERT INTO `events_2_videos`(`evt_id`,`video_id`,`frame`,`tss`,`tms`) VALUES(%d,%d,%d,%d,%d)", evtID, videoID, frame, tss, tms);
  1572. #ifdef DEBUG_ALL
  1573. LOG(tQBuf);
  1574. #endif
  1575. m_pDataReports->SilentQuery(tQBuf);
  1576. };
  1577. return false;
  1578. }
  1579. uint32_t sqliteDBC::addVideoRecord(uint32_t streamID, CSTR md5, CSTR ext, uint32_t tss, uint32_t tms, uint32_t fps)
  1580. {
  1581. uint32_t RetVal = 0;
  1582. if (m_bRepIsConnected)
  1583. {
  1584. updateStats(false, true);
  1585. char tQBuf[2048];
  1586. sprintf(tQBuf, "INSERT INTO `videos`(`media_stream_id`,`md5`,`uts`,`uts_ms`,`ext`,`fps`) VALUES(%d,'%s',%d,%d,'%s',%d)", streamID, md5, tss, tms, ext, fps);
  1587. #ifdef DEBUG_EXTRA
  1588. LOG(tQBuf);
  1589. #endif
  1590. RetVal = (uint32_t) m_pDataReports->QueryID(tQBuf);
  1591. };
  1592. return RetVal;
  1593. }
  1594. DBC_MODULE_EMBED_VIDEO_DATA * sqliteDBC::getEmbedVideoData(uint32_t ID)
  1595. {
  1596. DBC_MODULE_EMBED_VIDEO_DATA * tPtr = NULL;
  1597. if (m_bStoIsConnected)
  1598. {
  1599. char tQBuf[MAX_PATH_LEN];
  1600. AAssoc * result;
  1601. sprintf(tQBuf, "SELECT * FROM `module_eve` WHERE `id`=%d", ID);
  1602. #ifdef DEBUG_ALL
  1603. LOG(tQBuf);
  1604. #endif
  1605. result = m_pDataStorage->Query(tQBuf);
  1606. if (result != NULL)
  1607. {
  1608. if (result->size() > 0)
  1609. {
  1610. Assoc * cArr;
  1611. tPtr = (DBC_MODULE_EMBED_VIDEO_DATA *) malloc(sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  1612. if (tPtr != NULL)
  1613. {
  1614. Assoc::iterator it;
  1615. memset(tPtr, 0, sizeof(DBC_MODULE_EMBED_VIDEO_DATA));
  1616. cArr = result->at(0);
  1617. it = cArr->find("output_mode");
  1618. if (it != cArr->end())
  1619. {
  1620. tPtr->output_mode = atoi(it->second.c_str());
  1621. };
  1622. // Togo Bug
  1623. if (tPtr->output_mode == 0)
  1624. {
  1625. tPtr->output_mode = 2; // Video Output
  1626. }
  1627. it = cArr->find("web_port");
  1628. if (it != cArr->end())
  1629. {
  1630. tPtr->port = atoi(it->second.c_str());
  1631. };
  1632. it = cArr->find("width");
  1633. if (it != cArr->end())
  1634. {
  1635. tPtr->width = atoi(it->second.c_str());
  1636. };
  1637. it = cArr->find("height");
  1638. if (it != cArr->end())
  1639. {
  1640. tPtr->height = atoi(it->second.c_str());
  1641. };
  1642. it = cArr->find("compression_quality");
  1643. if (it != cArr->end())
  1644. {
  1645. tPtr->compression_quality = atoi(it->second.c_str());
  1646. };
  1647. it = cArr->find("show_ts");
  1648. if (it != cArr->end())
  1649. {
  1650. tPtr->show_ts = (bool) atoi(it->second.c_str());
  1651. };
  1652. it = cArr->find("ts_x");
  1653. if (it != cArr->end())
  1654. {
  1655. tPtr->ts_x = atoi(it->second.c_str());
  1656. };
  1657. it = cArr->find("ts_y");
  1658. if (it != cArr->end())
  1659. {
  1660. tPtr->ts_y = atoi(it->second.c_str());
  1661. };
  1662. it = cArr->find("ts_font_face");
  1663. if (it != cArr->end())
  1664. {
  1665. tPtr->ts_font_face = atoi(it->second.c_str());
  1666. };
  1667. it = cArr->find("ts_font_scale");
  1668. if (it != cArr->end())
  1669. {
  1670. tPtr->ts_font_scale = atof(it->second.c_str());
  1671. };
  1672. it = cArr->find("ts_font_thickness");
  1673. if (it != cArr->end())
  1674. {
  1675. tPtr->ts_font_thickness = atoi(it->second.c_str());
  1676. };
  1677. it = cArr->find("ts_line_type");
  1678. if (it != cArr->end())
  1679. {
  1680. tPtr->ts_line_type = atoi(it->second.c_str());
  1681. };
  1682. it = cArr->find("ts_transperant");
  1683. if (it != cArr->end())
  1684. {
  1685. tPtr->ts_transperant = (bool) atoi(it->second.c_str());
  1686. };
  1687. it = cArr->find("show_label");
  1688. if (it != cArr->end())
  1689. {
  1690. tPtr->show_label = (bool) atoi(it->second.c_str());
  1691. };
  1692. it = cArr->find("label_x");
  1693. if (it != cArr->end())
  1694. {
  1695. tPtr->label_x = atoi(it->second.c_str());
  1696. };
  1697. it = cArr->find("label_y");
  1698. if (it != cArr->end())
  1699. {
  1700. tPtr->label_y = atoi(it->second.c_str());
  1701. };
  1702. it = cArr->find("label_font_face");
  1703. if (it != cArr->end())
  1704. {
  1705. tPtr->label_font_face = atoi(it->second.c_str());
  1706. };
  1707. it = cArr->find("label_font_scale");
  1708. if (it != cArr->end())
  1709. {
  1710. tPtr->label_font_scale = atof(it->second.c_str());
  1711. };
  1712. it = cArr->find("label_font_thickness");
  1713. if (it != cArr->end())
  1714. {
  1715. tPtr->label_font_thickness = atoi(it->second.c_str());
  1716. };
  1717. it = cArr->find("label_line_type");
  1718. if (it != cArr->end())
  1719. {
  1720. tPtr->label_line_type = atoi(it->second.c_str());
  1721. };
  1722. it = cArr->find("label_transperant");
  1723. if (it != cArr->end())
  1724. {
  1725. tPtr->label_transperant = (bool) atoi(it->second.c_str());
  1726. };
  1727. it = cArr->find("show_logo");
  1728. if (it != cArr->end())
  1729. {
  1730. tPtr->show_logo = (bool) atoi(it->second.c_str());
  1731. };
  1732. it = cArr->find("logo_w");
  1733. if (it != cArr->end())
  1734. {
  1735. tPtr->logo_w = atoi(it->second.c_str());
  1736. };
  1737. it = cArr->find("logo_h");
  1738. if (it != cArr->end())
  1739. {
  1740. tPtr->logo_h = atoi(it->second.c_str());
  1741. };
  1742. it = cArr->find("logo_x");
  1743. if (it != cArr->end())
  1744. {
  1745. tPtr->logo_x = atoi(it->second.c_str());
  1746. };
  1747. it = cArr->find("logo_y");
  1748. if (it != cArr->end())
  1749. {
  1750. tPtr->logo_y = atoi(it->second.c_str());
  1751. };
  1752. it = cArr->find("logo_md5");
  1753. if (it != cArr->end())
  1754. {
  1755. strncpy(tPtr->logo_md5, it->second.c_str(), 32);
  1756. tPtr->logo_md5[32] = (char) 0;
  1757. };
  1758. };
  1759. };
  1760. };
  1761. deleteResult(result);
  1762. };
  1763. return tPtr;
  1764. }
  1765. DBC_MEDIA_STREAM_DATA * sqliteDBC::getMediaStreamData(uint32_t ID)
  1766. {
  1767. DBC_MEDIA_STREAM_DATA * tVal = NULL;
  1768. if (m_bStoIsConnected)
  1769. {
  1770. char tQBuf[CTEXT];
  1771. AAssoc * result;
  1772. sprintf(tQBuf, "SELECT * FROM media_streams WHERE `id`=%d", ID);
  1773. #ifdef DEBUG_ALL
  1774. LOG(tQBuf);
  1775. #endif
  1776. result = m_pDataStorage->Query(tQBuf);
  1777. if (result != NULL)
  1778. {
  1779. if (result->size() > 0)
  1780. {
  1781. Assoc * cArr;
  1782. tVal = (DBC_MEDIA_STREAM_DATA *) malloc(sizeof(DBC_MEDIA_STREAM_DATA));
  1783. if (tVal != NULL)
  1784. {
  1785. memset(tVal, 0, sizeof(DBC_MEDIA_STREAM_DATA));
  1786. Assoc::iterator it;
  1787. cArr = result->at(0);
  1788. tVal->id = ID;
  1789. it = cArr->find("source_id");
  1790. if (it != cArr->end())
  1791. {
  1792. tVal->source_id = atoi(it->second.c_str());
  1793. };
  1794. it = cArr->find("source_type");
  1795. if (it != cArr->end())
  1796. {
  1797. sprintf(tVal->source_type, it->second.c_str());
  1798. };
  1799. it = cArr->find("valid");
  1800. if (it != cArr->end())
  1801. {
  1802. tVal->valid = (bool) atoi(it->second.c_str());
  1803. };
  1804. it = cArr->find("token");
  1805. if (it != cArr->end())
  1806. {
  1807. sprintf(tVal->token, it->second.c_str());
  1808. };
  1809. it = cArr->find("img_width");
  1810. if (it != cArr->end())
  1811. {
  1812. tVal->width = atoi(it->second.c_str());
  1813. };
  1814. it = cArr->find("img_height");
  1815. if (it != cArr->end())
  1816. {
  1817. tVal->height = atoi(it->second.c_str());
  1818. };
  1819. it = cArr->find("fps");
  1820. if (it != cArr->end())
  1821. {
  1822. tVal->fps = atoi(it->second.c_str());
  1823. };
  1824. it = cArr->find("compression_type");
  1825. if (it != cArr->end())
  1826. {
  1827. sprintf(tVal->c_type, it->second.c_str());
  1828. };
  1829. it = cArr->find("mrl_proto");
  1830. if (it != cArr->end())
  1831. {
  1832. sprintf(tVal->mrl_proto, it->second.c_str());
  1833. };
  1834. it = cArr->find("mrl_port");
  1835. if (it != cArr->end())
  1836. {
  1837. tVal->mrl_port = atoi(it->second.c_str());
  1838. };
  1839. it = cArr->find("mrl");
  1840. if (it != cArr->end())
  1841. {
  1842. sprintf(tVal->mrl, it->second.c_str());
  1843. };
  1844. };
  1845. };
  1846. };
  1847. deleteResult(result);
  1848. };
  1849. return tVal;
  1850. }
  1851. uint32_t sqliteDBC::getMediaStreamID(uint32_t source_id, CSTR token)
  1852. {
  1853. uint32_t RetVal = 0;
  1854. if (m_bStoIsConnected)
  1855. {
  1856. char tQBuf[CTEXT];
  1857. AAssoc * result;
  1858. sprintf(tQBuf, "SELECT `id` FROM `media_streams` WHERE `source_id`=%d AND `token`='%s'", source_id, token);
  1859. #ifdef DEBUG_ALL
  1860. LOG(tQBuf);
  1861. #endif
  1862. result = m_pDataStorage->Query(tQBuf);
  1863. if (result != NULL)
  1864. {
  1865. if (result->size() > 0)
  1866. {
  1867. Assoc * cArr;
  1868. Assoc::iterator it;
  1869. cArr = result->at(0);
  1870. it = cArr->find("id");
  1871. if (it != cArr->end())
  1872. {
  1873. RetVal = atoi(it->second.c_str());
  1874. };
  1875. };
  1876. };
  1877. deleteResult(result);
  1878. };
  1879. return RetVal;
  1880. }
  1881. DBC_MODEM_MANAGER_DATA * sqliteDBC::getModemManagerData(uint32_t ID)
  1882. {
  1883. DBC_MODEM_MANAGER_DATA * tVal = NULL;
  1884. if (m_bStoIsConnected)
  1885. {
  1886. char tQBuf[CTEXT];
  1887. AAssoc * result;
  1888. sprintf(tQBuf, "SELECT * FROM `module_modem` WHERE `id`=%d", ID);
  1889. #ifdef DEBUG_ALL
  1890. LOG(tQBuf);
  1891. #endif
  1892. result = m_pDataStorage->Query(tQBuf);
  1893. if (result != NULL)
  1894. {
  1895. if (result->size() > 0)
  1896. {
  1897. Assoc * cArr;
  1898. tVal = (DBC_MODEM_MANAGER_DATA *) malloc(sizeof(DBC_MODEM_MANAGER_DATA));
  1899. if (tVal != NULL)
  1900. {
  1901. memset(tVal, 0, sizeof(DBC_MODEM_MANAGER_DATA));
  1902. tVal->id = ID;
  1903. Assoc::iterator it;
  1904. cArr = result->at(0);
  1905. it = cArr->find("modem_id");
  1906. if (it != cArr->end())
  1907. {
  1908. tVal->modem_id = atoi(it->second.c_str());
  1909. };
  1910. it = cArr->find("evt_mode");
  1911. if (it != cArr->end())
  1912. {
  1913. tVal->evt_mode = atoi(it->second.c_str());
  1914. };
  1915. it = cArr->find("time_window");
  1916. if (it != cArr->end())
  1917. {
  1918. tVal->time_window = atoi(it->second.c_str());
  1919. };
  1920. it = cArr->find("sms_enabled");
  1921. if (it != cArr->end())
  1922. {
  1923. tVal->sms_enabled = (bool) atoi(it->second.c_str());
  1924. };
  1925. it = cArr->find("call_enabled");
  1926. if (it != cArr->end())
  1927. {
  1928. tVal->call_enabled = (bool) atoi(it->second.c_str());
  1929. };
  1930. it = cArr->find("call_hangup_wait");
  1931. if (it != cArr->end())
  1932. {
  1933. tVal->call_hangup_wait = atoi(it->second.c_str());
  1934. };
  1935. };
  1936. };
  1937. };
  1938. deleteResult(result);
  1939. };
  1940. return tVal;
  1941. }
  1942. DBC_MODEM_PHONEBOOK_RECORD * sqliteDBC::getModemPhonebookRecord(uint32_t ID)
  1943. {
  1944. DBC_MODEM_PHONEBOOK_RECORD * tVal = NULL;
  1945. if (m_bStoIsConnected)
  1946. {
  1947. char tQBuf[CTEXT];
  1948. AAssoc * result;
  1949. sprintf(tQBuf, "SELECT * FROM `module_modem_phonebook` WHERE `id`=%d", ID);
  1950. #ifdef DEBUG_ALL
  1951. LOG(tQBuf);
  1952. #endif
  1953. result = m_pDataStorage->Query(tQBuf);
  1954. if (result != NULL)
  1955. {
  1956. if (result->size() > 0)
  1957. {
  1958. Assoc * cArr;
  1959. tVal = (DBC_MODEM_PHONEBOOK_RECORD *) malloc(sizeof(DBC_MODEM_PHONEBOOK_RECORD));
  1960. if (tVal != NULL)
  1961. {
  1962. memset(tVal, 0, sizeof(DBC_MODEM_PHONEBOOK_RECORD));
  1963. Assoc::iterator it;
  1964. cArr = result->at(0);
  1965. it = cArr->find("id");
  1966. if (it != cArr->end())
  1967. {
  1968. tVal->id = atoi(it->second.c_str());
  1969. };
  1970. it = cArr->find("number");
  1971. if (it != cArr->end())
  1972. {
  1973. strncpy(tVal->number, it->second.c_str(), 63);
  1974. tVal->number[63] = (char) 0;
  1975. };
  1976. it = cArr->find("mode");
  1977. if (it != cArr->end())
  1978. {
  1979. tVal->mode = atoi(it->second.c_str());
  1980. };
  1981. it = cArr->find("trig_special");
  1982. if (it != cArr->end())
  1983. {
  1984. tVal->trigger_special = atoi(it->second.c_str());
  1985. };
  1986. it = cArr->find("label");
  1987. if (it != cArr->end())
  1988. {
  1989. strncpy(tVal->label, it->second.c_str(), 63);
  1990. tVal->label[63] = (char) 0;
  1991. };
  1992. it = cArr->find("custom_msg");
  1993. if (it != cArr->end())
  1994. {
  1995. strncpy(tVal->custom_msg, it->second.c_str(), 127);
  1996. tVal->custom_msg[127] = (char) 0;
  1997. };
  1998. };
  1999. };
  2000. };
  2001. deleteResult(result);
  2002. };
  2003. return tVal;
  2004. }
  2005. DBC_MODULE_DATA * sqliteDBC::getModuleData(uint32_t ID)
  2006. {
  2007. DBC_MODULE_DATA * tVal = NULL;
  2008. if (m_bStoIsConnected)
  2009. {
  2010. char tQBuf[CTEXT];
  2011. AAssoc * result;
  2012. sprintf(tQBuf, "SELECT * FROM modules WHERE `id`=%d", ID);
  2013. #ifdef DEBUG_ALL
  2014. LOG(tQBuf);
  2015. #endif
  2016. result = m_pDataStorage->Query(tQBuf);
  2017. if (result != NULL)
  2018. {
  2019. if (result->size() > 0)
  2020. {
  2021. Assoc * cArr;
  2022. tVal = (DBC_MODULE_DATA *) malloc(sizeof(DBC_MODULE_DATA));
  2023. if (tVal != NULL)
  2024. {
  2025. Assoc::iterator it;
  2026. cArr = result->at(0);
  2027. it = cArr->find("id");
  2028. if (it != cArr->end())
  2029. {
  2030. tVal->module_id = atoi(it->second.c_str());
  2031. };
  2032. it = cArr->find("aux_id");
  2033. if (it != cArr->end())
  2034. {
  2035. tVal->aux_id = atoi(it->second.c_str());
  2036. };
  2037. it = cArr->find("type");
  2038. if (it != cArr->end())
  2039. {
  2040. tVal->type = atoi(it->second.c_str());
  2041. };
  2042. it = cArr->find("enabled");
  2043. if (it != cArr->end())
  2044. {
  2045. tVal->enabled = (bool) atoi(it->second.c_str());
  2046. };
  2047. };
  2048. };
  2049. };
  2050. deleteResult(result);
  2051. };
  2052. return tVal;
  2053. }
  2054. DBC_LRF_INI * sqliteDBC::getINIData(uint32_t lrfID)
  2055. {
  2056. DBC_LRF_INI * tVal = NULL;
  2057. if (m_bStoIsConnected)
  2058. {
  2059. char tQBuf[CTEXT];
  2060. AAssoc * result;
  2061. sprintf(tQBuf, "SELECT * FROM `lrf_ini_settings` WHERE `sensor_id`=%d", lrfID);
  2062. #ifdef DEBUG_ALL
  2063. LOG(tQBuf);
  2064. #endif
  2065. result = m_pDataStorage->Query(tQBuf);
  2066. if (result != NULL)
  2067. {
  2068. if (result->size() > 0)
  2069. {
  2070. Assoc * cArr;
  2071. tVal = (DBC_LRF_INI *) malloc(sizeof(DBC_LRF_INI));
  2072. if (tVal != NULL)
  2073. {
  2074. memset(tVal, 0, sizeof(DBC_LRF_INI));
  2075. Assoc::iterator it;
  2076. cArr = result->at(0);
  2077. it = cArr->find("AUTORESET_ENABLE");
  2078. if (it != cArr->end())
  2079. {
  2080. tVal->_AUTO_RESET_ENABLE = (bool) atoi(it->second.c_str());
  2081. };
  2082. it = cArr->find("AUTORESET_INTERLEAVE");
  2083. if (it != cArr->end())
  2084. {
  2085. tVal->_AUTO_RESET_INTERLEAVE = atoi(it->second.c_str());
  2086. };
  2087. it = cArr->find("AUTORESET_TIMEOUT");
  2088. if (it != cArr->end())
  2089. {
  2090. tVal->_AUTO_RESET_TIMEOUT = atoi(it->second.c_str());
  2091. };
  2092. it = cArr->find("BOARD_TEMPERATURE");
  2093. if (it != cArr->end())
  2094. {
  2095. tVal->_BOARD_TEMPERATURE = atoi(it->second.c_str());
  2096. };
  2097. it = cArr->find("BOARD_ZEROSHIFT");
  2098. if (it != cArr->end())
  2099. {
  2100. tVal->_BOARD_ZEROSHIFT = atoi(it->second.c_str());
  2101. };
  2102. it = cArr->find("BOARD_SPEED");
  2103. if (it != cArr->end())
  2104. {
  2105. tVal->_BOARD_SPEED = atoi(it->second.c_str());
  2106. };
  2107. it = cArr->find("RUN_DIR");
  2108. if (it != cArr->end())
  2109. {
  2110. tVal->_RUN_DIR = atoi(it->second.c_str());
  2111. };
  2112. it = cArr->find("NO_BLACK_TARGETS");
  2113. if (it != cArr->end())
  2114. {
  2115. tVal->_NO_BLACK_TARGETS = atoi(it->second.c_str());
  2116. };
  2117. it = cArr->find("JUST_MOVING_TARGETS");
  2118. if (it != cArr->end())
  2119. {
  2120. tVal->_JUST_MOVING_TARGETS = (bool) atoi(it->second.c_str());
  2121. };
  2122. it = cArr->find("JUST_MOVING_RADIAL");
  2123. if (it != cArr->end())
  2124. {
  2125. tVal->_JUST_MOVING_RADIAL = (bool) atoi(it->second.c_str());
  2126. };
  2127. it = cArr->find("JUST_MOVING_DISTANCE");
  2128. if (it != cArr->end())
  2129. {
  2130. tVal->_JUST_MOVING_DISTANCE = (bool) atoi(it->second.c_str());
  2131. };
  2132. it = cArr->find("FREEZ_FIRST_TH");
  2133. if (it != cArr->end())
  2134. {
  2135. tVal->_FREEZ_FIRST_TH = (bool) atoi(it->second.c_str());
  2136. };
  2137. it = cArr->find("SAVE_FIRST_TH");
  2138. if (it != cArr->end())
  2139. {
  2140. tVal->_SAVE_FIRST_TH = (bool) atoi(it->second.c_str());
  2141. };
  2142. };
  2143. };
  2144. };
  2145. deleteResult(result);
  2146. };
  2147. return tVal;
  2148. }
  2149. bool sqliteDBC::getEventSessionStatus(uint32_t nodeID)
  2150. {
  2151. bool rv = false;
  2152. if (m_bRepIsConnected)
  2153. {
  2154. char tQBuf[CTEXT];
  2155. AAssoc * result;
  2156. // Since Unimodule -> no need to check NodeID
  2157. // 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,
  2158. // EVT_LRF_END_SESSION_TO );
  2159. 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);
  2160. #ifdef DEBUG_ALL
  2161. LOG(tQBuf);
  2162. #endif
  2163. result = m_pDataReports->Query(tQBuf);
  2164. if (result != NULL)
  2165. {
  2166. if (result->size() > 0)
  2167. {
  2168. Assoc * cArr;
  2169. Assoc::iterator it;
  2170. cArr = result->at(0);
  2171. it = cArr->find("evt_type");
  2172. if (it != cArr->end())
  2173. {
  2174. if (atoi(it->second.c_str()) == EVT_LRF_START_SESSION)
  2175. {
  2176. rv = true;
  2177. }
  2178. };
  2179. };
  2180. };
  2181. deleteResult(result);
  2182. };
  2183. return rv;
  2184. }
  2185. /*
  2186. * The Last TS is from the current report (as latest)
  2187. */
  2188. uint32_t sqliteDBC::getLastEventTS()
  2189. {
  2190. uint32_t rv = 0;
  2191. if (m_bRepIsConnected)
  2192. {
  2193. char tQBuf[CTEXT];
  2194. AAssoc * result;
  2195. sprintf(tQBuf, "SELECT `ts` FROM `events` ORDER BY `ts` DESC, `ts_ms` DESC LIMIT 1");
  2196. #ifdef DEBUG_ALL
  2197. LOG(tQBuf);
  2198. #endif
  2199. result = m_pDataReports->Query(tQBuf);
  2200. if (result != NULL)
  2201. {
  2202. if (result->size() > 0)
  2203. {
  2204. Assoc * cArr;
  2205. Assoc::iterator it;
  2206. cArr = result->at(0);
  2207. it = cArr->find("ts");
  2208. if (it != cArr->end())
  2209. {
  2210. rv = atoi(it->second.c_str());
  2211. };
  2212. };
  2213. };
  2214. deleteResult(result);
  2215. };
  2216. return rv;
  2217. }
  2218. /*
  2219. * The first TS is generaly must show a DAY of first registered event so TS fetched
  2220. * from general statistics
  2221. */
  2222. uint32_t sqliteDBC::getFirstEventTS()
  2223. {
  2224. uint32_t rv = 0;
  2225. if (m_bStaIsConnected)
  2226. {
  2227. char tQBuf[CTEXT];
  2228. AAssoc * result;
  2229. sprintf(tQBuf, "SELECT `year`,`month`,`day` FROM `days` ORDER BY `year`,`month`,`day` LIMIT 1");
  2230. #ifdef DEBUG_ALL
  2231. LOG(tQBuf);
  2232. #endif
  2233. result = m_pDataStatistics->Query(tQBuf);
  2234. if (result != NULL)
  2235. {
  2236. if (result->size() > 0)
  2237. {
  2238. REPORT_DIRINFO RDF = {0};
  2239. Assoc * cArr;
  2240. Assoc::iterator it;
  2241. cArr = result->at(0);
  2242. it = cArr->find("year");
  2243. if (it != cArr->end())
  2244. {
  2245. strncpy(RDF.YEAR, it->second.c_str(), 4);
  2246. };
  2247. it = cArr->find("month");
  2248. if (it != cArr->end())
  2249. {
  2250. strncpy(RDF.MONTH, it->second.c_str(), 2);
  2251. };
  2252. it = cArr->find("day");
  2253. if (it != cArr->end())
  2254. {
  2255. strncpy(RDF.DAY, it->second.c_str(), 2);
  2256. };
  2257. rv = getTsFromRDF(RDF);
  2258. };
  2259. };
  2260. deleteResult(result);
  2261. };
  2262. return rv;
  2263. }
  2264. bool sqliteDBC::deleteReportDate(uint32_t ts)
  2265. {
  2266. if (ts)
  2267. {
  2268. REPORT_DIRINFO tRDF = {0};
  2269. fillRDF(ts, tRDF);
  2270. LOG("Delete RDF Dir");
  2271. if (deleteRDFDir(m_pDbReportBase, tRDF))
  2272. {
  2273. LOG("Dir Deleted OK");
  2274. return deleteReportStat(tRDF);
  2275. };
  2276. };
  2277. return false;
  2278. }
  2279. DBC_LRF_DATA * sqliteDBC::getLRFData(uint32_t ID)
  2280. {
  2281. DBC_LRF_DATA * tVal = NULL;
  2282. if (m_bStoIsConnected)
  2283. {
  2284. char tQBuf[CTEXT];
  2285. AAssoc * result;
  2286. sprintf(tQBuf, "SELECT * FROM lrf_sensors WHERE `id`=%d", ID);
  2287. #ifdef DEBUG_ALL
  2288. LOG(tQBuf);
  2289. #endif
  2290. result = m_pDataStorage->Query(tQBuf);
  2291. if (result != NULL)
  2292. {
  2293. if (result->size() > 0)
  2294. {
  2295. Assoc * cArr;
  2296. tVal = (DBC_LRF_DATA *) malloc(sizeof(DBC_LRF_DATA));
  2297. if (tVal != NULL)
  2298. {
  2299. memset(tVal, 0, sizeof(DBC_LRF_DATA));
  2300. Assoc::iterator it;
  2301. tVal->id = ID;
  2302. cArr = result->at(0);
  2303. it = cArr->find("data_status");
  2304. if (it != cArr->end())
  2305. {
  2306. tVal->data_status = atoi(it->second.c_str());
  2307. };
  2308. it = cArr->find("serial");
  2309. if (it != cArr->end())
  2310. {
  2311. strncpy(tVal->serial, it->second.c_str(), sizeof(tVal->serial) - 1);
  2312. tVal->serial[sizeof(tVal->serial) - 1] = (char) 0;
  2313. };
  2314. it = cArr->find("version");
  2315. if (it != cArr->end())
  2316. {
  2317. strncpy(tVal->version, it->second.c_str(), sizeof(tVal->version) - 1);
  2318. tVal->version[sizeof(tVal->version) - 1] = (char) 0;
  2319. };
  2320. it = cArr->find("x");
  2321. if (it != cArr->end())
  2322. {
  2323. tVal->x = atof(it->second.c_str());
  2324. };
  2325. it = cArr->find("y");
  2326. if (it != cArr->end())
  2327. {
  2328. tVal->y = atof(it->second.c_str());
  2329. };
  2330. it = cArr->find("z");
  2331. if (it != cArr->end())
  2332. {
  2333. tVal->z = atof(it->second.c_str());
  2334. };
  2335. it = cArr->find("x_rot");
  2336. if (it != cArr->end())
  2337. {
  2338. tVal->x_rot = atof(it->second.c_str());
  2339. };
  2340. it = cArr->find("y_rot");
  2341. if (it != cArr->end())
  2342. {
  2343. tVal->y_rot = atof(it->second.c_str());
  2344. };
  2345. it = cArr->find("z_rot");
  2346. if (it != cArr->end())
  2347. {
  2348. tVal->z_rot = atof(it->second.c_str());
  2349. };
  2350. it = cArr->find("uart_id");
  2351. if (it != cArr->end())
  2352. {
  2353. tVal->uart_id = atoi(it->second.c_str());
  2354. };
  2355. it = cArr->find("device_id");
  2356. if (it != cArr->end())
  2357. {
  2358. tVal->device_id = atoi(it->second.c_str());
  2359. };
  2360. it = cArr->find("ip");
  2361. if (it != cArr->end())
  2362. {
  2363. sprintf(tVal->master_host, it->second.c_str());
  2364. };
  2365. it = cArr->find("aux_ip");
  2366. if (it != cArr->end())
  2367. {
  2368. sprintf(tVal->slave_host, it->second.c_str());
  2369. };
  2370. it = cArr->find("port");
  2371. if (it != cArr->end())
  2372. {
  2373. tVal->master_port = atoi(it->second.c_str());
  2374. };
  2375. it = cArr->find("aux_port");
  2376. if (it != cArr->end())
  2377. {
  2378. tVal->slave_port = atoi(it->second.c_str());
  2379. };
  2380. it = cArr->find("auto_draw");
  2381. if (it != cArr->end())
  2382. {
  2383. tVal->auto_draw = atoi(it->second.c_str());
  2384. };
  2385. it = cArr->find("filter_id");
  2386. if (it != cArr->end())
  2387. {
  2388. tVal->filter_id = atoi(it->second.c_str());
  2389. };
  2390. it = cArr->find("enabled_lrf");
  2391. if (it != cArr->end())
  2392. {
  2393. tVal->enabled_lrf = atoi(it->second.c_str());
  2394. };
  2395. it = cArr->find("max_range");
  2396. if (it != cArr->end())
  2397. {
  2398. tVal->max_range = atof(it->second.c_str());
  2399. };
  2400. it = cArr->find("base_shift");
  2401. if (it != cArr->end())
  2402. {
  2403. tVal->base_shift = atoi(it->second.c_str());
  2404. };
  2405. // it = cArr->find("mirror_shift");
  2406. // if( it != cArr->end() ){
  2407. // tVal->mirror_shift = atoi( it->second.c_str() );
  2408. // };
  2409. it = cArr->find("detection_profile_id");
  2410. if (it != cArr->end())
  2411. {
  2412. tVal->detection_profile_id = atoi(it->second.c_str());
  2413. };
  2414. it = cArr->find("prefered_camera_id");
  2415. if (it != cArr->end())
  2416. {
  2417. tVal->prefered_camera_id = atoi(it->second.c_str());
  2418. };
  2419. /*
  2420. it = cArr->find("draw_lrf_id");
  2421. if( it != cArr->end() ){
  2422. tVal->draw_lrf_id = atoi( it->second.c_str() );
  2423. };
  2424. it = cArr->find("width_table_id");
  2425. if( it != cArr->end() ){
  2426. tVal->width_table_id = atoi( it->second.c_str() );
  2427. };
  2428. it = cArr->find("zoom_table_id");
  2429. if( it != cArr->end() ){
  2430. tVal->zoom_table_id = atoi( it->second.c_str() );
  2431. };
  2432. it = cArr->find("embd_cam");
  2433. if( it != cArr->end() ){
  2434. tVal->embd_cam = atoi( it->second.c_str() );
  2435. };
  2436. it = cArr->find("embd_cam_auto");
  2437. if( it != cArr->end() ){
  2438. tVal->embd_cam_auto = (bool)atoi( it->second.c_str() );
  2439. };
  2440. it = cArr->find("embd_dc");
  2441. if( it != cArr->end() ){
  2442. tVal->embd_dc = atoi( it->second.c_str() );
  2443. };
  2444. it = cArr->find("autoreset");
  2445. if( it != cArr->end() ){
  2446. tVal->autoreset = (bool)atoi( it->second.c_str() );
  2447. };
  2448. it = cArr->find("force_settings");
  2449. if( it != cArr->end() ){
  2450. tVal->force_settings = (bool)atoi( it->second.c_str() );
  2451. };
  2452. */
  2453. };
  2454. };
  2455. };
  2456. deleteResult(result);
  2457. };
  2458. return tVal;
  2459. }
  2460. PLGS_2D_PARAMETERS sqliteDBC::getLRFDetectionProfile(uint32_t lrfID, uint8_t profileID, bool master)
  2461. {
  2462. PLGS_2D_PARAMETERS retVal = NULL;
  2463. if (m_bStoIsConnected)
  2464. {
  2465. char tQBuf[CTEXT];
  2466. AAssoc * result;
  2467. sprintf(tQBuf, "SELECT * FROM `lrf_detection_profiles` WHERE `aux_id`=%d AND `profile_id`=%d AND `master`=%d", lrfID, profileID, (master) ? 1 : 0);
  2468. #ifdef DEBUG_ALL
  2469. LOG(tQBuf);
  2470. #endif
  2471. result = m_pDataStorage->Query(tQBuf);
  2472. if (result != NULL)
  2473. {
  2474. if (result->size() > 0)
  2475. {
  2476. retVal = (PLGS_2D_PARAMETERS) malloc(sizeof(LGS_2D_PARAMETERS));
  2477. memset(retVal, 0, sizeof(LGS_2D_PARAMETERS));
  2478. Assoc * cArr;
  2479. Assoc::iterator it;
  2480. cArr = result->at(0);
  2481. it = cArr->find("order");
  2482. if (it != cArr->end())
  2483. {
  2484. retVal->nOrder = atoi(it->second.c_str());
  2485. };
  2486. it = cArr->find("offset");
  2487. if (it != cArr->end())
  2488. {
  2489. retVal->nOffset = atoi(it->second.c_str());
  2490. };
  2491. it = cArr->find("delta");
  2492. if (it != cArr->end())
  2493. {
  2494. retVal->nDelta = atoi(it->second.c_str());
  2495. };
  2496. it = cArr->find("flip");
  2497. if (it != cArr->end())
  2498. {
  2499. retVal->nFlip = atoi(it->second.c_str());
  2500. };
  2501. it = cArr->find("width");
  2502. if (it != cArr->end())
  2503. {
  2504. retVal->nWidth = atoi(it->second.c_str());
  2505. };
  2506. it = cArr->find("sst");
  2507. if (it != cArr->end())
  2508. {
  2509. retVal->bSST = atoi(it->second.c_str());
  2510. };
  2511. it = cArr->find("sun");
  2512. if (it != cArr->end())
  2513. {
  2514. retVal->nSun = atoi(it->second.c_str());
  2515. };
  2516. };
  2517. };
  2518. deleteResult(result);
  2519. };
  2520. return retVal;
  2521. }
  2522. RELAY_PARAMETERS * sqliteDBC::getLRFDryContact(uint32_t lrfID)
  2523. {
  2524. RELAY_PARAMETERS * retVal = NULL;
  2525. if (m_bStoIsConnected)
  2526. {
  2527. char tQBuf[CTEXT];
  2528. AAssoc * result;
  2529. sprintf(tQBuf, "SELECT `dc_auto`, `dc_no`, `dc_window` FROM `lrf_sensors` WHERE `id`=%d", lrfID);
  2530. #ifdef DEBUG_ALL
  2531. LOG(tQBuf);
  2532. #endif
  2533. result = m_pDataStorage->Query(tQBuf);
  2534. if (result != NULL)
  2535. {
  2536. if (result->size() > 0)
  2537. {
  2538. retVal = (RELAY_PARAMETERS *) malloc(sizeof(RELAY_PARAMETERS));
  2539. if (retVal)
  2540. {
  2541. Assoc * cArr;
  2542. Assoc::iterator it;
  2543. cArr = result->at(0);
  2544. it = cArr->find("dc_auto");
  2545. if (it != cArr->end())
  2546. {
  2547. retVal->bAuto = atoi(it->second.c_str());
  2548. };
  2549. it = cArr->find("dc_no");
  2550. if (it != cArr->end())
  2551. {
  2552. retVal->bNormallyClose = (atoi(it->second.c_str()) == 0);
  2553. };
  2554. it = cArr->find("dc_window");
  2555. if (it != cArr->end())
  2556. {
  2557. retVal->nAutoDelay = atoi(it->second.c_str());
  2558. };
  2559. };
  2560. };
  2561. };
  2562. deleteResult(result);
  2563. };
  2564. return retVal;
  2565. }
  2566. uint8_t sqliteDBC::getLRFDataStatus(uint32_t ID)
  2567. {
  2568. uint8_t RetVal = 0;
  2569. if (m_bStoIsConnected)
  2570. {
  2571. char tQBuf[CTEXT];
  2572. AAssoc * result;
  2573. sprintf(tQBuf, "SELECT `data_status` FROM `lrf_sensors` WHERE `id`=%d", ID);
  2574. #ifdef DEBUG_ALL
  2575. LOG(tQBuf);
  2576. #endif
  2577. result = m_pDataStorage->Query(tQBuf);
  2578. if (result != NULL)
  2579. {
  2580. if (result->size() > 0)
  2581. {
  2582. Assoc * cArr;
  2583. Assoc::iterator it;
  2584. cArr = result->at(0);
  2585. it = cArr->find("data_status");
  2586. if (it != cArr->end())
  2587. {
  2588. RetVal = atoi(it->second.c_str());
  2589. };
  2590. };
  2591. };
  2592. deleteResult(result);
  2593. };
  2594. return RetVal;
  2595. }
  2596. uint32_t sqliteDBC::getLRFMaskID(uint32_t deviceID, uint8_t lrfID, uint8_t layer)
  2597. {
  2598. uint32_t RetVal = 0;
  2599. if (m_bStoIsConnected)
  2600. {
  2601. char tQBuf[CTEXT];
  2602. AAssoc * result;
  2603. sprintf(tQBuf, "SELECT `id` FROM `lrf_masks` WHERE `sensor_id`=%d AND `lrf_id`=%d AND `layer`=%d ", deviceID, lrfID, layer);
  2604. #ifdef DEBUG_ALL
  2605. LOG(tQBuf);
  2606. #endif
  2607. result = m_pDataStorage->Query(tQBuf);
  2608. if (result != NULL)
  2609. {
  2610. if (result->size() > 0)
  2611. {
  2612. Assoc * cArr;
  2613. Assoc::iterator it;
  2614. cArr = result->at(0);
  2615. it = cArr->find("id");
  2616. if (it != cArr->end())
  2617. {
  2618. RetVal = atoi(it->second.c_str());
  2619. };
  2620. };
  2621. };
  2622. deleteResult(result);
  2623. };
  2624. return RetVal;
  2625. }
  2626. uint32_t sqliteDBC::getLRFMaskPolygoneID(uint32_t maskID, uint8_t vertecID)
  2627. {
  2628. uint32_t RetVal = 0;
  2629. if (m_bStoIsConnected)
  2630. {
  2631. char tQBuf[CTEXT];
  2632. AAssoc * result;
  2633. sprintf(tQBuf, "SELECT `id` FROM `lrf_mask_vertexes` WHERE `mask_id`=%d AND `vertex_id`=%d", maskID, vertecID);
  2634. #ifdef DEBUG_ALL
  2635. LOG(tQBuf);
  2636. #endif
  2637. result = m_pDataStorage->Query(tQBuf);
  2638. if (result != NULL)
  2639. {
  2640. if (result->size() > 0)
  2641. {
  2642. Assoc * cArr;
  2643. Assoc::iterator it;
  2644. cArr = result->at(0);
  2645. it = cArr->find("id");
  2646. if (it != cArr->end())
  2647. {
  2648. RetVal = atoi(it->second.c_str());
  2649. };
  2650. };
  2651. };
  2652. deleteResult(result);
  2653. };
  2654. return RetVal;
  2655. }
  2656. uint8_t sqliteDBC::getLRFMasksStatus(uint32_t deviceID)
  2657. {
  2658. uint8_t RetVal = 0;
  2659. if (m_bStoIsConnected)
  2660. {
  2661. char tQBuf[CTEXT];
  2662. AAssoc * result;
  2663. sprintf(tQBuf, "SELECT `layer` FROM `lrf_masks` WHERE `sensor_id`=%d AND `status`='E'", deviceID);
  2664. #ifdef DEBUG_ALL
  2665. LOG(tQBuf);
  2666. #endif
  2667. result = m_pDataStorage->Query(tQBuf);
  2668. if (result != NULL)
  2669. {
  2670. if (result->size() > 0)
  2671. {
  2672. Assoc * cArr;
  2673. Assoc::iterator it;
  2674. for (int i = 0; i < result->size(); i++)
  2675. {
  2676. cArr = result->at(i);
  2677. it = cArr->find("layer");
  2678. if (it != cArr->end())
  2679. {
  2680. RetVal |= (1 << atoi(it->second.c_str()));
  2681. };
  2682. };
  2683. };
  2684. };
  2685. deleteResult(result);
  2686. };
  2687. return RetVal;
  2688. }
  2689. DBC_LRF_UPLOAD_DATA * sqliteDBC::getLRFUploadData(uint32_t upID)
  2690. {
  2691. DBC_LRF_UPLOAD_DATA * RetVal = NULL;
  2692. if (m_bStoIsConnected)
  2693. {
  2694. char tQBuf[CTEXT];
  2695. AAssoc * result;
  2696. sprintf(tQBuf, "SELECT * FROM `lrf_uploads` WHERE `id`=%d", upID);
  2697. #ifdef DEBUG_ALL
  2698. LOG(tQBuf);
  2699. #endif
  2700. result = m_pDataStorage->Query(tQBuf);
  2701. if (result->size() > 0)
  2702. {
  2703. Assoc * cArr;
  2704. RetVal = (DBC_LRF_UPLOAD_DATA *) malloc(sizeof(DBC_LRF_UPLOAD_DATA));
  2705. if (RetVal != NULL)
  2706. {
  2707. memset(RetVal, 0, sizeof(DBC_LRF_UPLOAD_DATA));
  2708. Assoc::iterator it;
  2709. RetVal->id = upID;
  2710. cArr = result->at(0);
  2711. it = cArr->find("md5");
  2712. if (it != cArr->end())
  2713. {
  2714. strncpy(RetVal->md5, it->second.c_str(), 32);
  2715. };
  2716. it = cArr->find("file");
  2717. if (it != cArr->end())
  2718. {
  2719. strncpy(RetVal->fname, it->second.c_str(), 16);
  2720. };
  2721. it = cArr->find("issue_reset");
  2722. if (it != cArr->end())
  2723. {
  2724. RetVal->transfer_mode = atoi(it->second.c_str());
  2725. };
  2726. };
  2727. };
  2728. deleteResult(result);
  2729. };
  2730. return RetVal;
  2731. }
  2732. DBC_WIDTH_TABLE * sqliteDBC::getLRFWidthTable(uint32_t lrfID)
  2733. {
  2734. DBC_WIDTH_TABLE * retVal = NULL;
  2735. if (m_bStoIsConnected)
  2736. {
  2737. retVal = (DBC_WIDTH_TABLE *) malloc(sizeof(DBC_WIDTH_TABLE));
  2738. if (retVal)
  2739. {
  2740. char tQBuf[CTEXT];
  2741. AAssoc * result;
  2742. sprintf(tQBuf, "SELECT `range_k`, `width_k` FROM `lrf_detection_width_tables` WHERE `aux_id`=%d ORDER BY `range_k`", lrfID);
  2743. #ifdef DEBUG_ALL
  2744. LOG(tQBuf);
  2745. #endif
  2746. memset(retVal, 0, sizeof(DBC_WIDTH_TABLE));
  2747. result = m_pDataStorage->Query(tQBuf);
  2748. if (result != NULL)
  2749. {
  2750. if (result->size() > 0)
  2751. {
  2752. retVal->count = result->size();
  2753. retVal->wt_records = (DBC_WT_RECORD *) calloc(retVal->count, sizeof(DBC_WT_RECORD));
  2754. if (retVal->wt_records)
  2755. {
  2756. Assoc * cArr;
  2757. Assoc::iterator it;
  2758. for (int i = 0; i < retVal->count; i++)
  2759. {
  2760. cArr = result->at(i);
  2761. it = cArr->find("range_k");
  2762. if (it != cArr->end())
  2763. {
  2764. retVal->wt_records[i].rangePercent = atoi(it->second.c_str());
  2765. };
  2766. it = cArr->find("width_k");
  2767. if (it != cArr->end())
  2768. {
  2769. retVal->wt_records[i].widthPercent = atoi(it->second.c_str());
  2770. };
  2771. };
  2772. }
  2773. else
  2774. {
  2775. // retVal->count = 0;
  2776. LOG_ERROR("No Memory To create WT Record!");
  2777. free(retVal);
  2778. retVal = NULL;
  2779. };
  2780. };
  2781. };
  2782. deleteResult(result);
  2783. }
  2784. else
  2785. {
  2786. LOG_ERROR("No Memory To create WT!");
  2787. };
  2788. };
  2789. return retVal;
  2790. }
  2791. uint32_t sqliteDBC::getNodeAuxParam(uint32_t NodeID)
  2792. {
  2793. uint32_t retVal = 0;
  2794. if (m_bStoIsConnected)
  2795. {
  2796. char tQBuf[CTEXT];
  2797. AAssoc * result;
  2798. sprintf(tQBuf, "SELECT `aux_parameter` FROM `nodes` WHERE `id`=%d", NodeID);
  2799. #ifdef DEBUG_ALL
  2800. LOG(tQBuf);
  2801. #endif
  2802. result = m_pDataStorage->Query(tQBuf);
  2803. if (result != NULL)
  2804. {
  2805. if (result->size() > 0)
  2806. {
  2807. Assoc * cArr;
  2808. Assoc::iterator it;
  2809. cArr = result->at(0);
  2810. it = cArr->find("aux_parameter");
  2811. if (it != cArr->end())
  2812. {
  2813. retVal = atoi(it->second.c_str());
  2814. };
  2815. };
  2816. };
  2817. deleteResult(result);
  2818. };
  2819. return retVal;
  2820. }
  2821. DBC_NODE_DATA * sqliteDBC::getNodeData(uint32_t ID)
  2822. {
  2823. DBC_NODE_DATA * tVal = NULL;
  2824. if (m_bStoIsConnected)
  2825. {
  2826. char tQBuf[CTEXT];
  2827. AAssoc * result;
  2828. sprintf(tQBuf, "SELECT * FROM `nodes` WHERE `id`=%d", ID);
  2829. #ifdef DEBUG_ALL
  2830. LOG(tQBuf);
  2831. #endif
  2832. result = m_pDataStorage->Query(tQBuf);
  2833. if (result != NULL)
  2834. {
  2835. if (result->size() > 0)
  2836. {
  2837. Assoc * cArr;
  2838. tVal = (DBC_NODE_DATA *) malloc(sizeof(DBC_NODE_DATA));
  2839. if (tVal != NULL)
  2840. {
  2841. memset(tVal, 0, sizeof(DBC_NODE_DATA));
  2842. Assoc::iterator it;
  2843. tVal->node_id = ID;
  2844. cArr = result->at(0);
  2845. it = cArr->find("type");
  2846. if (it != cArr->end())
  2847. {
  2848. tVal->type = /*(NODE_TYPE)*/ atoi(it->second.c_str());
  2849. };
  2850. it = cArr->find("aux_id");
  2851. if (it != cArr->end())
  2852. {
  2853. tVal->aux_id = atoi(it->second.c_str());
  2854. };
  2855. it = cArr->find("aux_parameter");
  2856. if (it != cArr->end())
  2857. {
  2858. tVal->aux_parameter = atoi(it->second.c_str());
  2859. };
  2860. it = cArr->find("online");
  2861. if (it != cArr->end())
  2862. {
  2863. tVal->online = (bool) atoi(it->second.c_str());
  2864. };
  2865. it = cArr->find("enable");
  2866. if (it != cArr->end())
  2867. {
  2868. tVal->enabled = (bool) atoi(it->second.c_str());
  2869. };
  2870. it = cArr->find("name");
  2871. if (it != cArr->end())
  2872. {
  2873. sprintf(tVal->name, it->second.c_str());
  2874. };
  2875. };
  2876. };
  2877. };
  2878. deleteResult(result);
  2879. };
  2880. return tVal;
  2881. }
  2882. uint32_t sqliteDBC::getNodeIdByTypeAndAux(uint8_t type, uint32_t aux_id)
  2883. {
  2884. uint32_t retVal = 0;
  2885. if (m_bStoIsConnected)
  2886. {
  2887. char tQBuf[CTEXT];
  2888. AAssoc * result;
  2889. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type`=%d AND `aux_id`=%d", type, aux_id);
  2890. #ifdef DEBUG_ALL
  2891. LOG(tQBuf);
  2892. #endif
  2893. result = m_pDataStorage->Query(tQBuf);
  2894. if (result != NULL)
  2895. {
  2896. if (result->size() > 0)
  2897. {
  2898. Assoc * cArr;
  2899. Assoc::iterator it;
  2900. cArr = result->at(0);
  2901. it = cArr->find("id");
  2902. if (it != cArr->end())
  2903. {
  2904. retVal = atoi(it->second.c_str());
  2905. };
  2906. };
  2907. };
  2908. deleteResult(result);
  2909. };
  2910. return retVal;
  2911. }
  2912. CSTR sqliteDBC::getNodeName(uint32_t NodeID)
  2913. {
  2914. CSTR retVal = NULL;
  2915. if (m_bStoIsConnected)
  2916. {
  2917. char tQBuf[CTEXT];
  2918. AAssoc * result;
  2919. sprintf(tQBuf, "SELECT `name` FROM `nodes` WHERE `id`=%d", NodeID);
  2920. #ifdef DEBUG_ALL
  2921. LOG(tQBuf);
  2922. #endif
  2923. result = m_pDataStorage->Query(tQBuf);
  2924. if (result != NULL)
  2925. {
  2926. if (result->size() > 0)
  2927. {
  2928. Assoc * cArr;
  2929. Assoc::iterator it;
  2930. cArr = result->at(0);
  2931. it = cArr->find("name");
  2932. if (it != cArr->end())
  2933. {
  2934. retVal = strdup(it->second.c_str());
  2935. };
  2936. };
  2937. };
  2938. deleteResult(result);
  2939. };
  2940. return retVal;
  2941. }
  2942. DBC_NET_IF_DATA * sqliteDBC::getNIF_Data(uint32_t nif_id)
  2943. {
  2944. DBC_NET_IF_DATA * tVal = NULL;
  2945. if (m_bStoIsConnected)
  2946. {
  2947. char tQBuf[CTEXT];
  2948. AAssoc * result;
  2949. sprintf(tQBuf, "SELECT `type`, `mode`, `device_name`, `mac`, `aux_id` FROM `network_interfaces` WHERE `id`=%d", nif_id);
  2950. #ifdef DEBUG_ALL
  2951. LOG(tQBuf);
  2952. #endif
  2953. result = m_pDataStorage->Query(tQBuf);
  2954. if (result != NULL)
  2955. {
  2956. if (result->size() > 0)
  2957. {
  2958. Assoc * cArr;
  2959. tVal = (DBC_NET_IF_DATA *) malloc(sizeof(DBC_NET_IF_DATA));
  2960. if (tVal != NULL)
  2961. {
  2962. Assoc::iterator it;
  2963. tVal->id = nif_id;
  2964. cArr = result->at(0);
  2965. it = cArr->find("type");
  2966. if (it != cArr->end())
  2967. {
  2968. tVal->type = atoi(it->second.c_str());
  2969. };
  2970. it = cArr->find("mode");
  2971. if (it != cArr->end())
  2972. {
  2973. tVal->mode = atoi(it->second.c_str());
  2974. };
  2975. it = cArr->find("device_name");
  2976. if (it != cArr->end())
  2977. {
  2978. strncpy(tVal->device_name, it->second.c_str(), 11);
  2979. tVal->device_name[10] = (char) 0;
  2980. };
  2981. it = cArr->find("mac");
  2982. if (it != cArr->end())
  2983. {
  2984. strncpy(tVal->mac, it->second.c_str(), 33);
  2985. tVal->mac[32] = (char) 0;
  2986. };
  2987. it = cArr->find("aux_id");
  2988. if (it != cArr->end())
  2989. {
  2990. tVal->aux_id = atoi(it->second.c_str());
  2991. };
  2992. };
  2993. };
  2994. };
  2995. deleteResult(result);
  2996. };
  2997. return tVal;
  2998. }
  2999. uint32_t sqliteDBC::getNIF_ID(CSTR key)
  3000. {
  3001. uint32_t tVal = 0;
  3002. if (m_bStoIsConnected)
  3003. {
  3004. char tQBuf[CTEXT];
  3005. AAssoc * result;
  3006. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `key`='%s'", key);
  3007. #ifdef DEBUG_ALL
  3008. LOG(tQBuf);
  3009. #endif
  3010. result = m_pDataStorage->Query(tQBuf);
  3011. if (result != NULL)
  3012. {
  3013. if (result->size() > 0)
  3014. {
  3015. Assoc * cArr;
  3016. Assoc::iterator it;
  3017. cArr = result->at(0);
  3018. it = cArr->find("id");
  3019. if (it != cArr->end())
  3020. {
  3021. tVal = atoi(it->second.c_str());
  3022. };
  3023. };
  3024. };
  3025. deleteResult(result);
  3026. };
  3027. return tVal;
  3028. }
  3029. uint32_t sqliteDBC::getNIF_IDbyDevice(CSTR device)
  3030. {
  3031. uint32_t tVal = 0;
  3032. if (m_bStoIsConnected)
  3033. {
  3034. char tQBuf[CTEXT];
  3035. AAssoc * result;
  3036. sprintf(tQBuf, "SELECT id FROM `network_interfaces` WHERE `device_name`='%s'", device);
  3037. #ifdef DEBUG_ALL
  3038. LOG(tQBuf);
  3039. #endif
  3040. result = m_pDataStorage->Query(tQBuf);
  3041. if (result != NULL)
  3042. {
  3043. if (result->size() > 0)
  3044. {
  3045. Assoc * cArr;
  3046. Assoc::iterator it;
  3047. cArr = result->at(0);
  3048. it = cArr->find("id");
  3049. if (it != cArr->end())
  3050. {
  3051. tVal = atoi(it->second.c_str());
  3052. };
  3053. };
  3054. };
  3055. deleteResult(result);
  3056. };
  3057. return tVal;
  3058. }
  3059. DBC_NETWORK_DATA * sqliteDBC::getNetworkData(uint32_t net_id)
  3060. {
  3061. DBC_NETWORK_DATA * tVal = NULL;
  3062. if (m_bStoIsConnected)
  3063. {
  3064. char tQBuf[CTEXT];
  3065. AAssoc * result;
  3066. sprintf(tQBuf, "SELECT * FROM `networks` WHERE `id`=%d", net_id);
  3067. #ifdef DEBUG
  3068. LOG(tQBuf);
  3069. #endif
  3070. result = m_pDataStorage->Query(tQBuf);
  3071. if (result != NULL)
  3072. {
  3073. if (result->size() > 0)
  3074. {
  3075. tVal = (DBC_NETWORK_DATA *) malloc(sizeof(DBC_NETWORK_DATA));
  3076. if (tVal)
  3077. {
  3078. Assoc * cArr;
  3079. Assoc::iterator it;
  3080. memset(tVal, 0, sizeof(DBC_NETWORK_DATA));
  3081. tVal->id = net_id;
  3082. cArr = result->at(0);
  3083. it = cArr->find("nif_id");
  3084. if (it != cArr->end())
  3085. {
  3086. tVal->nif_id = atoi(it->second.c_str());
  3087. };
  3088. it = cArr->find("type");
  3089. if (it != cArr->end())
  3090. {
  3091. tVal->type = (NETWORK_TYPE) atoi(it->second.c_str());
  3092. };
  3093. it = cArr->find("address");
  3094. if (it != cArr->end())
  3095. {
  3096. strncpy(tVal->address, it->second.c_str(), 64);
  3097. tVal->address[64] = (char) 0;
  3098. };
  3099. it = cArr->find("mask");
  3100. if (it != cArr->end())
  3101. {
  3102. strncpy(tVal->netmask, it->second.c_str(), 64);
  3103. tVal->netmask[64] = (char) 0;
  3104. };
  3105. it = cArr->find("broadcast");
  3106. if (it != cArr->end())
  3107. {
  3108. strncpy(tVal->broadcast, it->second.c_str(), 64);
  3109. tVal->broadcast[64] = (char) 0;
  3110. };
  3111. it = cArr->find("gateway");
  3112. if (it != cArr->end())
  3113. {
  3114. strncpy(tVal->gateway, it->second.c_str(), 64);
  3115. tVal->gateway[64] = (char) 0;
  3116. };
  3117. };
  3118. };
  3119. };
  3120. deleteResult(result);
  3121. };
  3122. return tVal;
  3123. }
  3124. uint32_t sqliteDBC::getNetworkInfrastructureID(uint32_t nif_id)
  3125. {
  3126. uint32_t tVal = 0;
  3127. if (m_bStoIsConnected)
  3128. {
  3129. char tQBuf[CTEXT];
  3130. AAssoc * result;
  3131. sprintf(tQBuf, "SELECT id FROM `network_infrastructure` WHERE `nif_id`=%d", nif_id);
  3132. #ifdef DEBUG_ALL
  3133. LOG(tQBuf);
  3134. #endif
  3135. result = m_pDataStorage->Query(tQBuf);
  3136. if (result != NULL)
  3137. {
  3138. if (result->size() > 0)
  3139. {
  3140. Assoc * cArr;
  3141. Assoc::iterator it;
  3142. cArr = result->at(0);
  3143. it = cArr->find("id");
  3144. if (it != cArr->end())
  3145. {
  3146. tVal = atoi(it->second.c_str());
  3147. };
  3148. };
  3149. };
  3150. deleteResult(result);
  3151. };
  3152. return tVal;
  3153. }
  3154. TARGET_SEGMENT * sqliteDBC::getRegisteredBubble(uint32_t ID)
  3155. {
  3156. TARGET_SEGMENT * tVal = NULL;
  3157. if (m_bStoIsConnected)
  3158. {
  3159. char tQBuf[CTEXT];
  3160. AAssoc * result;
  3161. sprintf(tQBuf, "SELECT * FROM `target_bubbles` WHERE `id`=%d", ID);
  3162. #ifdef DEBUG_ALL
  3163. LOG(tQBuf);
  3164. #endif
  3165. result = m_pDataStorage->Query(tQBuf);
  3166. if (result != NULL)
  3167. {
  3168. if (result->size() > 0)
  3169. {
  3170. tVal = (TARGET_SEGMENT *) malloc(sizeof(TARGET_SEGMENT));
  3171. if (tVal)
  3172. {
  3173. Assoc * cArr;
  3174. Assoc::iterator it;
  3175. memset(tVal, 0, sizeof(TARGET_SEGMENT));
  3176. tVal->db_id = ID;
  3177. cArr = result->at(0);
  3178. it = cArr->find("CENTER_X");
  3179. if (it != cArr->end())
  3180. {
  3181. tVal->system_center.x = atof(it->second.c_str());
  3182. };
  3183. it = cArr->find("CENTER_Y");
  3184. if (it != cArr->end())
  3185. {
  3186. tVal->system_center.y = atof(it->second.c_str());
  3187. };
  3188. it = cArr->find("CENTER_Z");
  3189. if (it != cArr->end())
  3190. {
  3191. tVal->system_center.z = atof(it->second.c_str());
  3192. };
  3193. it = cArr->find("BOUNDARY");
  3194. if (it != cArr->end())
  3195. {
  3196. tVal->boundary = atof(it->second.c_str());
  3197. };
  3198. it = cArr->find("BLOCK_COUNS");
  3199. if (it != cArr->end())
  3200. {
  3201. tVal->hit_count = atoi(it->second.c_str());
  3202. };
  3203. };
  3204. };
  3205. };
  3206. deleteResult(result);
  3207. };
  3208. return tVal;
  3209. }
  3210. REPORT_DIRINFO * sqliteDBC::getReportDirInfo(uint32_t id)
  3211. {
  3212. REPORT_DIRINFO * tVal = NULL;
  3213. if (m_bStaIsCreated)
  3214. {
  3215. char tQBuf[CTEXT];
  3216. AAssoc * result;
  3217. sprintf(tQBuf, "SELECT * FROM `days` WHERE `ROWID`=%d", id);
  3218. #ifdef DEBUG_ALL
  3219. LOG(tQBuf);
  3220. #endif
  3221. result = m_pDataStorage->Query(tQBuf);
  3222. if (result != NULL)
  3223. {
  3224. if (result->size() > 0)
  3225. {
  3226. tVal = (REPORT_DIRINFO *) malloc(sizeof(REPORT_DIRINFO));
  3227. if (tVal)
  3228. {
  3229. Assoc * cArr;
  3230. Assoc::iterator it;
  3231. memset(tVal, 0, sizeof(REPORT_DIRINFO));
  3232. cArr = result->at(0);
  3233. it = cArr->find("year");
  3234. if (it != cArr->end())
  3235. {
  3236. strncpy(tVal->YEAR, it->second.c_str(), 5);
  3237. };
  3238. it = cArr->find("month");
  3239. if (it != cArr->end())
  3240. {
  3241. strncpy(tVal->MONTH, it->second.c_str(), 3);
  3242. };
  3243. it = cArr->find("day");
  3244. if (it != cArr->end())
  3245. {
  3246. strncpy(tVal->DAY, it->second.c_str(), 3);
  3247. };
  3248. };
  3249. };
  3250. };
  3251. deleteResult(result);
  3252. };
  3253. return tVal;
  3254. }
  3255. DBC_RESOURCE_DATA * sqliteDBC::getResourceData(uint32_t ResourceID)
  3256. {
  3257. DBC_RESOURCE_DATA * tVal = NULL;
  3258. if (m_bStoIsConnected)
  3259. {
  3260. char tQBuf[CTEXT];
  3261. AAssoc * result;
  3262. sprintf(tQBuf, "SELECT `node_id`, `type`, `aux_id`, `device` FROM `resources` WHERE `id`=%d", ResourceID);
  3263. #ifdef DEBUG_ALL
  3264. LOG(tQBuf);
  3265. #endif
  3266. result = m_pDataStorage->Query(tQBuf);
  3267. if (result != NULL)
  3268. {
  3269. if (result->size() > 0)
  3270. {
  3271. Assoc * cArr;
  3272. /// @todo Valgrind tells us that tVal is lost and never freed
  3273. tVal = (DBC_RESOURCE_DATA *) malloc(sizeof(DBC_RESOURCE_DATA));
  3274. if (tVal != NULL)
  3275. {
  3276. memset(tVal, 0, sizeof(DBC_RESOURCE_DATA));
  3277. Assoc::iterator it;
  3278. cArr = result->at(0);
  3279. tVal->id = ResourceID;
  3280. it = cArr->find("node_id");
  3281. if (it != cArr->end())
  3282. {
  3283. tVal->node_id = atoi(it->second.c_str());
  3284. };
  3285. it = cArr->find("type");
  3286. if (it != cArr->end())
  3287. {
  3288. tVal->type = atoi(it->second.c_str());
  3289. };
  3290. it = cArr->find("aux_id");
  3291. if (it != cArr->end())
  3292. {
  3293. tVal->aux_id = atoi(it->second.c_str());
  3294. };
  3295. it = cArr->find("device");
  3296. if (it != cArr->end())
  3297. {
  3298. strncpy(tVal->device, it->second.c_str(), 64);
  3299. tVal->device[63] = (char) 0;
  3300. };
  3301. };
  3302. };
  3303. };
  3304. deleteResult(result);
  3305. };
  3306. return tVal;
  3307. }
  3308. uint32_t sqliteDBC::getResourceID(uint32_t NodeID, uint8_t type, uint32_t auxID)
  3309. {
  3310. uint32_t RetVal = 0;
  3311. if (m_bStoIsConnected)
  3312. {
  3313. char tQBuf[CTEXT];
  3314. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d AND `type`=%d AND `aux_id`=%d", NodeID, type, auxID);
  3315. #ifdef DEBUG_ALL
  3316. LOG(tQBuf);
  3317. #endif
  3318. AAssoc * result;
  3319. result = m_pDataStorage->Query(tQBuf);
  3320. if (result != NULL)
  3321. {
  3322. if (result->size() > 0)
  3323. {
  3324. Assoc * cArr;
  3325. Assoc::iterator it;
  3326. cArr = result->at(0);
  3327. it = cArr->find("id");
  3328. if (it != cArr->end())
  3329. {
  3330. RetVal = atoi(it->second.c_str());
  3331. };
  3332. };
  3333. };
  3334. deleteResult(result);
  3335. };
  3336. return RetVal;
  3337. }
  3338. DBC_SITE_DATA * sqliteDBC::getSiteData(uint32_t ID)
  3339. {
  3340. /** @todo Valgrind is reporting an important memory leak in this method.
  3341. * See these details:
  3342. *
  3343. * ~~~~
  3344. ==00:00:04:04.218 26021== 28,304 bytes in 122 blocks are definitely lost in loss record 3,966 of 3,972
  3345. ==00:00:04:04.218 26021== at 0x483877F: malloc (vg_replace_malloc.c:307)
  3346. ==00:00:04:04.218 26021== by 0x23452A: sqliteDBC::getSiteData(unsigned int) (SQLite_dataBinder.cpp:2981)
  3347. ==00:00:04:04.218 26021== by 0x1DA681: mojoKernel::validateReports() (mojoKernel.cpp:1347)
  3348. ==00:00:04:04.218 26021== by 0x1D7D16: mojoKernel::m_fThreadProc() (mojoKernel.cpp:511)
  3349. ==00:00:04:04.218 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62)
  3350. ==00:00:04:04.218 26021== by 0x49B7EA6: start_thread (pthread_create.c:477)
  3351. ==00:00:04:04.218 26021== by 0x7874ADE: clone (clone.S:95)
  3352. * ~~~~
  3353. */
  3354. DBC_SITE_DATA * tVal = NULL;
  3355. if (m_bStoIsConnected)
  3356. {
  3357. char tQBuf[CTEXT];
  3358. AAssoc * result;
  3359. sprintf(tQBuf, "SELECT * FROM `system_props` WHERE `id`=%d", ID);
  3360. #ifdef DEBUG_ALL
  3361. LOG(tQBuf);
  3362. #endif
  3363. result = m_pDataStorage->Query(tQBuf);
  3364. if (result != NULL)
  3365. {
  3366. if (result->size() > 0)
  3367. {
  3368. Assoc * cArr;
  3369. tVal = (DBC_SITE_DATA *) malloc(sizeof(DBC_SITE_DATA));
  3370. if (tVal != NULL)
  3371. {
  3372. memset(tVal, 0, sizeof(DBC_SITE_DATA));
  3373. Assoc::iterator it;
  3374. cArr = result->at(0);
  3375. it = cArr->find("type");
  3376. if (it != cArr->end())
  3377. {
  3378. tVal->account_id = atoi(it->second.c_str());
  3379. };
  3380. it = cArr->find("name");
  3381. if (it != cArr->end())
  3382. {
  3383. sprintf(tVal->name, it->second.c_str());
  3384. };
  3385. // it = cArr->find("server_ident");
  3386. // if( it != cArr->end() ){
  3387. // sprintf( tVal->ident, it->second.c_str() );
  3388. // };
  3389. it = cArr->find("anchor_lat");
  3390. if (it != cArr->end())
  3391. {
  3392. tVal->lat = atof(it->second.c_str());
  3393. };
  3394. it = cArr->find("anchor_lon");
  3395. if (it != cArr->end())
  3396. {
  3397. tVal->lon = atof(it->second.c_str());
  3398. };
  3399. it = cArr->find("anchor_alt");
  3400. if (it != cArr->end())
  3401. {
  3402. tVal->alt = atof(it->second.c_str());
  3403. };
  3404. it = cArr->find("hemisphere");
  3405. if (it != cArr->end())
  3406. {
  3407. tVal->SH = (it->second.c_str()[0] == 'S');
  3408. };
  3409. it = cArr->find("map_id");
  3410. if (it != cArr->end())
  3411. {
  3412. tVal->map_id = atoi(it->second.c_str());
  3413. };
  3414. it = cArr->find("local_binding");
  3415. if (it != cArr->end())
  3416. {
  3417. tVal->local_binding = (bool) atoi(it->second.c_str());
  3418. };
  3419. it = cArr->find("work_mode");
  3420. if (it != cArr->end())
  3421. {
  3422. tVal->work_mode = atoi(it->second.c_str());
  3423. };
  3424. it = cArr->find("report_cleanup");
  3425. if (it != cArr->end())
  3426. {
  3427. tVal->report_cleanup = atoi(it->second.c_str());
  3428. };
  3429. it = cArr->find("preserve_empty");
  3430. if (it != cArr->end())
  3431. {
  3432. tVal->preserve_empty = atoi(it->second.c_str());
  3433. };
  3434. it = cArr->find("remote_ip");
  3435. if (it != cArr->end())
  3436. {
  3437. sprintf(tVal->remote_ip, it->second.c_str());
  3438. };
  3439. it = cArr->find("remote_port");
  3440. if (it != cArr->end())
  3441. {
  3442. tVal->remote_port = atoi(it->second.c_str());
  3443. };
  3444. it = cArr->find("remote_backup_ip");
  3445. if (it != cArr->end())
  3446. {
  3447. sprintf(tVal->remote_backup_ip, it->second.c_str());
  3448. };
  3449. it = cArr->find("remote_backup_port");
  3450. if (it != cArr->end())
  3451. {
  3452. tVal->remote_port = atoi(it->second.c_str());
  3453. };
  3454. it = cArr->find("use_remote_server");
  3455. if (it != cArr->end())
  3456. {
  3457. tVal->use_remote_server = atoi(it->second.c_str());
  3458. };
  3459. it = cArr->find("skip_local_report");
  3460. if (it != cArr->end())
  3461. {
  3462. tVal->skip_local_report = atoi(it->second.c_str());
  3463. };
  3464. };
  3465. };
  3466. };
  3467. deleteResult(result);
  3468. };
  3469. return tVal;
  3470. }
  3471. uint32_t sqliteDBC::getServerID(CSTR identity)
  3472. {
  3473. uint32_t RetVal = 0;
  3474. if (m_bStoIsConnected)
  3475. {
  3476. char tQBuf[CTEXT];
  3477. sprintf(tQBuf, "SELECT `id` FROM `servers` WHERE `identity`='%s'", identity);
  3478. #ifdef DEBUG_ALL
  3479. LOG(tQBuf);
  3480. #endif
  3481. AAssoc * result;
  3482. result = m_pDataStorage->Query(tQBuf);
  3483. if (result != NULL)
  3484. {
  3485. if (result->size() > 0)
  3486. {
  3487. Assoc * cArr;
  3488. Assoc::iterator it;
  3489. cArr = result->at(0);
  3490. it = cArr->find("id");
  3491. if (it != cArr->end())
  3492. {
  3493. RetVal = atoi(it->second.c_str());
  3494. };
  3495. };
  3496. };
  3497. deleteResult(result);
  3498. };
  3499. return RetVal;
  3500. }
  3501. uint32_t sqliteDBC::getServerNode(uint32_t SID)
  3502. {
  3503. uint32_t RetVal = 0;
  3504. if (m_bStoIsConnected)
  3505. {
  3506. char tQBuf[CTEXT];
  3507. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type`=0 AND `aux_id`=%d", SID);
  3508. #ifdef DEBUG_ALL
  3509. LOG(tQBuf);
  3510. #endif
  3511. AAssoc * result;
  3512. result = m_pDataStorage->Query(tQBuf);
  3513. if (result != NULL)
  3514. {
  3515. if (result->size() > 0)
  3516. {
  3517. Assoc * cArr;
  3518. Assoc::iterator it;
  3519. cArr = result->at(0);
  3520. it = cArr->find("id");
  3521. if (it != cArr->end())
  3522. {
  3523. RetVal = atoi(it->second.c_str());
  3524. };
  3525. };
  3526. };
  3527. deleteResult(result);
  3528. };
  3529. return RetVal;
  3530. }
  3531. DBC_TARGET_FLITER_DATA * sqliteDBC::getTargetFilterData(uint32_t ID)
  3532. {
  3533. DBC_TARGET_FLITER_DATA * tVal = NULL;
  3534. if (m_bStoIsConnected)
  3535. {
  3536. char tQBuf[CTEXT];
  3537. AAssoc * result;
  3538. sprintf(tQBuf, "SELECT * FROM `target_filters` WHERE `id`=%d", ID);
  3539. #ifdef DEBUG_ALL
  3540. LOG(tQBuf);
  3541. #endif
  3542. result = m_pDataStorage->Query(tQBuf);
  3543. if (result != NULL)
  3544. {
  3545. if (result->size() > 0)
  3546. {
  3547. Assoc * cArr;
  3548. tVal = (DBC_TARGET_FLITER_DATA *) malloc(sizeof(DBC_TARGET_FLITER_DATA));
  3549. if (tVal != NULL)
  3550. {
  3551. memset(tVal, 0, sizeof(DBC_TARGET_FLITER_DATA));
  3552. Assoc::iterator it;
  3553. tVal->id = ID;
  3554. cArr = result->at(0);
  3555. it = cArr->find("type");
  3556. if (it != cArr->end())
  3557. {
  3558. tVal->type = (TARGET_FILTER_TYPE) atoi(it->second.c_str());
  3559. };
  3560. it = cArr->find("aux_id");
  3561. if (it != cArr->end())
  3562. {
  3563. tVal->aux_id = atoi(it->second.c_str());
  3564. };
  3565. it = cArr->find("enabled");
  3566. if (it != cArr->end())
  3567. {
  3568. tVal->enabled = (bool) atoi(it->second.c_str());
  3569. };
  3570. };
  3571. };
  3572. };
  3573. deleteResult(result);
  3574. };
  3575. return tVal;
  3576. }
  3577. TRAIN_FILTER_DATA * sqliteDBC::getTrainFilterData(uint32_t ID)
  3578. {
  3579. TRAIN_FILTER_DATA * tVal = NULL;
  3580. if (m_bStoIsConnected)
  3581. {
  3582. char tQBuf[CTEXT];
  3583. AAssoc * result;
  3584. sprintf(tQBuf, "SELECT * FROM `target_filter_train` WHERE `id`=%d", ID);
  3585. #ifdef DEBUG_ALL
  3586. LOG(tQBuf);
  3587. #endif
  3588. result = m_pDataStorage->Query(tQBuf);
  3589. if (result != NULL)
  3590. {
  3591. if (result->size() > 0)
  3592. {
  3593. Assoc * cArr;
  3594. tVal = (TRAIN_FILTER_DATA *) malloc(sizeof(TRAIN_FILTER_DATA));
  3595. if (tVal != NULL)
  3596. {
  3597. memset(tVal, 0, sizeof(TRAIN_FILTER_DATA));
  3598. Assoc::iterator it;
  3599. cArr = result->at(0);
  3600. it = cArr->find("UPPER_SENSOR");
  3601. if (it != cArr->end())
  3602. {
  3603. tVal->nUpperSensorID = atoi(it->second.c_str());
  3604. };
  3605. it = cArr->find("LOWER_SENSOR");
  3606. if (it != cArr->end())
  3607. {
  3608. tVal->nBottomSensorID = atoi(it->second.c_str());
  3609. };
  3610. it = cArr->find("TRAIN_DETECT");
  3611. if (it != cArr->end())
  3612. {
  3613. tVal->bDetectTrain = (bool) atoi(it->second.c_str());
  3614. };
  3615. it = cArr->find("TRAIN_WIDTH");
  3616. if (it != cArr->end())
  3617. {
  3618. tVal->nTrainWidth = atoi(it->second.c_str());
  3619. };
  3620. it = cArr->find("DECISION_TIME");
  3621. if (it != cArr->end())
  3622. {
  3623. tVal->nDecisionTime = 1000 * atoi(it->second.c_str());
  3624. };
  3625. // In case Wrong Parameter (less then Second)
  3626. if (tVal->nDecisionTime < 1000)
  3627. {
  3628. tVal->nDecisionTime = 10000;
  3629. };
  3630. it = cArr->find("SUSPECT_TIME");
  3631. if (it != cArr->end())
  3632. {
  3633. tVal->nSuspectTime = 1000 * atoi(it->second.c_str());
  3634. };
  3635. // In case Wrong Parameter (less then Second)
  3636. if (tVal->nSuspectTime < 1000)
  3637. {
  3638. tVal->nSuspectTime = 10000;
  3639. };
  3640. it = cArr->find("TRACK_TRAIN");
  3641. if (it != cArr->end())
  3642. {
  3643. tVal->bTrackTrain = (bool) atoi(it->second.c_str());
  3644. };
  3645. it = cArr->find("AUX_IP");
  3646. if (it != cArr->end())
  3647. {
  3648. strncpy(tVal->aux_ip, it->second.c_str(), 15);
  3649. };
  3650. it = cArr->find("AUX_PORT");
  3651. if (it != cArr->end())
  3652. {
  3653. tVal->aux_port = atoi(it->second.c_str());
  3654. };
  3655. #ifdef EFT_AUTO_RESET_UPPER_SENSOR
  3656. it = cArr->find("UPPER_RESTART");
  3657. if (it != cArr->end())
  3658. {
  3659. tVal->nUpperSensorRestartTO = atoi(it->second.c_str()) * 3600; // Hours -> Seconds
  3660. }
  3661. else
  3662. {
  3663. tVal->nUpperSensorRestartTO = 24 * 3600;
  3664. };
  3665. #endif
  3666. #ifdef EFT_MINIMAL_RANGE_SUSPICIOUS
  3667. it = cArr->find("MINIMAL_LEFT_RANGE");
  3668. if (it != cArr->end())
  3669. {
  3670. tVal->fLeftMinimalRange = atof(it->second.c_str());
  3671. }
  3672. else
  3673. {
  3674. tVal->fLeftMinimalRange = 0.0;
  3675. };
  3676. it = cArr->find("MINIMAL_RIGHT_RANGE");
  3677. if (it != cArr->end())
  3678. {
  3679. tVal->fRightMinimalRange = atof(it->second.c_str());
  3680. }
  3681. else
  3682. {
  3683. tVal->fRightMinimalRange = 0.0;
  3684. };
  3685. #endif
  3686. it = cArr->find("DEBUG");
  3687. if (it != cArr->end())
  3688. {
  3689. tVal->bDebugMode = (bool) atoi(it->second.c_str());
  3690. };
  3691. };
  3692. };
  3693. };
  3694. deleteResult(result);
  3695. };
  3696. return tVal;
  3697. }
  3698. bool sqliteDBC::cleanRegisteredBubbles(void)
  3699. {
  3700. if (m_bStoIsConnected)
  3701. {
  3702. char tQBuf[MAX_PATH_LEN];
  3703. sprintf(tQBuf, "DELETE FROM `target_bubbles`");
  3704. return m_pDataStorage->SilentQuery(tQBuf);
  3705. };
  3706. return false;
  3707. }
  3708. DBC_UART_DATA * sqliteDBC::getUartData(uint32_t UartID)
  3709. {
  3710. DBC_UART_DATA * tVal = NULL;
  3711. if (m_bStoIsConnected)
  3712. {
  3713. char tQBuf[MAX_PATH_LEN];
  3714. AAssoc * result;
  3715. sprintf(tQBuf, "SELECT * FROM uart_settings WHERE id = %d", UartID);
  3716. #ifdef DEBUG_ALL
  3717. LOG(tQBuf);
  3718. #endif
  3719. result = m_pDataStorage->Query(tQBuf);
  3720. if (result != NULL)
  3721. {
  3722. if (result->size() > 0)
  3723. {
  3724. Assoc * cArr;
  3725. tVal = (DBC_UART_DATA *) malloc(sizeof(DBC_UART_DATA));
  3726. if (tVal != NULL)
  3727. {
  3728. Assoc::iterator it;
  3729. cArr = result->at(0);
  3730. it = cArr->find("type");
  3731. if (it != cArr->end())
  3732. {
  3733. tVal->type = atoi(it->second.c_str());
  3734. };
  3735. it = cArr->find("speed");
  3736. if (it != cArr->end())
  3737. {
  3738. tVal->speed = atoi(it->second.c_str());
  3739. };
  3740. it = cArr->find("mode");
  3741. if (it != cArr->end())
  3742. {
  3743. tVal->mode = atoi(it->second.c_str());
  3744. };
  3745. it = cArr->find("port");
  3746. if (it != cArr->end())
  3747. {
  3748. tVal->port = atoi(it->second.c_str());
  3749. };
  3750. it = cArr->find("path");
  3751. if (it != cArr->end())
  3752. {
  3753. strncpy(tVal->path, it->second.c_str(), 128);
  3754. };
  3755. };
  3756. };
  3757. };
  3758. deleteResult(result);
  3759. };
  3760. return tVal;
  3761. }
  3762. uint32_t sqliteDBC::getUartID(uint32_t NodeID, uint8_t Type, CSTR path)
  3763. {
  3764. uint32_t RetVal = 0;
  3765. if (m_bStoIsConnected)
  3766. {
  3767. char tQBuf[CTEXT];
  3768. sprintf(tQBuf, "SELECT `id` FROM `uart_settings` WHERE `node_id`=%d AND `type`=%d AND `path`='%s' ", NodeID, Type, path);
  3769. #ifdef DEBUG_ALL
  3770. LOG(tQBuf);
  3771. #endif
  3772. AAssoc * result;
  3773. result = m_pDataStorage->Query(tQBuf);
  3774. if (result != NULL)
  3775. {
  3776. if (result->size() > 0)
  3777. {
  3778. Assoc * cArr;
  3779. Assoc::iterator it;
  3780. cArr = result->at(0);
  3781. it = cArr->find("id");
  3782. if (it != cArr->end())
  3783. {
  3784. RetVal = atoi(it->second.c_str());
  3785. };
  3786. };
  3787. };
  3788. deleteResult(result);
  3789. };
  3790. return RetVal;
  3791. }
  3792. uint32_t sqliteDBC::getWiFi_ID(uint32_t nif_id)
  3793. {
  3794. uint32_t tVal = 0;
  3795. if (m_bStoIsConnected)
  3796. {
  3797. char tQBuf[CTEXT];
  3798. AAssoc * result;
  3799. sprintf(tQBuf, "SELECT `id` FROM `wifis` WHERE `nif_id`=%d", nif_id);
  3800. #ifdef DEBUG_ALL
  3801. LOG(tQBuf);
  3802. #endif
  3803. result = m_pDataStorage->Query(tQBuf);
  3804. if (result != NULL)
  3805. {
  3806. if (result->size() > 0)
  3807. {
  3808. Assoc * cArr;
  3809. Assoc::iterator it;
  3810. cArr = result->at(0);
  3811. it = cArr->find("id");
  3812. if (it != cArr->end())
  3813. {
  3814. tVal = atoi(it->second.c_str());
  3815. };
  3816. };
  3817. };
  3818. deleteResult(result);
  3819. };
  3820. return tVal;
  3821. }
  3822. DBC_INT_LIST * sqliteDBC::listCameras()
  3823. {
  3824. DBC_INT_LIST * tVal = NULL;
  3825. if (m_bStoIsConnected)
  3826. {
  3827. char tQBuf[CTEXT];
  3828. AAssoc * result;
  3829. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `status`=3");
  3830. #ifdef DEBUG_ALL
  3831. LOG(tQBuf);
  3832. #endif
  3833. result = m_pDataStorage->Query(tQBuf);
  3834. if (result != NULL)
  3835. {
  3836. if (result->size() > 0)
  3837. {
  3838. Assoc * cArr;
  3839. tVal = new DBC_INT_LIST();
  3840. if (tVal != NULL)
  3841. {
  3842. Assoc::iterator it;
  3843. for (int i = 0; i < result->size(); i++)
  3844. {
  3845. cArr = result->at(i);
  3846. it = cArr->find("id");
  3847. if (it != cArr->end())
  3848. {
  3849. tVal->push_back(atoi(it->second.c_str()));
  3850. };
  3851. };
  3852. };
  3853. };
  3854. };
  3855. deleteResult(result);
  3856. };
  3857. return tVal;
  3858. }
  3859. DBC_INT_LIST * sqliteDBC::listCamPresets(uint32_t SETID)
  3860. {
  3861. DBC_INT_LIST * tVal = NULL;
  3862. if (m_bStoIsConnected)
  3863. {
  3864. char tQBuf[CTEXT];
  3865. AAssoc * result;
  3866. sprintf(tQBuf, "SELECT `id` FROM `cameras_presets` WHERE `preset_id`=%d", SETID);
  3867. #ifdef DEBUG_ALL
  3868. LOG(tQBuf);
  3869. #endif
  3870. result = m_pDataStorage->Query(tQBuf);
  3871. if (result != NULL)
  3872. {
  3873. if (result->size() > 0)
  3874. {
  3875. Assoc * cArr;
  3876. tVal = new DBC_INT_LIST();
  3877. if (tVal != NULL)
  3878. {
  3879. Assoc::iterator it;
  3880. for (int i = 0; i < result->size(); i++)
  3881. {
  3882. cArr = result->at(i);
  3883. it = cArr->find("id");
  3884. if (it != cArr->end())
  3885. {
  3886. tVal->push_back(atoi(it->second.c_str()));
  3887. };
  3888. };
  3889. };
  3890. };
  3891. };
  3892. deleteResult(result);
  3893. };
  3894. return tVal;
  3895. }
  3896. DBC_INT_LIST * sqliteDBC::listFilters(uint32_t ServerID)
  3897. {
  3898. DBC_INT_LIST * tVal = NULL;
  3899. if (m_bStoIsConnected)
  3900. {
  3901. char tQBuf[CTEXT];
  3902. AAssoc * result;
  3903. sprintf(tQBuf, "SELECT `id` FROM `target_filters` WHERE `enabled`=1");
  3904. #ifdef DEBUG_ALL
  3905. LOG(tQBuf);
  3906. #endif
  3907. result = m_pDataStorage->Query(tQBuf);
  3908. if (result != NULL)
  3909. {
  3910. if (result->size() > 0)
  3911. {
  3912. Assoc * cArr;
  3913. tVal = new DBC_INT_LIST();
  3914. if (tVal != NULL)
  3915. {
  3916. Assoc::iterator it;
  3917. for (int i = 0; i < result->size(); i++)
  3918. {
  3919. cArr = result->at(i);
  3920. it = cArr->find("id");
  3921. if (it != cArr->end())
  3922. {
  3923. tVal->push_back(atoi(it->second.c_str()));
  3924. };
  3925. };
  3926. };
  3927. };
  3928. };
  3929. deleteResult(result);
  3930. };
  3931. return tVal;
  3932. }
  3933. DBC_INT_LIST * sqliteDBC::listLRFSensors()
  3934. {
  3935. DBC_INT_LIST * tVal = NULL;
  3936. if (m_bStoIsConnected)
  3937. {
  3938. char tQBuf[CTEXT];
  3939. AAssoc * result;
  3940. sprintf(tQBuf, "SELECT `id` FROM `lrf_sensors` WHERE `data_status`>0");
  3941. #ifdef DEBUG_ALL
  3942. LOG(tQBuf);
  3943. #endif
  3944. result = m_pDataStorage->Query(tQBuf);
  3945. if (result != NULL)
  3946. {
  3947. if (result->size() > 0)
  3948. {
  3949. Assoc * cArr;
  3950. tVal = new DBC_INT_LIST();
  3951. if (tVal != NULL)
  3952. {
  3953. Assoc::iterator it;
  3954. for (int i = 0; i < result->size(); i++)
  3955. {
  3956. cArr = result->at(i);
  3957. it = cArr->find("id");
  3958. if (it != cArr->end())
  3959. {
  3960. tVal->push_back(atoi(it->second.c_str()));
  3961. };
  3962. };
  3963. };
  3964. };
  3965. };
  3966. deleteResult(result);
  3967. };
  3968. return tVal;
  3969. }
  3970. DBC_INT_LIST * sqliteDBC::listLRFUploads(uint32_t deviceID)
  3971. {
  3972. DBC_INT_LIST * tVal = NULL;
  3973. if (m_bStoIsConnected)
  3974. {
  3975. char tQBuf[CTEXT];
  3976. AAssoc * result;
  3977. sprintf(tQBuf, "SELECT `id` FROM `lrf_uploads` WHERE `status`=-1 AND `sensor_id`=%d", deviceID);
  3978. #ifdef DEBUG_ALL
  3979. LOG(tQBuf);
  3980. #endif
  3981. result = m_pDataStorage->Query(tQBuf);
  3982. if (result != NULL)
  3983. {
  3984. if (result->size() > 0)
  3985. {
  3986. Assoc * cArr;
  3987. tVal = new DBC_INT_LIST();
  3988. if (tVal != NULL)
  3989. {
  3990. Assoc::iterator it;
  3991. for (int i = 0; i < result->size(); i++)
  3992. {
  3993. cArr = result->at(i);
  3994. it = cArr->find("id");
  3995. if (it != cArr->end())
  3996. {
  3997. tVal->push_back(atoi(it->second.c_str()));
  3998. };
  3999. };
  4000. };
  4001. };
  4002. };
  4003. deleteResult(result);
  4004. };
  4005. return tVal;
  4006. }
  4007. DBC_INT_LIST * sqliteDBC::listMediaStreams(uint32_t SiteId, uint8_t listMode)
  4008. {
  4009. DBC_INT_LIST * tVal = NULL;
  4010. if (m_bStoIsConnected)
  4011. {
  4012. char tQBuf[CTEXT];
  4013. AAssoc * result;
  4014. switch (listMode)
  4015. {
  4016. case LIST_VALID_RECORDS : 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,14,15) AND `ms`.`valid`=1 AND `nd`.`online`=1"); break;
  4017. case LIST_INVALID_RECORDS: 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,14,15) AND `ms`.`valid`=1 AND `nd`.`online`=0"); break;
  4018. default : 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,14,15) AND `ms`.`valid`=1");
  4019. };
  4020. #ifdef DEBUG_ALL
  4021. LOG(tQBuf);
  4022. #endif
  4023. result = m_pDataStorage->Query(tQBuf);
  4024. if (result != NULL)
  4025. {
  4026. if (result->size() > 0)
  4027. {
  4028. Assoc * cArr;
  4029. tVal = new DBC_INT_LIST();
  4030. if (tVal != NULL)
  4031. {
  4032. Assoc::iterator it;
  4033. for (int i = 0; i < result->size(); i++)
  4034. {
  4035. cArr = result->at(i);
  4036. it = cArr->find("id");
  4037. if (it != cArr->end())
  4038. {
  4039. tVal->push_back(atoi(it->second.c_str()));
  4040. };
  4041. };
  4042. };
  4043. };
  4044. };
  4045. deleteResult(result);
  4046. };
  4047. return tVal;
  4048. }
  4049. DBC_INT_LIST * sqliteDBC::listModules()
  4050. {
  4051. DBC_INT_LIST * tVal = NULL;
  4052. if (m_bStoIsConnected)
  4053. {
  4054. AAssoc * result;
  4055. result = m_pDataStorage->Query("SELECT `id` FROM `modules` WHERE `enabled`=1");
  4056. if (result != NULL)
  4057. {
  4058. if (result->size() > 0)
  4059. {
  4060. Assoc * cArr;
  4061. tVal = new DBC_INT_LIST();
  4062. if (tVal != NULL)
  4063. {
  4064. Assoc::iterator it;
  4065. for (int i = 0; i < result->size(); i++)
  4066. {
  4067. cArr = result->at(i);
  4068. it = cArr->find("id");
  4069. if (it != cArr->end())
  4070. {
  4071. tVal->push_back(atoi(it->second.c_str()));
  4072. };
  4073. };
  4074. };
  4075. };
  4076. };
  4077. deleteResult(result);
  4078. };
  4079. return tVal;
  4080. }
  4081. DBC_INT_LIST * sqliteDBC::listNetworks(uint32_t nif_id)
  4082. {
  4083. DBC_INT_LIST * tVal = NULL;
  4084. if (m_bStoIsConnected)
  4085. {
  4086. char tQBuf[CTEXT];
  4087. AAssoc * result;
  4088. sprintf(tQBuf, "SELECT `id` FROM `networks` WHERE `nif_id`=%d", nif_id);
  4089. #ifdef DEBUG_ALL
  4090. LOG(tQBuf);
  4091. #endif
  4092. result = m_pDataStorage->Query(tQBuf);
  4093. if (result != NULL)
  4094. {
  4095. if (result->size() > 0)
  4096. {
  4097. Assoc * cArr;
  4098. tVal = new DBC_INT_LIST();
  4099. if (tVal != NULL)
  4100. {
  4101. Assoc::iterator it;
  4102. for (int i = 0; i < result->size(); i++)
  4103. {
  4104. cArr = result->at(i);
  4105. it = cArr->find("id");
  4106. if (it != cArr->end())
  4107. {
  4108. tVal->push_back(atoi(it->second.c_str()));
  4109. };
  4110. };
  4111. };
  4112. };
  4113. };
  4114. deleteResult(result);
  4115. };
  4116. return tVal;
  4117. }
  4118. DBC_INT_LIST * sqliteDBC::listNewModules(DBC_INT_LIST * old_ids)
  4119. {
  4120. DBC_INT_LIST * tVal = NULL;
  4121. if (m_bStoIsConnected)
  4122. {
  4123. char tParam[CTEXT];
  4124. // memset( tParam, 0, CTEXT );
  4125. sprintf(tParam, "0");
  4126. if (old_ids != NULL)
  4127. {
  4128. char tBuffer[CTEXT];
  4129. for (int i = 0; i < old_ids->size(); i++)
  4130. {
  4131. sprintf(tBuffer, "%s,%d", tParam, old_ids->at(i));
  4132. strncpy(tParam, tBuffer, CTEXT);
  4133. };
  4134. };
  4135. char tQBuf[CTEXT];
  4136. AAssoc * result;
  4137. sprintf(tQBuf, "SELECT `id` FROM `modules` WHERE `enabled`=1 AND `id` NOT IN (%s)", tParam);
  4138. #ifdef DEBUG_ALL
  4139. LOG(tQBuf);
  4140. #endif
  4141. result = m_pDataStorage->Query(tQBuf);
  4142. if (result != NULL)
  4143. {
  4144. if (result->size() > 0)
  4145. {
  4146. Assoc * cArr;
  4147. tVal = new DBC_INT_LIST();
  4148. if (tVal != NULL)
  4149. {
  4150. Assoc::iterator it;
  4151. for (int i = 0; i < result->size(); i++)
  4152. {
  4153. cArr = result->at(i);
  4154. it = cArr->find("id");
  4155. if (it != cArr->end())
  4156. {
  4157. tVal->push_back(atoi(it->second.c_str()));
  4158. };
  4159. };
  4160. };
  4161. };
  4162. };
  4163. deleteResult(result);
  4164. };
  4165. return tVal;
  4166. }
  4167. DBC_INT_LIST * sqliteDBC::listNewNodes(DBC_INT_LIST * old_ids)
  4168. {
  4169. DBC_INT_LIST * tVal = NULL;
  4170. if (m_bStoIsConnected)
  4171. {
  4172. char tParam[CTEXT];
  4173. // memset( tParam, 0, CTEXT );
  4174. sprintf(tParam, "0");
  4175. if (old_ids != NULL)
  4176. {
  4177. char tBuffer[CTEXT];
  4178. for (int i = 0; i < old_ids->size(); i++)
  4179. {
  4180. sprintf(tBuffer, "%s,%d", tParam, old_ids->at(i));
  4181. strncpy(tParam, tBuffer, CTEXT);
  4182. };
  4183. };
  4184. char tQBuf[CTEXT];
  4185. AAssoc * result;
  4186. sprintf(tQBuf, "SELECT id FROM nodes WHERE `type` > 0 AND `enable`=1 AND `id` NOT IN(%s)", tParam);
  4187. #ifdef DEBUG_ALL
  4188. LOG(tQBuf);
  4189. #endif
  4190. result = m_pDataStorage->Query(tQBuf);
  4191. if (result != NULL)
  4192. {
  4193. if (result->size() > 0)
  4194. {
  4195. Assoc * cArr;
  4196. tVal = new DBC_INT_LIST();
  4197. if (tVal != NULL)
  4198. {
  4199. Assoc::iterator it;
  4200. for (int i = 0; i < result->size(); i++)
  4201. {
  4202. cArr = result->at(i);
  4203. it = cArr->find("id");
  4204. if (it != cArr->end())
  4205. {
  4206. tVal->push_back(atoi(it->second.c_str()));
  4207. };
  4208. };
  4209. };
  4210. };
  4211. };
  4212. deleteResult(result);
  4213. };
  4214. return tVal;
  4215. }
  4216. DBC_INT_LIST * sqliteDBC::listNodes(bool all)
  4217. {
  4218. DBC_INT_LIST * tVal = NULL;
  4219. if (m_bStoIsConnected)
  4220. {
  4221. char tQBuf[1024];
  4222. AAssoc * result;
  4223. if (all)
  4224. {
  4225. sprintf(tQBuf, "SELECT `id` FROM `nodes`");
  4226. }
  4227. else
  4228. {
  4229. sprintf(tQBuf, "SELECT `id` FROM `nodes` WHERE `type` > 0 AND `enable`=1");
  4230. };
  4231. #ifdef DEBUG_ALL
  4232. LOG(tQBuf);
  4233. #endif
  4234. result = m_pDataStorage->Query(tQBuf);
  4235. if (result != NULL)
  4236. {
  4237. if (result->size() > 0)
  4238. {
  4239. Assoc * cArr;
  4240. tVal = new DBC_INT_LIST();
  4241. if (tVal != NULL)
  4242. {
  4243. Assoc::iterator it;
  4244. for (int i = 0; i < result->size(); i++)
  4245. {
  4246. cArr = result->at(i);
  4247. it = cArr->find("id");
  4248. if (it != cArr->end())
  4249. {
  4250. tVal->push_back(atoi(it->second.c_str()));
  4251. };
  4252. };
  4253. };
  4254. };
  4255. };
  4256. deleteResult(result);
  4257. };
  4258. return tVal;
  4259. }
  4260. DBC_INT_LIST * sqliteDBC::listNodeFilters(uint32_t NodeID)
  4261. {
  4262. DBC_INT_LIST * tVal = NULL;
  4263. if (m_bStoIsConnected)
  4264. {
  4265. char tQBuf[1024];
  4266. AAssoc * result;
  4267. sprintf(tQBuf, "SELECT `filter_id` FROM `node_target_filters` WHERE `node_id`=%d ORDER BY `order` ASC", NodeID);
  4268. #ifdef DEBUG_ALL
  4269. LOG(tQBuf);
  4270. #endif
  4271. result = m_pDataStorage->Query(tQBuf);
  4272. if (result != NULL)
  4273. {
  4274. if (result->size() > 0)
  4275. {
  4276. Assoc * cArr;
  4277. tVal = new DBC_INT_LIST();
  4278. if (tVal != NULL)
  4279. {
  4280. Assoc::iterator it;
  4281. for (int i = 0; i < result->size(); i++)
  4282. {
  4283. cArr = result->at(i);
  4284. it = cArr->find("filter_id");
  4285. if (it != cArr->end())
  4286. {
  4287. tVal->push_back(atoi(it->second.c_str()));
  4288. };
  4289. };
  4290. };
  4291. };
  4292. };
  4293. deleteResult(result);
  4294. };
  4295. return tVal;
  4296. }
  4297. DBC_INT_LIST * sqliteDBC::listOlderReportStat(CSTR Y, CSTR M, CSTR D)
  4298. {
  4299. DBC_INT_LIST * tVal = NULL;
  4300. if (m_bStaIsConnected)
  4301. {
  4302. char tQBuf[MAX_PATH_LEN];
  4303. AAssoc * result;
  4304. 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);
  4305. result = m_pDataStatistics->Query(tQBuf);
  4306. if (result != NULL)
  4307. {
  4308. if (result->size() > 0)
  4309. {
  4310. Assoc * cArr;
  4311. tVal = new DBC_INT_LIST();
  4312. if (tVal != NULL)
  4313. {
  4314. Assoc::iterator it;
  4315. for (int i = 0; i < result->size(); i++)
  4316. {
  4317. cArr = result->at(i);
  4318. it = cArr->find("ROWID");
  4319. if (it != cArr->end())
  4320. {
  4321. tVal->push_back(atoi(it->second.c_str()));
  4322. };
  4323. };
  4324. };
  4325. };
  4326. };
  4327. deleteResult(result);
  4328. };
  4329. return tVal;
  4330. }
  4331. DBC_INT_LIST * sqliteDBC::listOlderReportStat(uint32_t TS)
  4332. {
  4333. DBC_INT_LIST * tVal = NULL;
  4334. if (m_bStaIsConnected)
  4335. {
  4336. char tQBuf[MAX_PATH_LEN];
  4337. AAssoc * result;
  4338. sprintf(tQBuf, "SELECT `ROWID` FROM `days` WHERE `ts`< %d", TS);
  4339. result = m_pDataStatistics->Query(tQBuf);
  4340. if (result != NULL)
  4341. {
  4342. if (result->size() > 0)
  4343. {
  4344. Assoc * cArr;
  4345. tVal = new DBC_INT_LIST();
  4346. if (tVal != NULL)
  4347. {
  4348. Assoc::iterator it;
  4349. for (int i = 0; i < result->size(); i++)
  4350. {
  4351. cArr = result->at(i);
  4352. it = cArr->find("ROWID");
  4353. if (it != cArr->end())
  4354. {
  4355. tVal->push_back(atoi(it->second.c_str()));
  4356. };
  4357. };
  4358. };
  4359. };
  4360. };
  4361. deleteResult(result);
  4362. };
  4363. return tVal;
  4364. }
  4365. DBC_INT_LIST * sqliteDBC::listPhonebook()
  4366. {
  4367. DBC_INT_LIST * tVal = NULL;
  4368. if (m_bStoIsConnected)
  4369. {
  4370. char tQBuf[CTEXT];
  4371. AAssoc * result;
  4372. sprintf(tQBuf, "SELECT `id` FROM `module_modem_phonebook`");
  4373. #ifdef DEBUG_ALL
  4374. LOG(tQBuf);
  4375. #endif
  4376. result = m_pDataStorage->Query(tQBuf);
  4377. if (result != NULL)
  4378. {
  4379. if (result->size() > 0)
  4380. {
  4381. Assoc * cArr;
  4382. tVal = new DBC_INT_LIST();
  4383. if (tVal != NULL)
  4384. {
  4385. Assoc::iterator it;
  4386. for (int i = 0; i < result->size(); i++)
  4387. {
  4388. cArr = result->at(i);
  4389. it = cArr->find("id");
  4390. if (it != cArr->end())
  4391. {
  4392. tVal->push_back(atoi(it->second.c_str()));
  4393. };
  4394. };
  4395. };
  4396. };
  4397. };
  4398. deleteResult(result);
  4399. };
  4400. return tVal;
  4401. }
  4402. DBC_INT_LIST * sqliteDBC::listFrozenBubbles(uint8_t cc)
  4403. {
  4404. DBC_INT_LIST * tVal = NULL;
  4405. if (m_bStoIsConnected)
  4406. {
  4407. char tQBuf[CTEXT];
  4408. AAssoc * result;
  4409. if (cc)
  4410. {
  4411. sprintf(tQBuf, "SELECT `id` FROM `target_bubbles` WHERE `BLOCK_COUNS`>%d", cc);
  4412. }
  4413. else
  4414. {
  4415. sprintf(tQBuf, "SELECT `id` FROM `target_bubbles` WHERE `BLOCKED`=1");
  4416. }
  4417. #ifdef DEBUG_ALL
  4418. LOG(tQBuf);
  4419. #endif
  4420. result = m_pDataStorage->Query(tQBuf);
  4421. if (result != NULL)
  4422. {
  4423. if (result->size() > 0)
  4424. {
  4425. Assoc * cArr;
  4426. tVal = new DBC_INT_LIST();
  4427. if (tVal != NULL)
  4428. {
  4429. Assoc::iterator it;
  4430. for (int i = 0; i < result->size(); i++)
  4431. {
  4432. cArr = result->at(i);
  4433. it = cArr->find("id");
  4434. if (it != cArr->end())
  4435. {
  4436. tVal->push_back(atoi(it->second.c_str()));
  4437. };
  4438. };
  4439. };
  4440. };
  4441. };
  4442. deleteResult(result);
  4443. };
  4444. return tVal;
  4445. }
  4446. DBC_INT_LIST * sqliteDBC::listResources(uint32_t NodeID)
  4447. {
  4448. DBC_INT_LIST * tVal = NULL;
  4449. if (m_bStoIsConnected)
  4450. {
  4451. char tQBuf[CTEXT];
  4452. AAssoc * result;
  4453. if (NodeID == 0)
  4454. {
  4455. NodeID = m_nServerNodeID;
  4456. }
  4457. sprintf(tQBuf, "SELECT `id` FROM `resources` WHERE `node_id`=%d", NodeID);
  4458. #ifdef DEBUG_ALL
  4459. LOG(tQBuf);
  4460. #endif
  4461. result = m_pDataStorage->Query(tQBuf);
  4462. if (result != NULL)
  4463. {
  4464. if (result->size() > 0)
  4465. {
  4466. Assoc * cArr;
  4467. tVal = new DBC_INT_LIST();
  4468. if (tVal != NULL)
  4469. {
  4470. Assoc::iterator it;
  4471. for (int i = 0; i < result->size(); i++)
  4472. {
  4473. cArr = result->at(i);
  4474. it = cArr->find("id");
  4475. if (it != cArr->end())
  4476. {
  4477. tVal->push_back(atoi(it->second.c_str()));
  4478. };
  4479. };
  4480. };
  4481. };
  4482. };
  4483. deleteResult(result);
  4484. };
  4485. return tVal;
  4486. }
  4487. bool sqliteDBC::pushMemCMD(uint32_t NODE_ID, uint8_t CMD, CSTR DATA)
  4488. {
  4489. DBC_PUSHED_CMD * RetVal = NULL;
  4490. if (m_bMemIsConnected)
  4491. {
  4492. char QS[MAX_PATH];
  4493. sprintf(QS, "INSERT INTO `nodes_qcq`(`node_id`,`cmd`,`status`,`parameter`) VALUES(%d,%d,0,%s)", NODE_ID, CMD, DATA);
  4494. #ifdef DEBUG_ALL
  4495. LOG(QS);
  4496. #endif
  4497. m_pDataMemory->SilentQuery(QS);
  4498. return true;
  4499. };
  4500. return false;
  4501. }
  4502. void sqliteDBC::registerServer(uint32_t SiteID, CSTR identity, int PID, uint32_t * ServID, uint32_t * ServNode)
  4503. {
  4504. if (m_bStoIsConnected)
  4505. {
  4506. m_nSiteID = SiteID;
  4507. char tQBuf[MAX_PATH_LEN];
  4508. #ifdef DEFAULT_SERVER_ID
  4509. 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'", DEFAULT_SERVER_ID, SiteID, identity, _BUILD_STRING_, identity,
  4510. _BUILD_STRING_);
  4511. #else
  4512. sprintf(tQBuf, "INSERT INTO `servers`(`site_id`,`identity`,`version_string`) VALUES (%d,'%s','%s') ON CONFLICT(`identity`) DO UPDATE SET `version_string`='%s'", SiteID, identity, _BUILD_STRING_, _BUILD_STRING_);
  4513. #endif
  4514. #ifdef DEBUG_ALL
  4515. LOG(tQBuf);
  4516. #endif
  4517. m_pDataStorage->SilentQuery(tQBuf);
  4518. #ifdef DEFAULT_SERVER_ID
  4519. m_nServerID = DEFAULT_SERVER_ID;
  4520. #else
  4521. m_nServerID = getServerID(identity);
  4522. #endif
  4523. *ServID = m_nServerID;
  4524. if (m_nServerID)
  4525. {
  4526. // if ( bRecreateMemStructures )
  4527. initMemDB(m_nServerID);
  4528. sprintf(tQBuf, "UPDATE `servers` SET `pid`=%d,`ts`=STRFTIME('%%s') WHERE `id`=%d", PID, m_nServerID);
  4529. #ifdef DEBUG_ALL
  4530. LOG(tQBuf);
  4531. #endif
  4532. m_pDataMemory->SilentQuery(tQBuf);
  4533. 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);
  4534. #ifdef DEBUG_ALL
  4535. LOG(tQBuf);
  4536. #endif
  4537. m_nServerNodeID = (uint32_t) m_pDataStorage->QueryID(tQBuf);
  4538. if (m_nServerNodeID == 0)
  4539. {
  4540. m_nServerNodeID = getServerNode(m_nServerID);
  4541. };
  4542. *ServNode = m_nServerNodeID;
  4543. // Clenup Nodes and services ...
  4544. sprintf(tQBuf, "UPDATE `nodes` SET `online`=0");
  4545. #ifdef DEBUG_ALL
  4546. LOG(tQBuf);
  4547. #endif
  4548. m_pDataStorage->SilentQuery(tQBuf);
  4549. sprintf(tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `manual`=0");
  4550. #ifdef DEBUG_ALL
  4551. LOG(tQBuf);
  4552. #endif
  4553. m_pDataStorage->SilentQuery(tQBuf);
  4554. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `manual`=0 )");
  4555. #ifdef DEBUG_ALL
  4556. LOG(tQBuf);
  4557. #endif
  4558. m_pDataStorage->SilentQuery(tQBuf);
  4559. // sprintf(tQBuf,"UPDATE `cameras` SET `online`=1 WHERE `manual`=1" );
  4560. #ifdef DEBUG_ALL
  4561. // LOG(tQBuf);
  4562. #endif
  4563. // m_pDataStorage->SilentQuery( tQBuf );
  4564. // sprintf(tQBuf,"UPDATE `media_streams` SET `valid`=1 WHERE `source_id` IN ( SELECT `id` FROM `cameras` WHERE `online`=1 )" );
  4565. #ifdef DEBUG_ALL
  4566. // LOG(tQBuf);
  4567. #endif
  4568. // m_pDataStorage->SilentQuery( tQBuf );
  4569. // sprintf(tQBuf,"UPDATE `media_server_streams` SET `valid`=0, `online`=0" );
  4570. #ifdef DEBUG_ALL
  4571. // LOG(tQBuf);
  4572. #endif
  4573. // m_pDataStorage->SilentQuery( tQBuf );
  4574. // cleanResources( tServerNodeID );
  4575. setNodeOnline(m_nServerNodeID);
  4576. };
  4577. };
  4578. }
  4579. bool sqliteDBC::setLRFDataStatus(uint32_t lrfID, uint8_t ST)
  4580. {
  4581. if (m_bStoIsConnected)
  4582. {
  4583. char tQBuf[MAX_PATH_LEN];
  4584. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `data_status`=%d WHERE `id`=%d", ST, lrfID);
  4585. #ifdef DEBUG_ALL
  4586. LOG(tQBuf);
  4587. #endif
  4588. m_pDataStorage->SilentQuery(tQBuf);
  4589. return true;
  4590. };
  4591. return false;
  4592. }
  4593. bool sqliteDBC::setLRFDetectionProfile(uint32_t lrfID, uint8_t profileID)
  4594. {
  4595. if (m_bStoIsConnected)
  4596. {
  4597. char tQBuf[MAX_PATH_LEN];
  4598. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `detection_profile_id`=%d WHERE `id`=%d", profileID, lrfID);
  4599. #ifdef DEBUG_ALL
  4600. LOG(tQBuf);
  4601. #endif
  4602. m_pDataStorage->SilentQuery(tQBuf);
  4603. return true;
  4604. };
  4605. return false;
  4606. }
  4607. bool sqliteDBC::setLRFMaskStatus(uint32_t maskID, bool status)
  4608. {
  4609. if (m_bStoIsConnected)
  4610. {
  4611. char tQBuf[MAX_PATH_LEN];
  4612. if (status)
  4613. {
  4614. sprintf(tQBuf, "UPDATE `lrf_masks` SET `status`='E' WHERE `id`=%d", maskID);
  4615. }
  4616. else
  4617. {
  4618. sprintf(tQBuf, "UPDATE `lrf_masks` SET `status`='D' WHERE `id`=%d", maskID);
  4619. };
  4620. #ifdef DEBUG_ALL
  4621. LOG(tQBuf);
  4622. #endif
  4623. m_pDataStorage->SilentQuery(tQBuf);
  4624. return true;
  4625. };
  4626. return false;
  4627. }
  4628. bool sqliteDBC::setLRFAltSerial(uint32_t lrfID, CSTR Serial)
  4629. {
  4630. if (m_bStoIsConnected)
  4631. {
  4632. char tQBuf[MAX_PATH_LEN];
  4633. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `alt_serial`='%s' WHERE `id`=%d", Serial, lrfID);
  4634. #ifdef DEBUG_ALL
  4635. LOG(tQBuf);
  4636. #endif
  4637. m_pDataStorage->SilentQuery(tQBuf);
  4638. return true;
  4639. };
  4640. return false;
  4641. }
  4642. bool sqliteDBC::setLRFAltVerison(uint32_t lrfID, CSTR version)
  4643. {
  4644. if (m_bStoIsConnected)
  4645. {
  4646. char tQBuf[MAX_PATH_LEN];
  4647. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `alt_version`='%s' WHERE `id`=%d", version, lrfID);
  4648. #ifdef DEBUG_ALL
  4649. LOG(tQBuf);
  4650. #endif
  4651. m_pDataStorage->SilentQuery(tQBuf);
  4652. return true;
  4653. };
  4654. return false;
  4655. }
  4656. bool sqliteDBC::setNodeOffline(uint32_t NodeID)
  4657. {
  4658. if (m_bStoIsConnected)
  4659. {
  4660. char tQBuf[CTEXT];
  4661. if (NodeID)
  4662. {
  4663. sprintf(tQBuf, "UPDATE nodes SET `online`=0 WHERE `id` = %d", NodeID);
  4664. #ifdef DEBUG_ALL
  4665. LOG(tQBuf);
  4666. #endif
  4667. m_pDataStorage->SilentQuery(tQBuf);
  4668. sprintf(tQBuf, "UPDATE `resources` SET `active`=0 WHERE `node_id` = %d", NodeID);
  4669. #ifdef DEBUG_ALL
  4670. LOG(tQBuf);
  4671. #endif
  4672. m_pDataStorage->SilentQuery(tQBuf);
  4673. return true;
  4674. };
  4675. };
  4676. return false;
  4677. }
  4678. bool sqliteDBC::setNodeOnline(uint32_t NodeID, uint8_t status)
  4679. {
  4680. if (m_bStoIsConnected)
  4681. {
  4682. char tQBuf[CTEXT];
  4683. if (NodeID)
  4684. {
  4685. sprintf(tQBuf, "UPDATE `nodes` SET `online`=%d WHERE `id`=%d", status, NodeID);
  4686. #ifdef DEBUG_ALL
  4687. LOG(tQBuf);
  4688. #endif
  4689. m_pDataStorage->SilentQuery(tQBuf);
  4690. sprintf(tQBuf, "UPDATE `resources` SET `active`=1 WHERE `node_id`=%d", NodeID);
  4691. #ifdef DEBUG_ALL
  4692. LOG(tQBuf);
  4693. #endif
  4694. m_pDataStorage->SilentQuery(tQBuf);
  4695. return true;
  4696. };
  4697. };
  4698. return false;
  4699. }
  4700. bool sqliteDBC::setRessourceActive(uint32_t RessourceID, bool Status)
  4701. {
  4702. if (m_bStoIsConnected)
  4703. {
  4704. char tQBuf[CTEXT];
  4705. if (RessourceID)
  4706. {
  4707. sprintf(tQBuf, "UPDATE `resources` SET `active`=%d WHERE `id`=%d", Status, RessourceID);
  4708. #ifdef DEBUG_ALL
  4709. LOG(tQBuf);
  4710. #endif
  4711. m_pDataStorage->SilentQuery(tQBuf);
  4712. return true;
  4713. };
  4714. };
  4715. return false;
  4716. }
  4717. bool sqliteDBC::setStreamValid(uint32_t streamID, bool status)
  4718. {
  4719. if (m_bStoIsConnected)
  4720. {
  4721. char tQBuf[MAX_PATH_LEN];
  4722. if (status)
  4723. {
  4724. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `id`=%d", streamID);
  4725. }
  4726. else
  4727. {
  4728. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `id`=%d", streamID);
  4729. };
  4730. #ifdef DEBUG_ALL
  4731. LOG(tQBuf);
  4732. #endif
  4733. m_pDataStorage->SilentQuery(tQBuf);
  4734. return true;
  4735. };
  4736. return false;
  4737. }
  4738. void sqliteDBC::unregisterServer(void)
  4739. {
  4740. if (m_bStoIsConnected)
  4741. {
  4742. char tQBuf[MAX_PATH_LEN];
  4743. sprintf(tQBuf, "UPDATE `cameras` SET `online`=0");
  4744. #ifdef DEBUG_ALL
  4745. LOG(tQBuf);
  4746. #endif
  4747. m_pDataStorage->SilentQuery(tQBuf);
  4748. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0");
  4749. #ifdef DEBUG_ALL
  4750. LOG(tQBuf);
  4751. #endif
  4752. m_pDataStorage->SilentQuery(tQBuf);
  4753. };
  4754. setNodeOffline(m_nServerNodeID);
  4755. }
  4756. bool sqliteDBC::updateSystemInfo(DBC_SYS_INFO * ptr)
  4757. {
  4758. if (m_bStoIsConnected)
  4759. {
  4760. char tQBuf[MAX_PATH_LEN];
  4761. sprintf(tQBuf, "UPDATE `system_props` SET `os_info`='%s', `hw_info`='%s' WHERE `id`=%d", ptr->OS_INFO, ptr->HW_INFO, m_nSiteID);
  4762. #ifdef DEBUG_ALL
  4763. LOG(tQBuf);
  4764. #endif
  4765. m_pDataStorage->SilentQuery(tQBuf);
  4766. return true;
  4767. };
  4768. return false;
  4769. }
  4770. bool sqliteDBC::fillShadow2D(uint32_t ID, SHADOW & SH)
  4771. {
  4772. if (m_bStoIsConnected)
  4773. {
  4774. char tQBuf[CTEXT];
  4775. AAssoc * result;
  4776. 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);
  4777. #ifdef DEBUG_ALL
  4778. LOG(tQBuf);
  4779. #endif
  4780. result = m_pDataStorage->Query(tQBuf);
  4781. if (result != NULL)
  4782. {
  4783. if (result->size() > 0)
  4784. {
  4785. SH.m_nEnabled = 0xFFFFFFFF;
  4786. SH.m_nPolygonCount = result->size();
  4787. SH.pPolygones = (SHADOW_POLY *) calloc(SH.m_nPolygonCount, sizeof(SHADOW_POLY));
  4788. if (SH.pPolygones)
  4789. {
  4790. Assoc * cArr;
  4791. for (uint8_t idx = 0; idx < result->size(); idx++)
  4792. {
  4793. Assoc::iterator it;
  4794. cArr = result->at(idx);
  4795. it = cArr->find("id");
  4796. if (it != cArr->end())
  4797. {
  4798. SH.pPolygones[idx].m_nDBID = atoi(it->second.c_str());
  4799. };
  4800. it = cArr->find("layer");
  4801. if (it != cArr->end())
  4802. {
  4803. SH.pPolygones[idx].bType = atoi(it->second.c_str());
  4804. };
  4805. it = cArr->find("status");
  4806. if (it != cArr->end())
  4807. {
  4808. SH.m_nEnabled &= (it->second.c_str()[0] == 'E') ? 0xFFFFFFFF : ~(1 << SH.pPolygones[idx].bType);
  4809. };
  4810. sprintf(tQBuf, "SELECT * FROM `lrf_mask_vertex_points` WHERE `vertex_id`=%d ORDER BY `id`", SH.pPolygones[idx].m_nDBID);
  4811. #ifdef DEBUG_ALL
  4812. LOG(tQBuf);
  4813. #endif
  4814. AAssoc * v_result = m_pDataStorage->Query(tQBuf);
  4815. if (v_result != NULL)
  4816. {
  4817. if (v_result->size() > 0)
  4818. {
  4819. SH.pPolygones[idx].wCount = v_result->size();
  4820. SH.pPolygones[idx].pPoint = (HZ_POINT *) calloc(SH.pPolygones[idx].wCount, sizeof(HZ_POINT));
  4821. if (SH.pPolygones[idx].pPoint)
  4822. {
  4823. for (uint32_t zdx = 0; zdx < SH.pPolygones[idx].wCount; zdx++)
  4824. {
  4825. cArr = v_result->at(zdx);
  4826. it = cArr->find("x");
  4827. if (it != cArr->end())
  4828. {
  4829. SH.pPolygones[idx].pPoint[zdx].wX = atoi(it->second.c_str());
  4830. };
  4831. it = cArr->find("y");
  4832. if (it != cArr->end())
  4833. {
  4834. SH.pPolygones[idx].pPoint[zdx].wY = atoi(it->second.c_str());
  4835. };
  4836. LOG("Point Added!");
  4837. };
  4838. }
  4839. else
  4840. {
  4841. LOG("No mem for Points!");
  4842. };
  4843. }
  4844. else
  4845. {
  4846. LOG("No Points!");
  4847. SH.pPolygones[idx].wCount = 0;
  4848. SH.pPolygones[idx].pPoint = NULL;
  4849. };
  4850. deleteResult(v_result);
  4851. };
  4852. };
  4853. };
  4854. };
  4855. deleteResult(result);
  4856. };
  4857. return true;
  4858. };
  4859. return false;
  4860. }
  4861. bool sqliteDBC::getAppDebugMode()
  4862. {
  4863. bool rVal = false;
  4864. if (m_bStoIsConnected)
  4865. {
  4866. char tQBuf[CTEXT];
  4867. AAssoc * result;
  4868. sprintf(tQBuf, "SELECT `work_mode` FROM `system_props` WHERE `id`=1");
  4869. #ifdef DEBUG_ALL
  4870. LOG(tQBuf);
  4871. #endif
  4872. result = m_pDataStorage->Query(tQBuf);
  4873. if (result != NULL)
  4874. {
  4875. if (result->size() > 0)
  4876. {
  4877. Assoc * cArr;
  4878. Assoc::iterator it;
  4879. cArr = result->at(0);
  4880. it = cArr->find("work_mode");
  4881. if (it != cArr->end())
  4882. {
  4883. rVal = (bool) atoi(it->second.c_str());
  4884. };
  4885. };
  4886. };
  4887. deleteResult(result);
  4888. };
  4889. return rVal;
  4890. }
  4891. BUBBLE_FILTER_DATA * sqliteDBC::getBubbleFilterData(uint32_t id)
  4892. {
  4893. BUBBLE_FILTER_DATA * tVal = NULL;
  4894. if (m_bStoIsConnected)
  4895. {
  4896. char tQBuf[CTEXT];
  4897. AAssoc * result;
  4898. sprintf(tQBuf, "SELECT * FROM `target_filter_bubble` WHERE `id`=%d", id);
  4899. #ifdef DEBUG_ALL
  4900. LOG(tQBuf);
  4901. #endif
  4902. result = m_pDataStorage->Query(tQBuf);
  4903. if (result != NULL)
  4904. {
  4905. if (result->size() > 0)
  4906. {
  4907. Assoc * cArr;
  4908. tVal = (BUBBLE_FILTER_DATA *) malloc(sizeof(BUBBLE_FILTER_DATA));
  4909. if (tVal != NULL)
  4910. {
  4911. memset(tVal, 0, sizeof(BUBBLE_FILTER_DATA));
  4912. ;
  4913. Assoc::iterator it;
  4914. tVal->id = id;
  4915. cArr = result->at(0);
  4916. it = cArr->find("BEGINNING_RANGE");
  4917. if (it != cArr->end())
  4918. {
  4919. tVal->beginning_range = atof(it->second.c_str());
  4920. };
  4921. it = cArr->find("MAX_RANGE");
  4922. if (it != cArr->end())
  4923. {
  4924. tVal->maximal_range = atof(it->second.c_str());
  4925. };
  4926. it = cArr->find("MIN_BLOCK_COUNS");
  4927. if (it != cArr->end())
  4928. {
  4929. tVal->min_block_counts = atoi(it->second.c_str());
  4930. };
  4931. #ifdef BUBBLE_TIME_BUCKET
  4932. it = cArr->find("BUCKET_TIME");
  4933. if (it != cArr->end())
  4934. {
  4935. tVal->bucket_time = atoi(it->second.c_str());
  4936. };
  4937. #endif
  4938. it = cArr->find("TTL");
  4939. if (it != cArr->end())
  4940. {
  4941. tVal->ttl = atoi(it->second.c_str());
  4942. };
  4943. it = cArr->find("T2T");
  4944. if (it != cArr->end())
  4945. {
  4946. tVal->t2t = atoi(it->second.c_str());
  4947. };
  4948. it = cArr->find("PROPOGATE");
  4949. if (it != cArr->end())
  4950. {
  4951. tVal->propogate = atoi(it->second.c_str());
  4952. };
  4953. it = cArr->find("METHOD");
  4954. if (it != cArr->end())
  4955. {
  4956. tVal->method = atoi(it->second.c_str());
  4957. };
  4958. it = cArr->find("ENABLE_EVENT");
  4959. if (it != cArr->end())
  4960. {
  4961. tVal->enable_evt = (bool) atoi(it->second.c_str());
  4962. };
  4963. };
  4964. #ifdef BUBBLE_TIME_BUCKET
  4965. if (tVal->bucket_time < BUBBLE_FILTER_MIN_BUCKET_TIME)
  4966. {
  4967. tVal->bucket_time = BUBBLE_FILTER_MIN_BUCKET_TIME;
  4968. }
  4969. #endif
  4970. };
  4971. };
  4972. deleteResult(result);
  4973. };
  4974. return tVal;
  4975. }
  4976. DBC_CAM_DATA * sqliteDBC::getCamData(uint32_t ID)
  4977. {
  4978. DBC_CAM_DATA * tVal = NULL;
  4979. if (m_bStoIsConnected)
  4980. {
  4981. char tQBuf[CTEXT];
  4982. AAssoc * result;
  4983. sprintf(tQBuf, "SELECT * FROM cameras WHERE `id`=%d", ID);
  4984. #ifdef DEBUG_ALL
  4985. LOG(tQBuf);
  4986. #endif
  4987. result = m_pDataStorage->Query(tQBuf);
  4988. if (result != NULL)
  4989. {
  4990. if (result->size() > 0)
  4991. {
  4992. Assoc * cArr;
  4993. tVal = (DBC_CAM_DATA *) malloc(sizeof(DBC_CAM_DATA));
  4994. if (tVal != NULL)
  4995. {
  4996. memset(tVal, 0, sizeof(DBC_CAM_DATA));
  4997. tVal->id = ID;
  4998. Assoc::iterator it;
  4999. cArr = result->at(0);
  5000. it = cArr->find("type");
  5001. if (it != cArr->end())
  5002. {
  5003. tVal->type = atoi(it->second.c_str());
  5004. };
  5005. it = cArr->find("online");
  5006. if (it != cArr->end())
  5007. {
  5008. tVal->online = (bool) atoi(it->second.c_str());
  5009. };
  5010. it = cArr->find("stream_id");
  5011. if (it != cArr->end())
  5012. {
  5013. tVal->stream_id = atoi(it->second.c_str());
  5014. };
  5015. it = cArr->find("hardware");
  5016. if (it != cArr->end())
  5017. {
  5018. strncpy(tVal->hardware, it->second.c_str(), 32);
  5019. };
  5020. it = cArr->find("name");
  5021. if (it != cArr->end())
  5022. {
  5023. strncpy(tVal->name, it->second.c_str(), 64);
  5024. };
  5025. it = cArr->find("status");
  5026. if (it != cArr->end())
  5027. {
  5028. tVal->status = atoi(it->second.c_str());
  5029. };
  5030. it = cArr->find("session");
  5031. if (it != cArr->end())
  5032. {
  5033. strncpy(tVal->session, it->second.c_str(), 32);
  5034. };
  5035. it = cArr->find("protocol");
  5036. if (it != cArr->end())
  5037. {
  5038. tVal->protocol = atoi(it->second.c_str());
  5039. };
  5040. it = cArr->find("protocol_aux");
  5041. if (it != cArr->end())
  5042. {
  5043. tVal->protocol_aux = atoi(it->second.c_str());
  5044. };
  5045. it = cArr->find("uart_id");
  5046. if (it != cArr->end())
  5047. {
  5048. tVal->uart_id = atoi(it->second.c_str());
  5049. };
  5050. it = cArr->find("x");
  5051. if (it != cArr->end())
  5052. {
  5053. tVal->x = atof(it->second.c_str());
  5054. };
  5055. it = cArr->find("y");
  5056. if (it != cArr->end())
  5057. {
  5058. tVal->y = atof(it->second.c_str());
  5059. };
  5060. it = cArr->find("z");
  5061. if (it != cArr->end())
  5062. {
  5063. tVal->z = atof(it->second.c_str());
  5064. };
  5065. it = cArr->find("x_rot");
  5066. if (it != cArr->end())
  5067. {
  5068. tVal->x_rot = atof(it->second.c_str());
  5069. };
  5070. it = cArr->find("y_rot");
  5071. if (it != cArr->end())
  5072. {
  5073. tVal->y_rot = atof(it->second.c_str());
  5074. };
  5075. it = cArr->find("z_rot");
  5076. if (it != cArr->end())
  5077. {
  5078. tVal->z_rot = atof(it->second.c_str());
  5079. };
  5080. it = cArr->find("imager_type");
  5081. if (it != cArr->end())
  5082. {
  5083. tVal->imager_type = atoi(it->second.c_str());
  5084. };
  5085. it = cArr->find("lens_f");
  5086. if (it != cArr->end())
  5087. {
  5088. tVal->lens_f = atoi(it->second.c_str());
  5089. };
  5090. it = cArr->find("ptz_speed_step");
  5091. if (it != cArr->end())
  5092. {
  5093. tVal->ptz_speed_step = atof(it->second.c_str());
  5094. };
  5095. it = cArr->find("pan_model");
  5096. if (it != cArr->end())
  5097. {
  5098. tVal->pan_model = atoi(it->second.c_str());
  5099. };
  5100. it = cArr->find("pan_angles");
  5101. if (it != cArr->end())
  5102. {
  5103. tVal->pan_angles = atoi(it->second.c_str());
  5104. };
  5105. it = cArr->find("tilt_model");
  5106. if (it != cArr->end())
  5107. {
  5108. tVal->tilt_model = atoi(it->second.c_str());
  5109. };
  5110. it = cArr->find("tilt_angles");
  5111. if (it != cArr->end())
  5112. {
  5113. tVal->tilt_angles = atoi(it->second.c_str());
  5114. };
  5115. it = cArr->find("zoom_model");
  5116. if (it != cArr->end())
  5117. {
  5118. tVal->zoom_model = atoi(it->second.c_str());
  5119. };
  5120. it = cArr->find("zoom");
  5121. if (it != cArr->end())
  5122. {
  5123. tVal->zoom = atof(it->second.c_str());
  5124. };
  5125. it = cArr->find("max_range");
  5126. if (it != cArr->end())
  5127. {
  5128. tVal->max_range = atof(it->second.c_str());
  5129. };
  5130. it = cArr->find("access_login");
  5131. if (it != cArr->end())
  5132. {
  5133. strncpy(tVal->username, it->second.c_str(), 16);
  5134. };
  5135. it = cArr->find("access_pass");
  5136. if (it != cArr->end())
  5137. {
  5138. strncpy(tVal->password, it->second.c_str(), 16);
  5139. };
  5140. it = cArr->find("uuid");
  5141. if (it != cArr->end())
  5142. {
  5143. strncpy(tVal->uuid, it->second.c_str(), 64);
  5144. };
  5145. it = cArr->find("mac");
  5146. if (it != cArr->end())
  5147. {
  5148. strncpy(tVal->mac, it->second.c_str(), 16);
  5149. };
  5150. it = cArr->find("ip");
  5151. if (it != cArr->end())
  5152. {
  5153. strncpy(tVal->ip, it->second.c_str(), 16);
  5154. };
  5155. it = cArr->find("port");
  5156. if (it != cArr->end())
  5157. {
  5158. tVal->port = atoi(it->second.c_str());
  5159. };
  5160. it = cArr->find("onvif_access");
  5161. if (it != cArr->end())
  5162. {
  5163. strncpy(tVal->onvif_access, it->second.c_str(), 5);
  5164. };
  5165. it = cArr->find("onvif_ctrl");
  5166. if (it != cArr->end())
  5167. {
  5168. strncpy(tVal->onvif_ctrl, it->second.c_str(), 250);
  5169. };
  5170. it = cArr->find("ptz_token");
  5171. if (it != cArr->end())
  5172. {
  5173. strncpy(tVal->ptz_token, it->second.c_str(), 64);
  5174. };
  5175. it = cArr->find("trigger_mode");
  5176. if (it != cArr->end())
  5177. {
  5178. tVal->triggering_mode = atoi(it->second.c_str());
  5179. };
  5180. it = cArr->find("auth_mode");
  5181. if (it != cArr->end())
  5182. {
  5183. tVal->auth_mode = atoi(it->second.c_str());
  5184. };
  5185. it = cArr->find("ptz_axel");
  5186. if (it != cArr->end())
  5187. {
  5188. tVal->ptz_axel = atoi(it->second.c_str());
  5189. };
  5190. it = cArr->find("manual");
  5191. if (it != cArr->end())
  5192. {
  5193. tVal->manual = (bool) atoi(it->second.c_str());
  5194. };
  5195. it = cArr->find("homing_enable");
  5196. if (it != cArr->end())
  5197. {
  5198. tVal->homing_enable = (bool) atoi(it->second.c_str());
  5199. };
  5200. it = cArr->find("homing_to");
  5201. if (it != cArr->end())
  5202. {
  5203. tVal->homing_to = atoi(it->second.c_str());
  5204. };
  5205. it = cArr->find("home_token");
  5206. if (it != cArr->end())
  5207. {
  5208. strncpy(tVal->home_token, it->second.c_str(), 32);
  5209. };
  5210. it = cArr->find("eco_ir");
  5211. if (it != cArr->end())
  5212. {
  5213. tVal->eco_ir = (bool) atoi(it->second.c_str());
  5214. };
  5215. it = cArr->find("prefered_events_only");
  5216. if (it != cArr->end())
  5217. {
  5218. tVal->prefered_events_only = (bool) atoi(it->second.c_str());
  5219. };
  5220. it = cArr->find("flip_mode");
  5221. if (it != cArr->end())
  5222. {
  5223. tVal->flip_mode = atoi(it->second.c_str());
  5224. };
  5225. };
  5226. };
  5227. };
  5228. deleteResult(result);
  5229. };
  5230. return tVal;
  5231. }
  5232. uint32_t sqliteDBC::getCamId(CSTR UUID)
  5233. {
  5234. uint32_t RetVal = 0;
  5235. if (m_bStoIsConnected)
  5236. {
  5237. char tQBuf[CTEXT];
  5238. AAssoc * result;
  5239. sprintf(tQBuf, "SELECT `id` FROM `cameras` WHERE `uuid`='%s'", UUID);
  5240. #ifdef DEBUG_ALL
  5241. LOG(tQBuf);
  5242. #endif
  5243. result = m_pDataStorage->Query(tQBuf);
  5244. if (result != NULL)
  5245. {
  5246. if (result->size() > 0)
  5247. {
  5248. Assoc * cArr;
  5249. Assoc::iterator it;
  5250. cArr = result->at(0);
  5251. it = cArr->find("id");
  5252. if (it != cArr->end())
  5253. {
  5254. RetVal = atoi(it->second.c_str());
  5255. };
  5256. };
  5257. };
  5258. deleteResult(result);
  5259. };
  5260. return RetVal;
  5261. }
  5262. CAM_PRESET * sqliteDBC::getCamPreset(uint32_t SETID)
  5263. {
  5264. CAM_PRESET * tVal = NULL;
  5265. if (m_bStoIsConnected)
  5266. {
  5267. char tQBuf[CTEXT];
  5268. AAssoc * result;
  5269. sprintf(tQBuf, "SELECT * FROM `cameras_presets` WHERE `id`=%d", SETID);
  5270. #ifdef DEBUG_ALL
  5271. LOG(tQBuf);
  5272. #endif
  5273. result = m_pDataStorage->Query(tQBuf);
  5274. if (result != NULL)
  5275. {
  5276. if (result->size() > 0)
  5277. {
  5278. Assoc * cArr;
  5279. tVal = (CAM_PRESET *) malloc(sizeof(CAM_PRESET));
  5280. if (tVal != NULL)
  5281. {
  5282. memset(tVal, 0, sizeof(CAM_PRESET));
  5283. tVal->id = SETID;
  5284. Assoc::iterator it;
  5285. cArr = result->at(0);
  5286. it = cArr->find("token");
  5287. if (it != cArr->end())
  5288. {
  5289. strncpy(tVal->token, it->second.c_str(), 2);
  5290. tVal->token[3] = (char) 0;
  5291. };
  5292. it = cArr->find("role");
  5293. if (it != cArr->end())
  5294. {
  5295. tVal->role = atoi(it->second.c_str());
  5296. };
  5297. it = cArr->find("row");
  5298. if (it != cArr->end())
  5299. {
  5300. tVal->row = atoi(it->second.c_str());
  5301. };
  5302. it = cArr->find("column");
  5303. if (it != cArr->end())
  5304. {
  5305. tVal->col = atoi(it->second.c_str());
  5306. };
  5307. };
  5308. };
  5309. };
  5310. deleteResult(result);
  5311. };
  5312. return tVal;
  5313. }
  5314. DBC_PRESETS_INFO * sqliteDBC::getCamPresetInfo(uint32_t CamID)
  5315. {
  5316. DBC_PRESETS_INFO * tVal = NULL;
  5317. if (m_bStoIsConnected)
  5318. {
  5319. char tQBuf[CTEXT];
  5320. AAssoc * result;
  5321. sprintf(tQBuf, "SELECT * FROM `cameras_preset_sets` WHERE `camera_id`=%d", CamID);
  5322. #ifdef DEBUG_ALL
  5323. LOG(tQBuf);
  5324. #endif
  5325. result = m_pDataStorage->Query(tQBuf);
  5326. if (result != NULL)
  5327. {
  5328. if (result->size() > 0)
  5329. {
  5330. Assoc * cArr;
  5331. tVal = (DBC_PRESETS_INFO *) malloc(sizeof(DBC_PRESETS_INFO));
  5332. if (tVal != NULL)
  5333. {
  5334. memset(tVal, 0, sizeof(DBC_PRESETS_INFO));
  5335. Assoc::iterator it;
  5336. cArr = result->at(0);
  5337. it = cArr->find("maxCount");
  5338. if (it != cArr->end())
  5339. {
  5340. tVal->maxCount = atoi(it->second.c_str());
  5341. };
  5342. it = cArr->find("p_segs");
  5343. if (it != cArr->end())
  5344. {
  5345. tVal->gCols = atoi(it->second.c_str());
  5346. };
  5347. it = cArr->find("z_segs");
  5348. if (it != cArr->end())
  5349. {
  5350. tVal->gRows = atoi(it->second.c_str());
  5351. };
  5352. };
  5353. };
  5354. };
  5355. deleteResult(result);
  5356. };
  5357. return tVal;
  5358. }
  5359. CSTR sqliteDBC::getDataParam(uint8_t pid)
  5360. {
  5361. switch (pid)
  5362. {
  5363. case 0: return strdup(m_pDbFile); break;
  5364. case 1: return strdup(web_access_user); break;
  5365. case 2: return strdup(web_access_group); break;
  5366. };
  5367. return NULL;
  5368. }
  5369. bool sqliteDBC::updateIPCam(uint32_t CamID, CSTR Addr)
  5370. {
  5371. if (m_bStoIsConnected)
  5372. {
  5373. char tQBuf[MAX_PATH_LEN];
  5374. char tIpBuf[CTEXT];
  5375. char * tPos;
  5376. char * tPos2;
  5377. uint16_t tPort = 0;
  5378. tPos = strstr(Addr, "//");
  5379. if (tPos != NULL)
  5380. {
  5381. tPos2 = strstr(tPos + 2, ":");
  5382. if (tPos2 != NULL)
  5383. {
  5384. LOG("IP Port Defined!");
  5385. int len, len2, len3;
  5386. len = tPos2 - tPos - 2;
  5387. strncpy(tIpBuf, tPos + 2, len);
  5388. tIpBuf[len] = (char) 0;
  5389. tPort = atoi(tPos2 + 1);
  5390. // strncpy( tPath, tPos + 2 + len, CTEXT );
  5391. }
  5392. else
  5393. {
  5394. tPos2 = strstr(tPos + 2, "/");
  5395. if (tPos2 != NULL)
  5396. {
  5397. int len, len2, len3;
  5398. len = tPos2 - tPos - 2;
  5399. strncpy(tIpBuf, tPos + 2, len);
  5400. tIpBuf[len] = (char) 0;
  5401. // strncpy( tPath, tPos + 2 + len, CTEXT );
  5402. }
  5403. else
  5404. {
  5405. tPos2 = strstr(tPos + 2, "/");
  5406. LOG_ERROR("Pos 2 - err");
  5407. tIpBuf[0] = (char) 0;
  5408. };
  5409. };
  5410. }
  5411. else
  5412. {
  5413. LOG_ERROR("Pos 0 - err");
  5414. tIpBuf[0] = (char) 0;
  5415. };
  5416. if (tPort)
  5417. {
  5418. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s', `port`=%d WHERE `id`=%d", Addr, tIpBuf, tPort, CamID);
  5419. }
  5420. else
  5421. {
  5422. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1, `onvif_ctrl`='%s', `ip`='%s' WHERE `id`=%d", Addr, tIpBuf, CamID);
  5423. }
  5424. #ifdef DEBUG_ALL
  5425. LOG(tQBuf);
  5426. #endif
  5427. m_pDataStorage->SilentQuery(tQBuf);
  5428. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID);
  5429. #ifdef DEBUG_ALL
  5430. LOG(tQBuf);
  5431. #endif
  5432. m_pDataStorage->SilentQuery(tQBuf);
  5433. return true;
  5434. };
  5435. return false;
  5436. }
  5437. bool sqliteDBC::updateCamRange(uint32_t camID, float range)
  5438. {
  5439. if (m_bStoIsConnected)
  5440. {
  5441. char tQBuf[MAX_PATH_LEN];
  5442. sprintf(tQBuf, "UPDATE `cameras` SET `max_range`=%.3f WHERE `id`=%d", range, camID);
  5443. #ifdef DEBUG_ALL
  5444. LOG(tQBuf);
  5445. #endif
  5446. m_pDataStorage->SilentQuery(tQBuf);
  5447. return true;
  5448. };
  5449. return false;
  5450. }
  5451. bool sqliteDBC::updateCamOnlineStatus(uint32_t camID, bool online)
  5452. {
  5453. if (m_bStoIsConnected)
  5454. {
  5455. char tQBuf[MAX_PATH_LEN];
  5456. if (online)
  5457. {
  5458. sprintf(tQBuf, "UPDATE `cameras` SET `online`=1 WHERE `id`=%d", camID);
  5459. }
  5460. else
  5461. {
  5462. sprintf(tQBuf, "UPDATE `cameras` SET `online`=0 WHERE `id`=%d", camID);
  5463. };
  5464. m_pDataStorage->SilentQuery(tQBuf);
  5465. if (online)
  5466. {
  5467. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", camID);
  5468. }
  5469. else
  5470. {
  5471. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=0 WHERE `source_id`=%d", camID);
  5472. };
  5473. m_pDataStorage->SilentQuery(tQBuf);
  5474. return true;
  5475. };
  5476. return false;
  5477. }
  5478. bool sqliteDBC::updateIPCam(CSTR uuid, CSTR Addr)
  5479. {
  5480. if (m_bStoIsConnected)
  5481. {
  5482. uint32_t cID = getCamId(uuid);
  5483. if (cID)
  5484. {
  5485. return updateIPCam(cID, Addr);
  5486. };
  5487. };
  5488. return false;
  5489. }
  5490. bool sqliteDBC::updateLocalCam(CSTR uuid)
  5491. {
  5492. if (m_bStoIsConnected)
  5493. {
  5494. uint32_t cID = getCamId(uuid);
  5495. if (cID)
  5496. {
  5497. return updateLocalCam(cID);
  5498. };
  5499. };
  5500. return false;
  5501. }
  5502. bool sqliteDBC::updateLocalCam(uint32_t CamID)
  5503. {
  5504. if (m_bStoIsConnected)
  5505. {
  5506. char tQBuf[MAX_PATH_LEN];
  5507. sprintf(tQBuf, "UPDATE cameras SET `online`=1 WHERE `id`=%d", CamID);
  5508. #ifdef DEBUG_ALL
  5509. LOG(tQBuf);
  5510. #endif
  5511. m_pDataStorage->SilentQuery(tQBuf);
  5512. sprintf(tQBuf, "UPDATE `media_streams` SET `valid`=1 WHERE `source_id`=%d", CamID);
  5513. #ifdef DEBUG_ALL
  5514. LOG(tQBuf);
  5515. #endif
  5516. m_pDataStorage->SilentQuery(tQBuf);
  5517. return true;
  5518. };
  5519. return false;
  5520. }
  5521. bool sqliteDBC::updateLRFComStat(uint32_t lrfID, uint32_t ErrorPerMinute)
  5522. {
  5523. if (m_bStoIsConnected)
  5524. {
  5525. char tQBuf[MAX_PATH_LEN];
  5526. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `com_epm`=%d WHERE `id`=%d", ErrorPerMinute, lrfID);
  5527. #ifdef DEBUG_ALL
  5528. LOG(tQBuf);
  5529. #endif
  5530. m_pDataStorage->SilentQuery(tQBuf);
  5531. return true;
  5532. };
  5533. return false;
  5534. }
  5535. bool sqliteDBC::updateLRFFrozenStatus(uint32_t lrfID, bool FFT, bool SFT)
  5536. {
  5537. if (m_bStoIsConnected)
  5538. {
  5539. char tQBuf[MAX_PATH_LEN];
  5540. sprintf(tQBuf, "UPDATE `lrf_ini_settings` SET `FREEZ_FIRST_TH`=%d, `SAVE_FIRST_TH`=%d WHERE `sensor_id`=%d", FFT, SFT, lrfID);
  5541. #ifdef DEBUG_ALL
  5542. LOG(tQBuf);
  5543. #endif
  5544. m_pDataStorage->SilentQuery(tQBuf);
  5545. return true;
  5546. };
  5547. return false;
  5548. }
  5549. 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)
  5550. {
  5551. if (m_bStoIsConnected)
  5552. {
  5553. char tQBuf[MAX_PATH_LEN];
  5554. if (master)
  5555. {
  5556. sprintf(tQBuf,
  5557. "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 "
  5558. "`order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
  5559. lrfID, profileID, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes);
  5560. }
  5561. else
  5562. {
  5563. sprintf(tQBuf,
  5564. "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 "
  5565. "`order`=%d, `offset`=%d, `delta`=%d, `flip`=%d, `width`=%d, `sst`=%d, `sun`=%d, `gp`=%d, `spikes`=%d",
  5566. lrfID, profileID, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes, nOrder, nOffset, nDelta, nFlip, nWidth, bSST, nSun, GP, spikes);
  5567. };
  5568. #ifdef DEBUG_ALL
  5569. LOG(tQBuf);
  5570. #endif
  5571. m_pDataStorage->SilentQuery(tQBuf);
  5572. return true;
  5573. };
  5574. return false;
  5575. }
  5576. bool sqliteDBC::updateLRFDryContact(uint32_t lrfID, bool bAuto, bool bNO, uint16_t wWindow)
  5577. {
  5578. if (m_bStoIsConnected)
  5579. {
  5580. char tQBuf[MAX_PATH_LEN];
  5581. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `dc_auto`=%d, `dc_no`=%d, `dc_window`=%d WHERE `id`=%d", bAuto, bNO, wWindow, lrfID);
  5582. #ifdef DEBUG_ALL
  5583. LOG(tQBuf);
  5584. #endif
  5585. m_pDataStorage->SilentQuery(tQBuf);
  5586. return true;
  5587. };
  5588. return false;
  5589. }
  5590. bool sqliteDBC::updateLRFSerial(uint32_t lrfID, CSTR Serial)
  5591. {
  5592. if (m_bStoIsConnected)
  5593. {
  5594. char tQBuf[MAX_PATH_LEN];
  5595. CSTR tES = m_pDataStorage->escapeString(Serial);
  5596. if (tES)
  5597. {
  5598. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `serial`='%s' WHERE `id`=%d", tES, lrfID);
  5599. free(tES);
  5600. #ifdef DEBUG_ALL
  5601. LOG(tQBuf);
  5602. #endif
  5603. m_pDataStorage->SilentQuery(tQBuf);
  5604. return true;
  5605. };
  5606. };
  5607. return false;
  5608. }
  5609. bool sqliteDBC::updateLRFUpload(uint32_t id, uint8_t status)
  5610. {
  5611. if (m_bStoIsConnected)
  5612. {
  5613. char tQBuf[MAX_PATH_LEN];
  5614. sprintf(tQBuf, "UPDATE `lrf_uploads` SET `status`=%d WHERE `id`=%d", status, id);
  5615. #ifdef DEBUG_ALL
  5616. LOG(tQBuf);
  5617. #endif
  5618. m_pDataStorage->SilentQuery(tQBuf);
  5619. return true;
  5620. };
  5621. return false;
  5622. }
  5623. bool sqliteDBC::updateLRFUnfinishedUploads(uint32_t deviceID)
  5624. {
  5625. if (m_bStoIsConnected)
  5626. {
  5627. char tQBuf[MAX_PATH_LEN];
  5628. sprintf(tQBuf, "UPDATE `lrf_uploads` SET `status`=-1 WHERE `sensor_id`=%d AND `status` < 101 ", deviceID);
  5629. #ifdef DEBUG_ALL
  5630. LOG(tQBuf);
  5631. #endif
  5632. m_pDataStorage->SilentQuery(tQBuf);
  5633. return true;
  5634. };
  5635. return false;
  5636. }
  5637. bool sqliteDBC::updateLRFVerison(uint32_t lrfID, CSTR version)
  5638. {
  5639. if (m_bStoIsConnected)
  5640. {
  5641. char tQBuf[MAX_PATH_LEN];
  5642. CSTR tES = m_pDataStorage->escapeString(version);
  5643. if (tES)
  5644. {
  5645. sprintf(tQBuf, "UPDATE `lrf_sensors` SET `version`='%s' WHERE `id`=%d", tES, lrfID);
  5646. free(tES);
  5647. #ifdef DEBUG_ALL
  5648. LOG(tQBuf);
  5649. #endif
  5650. m_pDataStorage->SilentQuery(tQBuf);
  5651. return true;
  5652. };
  5653. };
  5654. return false;
  5655. }
  5656. uint32_t sqliteDBC::getRegisteredBubbleId(float X, float Y, float Z)
  5657. {
  5658. uint32_t RetVal = 0;
  5659. if (m_bStoIsConnected)
  5660. {
  5661. char tQBuf[MAX_PATH_LEN];
  5662. sprintf(tQBuf,
  5663. "SELECT `id` FROM `target_bubbles` WHERE ( ABS(`CENTER_X`-(%0.3f)) < `BOUNDARY` ) AND "
  5664. "( ABS(`CENTER_Y`-(%0.3f)) < `BOUNDARY` ) AND "
  5665. "( ABS(`CENTER_Z`-(%0.3f)) < `BOUNDARY` ) LIMIT 1",
  5666. X, Y, Z);
  5667. #ifdef DEBUG
  5668. LOG(tQBuf);
  5669. #endif
  5670. AAssoc * result = m_pDataStorage->Query(tQBuf);
  5671. if (result != NULL)
  5672. {
  5673. if (result->size() > 0)
  5674. {
  5675. Assoc * cArr;
  5676. Assoc::iterator it;
  5677. cArr = result->at(0);
  5678. it = cArr->find("id");
  5679. if (it != cArr->end())
  5680. {
  5681. RetVal = atoi(it->second.c_str());
  5682. };
  5683. };
  5684. };
  5685. deleteResult(result);
  5686. };
  5687. return RetVal;
  5688. }
  5689. uint8_t sqliteDBC::updateRegisteredBubble(TARGET_SEGMENT * ptr)
  5690. {
  5691. uint8_t RetVal = 0;
  5692. if (ptr == NULL)
  5693. {
  5694. return RetVal;
  5695. }
  5696. if (m_bStoIsConnected)
  5697. {
  5698. char tQBuf[MAX_PATH_LEN];
  5699. uint32_t tID = getRegisteredBubbleId(ptr->system_center.x, ptr->system_center.y, ptr->system_center.z);
  5700. if (tID)
  5701. {
  5702. // Old record exists
  5703. sprintf(tQBuf, "UPDATE `target_bubbles` SET `CENTER_X`=%0.3f, `CENTER_Y`=%0.3f, `CENTER_Z`=%0.3f, `BOUNDARY`=%0.3f, `BLOCK_COUNS`=`BLOCK_COUNS`+1 WHERE `id`=%d RETURNING `BLOCK_COUNS`", ptr->system_center.x, ptr->system_center.y,
  5704. ptr->system_center.z, ptr->boundary, tID);
  5705. AAssoc * result = m_pDataStorage->Query(tQBuf);
  5706. if (result != NULL)
  5707. {
  5708. if (result->size() > 0)
  5709. {
  5710. Assoc * cArr;
  5711. Assoc::iterator it;
  5712. cArr = result->at(0);
  5713. it = cArr->find("BLOCK_COUNS");
  5714. if (it != cArr->end())
  5715. {
  5716. RetVal = atoi(it->second.c_str());
  5717. };
  5718. };
  5719. };
  5720. deleteResult(result);
  5721. }
  5722. else
  5723. {
  5724. // Create New Record for Bubble
  5725. sprintf(tQBuf, "INSERT INTO `target_bubbles`(`CENTER_X`,`CENTER_Y`,`CENTER_Z`,`BOUNDARY`,`BLOCK_COUNS`) VALUES(%0.3f,%0.3f,%0.3f,%0.3f,1)", ptr->system_center.x, ptr->system_center.y, ptr->system_center.z, ptr->boundary);
  5726. m_pDataStorage->SilentQuery(tQBuf);
  5727. };
  5728. };
  5729. return RetVal;
  5730. }
  5731. bool sqliteDBC::updateServerIdentity(uint32_t m_nServerID, CSTR m_sIdentity)
  5732. {
  5733. if (m_bStoIsConnected)
  5734. {
  5735. char tQBuf[MAX_PATH_LEN];
  5736. CSTR tES = m_pDataStorage->escapeString(m_sIdentity);
  5737. if (tES)
  5738. {
  5739. sprintf(tQBuf, "UPDATE `servers` SET `identity`='%s' WHERE `id`=%d", tES, m_nServerID);
  5740. free(tES);
  5741. #ifdef DEBUG_ALL
  5742. LOG(tQBuf);
  5743. #endif
  5744. m_pDataStorage->SilentQuery(tQBuf);
  5745. return true;
  5746. };
  5747. };
  5748. return false;
  5749. }
  5750. #undef LOG
  5751. #undef LOG_ERROR