|
| 1 | +//********************************************************************************// |
| 2 | +// MIT License // |
| 3 | +// // |
| 4 | +// Copyright (c) 2024 TeamViewer Germany GmbH // |
| 5 | +// // |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy // |
| 7 | +// of this software and associated documentation files (the "Software"), to deal // |
| 8 | +// in the Software without restriction, including without limitation the rights // |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // |
| 10 | +// copies of the Software, and to permit persons to whom the Software is // |
| 11 | +// furnished to do so, subject to the following conditions: // |
| 12 | +// // |
| 13 | +// The above copyright notice and this permission notice shall be included in all // |
| 14 | +// copies or substantial portions of the Software. // |
| 15 | +// // |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // |
| 22 | +// SOFTWARE. // |
| 23 | +//********************************************************************************// |
| 24 | +#include "PyAugmentRCSessionModule.h" |
| 25 | +#include <TVAgentAPI/IAugmentRCSessionModule.h> |
| 26 | +#include <TVAgentAPI/IAgentAPI.h> |
| 27 | +#include <TVAgentAPI/IAgentConnection.h> |
| 28 | + |
| 29 | +#include "PyAgentConnection.h" |
| 30 | +#include "PyTVAgentAPI.h" |
| 31 | +#include "PythonHelpers.h" |
| 32 | +#include "Typesystem.h" |
| 33 | + |
| 34 | +using namespace tvagentapipy; |
| 35 | + |
| 36 | +namespace |
| 37 | +{ |
| 38 | + |
| 39 | +// Methods |
| 40 | + |
| 41 | +PyObject* PyAugmentRCSessionModule_setCallbacks( |
| 42 | + PyAugmentRCSessionModule* self, |
| 43 | + PyObject* args, |
| 44 | + PyObject* kwargs) |
| 45 | +{ |
| 46 | + char augmentRCSessionInvitationReceivedArgName[] = "augmentRCSessionInvitationReceivedCallback"; |
| 47 | + |
| 48 | + PyObject* augmentRCSessionInvitationReceivedCallback = Py_None; |
| 49 | + |
| 50 | + char* kwargList[] = {augmentRCSessionInvitationReceivedArgName, {}}; |
| 51 | + if (!PyArg_ParseTupleAndKeywords( |
| 52 | + args, |
| 53 | + kwargs, |
| 54 | + "|O:setCallbacks", |
| 55 | + kwargList, |
| 56 | + &augmentRCSessionInvitationReceivedCallback)) |
| 57 | + { |
| 58 | + return nullptr; |
| 59 | + } |
| 60 | + |
| 61 | + augmentRCSessionInvitationReceivedCallback = augmentRCSessionInvitationReceivedCallback == Py_None ? nullptr : augmentRCSessionInvitationReceivedCallback; |
| 62 | + |
| 63 | + if (augmentRCSessionInvitationReceivedCallback && !PyCallable_Check(augmentRCSessionInvitationReceivedCallback)) |
| 64 | + { |
| 65 | + PyErr_Format(PyExc_TypeError, "%R is not callable", augmentRCSessionInvitationReceivedCallback); |
| 66 | + return nullptr; |
| 67 | + } |
| 68 | + |
| 69 | + Py_XINCREF(augmentRCSessionInvitationReceivedCallback); |
| 70 | + Py_XDECREF(self->m_pyAugmentRCSessionInvitationReceivedCallback); |
| 71 | + self->m_pyAugmentRCSessionInvitationReceivedCallback = augmentRCSessionInvitationReceivedCallback; |
| 72 | + |
| 73 | + auto augmentRCSessionInvitation = [](const char* url, void* userdata) noexcept |
| 74 | + { |
| 75 | + auto pyAugmentRCSessionInvitationReceivedCallback = static_cast<PyObject*>(userdata); |
| 76 | + |
| 77 | + PyObject* args = Py_BuildValue("(s)", url); |
| 78 | + |
| 79 | + PyObject* result = PyObject_CallObject(pyAugmentRCSessionInvitationReceivedCallback, args); |
| 80 | + Py_DECREF(args); |
| 81 | + Py_XDECREF(result); |
| 82 | + }; |
| 83 | + |
| 84 | + tvagentapi::IAugmentRCSessionModule::ReceivedInvitationCallback invitationReceivedCb{}; |
| 85 | + if (self->m_pyAugmentRCSessionInvitationReceivedCallback) |
| 86 | + { |
| 87 | + invitationReceivedCb = {augmentRCSessionInvitation, self->m_pyAugmentRCSessionInvitationReceivedCallback}; |
| 88 | + } |
| 89 | + |
| 90 | + self->m_module->setCallbacks({invitationReceivedCb}); |
| 91 | + |
| 92 | + Py_RETURN_NONE; |
| 93 | +} |
| 94 | + |
| 95 | +PyObject* PyAugmentRCSessionModule_startListening(PyAugmentRCSessionModule* self, PyObject* args) |
| 96 | +{ |
| 97 | + (void)args; |
| 98 | + return NoneOrInternalError(self->m_module->augmentRCSessionStartListening()); |
| 99 | +} |
| 100 | + |
| 101 | +PyObject* PyAugmentRCSessionModule_stopListening(PyAugmentRCSessionModule* self, PyObject* args) |
| 102 | +{ |
| 103 | + (void)args; |
| 104 | + return NoneOrInternalError(self->m_module->augmentRCSessionStopListening()); |
| 105 | +} |
| 106 | + |
| 107 | +PyObject* PyAugmentRCSessionModule_isSupported(PyAugmentRCSessionModule* self, PyObject* args) |
| 108 | +{ |
| 109 | + (void)args; |
| 110 | + if (self->m_module->isSupported()) |
| 111 | + { |
| 112 | + Py_RETURN_TRUE; |
| 113 | + } |
| 114 | + Py_RETURN_FALSE; |
| 115 | +} |
| 116 | + |
| 117 | +namespace DocStrings |
| 118 | +{ |
| 119 | + |
| 120 | +PyDoc_STRVAR(isSupported, |
| 121 | +R"__(isSupported($self) |
| 122 | +-- |
| 123 | +
|
| 124 | +Returns whether the current module is supported by the SDK and the IoT Agent counterpart. |
| 125 | +
|
| 126 | +:return True if the current module is supported, False otherwise. |
| 127 | +)__"); |
| 128 | + |
| 129 | +PyDoc_STRVAR(setCallbacks, |
| 130 | +R"__(setCallbacks($self, /, |
| 131 | + AugmentRCSessionInvitationReceived=None) |
| 132 | +-- |
| 133 | +
|
| 134 | +Sets callbacks to handle various events like chat room and message updates. |
| 135 | +
|
| 136 | +:param AugmentRCSessionInvitationReceivedCallback: called when an invitation to an Augment RC Session is received |
| 137 | +:type AugmentRCSessionInvitationReceivedCallback: callback(str url) |
| 138 | +)__"); |
| 139 | + |
| 140 | +PyDoc_STRVAR(startListening, |
| 141 | +R"__(startListening($self) |
| 142 | +-- |
| 143 | +
|
| 144 | +Announces that the module starts listening for AugmentRCSession invitations. |
| 145 | +)__"); |
| 146 | + |
| 147 | +PyDoc_STRVAR(stopListening, |
| 148 | +R"__(stopListening($self) |
| 149 | +-- |
| 150 | +
|
| 151 | +Announces that the module stops listening for AugmentRCSession invitations. |
| 152 | +)__"); |
| 153 | + |
| 154 | +} // namespace DocStrings |
| 155 | + |
| 156 | +PyMethodDef PyAugmentRCSessionModule_methods[] = |
| 157 | +{ |
| 158 | + { |
| 159 | + "isSupported", |
| 160 | + WeakConnectionCall<PyAugmentRCSessionModule, PyAugmentRCSessionModule_isSupported>, |
| 161 | + METH_NOARGS, |
| 162 | + DocStrings::isSupported |
| 163 | + }, |
| 164 | + { |
| 165 | + "setCallbacks", |
| 166 | + PyCFunctionCast(WeakConnectionCall<PyAugmentRCSessionModule, PyAugmentRCSessionModule_setCallbacks>), |
| 167 | + METH_VARARGS | METH_KEYWORDS, |
| 168 | + DocStrings::setCallbacks |
| 169 | + }, |
| 170 | + { |
| 171 | + "startListening", |
| 172 | + WeakConnectionCall<PyAugmentRCSessionModule, PyAugmentRCSessionModule_startListening>, |
| 173 | + METH_NOARGS, |
| 174 | + DocStrings::startListening |
| 175 | + }, |
| 176 | + |
| 177 | + { |
| 178 | + "stopListening", |
| 179 | + WeakConnectionCall<PyAugmentRCSessionModule, PyAugmentRCSessionModule_stopListening>, |
| 180 | + METH_NOARGS, |
| 181 | + DocStrings::stopListening |
| 182 | + }, |
| 183 | + |
| 184 | + {} // Sentinel |
| 185 | +}; |
| 186 | + |
| 187 | +} // namespace |
| 188 | + |
| 189 | +namespace tvagentapipy |
| 190 | +{ |
| 191 | + |
| 192 | +PyAugmentRCSessionModule::PyAugmentRCSessionModule(PyAgentConnection* pyAgentConnection) |
| 193 | + : m_pyWeakAgentConnection{reinterpret_cast<PyObject*>(pyAgentConnection)} |
| 194 | +{ |
| 195 | + m_module = static_cast<tvagentapi::IAugmentRCSessionModule*>( |
| 196 | + pyAgentConnection->m_connection->getModule( |
| 197 | + tvagentapi::IModule::Type::AugmentRCSession |
| 198 | + )); |
| 199 | +} |
| 200 | + |
| 201 | +PyAugmentRCSessionModule::~PyAugmentRCSessionModule() |
| 202 | +{ |
| 203 | + Py_XDECREF(m_pyAugmentRCSessionInvitationReceivedCallback); |
| 204 | +} |
| 205 | + |
| 206 | +bool PyAugmentRCSessionModule::IsReady() const |
| 207 | +{ |
| 208 | + return !!m_module; |
| 209 | +} |
| 210 | + |
| 211 | +PyTypeObject* GetPyTypeAugmentRCSessionModule() |
| 212 | +{ |
| 213 | + static PyTypeObject* pyAugmentRCSessionModuleType = []() -> PyTypeObject* |
| 214 | + { |
| 215 | + static PyTypeObject result = PyTypeObjectInitialized(); |
| 216 | + result.tp_name = "tvagentapi.AugmentRCSessionModule"; |
| 217 | + result.tp_basicsize = sizeof(PyAugmentRCSessionModule); |
| 218 | + result.tp_dealloc = reinterpret_cast<destructor>(DeallocWrapperObject<PyAugmentRCSessionModule>); |
| 219 | + result.tp_flags = Py_TPFLAGS_DEFAULT; |
| 220 | + result.tp_doc = "AugmentRCSession module class"; |
| 221 | + result.tp_methods = PyAugmentRCSessionModule_methods; |
| 222 | + |
| 223 | + if (PyType_Ready(&result) < 0) |
| 224 | + { |
| 225 | + return nullptr; |
| 226 | + } |
| 227 | + |
| 228 | + return &result; |
| 229 | + }(); |
| 230 | + return pyAugmentRCSessionModuleType; |
| 231 | +} |
| 232 | + |
| 233 | +} // namespace tvagentapipy |
0 commit comments