implemented support for lists and tables in external subagents
[public/netxms.git] / src / server / core / mobile.cpp
CommitLineData
f22c4f7c
VK
1/*
2** NetXMS - Network Management System
3** Copyright (C) 2003-2012 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
f22c4f7c
VK
20**/
21
22#include "nxcore.h"
23
24/**
25 * Default constructor
26 */
6fd6de0a 27MobileDevice::MobileDevice() : DataCollectionTarget()
f22c4f7c
VK
28{
29 m_lastReportTime = 0;
30 m_deviceId = NULL;
31 m_vendor = NULL;
32 m_model = NULL;
33 m_serialNumber = NULL;
34 m_osName = NULL;
35 m_osVersion = NULL;
36 m_userId = NULL;
171c2fd6 37 m_batteryLevel = -1;
f22c4f7c
VK
38}
39
40/**
41 * Constructor for creating new mobile device object
42 */
6fd6de0a 43MobileDevice::MobileDevice(const TCHAR *name, const TCHAR *deviceId) : DataCollectionTarget(name)
f22c4f7c
VK
44{
45 m_lastReportTime = 0;
46 m_deviceId = _tcsdup(deviceId);
47 m_vendor = NULL;
48 m_model = NULL;
49 m_serialNumber = NULL;
50 m_osName = NULL;
51 m_osVersion = NULL;
52 m_userId = NULL;
171c2fd6 53 m_batteryLevel = -1;
f22c4f7c
VK
54}
55
56/**
57 * Destructor
58 */
59MobileDevice::~MobileDevice()
60{
61 safe_free(m_deviceId);
62 safe_free(m_vendor);
63 safe_free(m_model);
64 safe_free(m_serialNumber);
65 safe_free(m_osName);
66 safe_free(m_osVersion);
67 safe_free(m_userId);
68}
9aa67910
VK
69
70/**
71 * Create object from database data
72 */
73BOOL MobileDevice::CreateFromDB(DWORD dwId)
74{
75 m_dwId = dwId;
76
77 if (!loadCommonProperties())
78 {
79 DbgPrintf(2, _T("Cannot load common properties for cluster object %d"), dwId);
80 return FALSE;
81 }
82
83 TCHAR query[256];
171c2fd6 84 _sntprintf(query, 256, _T("SELECT device_id,vendor,model,serial_number,os_name,os_version,user_id,battery_level FROM mobile_devices WHERE id=%d"), (int)m_dwId);
9aa67910
VK
85 DB_RESULT hResult = DBSelect(g_hCoreDB, query);
86 if (hResult == NULL)
87 return FALSE;
88
89 m_deviceId = DBGetField(hResult, 0, 0, NULL, 0);
90 m_vendor = DBGetField(hResult, 0, 1, NULL, 0);
91 m_model = DBGetField(hResult, 0, 2, NULL, 0);
92 m_serialNumber = DBGetField(hResult, 0, 3, NULL, 0);
93 m_osName = DBGetField(hResult, 0, 4, NULL, 0);
94 m_osVersion = DBGetField(hResult, 0, 5, NULL, 0);
95 m_userId = DBGetField(hResult, 0, 6, NULL, 0);
171c2fd6 96 m_batteryLevel = DBGetFieldLong(hResult, 0, 7);
9aa67910
VK
97 DBFreeResult(hResult);
98
99 // Load DCI and access list
100 loadACLFromDB();
101 loadItemsFromDB();
102 for(int i = 0; i < m_dcObjects->size(); i++)
103 if (!m_dcObjects->get(i)->loadThresholdsFromDB())
104 return FALSE;
105
106 return TRUE;
107}
108
109/**
110 * Save object to database
111 */
112BOOL MobileDevice::SaveToDB(DB_HANDLE hdb)
113{
114 // Lock object's access
115 LockData();
116
117 saveCommonProperties(hdb);
118
119 BOOL bResult;
120 DB_STATEMENT hStmt;
121 if (IsDatabaseRecordExist(hdb, _T("mobile_devices"), _T("id"), m_dwId))
171c2fd6 122 hStmt = DBPrepare(hdb, _T("UPDATE mobile_devices SET device_id=?,vendor=?,model=?,serial_number=?,os_name=?,os_version=?,user_id=?,battery_level=? WHERE id=?"));
9aa67910 123 else
171c2fd6 124 hStmt = DBPrepare(hdb, _T("INSERT INTO mobile_devices (device_id,vendor,model,serial_number,os_name,os_version,user_id,battery_level,id) VALUES (?,?,?,?,?,?,?,?,?)"));
9aa67910
VK
125 if (hStmt != NULL)
126 {
127 DBBind(hStmt, 1, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_deviceId), DB_BIND_STATIC);
128 DBBind(hStmt, 2, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_vendor), DB_BIND_STATIC);
129 DBBind(hStmt, 3, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_model), DB_BIND_STATIC);
130 DBBind(hStmt, 4, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_serialNumber), DB_BIND_STATIC);
131 DBBind(hStmt, 5, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_osName), DB_BIND_STATIC);
132 DBBind(hStmt, 6, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_osVersion), DB_BIND_STATIC);
133 DBBind(hStmt, 7, DB_SQLTYPE_VARCHAR, CHECK_NULL_EX(m_userId), DB_BIND_STATIC);
171c2fd6
VK
134 DBBind(hStmt, 8, DB_SQLTYPE_INTEGER, m_batteryLevel);
135 DBBind(hStmt, 9, DB_SQLTYPE_INTEGER, m_dwId);
9aa67910
VK
136
137 bResult = DBExecute(hStmt);
138
139 DBFreeStatement(hStmt);
140 }
141 else
142 {
143 bResult = FALSE;
144 }
145
146 // Save data collection items
147 if (bResult)
148 {
149 lockDciAccess();
150 for(int i = 0; i < m_dcObjects->size(); i++)
151 m_dcObjects->get(i)->saveToDB(hdb);
152 unlockDciAccess();
153 }
154
155 // Save access list
156 saveACLToDB(hdb);
157
158 // Clear modifications flag and unlock object
159 if (bResult)
160 m_bIsModified = FALSE;
161 UnlockData();
162
163 return bResult;
164}
165
166/**
167 * Delete object from database
168 */
169BOOL MobileDevice::DeleteFromDB()
170{
171 TCHAR szQuery[256];
172 BOOL bSuccess;
173
6fd6de0a 174 bSuccess = DataCollectionTarget::DeleteFromDB();
9aa67910
VK
175 if (bSuccess)
176 {
177 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("DELETE FROM mobile_devices WHERE id=%d"), (int)m_dwId);
178 QueueSQLRequest(szQuery);
9aa67910
VK
179 }
180 return bSuccess;
181}
182
183/**
184 * Create CSCP message with object's data
185 */
171c2fd6 186void MobileDevice::CreateMessage(CSCPMessage *msg)
9aa67910 187{
6fd6de0a 188 DataCollectionTarget::CreateMessage(msg);
171c2fd6
VK
189 msg->SetVariable(VID_DEVICE_ID, CHECK_NULL_EX(m_deviceId));
190 msg->SetVariable(VID_VENDOR, CHECK_NULL_EX(m_vendor));
191 msg->SetVariable(VID_MODEL, CHECK_NULL_EX(m_model));
6583a0f8
VK
192 msg->SetVariable(VID_SERIAL_NUMBER, CHECK_NULL_EX(m_serialNumber));
193 msg->SetVariable(VID_OS_NAME, CHECK_NULL_EX(m_osName));
194 msg->SetVariable(VID_OS_VERSION, CHECK_NULL_EX(m_osVersion));
171c2fd6 195 msg->SetVariable(VID_USER_ID, CHECK_NULL_EX(m_userId));
6fd6de0a 196 msg->SetVariable(VID_BATTERY_LEVEL, (DWORD)m_batteryLevel);
9aa67910
VK
197}
198
199/**
200 * Modify object from message
201 */
202DWORD MobileDevice::ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked)
203{
204 if (!bAlreadyLocked)
205 LockData();
206
6fd6de0a 207 return DataCollectionTarget::ModifyFromMessage(pRequest, TRUE);
9aa67910 208}
6583a0f8
VK
209
210/**
211 * Update system information from NXCP message
212 */
213void MobileDevice::updateSystemInfo(CSCPMessage *msg)
214{
215 LockData();
216
217 safe_free(m_vendor);
218 m_vendor = msg->GetVariableStr(VID_VENDOR);
219
220 safe_free(m_model);
221 m_model = msg->GetVariableStr(VID_MODEL);
222
223 safe_free(m_serialNumber);
224 m_serialNumber = msg->GetVariableStr(VID_SERIAL_NUMBER);
225
226 safe_free(m_osName);
227 m_osName = msg->GetVariableStr(VID_OS_NAME);
228
229 safe_free(m_osVersion);
230 m_osVersion = msg->GetVariableStr(VID_OS_VERSION);
231
232 safe_free(m_userId);
233 m_userId = msg->GetVariableStr(VID_USER_NAME);
234
235 Modify();
236 UnlockData();
237}
238
239/**
240 * Update status from NXCP message
241 */
242void MobileDevice::updateStatus(CSCPMessage *msg)
243{
244 LockData();
245
246 if (msg->IsVariableExist(VID_BATTERY_LEVEL))
247 m_batteryLevel = (int)msg->GetVariableLong(VID_BATTERY_LEVEL);
248 else
249 m_batteryLevel = -1;
250
251 if (msg->IsVariableExist(VID_GEOLOCATION_TYPE))
252 m_geoLocation = GeoLocation(*msg);
253
254 if (msg->IsVariableExist(VID_IP_ADDRESS))
255 m_dwIpAddr = msg->GetVariableLong(VID_IP_ADDRESS);
256
257 TCHAR temp[32];
258 DbgPrintf(5, _T("Mobile device %s [%d] updated from agent (battery=%d addr=%s loc=[%s %s])"),
259 m_szName, (int)m_dwId, m_batteryLevel, IpToStr(m_dwIpAddr, temp),
260 m_geoLocation.getLatitudeAsString(), m_geoLocation.getLongitudeAsString());
261
262 Modify();
263 UnlockData();
264}