Web-Interface for Motion

    After updating the video server under debian, it was decided to remake the video surveillance system.
    It was decided to leave Motion, but there was a need for a more human interface for viewing archived records.
    Regular searches on the Internet did not yield any acceptable results, as a result of which it was decided to create their own product.
    After some hesitation, the choice fell on Rails. No religion, I just wanted to learn more about this framework and a wonderful programming language. PostgreSQL is used as a DBMS.
    The result of working under the hood ...

    First, a little more in detail about the motion setup.
    Due to the fact that I wanted, if possible, to get along with pure HTML5, I had to reinstall motion manually, including support for writing files to ogg. Fortunately, the authors of this program implemented it, for which many thanks to them. The assembly and installation process is described well on the project page, so I will not paint it here, especially since it will differ for different distributions. Link to the home page www.lavrsen.dk/foswiki/bin/view/Motion/MotionGuideInstallation .
    I will focus only on product setup.
    Since motion was originally installed from packages, after reinstallation, I did not transfer the configuration folder from / etc to / usr / local / etc.
    And one more point, motion itself is launched using runit, so in the config it is disabled in daemon mode.
    The alignment of forces is as follows:
    1. The motion configs are in / etc / motion.
    2. The video is written to a separate hard drive mounted in the / video directory, in the folder with the camera names.
    3. Records are stored in the database, which stores information about the time of the event, the full path to the event file, and the type of file (in my case, video).
    Table structure 4. For live viewing (in real time) from cameras, the motion interface is used. The main changes in the config are as follows: /etc/motion.conf ffmpeg_video_codec ogg webcontrol_port 8080 webcontrol_localhost off (if the web-interface will be launched on another server) webcontrol_html_output on webcontrol_authentication login: pass
    CREATE TABLE records
    (
    id serial NOT NULL,
    thread integer,
    filename character varying(255),
    frame integer,
    file_type integer,
    event_timestamp timestamp without time zone,
    created_at timestamp without time zone NOT NULL,
    updated_at timestamp without time zone NOT NULL,
    CONSTRAINT records_pkey PRIMARY KEY (id )
    )
    WITH (
    OIDS=FALSE
    );
    ALTER TABLE records
    OWNER TO motion;

    -- Index: thread

    -- DROP INDEX thread;

    CREATE INDEX thread
    ON records
    USING btree
    (thread );












    sql_query insert into records (thread, filename, frame, file_type, event_timestamp, created_at, updated_at) values ​​('% t', '% f', '% q', '% n', '% Y-% m-% d % T ', NOW (), NOW ())

    And accordingly the settings for connecting to the database.

    Next we connect cameras
    thread /etc/motion/thread1.conf
    thread /etc/motion/thread2.conf
    ...
    thread /etc/motion/threadN.conf,
    where N depends on the number of our cameras.

    The main points in threadX.conf, where X is any number of
    stream_port PortNumber - this port will need to be written in the "Streaming port" field when configuring cameras in the web-interface.
    These are the main changes when setting up motion. I will not describe how to configure motion in this article.
    Rails setup is well described in the article habrahabr.ru/post/140910 . You may also need to install NodeJS - github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

    Now we will go directly to the configuration of the interface:
    1. Clone the site using git.
    Bitbucket:
    git clone webdev4u@bitbucket.org/webdev4u/motion_web.git
    Github:
    git clone github.com/webdev4u/motion_web.git
    2. Rename config / settings.local.yml to config / settings.yml and enter the server address there on which motion is running.
    3. Rename config / database.yml.example to config / database.yml and enter the settings for your database there.
    4. Change the data in db / seeds.rb for the admin user.
    5. rake db: migrate
    6. rake db: seed
    7. You can run rails s for verification. The server will listen on port 3000. If everything is fine, you can work.
    8. And finally, set up the task for the crown to clean the base. By default, records are stored for 21 days, but you can change this parameter in the file app / models / record.rb line 12, but it is better in lib / tasks / crontask.rake the line
    Record.clean_old_records
    to replace
    Record.clean_old_records Required_number_days.
    Then
    run the whenever --update-crontab command from under the user on whose behalf the site will work.

    Screenshot of the main page:

    Login page:

    Live view:

    List of cameras:

    View archive:

    Add user:

    Add camera:

    Also popular now: