<commit_before># appbase-cctv
A CCTV system for the IoT backed on [Appbase](https://appbase.io/).

## Dependencies
This is the list of libraries required by appbase-cctv, plus the install command for Debian (*jessie*, derivatives may also apply).
- [libcurl](https://curl.haxx.se/libcurl/c/): `apt-get install libcurl[3|4]-[gnutls|openssl|...]-dev`
- [json-c](http://json-c.github.io/json-c/): `apt-get install libjson-c-dev`
- [modpbase64](https://github.com/client9/stringencoders): `apt-get install libmodpbase64-dev`
- [jpeg](https://github.com/Windower/libjpeg): `apt-get install libjpeg-dev`
- [yajl](https://lloyd.github.io/yajl/): `apt-get install libyajl-dev`
- [SDL2_image](https://www.libsdl.org/projects/SDL_image/): `apt-get install libsdl2-image-dev`
- pthread: already included in libc

## Usage

### Compile
You need CMake, version 3.0 at least. On Debian jessie, you would just do:
```
apt-get install cmake
```
Make sure you have all the dependencies listed above installed. You should have the development version, as pointed out. Then, it is advisable to run CMake in a separate folder to avoid polluting the source tree with CMake's stuff.
```
mkdir build && cd build
cmake -G "Unix Makefiles" ..
```
Verify that a `Makefile` has been created, and then just run:
```
make
```
It should create two executables, namely `appbase-cctv-client` and `appbase-cctv-daemno`, and a shared library, `libappbase-common.so`. This library exports common functions used by both executables.

### Run
So the architecture is fairly simple. There are two pieces, a daemon and a client. Choosing appropriate names was tricky, since the daemon is also a client, strictly speaking.
The daemon takes pictures from the camera in raw YUYV (also known as YUY2) format, converts them to JPEG if requested, and uploads them to Appbase. The client constantly streams them down and displays them.
For this whole system to run, you should have a valid Appbase account beforehand, and have created an application.

You would first run the client:
```
Usage: ./appbase-cctv-client [OPTIONS] <app name> <username> <password>
Options:
    -d    Display debug messages
    -j    Decode images from JPEG
```
It takes as arguments, your Appbase application name, username and password. So if your app is "myapp", your username is "foo", and your password is "<PASSWORD>", you would run:
```
./appbase-cctv-client -j myapp foo bar
```
You should see a window appearing. As you could see, `-j` says the images were converted to JPEG and thus have to be decoded. This is kind of mandatory, since in my tests, raw images were >100 KB in size, and were rejected by Appbase backend. JPEG conversion greatly reduces them in size.

Now you would run the daemon in a new console, which takes similar arguments. App name, username and password should be the same.
```
Usage: ./appbase-cctv-daemon [OPTIONS] <app name> <username> <password><commit_msg>Fix typo<commit_after># appbase-cctv
A CCTV system for the IoT backed on [Appbase](https://appbase.io/).

## Dependencies
This is the list of libraries required by appbase-cctv, plus the install command for Debian (*jessie*, derivatives may also apply).
- [libcurl](https://curl.haxx.se/libcurl/c/): `apt-get install libcurl[3|4]-[gnutls|openssl|...]-dev`
- [json-c](http://json-c.github.io/json-c/): `apt-get install libjson-c-dev`
- [modpbase64](https://github.com/client9/stringencoders): `apt-get install libmodpbase64-dev`
- [jpeg](https://github.com/Windower/libjpeg): `apt-get install libjpeg-dev`
- [yajl](https://lloyd.github.io/yajl/): `apt-get install libyajl-dev`
- [SDL2_image](https://www.libsdl.org/projects/SDL_image/): `apt-get install libsdl2-image-dev`
- pthread: already included in libc

## Usage

### Compile
You need CMake, version 3.0 at least. On Debian jessie, you would just do:
```
apt-get install cmake
```
Make sure you have all the dependencies listed above installed. You should have the development version, as pointed out. Then, it is advisable to run CMake in a separate folder to avoid polluting the source tree with CMake's stuff.
```
mkdir build && cd build
cmake -G "Unix Makefiles" ..
```
Verify that a `Makefile` has been created, and then just run:
```
make
```
It should create two executables, namely `appbase-cctv-client` and `appbase-cctv-daemon`, and a shared library, `libappbase-common.so`. This library exports common functions used by both executables.

### Run
So the architecture is fairly simple. There are two pieces, a daemon and a client. Choosing appropriate names was tricky, since the daemon is also a client, strictly speaking.
The daemon takes pictures from the camera in raw YUYV (also known as YUY2) format, converts them to JPEG if requested, and uploads them to Appbase. The client constantly streams them down and displays them.
For this whole system to run, you should have a valid Appbase account beforehand, and have created an application.

You would first run the client:
```
Usage: ./appbase-cctv-client [OPTIONS] <app name> <username> <password>
Options:
    -d    Display debug messages
    -j    Decode images from JPEG
```
It takes as arguments, your Appbase application name, username and password. So if your app is "myapp", your username is "foo", and your password is "<PASSWORD>", you would run:
```
./appbase-cctv-client -j myapp foo bar
```
You should see a window appearing. As you could see, `-j` says the images were converted to JPEG and thus have to be decoded. This is kind of mandatory, since in my tests, raw images were >100 KB in size, and were rejected by Appbase backend. JPEG conversion greatly reduces them in size.

Now you would run the daemon in a new console, which takes similar arguments. App name, username and password should be the same.
```
Usage: ./appbase-cctv-daemon [OPTIONS] <app name> <username> <password>
{'fraction_non_alphanumeric': 0.1050470487596236, 'fraction_numerical': 0.0051325919589392645, 'mean_word_length': 4.333941605839416, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 12, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5225566', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1722, 'n_words': 772}