Device.cpp 4.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * Media Library
 *****************************************************************************
 * Copyright (C) 2015 Hugo Beauzée-Luyssen, Videolabs
 *
 * Authors: Hugo Beauzée-Luyssen<hugo@beauzee.fr>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

23 24 25 26
#if HAVE_CONFIG_H
# include "config.h"
#endif

27
#include "Device.h"
28

29 30 31
namespace medialibrary
{

32 33 34
const std::string Device::Table::Name = "Device";
const std::string Device::Table::PrimaryKeyColumn = "id_device";
int64_t Device::* const Device::Table::PrimaryKey = &Device::m_id;
35

36
Device::Device( MediaLibraryPtr ml, sqlite::Row& row )
37
    : m_ml( ml )
38 39 40 41 42 43
    , m_id( row.extract<decltype(m_id)>() )
    , m_uuid( row.extract<decltype(m_uuid)>() )
    , m_scheme( row.extract<decltype(m_scheme)>() )
    , m_isRemovable( row.extract<decltype(m_isRemovable)>() )
    , m_isPresent( row.extract<decltype(m_isPresent)>() )
    , m_lastSeen( row.extract<decltype(m_lastSeen)>() )
44 45 46
{
}

47 48
Device::Device( MediaLibraryPtr ml, const std::string& uuid, const std::string& scheme,
                bool isRemovable, time_t lastSeen )
49
    : m_ml( ml )
50
    , m_id( 0 )
51
    , m_uuid( uuid )
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
52
    , m_scheme( scheme )
53
    , m_isRemovable( isRemovable )
54
    // Assume we can't add an absent device
55
    , m_isPresent( true )
56
    , m_lastSeen( lastSeen )
57 58 59
{
}

60
int64_t Device::id() const
61 62 63 64
{
    return m_id;
}

65
const std::string&Device::uuid() const
66 67 68 69
{
    return m_uuid;
}

70
bool Device::isRemovable() const
71 72 73 74
{
    return m_isRemovable;
}

75
bool Device::isPresent() const
76 77 78 79
{
    return m_isPresent;
}

80
void Device::setPresent(bool value)
81
{
82
    assert( m_isPresent != value );
83
    static const std::string req = "UPDATE " + Device::Table::Name +
84
            " SET is_present = ? WHERE id_device = ?";
85
    if ( sqlite::Tools::executeUpdate( m_ml->getConn(), req, value, m_id ) == false )
86 87 88 89
        return;
    m_isPresent = value;
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
90 91 92 93 94
const std::string& Device::scheme() const
{
    return m_scheme;
}

95 96 97 98 99 100 101
time_t Device::lastSeen() const
{
    return m_lastSeen;
}

void Device::updateLastSeen()
{
102
    const std::string req = "UPDATE " + Device::Table::Name + " SET "
103 104 105 106 107 108 109
            "last_seen = ? WHERE id_device = ?";
    auto lastSeen = std::chrono::duration_cast<std::chrono::seconds>(
        std::chrono::system_clock::now().time_since_epoch()
    ).count();
    sqlite::Tools::executeUpdate( m_ml->getConn(), req, lastSeen, m_id );
}

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
110
std::shared_ptr<Device> Device::create( MediaLibraryPtr ml, const std::string& uuid, const std::string& scheme, bool isRemovable )
111
{
112
    static const std::string req = "INSERT INTO " + Device::Table::Name
113 114 115 116 117 118
            + "(uuid, scheme, is_removable, is_present, last_seen) VALUES(?, ?, ?, ?, ?)";
    auto lastSeen = std::chrono::duration_cast<std::chrono::seconds>(
        std::chrono::system_clock::now().time_since_epoch()
    ).count();
    auto self = std::make_shared<Device>( ml, uuid, scheme, isRemovable, lastSeen );
    if ( insert( ml, self, req, uuid, scheme, isRemovable, self->isPresent(), lastSeen ) == false )
119 120 121 122
        return nullptr;
    return self;
}

123
void Device::createTable( sqlite::Connection* connection )
124
{
125 126 127 128 129
    const std::string reqs[] = {
        #include "database/tables/Device_v14.sql"
    };
    for ( const auto& req : reqs )
        sqlite::Tools::executeRequest( connection, req );
130 131
}

132
std::shared_ptr<Device> Device::fromUuid( MediaLibraryPtr ml, const std::string& uuid )
133
{
134
    static const std::string req = "SELECT * FROM " + Device::Table::Name +
135
            " WHERE uuid = ?";
136
    return fetch( ml, req, uuid );
137 138
}

139 140
void Device::removeOldDevices( MediaLibraryPtr ml, std::chrono::seconds maxLifeTime )
{
141
    static const std::string req = "DELETE FROM " + Device::Table::Name + " "
142 143 144 145 146 147
            "WHERE last_seen < ?";
    auto deadline = std::chrono::duration_cast<std::chrono::seconds>(
                (std::chrono::system_clock::now() - maxLifeTime).time_since_epoch() );
    sqlite::Tools::executeDelete( ml->getConn(), req, deadline.count() );
}

148
}