"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/server/core/mobile.cpp" (23 Feb 2021, 11114 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "mobile.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.6.300_vs_3.7.95.

    1 /*
    2 ** NetXMS - Network Management System
    3 ** Copyright (C) 2003-2020 Victor Kirhenshtein
    4 **
    5 ** This program is free software; you can redistribute it and/or modify
    6 ** it under the terms of the GNU General Public License as published by
    7 ** the Free Software Foundation; either version 2 of the License, or
    8 ** (at your option) any later version.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 ** GNU General Public License for more details.
   14 **
   15 ** You should have received a copy of the GNU General Public License
   16 ** along with this program; if not, write to the Free Software
   17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18 **
   19 ** File: mobile.cpp
   20 **/
   21 
   22 #include "nxcore.h"
   23 
   24 /**
   25  * Default constructor
   26  */
   27 MobileDevice::MobileDevice() : super()
   28 {
   29    m_commProtocol[0] = 0;
   30     m_lastReportTime = 0;
   31     m_batteryLevel = -1;
   32     m_speed = -1;
   33     m_direction = -1;
   34     m_altitude = 0;
   35 }
   36 
   37 /**
   38  * Constructor for creating new mobile device object
   39  */
   40 MobileDevice::MobileDevice(const TCHAR *name, const TCHAR *deviceId) : super(name), m_deviceId(deviceId)
   41 {
   42    m_commProtocol[0] = 0;
   43     m_lastReportTime = 0;
   44     m_batteryLevel = -1;
   45    m_speed = -1;
   46    m_direction = -1;
   47    m_altitude = 0;
   48 }
   49 
   50 /**
   51  * Destructor
   52  */
   53 MobileDevice::~MobileDevice()
   54 {
   55 }
   56 
   57 /**
   58  * Create object from database data
   59  */
   60 bool MobileDevice::loadFromDatabase(DB_HANDLE hdb, UINT32 dwId)
   61 {
   62    m_id = dwId;
   63 
   64    if (!loadCommonProperties(hdb) || !super::loadFromDatabase(hdb, dwId))
   65    {
   66       DbgPrintf(2, _T("Cannot load common properties for mobile device object %d"), dwId);
   67       return false;
   68    }
   69 
   70    DB_STATEMENT hStmt = DBPrepare(hdb, _T("SELECT device_id,vendor,model,serial_number,os_name,os_version,user_id,battery_level,comm_protocol,speed,direction,altitude FROM mobile_devices WHERE id=?"));
   71    if (hStmt == nullptr)
   72       return false;
   73 
   74    DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, m_id);
   75     DB_RESULT hResult = DBSelectPrepared(hStmt);
   76     if (hResult == nullptr)
   77     {
   78        DBFreeStatement(hStmt);
   79         return false;
   80     }
   81 
   82     m_deviceId = DBGetFieldAsSharedString(hResult, 0, 0);
   83     m_vendor = DBGetFieldAsSharedString(hResult, 0, 1);
   84     m_model = DBGetFieldAsSharedString(hResult, 0, 2);
   85     m_serialNumber = DBGetFieldAsSharedString(hResult, 0, 3);
   86     m_osName = DBGetFieldAsSharedString(hResult, 0, 4);
   87     m_osVersion = DBGetFieldAsSharedString(hResult, 0, 5);
   88     m_userId = DBGetFieldAsSharedString(hResult, 0, 6);
   89     m_batteryLevel = static_cast<int8_t>(DBGetFieldLong(hResult, 0, 7));
   90     DBGetField(hResult, 0, 8, m_commProtocol, 32);
   91    m_speed = static_cast<float>(DBGetFieldDouble(hResult, 0, 9));
   92    m_direction = static_cast<int16_t>(DBGetFieldLong(hResult, 0, 10));
   93    m_altitude = DBGetFieldLong(hResult, 0, 11);
   94 
   95     DBFreeResult(hResult);
   96    DBFreeStatement(hStmt);
   97 
   98    // Load DCI and access list
   99    loadACLFromDB(hdb);
  100    loadItemsFromDB(hdb);
  101    for(int i = 0; i < m_dcObjects->size(); i++)
  102       if (!m_dcObjects->get(i)->loadThresholdsFromDB(hdb))
  103          return false;
  104    loadDCIListForCleanup(hdb);
  105 
  106    return true;
  107 }
  108 
  109 /**
  110  * Save object to database
  111  */
  112 bool MobileDevice::saveToDatabase(DB_HANDLE hdb)
  113 {
  114    bool success = super::saveToDatabase(hdb);
  115    if (success && (m_modified & MODIFY_OTHER))
  116    {
  117       static const TCHAR *columns[] = {
  118          _T("device_id") ,_T("vendor"), _T("model"), _T("serial_number"), _T("os_name"), _T("os_version"), _T("user_id"), _T("battery_level"),
  119          _T("comm_protocol"), _T("speed"), _T("direction"), _T("altitude"), nullptr
  120       };
  121       DB_STATEMENT hStmt = DBPrepareMerge(hdb, _T("mobile_devices"), _T("id"), m_id, columns);
  122       if (hStmt != nullptr)
  123       {
  124          lockProperties();
  125 
  126          DBBind(hStmt, 1, DB_SQLTYPE_VARCHAR, m_deviceId, DB_BIND_STATIC);
  127          DBBind(hStmt, 2, DB_SQLTYPE_VARCHAR, m_vendor, DB_BIND_STATIC);
  128          DBBind(hStmt, 3, DB_SQLTYPE_VARCHAR, m_model, DB_BIND_STATIC);
  129          DBBind(hStmt, 4, DB_SQLTYPE_VARCHAR, m_serialNumber, DB_BIND_STATIC);
  130          DBBind(hStmt, 5, DB_SQLTYPE_VARCHAR, m_osName, DB_BIND_STATIC);
  131          DBBind(hStmt, 6, DB_SQLTYPE_VARCHAR, m_osVersion, DB_BIND_STATIC);
  132          DBBind(hStmt, 7, DB_SQLTYPE_VARCHAR, m_userId, DB_BIND_STATIC);
  133          DBBind(hStmt, 8, DB_SQLTYPE_INTEGER, m_batteryLevel);
  134          DBBind(hStmt, 9, DB_SQLTYPE_VARCHAR, m_commProtocol, DB_BIND_STATIC);
  135          DBBind(hStmt, 10, DB_SQLTYPE_DOUBLE, m_speed);
  136          DBBind(hStmt, 11, DB_SQLTYPE_INTEGER, m_direction);
  137          DBBind(hStmt, 12, DB_SQLTYPE_INTEGER, m_altitude);
  138          DBBind(hStmt, 13, DB_SQLTYPE_INTEGER, m_id);
  139 
  140          success = DBExecute(hStmt);
  141 
  142          unlockProperties();
  143 
  144          DBFreeStatement(hStmt);
  145       }
  146       else
  147       {
  148          success = false;
  149       }
  150    }
  151    return success;
  152 }
  153 
  154 /**
  155  * Delete object from database
  156  */
  157 bool MobileDevice::deleteFromDatabase(DB_HANDLE hdb)
  158 {
  159    bool success = super::deleteFromDatabase(hdb);
  160    if (success)
  161       success = executeQueryOnObject(hdb, _T("DELETE FROM mobile_devices WHERE id=?"));
  162    return success;
  163 }
  164 
  165 /**
  166  * Create CSCP message with object's data
  167  */
  168 void MobileDevice::fillMessageInternal(NXCPMessage *msg, UINT32 userId)
  169 {
  170    super::fillMessageInternal(msg, userId);
  171 
  172    msg->setField(VID_COMM_PROTOCOL, m_commProtocol);
  173     msg->setField(VID_DEVICE_ID, m_deviceId);
  174     msg->setField(VID_VENDOR, m_vendor);
  175     msg->setField(VID_MODEL, m_model);
  176     msg->setField(VID_SERIAL_NUMBER, m_serialNumber);
  177     msg->setField(VID_OS_NAME, m_osName);
  178     msg->setField(VID_OS_VERSION, m_osVersion);
  179     msg->setField(VID_USER_ID, m_userId);
  180     msg->setField(VID_BATTERY_LEVEL, static_cast<int16_t>(m_batteryLevel));
  181     msg->setFieldFromTime(VID_LAST_CHANGE_TIME, m_lastReportTime);
  182    msg->setField(VID_SPEED, m_speed);
  183    msg->setField(VID_DIRECTION, m_direction);
  184    msg->setField(VID_ALTITUDE, m_altitude);
  185 }
  186 
  187 /**
  188  * Modify object from message
  189  */
  190 UINT32 MobileDevice::modifyFromMessageInternal(NXCPMessage *pRequest)
  191 {
  192    return super::modifyFromMessageInternal(pRequest);
  193 }
  194 
  195 /**
  196  * Update system information from NXCP message
  197  */
  198 void MobileDevice::updateSystemInfo(const MobileDeviceInfo& deviceInfo)
  199 {
  200     lockProperties();
  201 
  202     m_lastReportTime = time(nullptr);
  203    _tcslcpy(m_commProtocol, deviceInfo.commProtocol, MAX_OBJECT_NAME);
  204     m_vendor = deviceInfo.vendor;
  205     m_model = deviceInfo.model;
  206     m_serialNumber = deviceInfo.serialNumber;
  207     m_osName = deviceInfo.osName;
  208     m_osVersion = deviceInfo.osVersion;
  209     m_userId = deviceInfo.userId;
  210 
  211     setModified(MODIFY_OTHER | MODIFY_COMMON_PROPERTIES);
  212     unlockProperties();
  213 }
  214 
  215 /**
  216  * Update status from NXCP message
  217  */
  218 void MobileDevice::updateStatus(const MobileDeviceStatus& status)
  219 {
  220     lockProperties();
  221 
  222     m_lastReportTime = time(nullptr);
  223     _tcslcpy(m_commProtocol, status.commProtocol, MAX_OBJECT_NAME);
  224     m_batteryLevel = status.batteryLevel;
  225    m_speed = status.speed;
  226    m_direction = status.direction;
  227     if (status.geoLocation.isValid())
  228     {
  229       m_altitude = status.altitude;
  230        updateGeoLocation(status.geoLocation);
  231    }
  232     if (!m_ipAddress.equals(status.ipAddress))
  233     {
  234        m_ipAddress = status.ipAddress;
  235       setModified(MODIFY_COMMON_PROPERTIES);
  236     }
  237 
  238     TCHAR temp[64];
  239     DbgPrintf(5, _T("Mobile device %s [%u] status update (battery=%d addr=%s loc=[%s %s])"),
  240               m_name, m_id, m_batteryLevel, m_ipAddress.toString(temp),
  241                  m_geoLocation.getLatitudeAsString(), m_geoLocation.getLongitudeAsString());
  242 
  243     setModified(MODIFY_OTHER);
  244     unlockProperties();
  245 }
  246 
  247 /**
  248  * Get value for server's internal parameter
  249  */
  250 DataCollectionError MobileDevice::getInternalMetric(const TCHAR *name, TCHAR *buffer, size_t size)
  251 {
  252    DataCollectionError rc = super::getInternalMetric(name, buffer, size);
  253     if (rc != DCE_NOT_SUPPORTED)
  254         return rc;
  255     rc = DCE_SUCCESS;
  256 
  257    if (!_tcsicmp(name, _T("MobileDevice.Altitude")))
  258    {
  259       _sntprintf(buffer, size, _T("%d"), m_altitude);
  260    }
  261    else if (!_tcsicmp(name, _T("MobileDevice.BatteryLevel")))
  262    {
  263       _sntprintf(buffer, size, _T("%d"), static_cast<int32_t>(m_batteryLevel));
  264    }
  265    else if (!_tcsicmp(name, _T("MobileDevice.CommProtocol")))
  266    {
  267       _tcslcpy(buffer, m_commProtocol, size);
  268    }
  269    else if (!_tcsicmp(name, _T("MobileDevice.DeviceId")))
  270    {
  271         _tcslcpy(buffer, m_deviceId, size);
  272    }
  273    else if (!_tcsicmp(name, _T("MobileDevice.Direction")))
  274    {
  275       _sntprintf(buffer, size, _T("%d"), static_cast<int32_t>(m_direction));
  276    }
  277    else if (!_tcsicmp(name, _T("MobileDevice.LastReportTime")))
  278    {
  279         _sntprintf(buffer, size, INT64_FMT, static_cast<int64_t>(m_lastReportTime));
  280    }
  281    else if (!_tcsicmp(name, _T("MobileDevice.Model")))
  282    {
  283         _tcslcpy(buffer, m_model, size);
  284    }
  285    else if (!_tcsicmp(name, _T("MobileDevice.OS.Name")))
  286    {
  287         _tcslcpy(buffer, m_osName, size);
  288    }
  289    else if (!_tcsicmp(name, _T("MobileDevice.OS.Version")))
  290    {
  291         _tcslcpy(buffer, m_osVersion, size);
  292    }
  293    else if (!_tcsicmp(name, _T("MobileDevice.SerialNumber")))
  294    {
  295         _tcslcpy(buffer, m_serialNumber, size);
  296    }
  297    else if (!_tcsicmp(name, _T("MobileDevice.Speed")))
  298    {
  299       _sntprintf(buffer, size, _T("%f"), m_speed);
  300    }
  301    else if (!_tcsicmp(name, _T("MobileDevice.Vendor")))
  302    {
  303         _tcslcpy(buffer, m_vendor, size);
  304    }
  305    else if (!_tcsicmp(name, _T("MobileDevice.UserId")))
  306    {
  307         _tcslcpy(buffer, m_userId, size);
  308    }
  309    else
  310    {
  311       rc = DCE_NOT_SUPPORTED;
  312    }
  313 
  314    return rc;
  315 }
  316 
  317 /**
  318  * Calculate compound status
  319  */
  320 void MobileDevice::calculateCompoundStatus(BOOL bForcedRecalc)
  321 {
  322    NetObj::calculateCompoundStatus(bForcedRecalc);
  323 
  324    // Assume normal status by default for mobile device
  325    if (m_status == STATUS_UNKNOWN)
  326    {
  327       lockProperties();
  328       m_status = STATUS_NORMAL;
  329       setModified(MODIFY_RUNTIME);
  330       unlockProperties();
  331    }
  332 }
  333 
  334 /**
  335  * Create NXSL object for this object
  336  */
  337 NXSL_Value *MobileDevice::createNXSLObject(NXSL_VM *vm) const
  338 {
  339    return vm->createValue(new NXSL_Object(vm, &g_nxslMobileDeviceClass, new shared_ptr<MobileDevice>(self())));
  340 }
  341 
  342 /**
  343  * Serialize object to JSON
  344  */
  345 json_t *MobileDevice::toJson()
  346 {
  347    json_t *root = super::toJson();
  348 
  349    lockProperties();
  350    json_object_set_new(root, "altitude", json_integer(m_altitude));
  351    json_object_set_new(root, "batteryLevel", json_integer(m_batteryLevel));
  352    json_object_set_new(root, "commProtocol", json_string_t(m_commProtocol));
  353    json_object_set_new(root, "deviceId", json_string_t(m_deviceId));
  354    json_object_set_new(root, "direction", json_integer(m_direction));
  355    json_object_set_new(root, "ipAddress", m_ipAddress.toJson());
  356    json_object_set_new(root, "lastReportTime", json_integer(m_lastReportTime));
  357    json_object_set_new(root, "model", json_string_t(m_model));
  358    json_object_set_new(root, "osName", json_string_t(m_osName));
  359    json_object_set_new(root, "osVersion", json_string_t(m_osVersion));
  360    json_object_set_new(root, "serialNumber", json_string_t(m_serialNumber));
  361    json_object_set_new(root, "speed", json_real(m_speed));
  362    json_object_set_new(root, "userId", json_string_t(m_userId));
  363    json_object_set_new(root, "vendor", json_string_t(m_vendor));
  364    unlockProperties();
  365 
  366    return root;
  367 }