~aleteoryx/muditaos

ref: 137622902f94c96146504f241a2f825f55c04e7c muditaos/module-services/service-db/agents/settings/FactorySettings.cpp -rw-r--r-- 2.1 KiB
13762290 — Lefucjusz [MOS-766] Change factory entries initialization routine 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "FactorySettings.hpp"

#include <log/log.hpp>
#include <service-db/SettingsMessages.hpp>

namespace settings
{

    std::unique_ptr<QueryResult> FactorySettings::getMfgEntries()
    {
        auto factoryData          = std::make_unique<QueryResult>();
        const auto factoryContent = readMfgSettings(filePath);

        for (const auto &[path, value] : factoryContent.object_items()) {
            factoryData->addRow({Field(path.c_str()), Field(value.string_value().c_str())});
        }

        return factoryData;
    }

    void FactorySettings::initDb(Database *database)
    {
        const auto factoryData = getMfgEntries();

        if (factoryData->getRowCount() <= 0) {
            LOG_FATAL("No EEPROM factory data available!");
            return;
        }

        do {
            const auto key        = (*factoryData)[0].getString();
            const auto value      = (*factoryData)[1].getCString();
            const auto path       = settings::factory::entry_key + std::string("/") + key;
            const auto pathString = settings::EntryPath{"", "", "", path, settings::SettingsScope::Global}.to_string();

            if (!database->execute(settings::Statements::insertValue, pathString.c_str(), value)) {
                LOG_ERROR("Failed to set entry '%s' = '%s'!", pathString.c_str(), value);
            }
            else {
                LOG_INFO("Set entry '%s' = '%s'", pathString.c_str(), value);
            }

        } while (factoryData->nextRow());
    }

    json11::Json FactorySettings::readMfgSettings(const std::string &path)
    {
        std::ifstream file(path.c_str());
        const std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());

        std::string parserError;
        auto factoryObj = json11::Json::parse(content, parserError);

        if (!parserError.empty()) {
            LOG_FATAL("Factory data parse error: %s", parserError.c_str());
        }

        return factoryObj;
    }
} // namespace settings