File: connectionhttp.cpp

package info (click to toggle)
indi-starbook-ten 0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 720 kB
  • sloc: cpp: 7,297; makefile: 2
file content (128 lines) | stat: -rw-r--r-- 3,180 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*******************************************************************************
  Copyright(c) 2017 Jasem Mutlaq. All rights reserved.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public
 License version 2 as published by the Free Software Foundation.

 This library 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
 Library General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with this library; see the file COPYING.LIB.  If not, write to
 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.
*******************************************************************************/

#include "connectionhttp.h"

#include "indilogger.h"
#include "indistandardproperty.h"

#include <cstring>
#include <unistd.h>

namespace Connection
{
extern const char *CONNECTION_TAB;

HTTP::HTTP(INDI::DefaultDevice *dev) : Interface(dev, CONNECTION_CUSTOM)
{
    char defaultHostname[MAXINDINAME] = {0};

    IUGetConfigText(dev->getDeviceName(), AddressTP.name, "BASE_URL", defaultHostname, MAXINDINAME);

    // Address/Port
    IUFillText(&AddressT[0], "BASE_URL", "Address", defaultHostname);
    IUFillTextVector(&AddressTP, AddressT, 1, getDeviceName(), "DEVICE_BASE_URL", "Base URL", CONNECTION_TAB,
                     IP_RW, 60, IPS_IDLE);
}

bool HTTP::ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
{
    if (!strcmp(dev, m_Device->getDeviceName()))
    {
        // HTTP Server settings
        if (!strcmp(name, AddressTP.name))
        {
            IUUpdateText(&AddressTP, texts, names, n);
            AddressTP.s = IPS_OK;
            IDSetText(&AddressTP, nullptr);
            return true;
        }
    }

    return false;
}

bool HTTP::Connect()
{
    if (AddressT[0].text == nullptr || AddressT[0].text[0] == '\0')
    {
        LOG_ERROR("Error! Server address is missing or invalid.");
        return false;
    }

    const char *baseurl = AddressT[0].text;

    LOGF_INFO("Connecting to %s ...", baseurl);

#if 0
    if (m_Device->isSimulation() == false)
    {
        client = new httplib::Client(baseurl);
    }
#else
        client = new httplib::Client(baseurl);
#endif

    bool rc = Handshake();

    if (rc)
    {
        LOGF_INFO("%s is online.", getDeviceName());
#if 0
        m_Device->saveConfig(true, "DEVICE_BASE_URL");
#endif
    }
    else
        LOG_DEBUG("Handshake failed.");

    return rc;
}

bool HTTP::Disconnect()
{
    delete client;
    client = nullptr;
    return true;
}

void HTTP::Activated()
{
    m_Device->defineProperty(&AddressTP);
#if 0
    m_Device->loadConfig(true, "DEVICE_BASE_URL");
#endif
}

void HTTP::Deactivated()
{
    m_Device->deleteProperty(AddressTP.name);
}

bool HTTP::saveConfigItems(FILE *fp)
{
    IUSaveConfigText(fp, &AddressTP);

    return true;
}

void HTTP::setDefaultHost(const char *addressHost)
{
    IUSaveText(&AddressT[0], addressHost);
}
}