object save optimization - object properties divided into groups and anly modified...
[public/netxms.git] / src / server / core / condition.cpp
CommitLineData
e83d726c 1/*
5039dede 2** NetXMS - Network Management System
db091a1f 3** Copyright (C) 2003-2016 Victor Kirhenshtein
5039dede
AK
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: container.cpp
20**
21**/
22
23#include "nxcore.h"
24
21c9acce
VK
25/**
26 * Default constructor
27 */
9f06d008 28ConditionObject::ConditionObject() : NetObj()
5039dede 29{
9fddfb91
VK
30 m_scriptSource = NULL;
31 m_script = NULL;
32 m_dciList = NULL;
33 m_dciCount = 0;
34 m_sourceObject = 0;
35 m_activeStatus = STATUS_MAJOR;
36 m_inactiveStatus = STATUS_NORMAL;
37 m_isActive = false;
38 m_lastPoll = 0;
39 m_queuedForPolling = false;
40 m_activationEventCode = EVENT_CONDITION_ACTIVATED;
41 m_deactivationEventCode = EVENT_CONDITION_DEACTIVATED;
5039dede
AK
42}
43
21c9acce
VK
44/**
45 * Constructor for new objects
46 */
9f06d008 47ConditionObject::ConditionObject(bool hidden) : NetObj()
5039dede 48{
9fddfb91
VK
49 m_scriptSource = NULL;
50 m_script = NULL;
51 m_dciList = NULL;
52 m_dciCount = 0;
53 m_sourceObject = 0;
54 m_activeStatus = STATUS_MAJOR;
55 m_inactiveStatus = STATUS_NORMAL;
01152a54 56 m_isHidden = hidden;
9fddfb91
VK
57 m_isActive = false;
58 m_lastPoll = 0;
59 m_queuedForPolling = false;
60 m_activationEventCode = EVENT_CONDITION_ACTIVATED;
61 m_deactivationEventCode = EVENT_CONDITION_DEACTIVATED;
5039dede
AK
62}
63
9fddfb91
VK
64/**
65 * Destructor
66 */
9f06d008 67ConditionObject::~ConditionObject()
5039dede 68{
9f06d008
VK
69 free(m_dciList);
70 free(m_scriptSource);
9fddfb91 71 delete m_script;
5039dede
AK
72}
73
9fddfb91
VK
74/**
75 * Load object from database
76 */
9f06d008 77bool ConditionObject::loadFromDatabase(DB_HANDLE hdb, UINT32 dwId)
5039dede
AK
78{
79 TCHAR szQuery[512];
80 DB_RESULT hResult;
967893bb 81 UINT32 i;
5039dede 82
c42b4551 83 m_id = dwId;
5039dede 84
9bd1bace
VK
85 if (!loadCommonProperties(hdb))
86 return false;
5039dede
AK
87
88 // Load properties
89 _sntprintf(szQuery, 512, _T("SELECT activation_event,deactivation_event,")
90 _T("source_object,active_status,inactive_status,")
91 _T("script FROM conditions WHERE id=%d"), dwId);
9bd1bace 92 hResult = DBSelect(hdb, szQuery);
5039dede 93 if (hResult == NULL)
9bd1bace 94 return false; // Query failed
5039dede
AK
95
96 if (DBGetNumRows(hResult) == 0)
97 {
98 // No object with given ID in database
99 DBFreeResult(hResult);
9bd1bace 100 return false;
5039dede
AK
101 }
102
9fddfb91
VK
103 m_activationEventCode = DBGetFieldULong(hResult, 0, 0);
104 m_deactivationEventCode = DBGetFieldULong(hResult, 0, 1);
105 m_sourceObject = DBGetFieldULong(hResult, 0, 2);
106 m_activeStatus = DBGetFieldLong(hResult, 0, 3);
107 m_inactiveStatus = DBGetFieldLong(hResult, 0, 4);
108 m_scriptSource = DBGetField(hResult, 0, 5, NULL, 0);
109 DecodeSQLString(m_scriptSource);
e83d726c 110
5039dede
AK
111 DBFreeResult(hResult);
112
113 // Compile script
d881ea08 114 NXSL_Program *p = (NXSL_Program *)NXSLCompile(m_scriptSource, szQuery, 512, NULL);
6b29839d
VK
115 if (p != NULL)
116 {
117 m_script = new NXSL_VM(new NXSL_ServerEnv);
118 if (!m_script->load(p))
119 {
c42b4551 120 nxlog_write(MSG_COND_SCRIPT_COMPILATION_ERROR, EVENTLOG_ERROR_TYPE, "dss", m_id, m_name, m_script->getErrorText());
6b29839d
VK
121 delete_and_null(m_script);
122 }
123 delete p;
124 }
125 else
126 {
127 m_script = NULL;
c42b4551 128 nxlog_write(MSG_COND_SCRIPT_COMPILATION_ERROR, EVENTLOG_ERROR_TYPE, "dss", m_id, m_name, szQuery);
6b29839d 129 }
5039dede
AK
130
131 // Load DCI map
132 _sntprintf(szQuery, 512, _T("SELECT dci_id,node_id,dci_func,num_polls ")
133 _T("FROM cond_dci_map WHERE condition_id=%d ORDER BY sequence_number"), dwId);
9bd1bace 134 hResult = DBSelect(hdb, szQuery);
5039dede 135 if (hResult == NULL)
9bd1bace 136 return false; // Query failed
5039dede 137
9fddfb91
VK
138 m_dciCount = DBGetNumRows(hResult);
139 if (m_dciCount > 0)
5039dede 140 {
9fddfb91
VK
141 m_dciList = (INPUT_DCI *)malloc(sizeof(INPUT_DCI) * m_dciCount);
142 for(i = 0; i < m_dciCount; i++)
5039dede 143 {
9fddfb91
VK
144 m_dciList[i].id = DBGetFieldULong(hResult, i, 0);
145 m_dciList[i].nodeId = DBGetFieldULong(hResult, i, 1);
146 m_dciList[i].function = DBGetFieldLong(hResult, i, 2);
147 m_dciList[i].polls = DBGetFieldLong(hResult, i, 3);
5039dede
AK
148 }
149 }
150 DBFreeResult(hResult);
151
9bd1bace 152 return loadACLFromDB(hdb);
5039dede
AK
153}
154
9fddfb91
VK
155/**
156 * Save object to database
157 */
b4277312 158bool ConditionObject::saveToDatabase(DB_HANDLE hdb)
5039dede
AK
159{
160 TCHAR *pszEscScript, *pszQuery;
161 DB_RESULT hResult;
162 BOOL bNewObject = TRUE;
967893bb 163 UINT32 i;
5039dede 164
c42b4551 165 lockProperties();
5039dede 166
89135050 167 saveCommonProperties(hdb);
5039dede 168
3d48d1b4 169 if (m_modified & MODIFY_OTHER)
5039dede 170 {
3d48d1b4
VK
171 pszEscScript = EncodeSQLString(CHECK_NULL_EX(m_scriptSource));
172 size_t qlen = _tcslen(pszEscScript) + 1024;
173 pszQuery = (TCHAR *)malloc(sizeof(TCHAR) * qlen);
174
175 // Check for object's existence in database
176 _sntprintf(pszQuery, qlen, _T("SELECT id FROM conditions WHERE id=%d"), m_id);
177 hResult = DBSelect(hdb, pszQuery);
178 if (hResult != NULL)
179 {
180 if (DBGetNumRows(hResult) > 0)
181 bNewObject = FALSE;
182 DBFreeResult(hResult);
183 }
5039dede 184
3d48d1b4
VK
185 // Form and execute INSERT or UPDATE query
186 if (bNewObject)
187 {
188 _sntprintf(pszQuery, qlen,
189 _T("INSERT INTO conditions (id,activation_event,")
190 _T("deactivation_event,source_object,active_status,")
191 _T("inactive_status,script) VALUES (%d,%d,%d,%d,%d,%d,'%s')"),
192 m_id, m_activationEventCode, m_deactivationEventCode,
193 m_sourceObject, m_activeStatus, m_inactiveStatus, pszEscScript);
194 }
195 else
196 {
197 _sntprintf(pszQuery, qlen,
198 _T("UPDATE conditions SET activation_event=%d,")
199 _T("deactivation_event=%d,source_object=%d,active_status=%d,")
200 _T("inactive_status=%d,script='%s' WHERE id=%d"),
201 m_activationEventCode, m_deactivationEventCode, m_sourceObject,
202 m_activeStatus, m_inactiveStatus, pszEscScript, m_id);
203 }
204 free(pszEscScript);
205 DBQuery(hdb, pszQuery);
5039dede 206
3d48d1b4
VK
207 // Save DCI mapping
208 _sntprintf(pszQuery, qlen, _T("DELETE FROM cond_dci_map WHERE condition_id=%d"), m_id);
5039dede 209 DBQuery(hdb, pszQuery);
3d48d1b4
VK
210 for(i = 0; i < m_dciCount; i++)
211 {
212 _sntprintf(pszQuery, qlen, _T("INSERT INTO cond_dci_map (condition_id,sequence_number,dci_id,node_id,")
213 _T("dci_func,num_polls) VALUES (%d,%d,%d,%d,%d,%d)"),
214 m_id, i, m_dciList[i].id, m_dciList[i].nodeId,
215 m_dciList[i].function, m_dciList[i].polls);
216 DBQuery(hdb, pszQuery);
217 }
218 free(pszQuery);
5039dede 219 }
5039dede
AK
220
221 // Save access list
89135050 222 saveACLToDB(hdb);
5039dede
AK
223
224 // Unlock object and clear modification flag
3d48d1b4 225 m_modified = 0;
c42b4551 226 unlockProperties();
b4277312 227 return true;
5039dede
AK
228}
229
22ee6d97
VK
230/**
231 * Delete object from database
232 */
9f06d008 233bool ConditionObject::deleteFromDatabase(DB_HANDLE hdb)
5039dede 234{
c42b4551 235 bool success = NetObj::deleteFromDatabase(hdb);
22ee6d97
VK
236 if (success)
237 success = executeQueryOnObject(hdb, _T("DELETE FROM conditions WHERE id=?"));
238 if (success)
239 success = executeQueryOnObject(hdb, _T("DELETE FROM cond_dci_map WHERE condition_id=?"));
240 return success;
5039dede
AK
241}
242
22ee6d97
VK
243/**
244 * Create NXCP message from object
245 */
9f06d008 246void ConditionObject::fillMessageInternal(NXCPMessage *pMsg)
5039dede 247{
8fe90adb 248 NetObj::fillMessageInternal(pMsg);
e83d726c 249
b368969c
VK
250 pMsg->setField(VID_SCRIPT, CHECK_NULL_EX(m_scriptSource));
251 pMsg->setField(VID_ACTIVATION_EVENT, m_activationEventCode);
252 pMsg->setField(VID_DEACTIVATION_EVENT, m_deactivationEventCode);
253 pMsg->setField(VID_SOURCE_OBJECT, m_sourceObject);
254 pMsg->setField(VID_ACTIVE_STATUS, (WORD)m_activeStatus);
255 pMsg->setField(VID_INACTIVE_STATUS, (WORD)m_inactiveStatus);
256 pMsg->setField(VID_NUM_ITEMS, m_dciCount);
8fe90adb 257 for(UINT32 i = 0, dwId = VID_DCI_LIST_BASE; (i < m_dciCount) && (dwId < (VID_DCI_LIST_LAST + 1)); i++)
5039dede 258 {
b368969c
VK
259 pMsg->setField(dwId++, m_dciList[i].id);
260 pMsg->setField(dwId++, m_dciList[i].nodeId);
261 pMsg->setField(dwId++, (WORD)m_dciList[i].function);
262 pMsg->setField(dwId++, (WORD)m_dciList[i].polls);
263 pMsg->setField(dwId++, (WORD)GetDCObjectType(m_dciList[i].nodeId, m_dciList[i].id));
9fddfb91 264 dwId += 5;
5039dede
AK
265 }
266}
267
9fddfb91
VK
268/**
269 * Modify object from NXCP message
270 */
9f06d008 271UINT32 ConditionObject::modifyFromMessageInternal(NXCPMessage *pRequest)
5039dede 272{
967893bb 273 UINT32 i, dwId;
5039dede 274 NetObj *pObject;
5039dede 275
5039dede 276 // Change script
5c44534b 277 if (pRequest->isFieldExist(VID_SCRIPT))
5039dede
AK
278 {
279 TCHAR szError[1024];
280
9fddfb91
VK
281 safe_free(m_scriptSource);
282 delete m_script;
b368969c 283 m_scriptSource = pRequest->getFieldAsString(VID_SCRIPT);
d881ea08 284 NXSL_Program *p = (NXSL_Program *)NXSLCompile(m_scriptSource, szError, 1024, NULL);
6b29839d
VK
285 if (p != NULL)
286 {
287 m_script = new NXSL_VM(new NXSL_ServerEnv);
288 if (!m_script->load(p))
289 {
c42b4551 290 nxlog_write(MSG_COND_SCRIPT_COMPILATION_ERROR, EVENTLOG_ERROR_TYPE, "dss", m_id, m_name, m_script->getErrorText());
6b29839d
VK
291 delete_and_null(m_script);
292 }
293 delete p;
294 }
295 else
296 {
297 m_script = NULL;
c42b4551 298 nxlog_write(MSG_COND_SCRIPT_COMPILATION_ERROR, EVENTLOG_ERROR_TYPE, "dss", m_id, m_name, szError);
6b29839d 299 }
5039dede
AK
300 }
301
302 // Change activation event
5c44534b 303 if (pRequest->isFieldExist(VID_ACTIVATION_EVENT))
b368969c 304 m_activationEventCode = pRequest->getFieldAsUInt32(VID_ACTIVATION_EVENT);
5039dede
AK
305
306 // Change deactivation event
5c44534b 307 if (pRequest->isFieldExist(VID_DEACTIVATION_EVENT))
b368969c 308 m_deactivationEventCode = pRequest->getFieldAsUInt32(VID_DEACTIVATION_EVENT);
5039dede
AK
309
310 // Change source object
5c44534b 311 if (pRequest->isFieldExist(VID_SOURCE_OBJECT))
b368969c 312 m_sourceObject = pRequest->getFieldAsUInt32(VID_SOURCE_OBJECT);
5039dede
AK
313
314 // Change active status
5c44534b 315 if (pRequest->isFieldExist(VID_ACTIVE_STATUS))
b368969c 316 m_activeStatus = pRequest->getFieldAsUInt16(VID_ACTIVE_STATUS);
5039dede
AK
317
318 // Change inactive status
5c44534b 319 if (pRequest->isFieldExist(VID_INACTIVE_STATUS))
b368969c 320 m_inactiveStatus = pRequest->getFieldAsUInt16(VID_INACTIVE_STATUS);
5039dede
AK
321
322 // Change DCI list
5c44534b 323 if (pRequest->isFieldExist(VID_NUM_ITEMS))
5039dede 324 {
9fddfb91 325 safe_free(m_dciList);
b368969c 326 m_dciCount = pRequest->getFieldAsUInt32(VID_NUM_ITEMS);
9fddfb91 327 if (m_dciCount > 0)
5039dede 328 {
9fddfb91
VK
329 m_dciList = (INPUT_DCI *)malloc(sizeof(INPUT_DCI) * m_dciCount);
330 for(i = 0, dwId = VID_DCI_LIST_BASE; (i < m_dciCount) && (dwId < (VID_DCI_LIST_LAST + 1)); i++)
5039dede 331 {
b368969c
VK
332 m_dciList[i].id = pRequest->getFieldAsUInt32(dwId++);
333 m_dciList[i].nodeId = pRequest->getFieldAsUInt32(dwId++);
334 m_dciList[i].function = pRequest->getFieldAsUInt16(dwId++);
335 m_dciList[i].polls = pRequest->getFieldAsUInt16(dwId++);
5039dede
AK
336 dwId += 6;
337 }
338
339 // Update cache size of DCIs
9fddfb91 340 for(i = 0; i < m_dciCount; i++)
5039dede 341 {
9fddfb91 342 pObject = FindObjectById(m_dciList[i].nodeId);
5039dede
AK
343 if (pObject != NULL)
344 {
77b2c6de
VK
345 if ((pObject->getObjectClass() == OBJECT_NODE) ||
346 (pObject->getObjectClass() == OBJECT_CLUSTER) ||
ce9e00cc
VK
347 (pObject->getObjectClass() == OBJECT_MOBILEDEVICE) ||
348 (pObject->getObjectClass() == OBJECT_SENSOR))
5039dede 349 {
77b2c6de 350 ((DataCollectionTarget *)pObject)->updateDCItemCacheSize(m_dciList[i].id, m_id);
5039dede
AK
351 }
352 }
353 }
354 }
355 else
356 {
9fddfb91 357 m_dciList = NULL;
5039dede
AK
358 }
359 }
360
8fe90adb 361 return NetObj::modifyFromMessageInternal(pRequest);
5039dede
AK
362}
363
21c9acce
VK
364/**
365 * Lock for polling
366 */
9f06d008 367void ConditionObject::lockForPoll()
5039dede 368{
9fddfb91 369 m_queuedForPolling = TRUE;
5039dede
AK
370}
371
21c9acce 372/**
208d7427 373 * Poller entry point
21c9acce 374 */
9f06d008 375void ConditionObject::doPoll(PollerInfo *poller)
5039dede 376{
208d7427
VK
377 poller->startExecution();
378 check();
c42b4551 379 lockProperties();
9fddfb91
VK
380 m_queuedForPolling = FALSE;
381 m_lastPoll = time(NULL);
c42b4551 382 unlockProperties();
208d7427 383 delete poller;
5039dede
AK
384}
385
21c9acce
VK
386/**
387 * Check condition
388 */
9f06d008 389void ConditionObject::check()
5039dede 390{
5039dede
AK
391 NXSL_Value **ppValueList, *pValue;
392 NetObj *pObject;
967893bb 393 UINT32 i, dwNumValues;
db091a1f 394 int iOldStatus = m_status;
5039dede 395
db091a1f 396 if ((m_script == NULL) || (m_status == STATUS_UNMANAGED) || IsShutdownInProgress())
5039dede
AK
397 return;
398
c42b4551 399 lockProperties();
9fddfb91
VK
400 ppValueList = (NXSL_Value **)malloc(sizeof(NXSL_Value *) * m_dciCount);
401 memset(ppValueList, 0, sizeof(NXSL_Value *) * m_dciCount);
402 for(i = 0; i < m_dciCount; i++)
5039dede 403 {
9fddfb91 404 pObject = FindObjectById(m_dciList[i].nodeId);
5039dede
AK
405 if (pObject != NULL)
406 {
c42b4551 407 if (pObject->getObjectClass() == OBJECT_NODE)
5039dede 408 {
9fddfb91
VK
409 DCObject *pItem = ((Node *)pObject)->getDCObjectById(m_dciList[i].id);
410 if (pItem != NULL)
5039dede 411 {
9fddfb91
VK
412 if (pItem->getType() == DCO_TYPE_ITEM)
413 {
414 ppValueList[i] = ((DCItem *)pItem)->getValueForNXSL(m_dciList[i].function, m_dciList[i].polls);
415 }
416 else if (pItem->getType() == DCO_TYPE_TABLE)
417 {
418 Table *t = ((DCTable *)pItem)->getLastValue();
419 if (t != NULL)
420 {
421 ppValueList[i] = new NXSL_Value(new NXSL_Object(&g_nxslTableClass, t));
422 }
423 }
5039dede
AK
424 }
425 }
426 }
427 if (ppValueList[i] == NULL)
428 ppValueList[i] = new NXSL_Value;
429 }
9fddfb91 430 dwNumValues = m_dciCount;
c42b4551 431 unlockProperties();
5039dede 432
22aa5156
VK
433 // Create array from values
434 NXSL_Array *array = new NXSL_Array;
435 for(i = 0; i < dwNumValues; i++)
436 {
437 array->set(i + 1, new NXSL_Value(ppValueList[i]));
438 }
9fddfb91 439 m_script->setGlobalVariable(_T("$values"), new NXSL_Value(array));
22aa5156 440
c42b4551 441 DbgPrintf(6, _T("Running evaluation script for condition %d \"%s\""), m_id, m_name);
6b29839d 442 if (m_script->run(dwNumValues, ppValueList))
5039dede 443 {
9fddfb91 444 pValue = m_script->getResult();
bb5365ed 445 if (pValue->getValueAsInt32() == 0)
5039dede 446 {
9fddfb91 447 if (m_isActive)
5039dede
AK
448 {
449 // Deactivate condition
c42b4551 450 lockProperties();
db091a1f 451 m_status = m_inactiveStatus;
9fddfb91 452 m_isActive = FALSE;
3d48d1b4 453 setModified(MODIFY_RUNTIME);
c42b4551 454 unlockProperties();
5039dede 455
9fddfb91
VK
456 PostEvent(m_deactivationEventCode,
457 (m_sourceObject == 0) ? g_dwMgmtNode : m_sourceObject,
db091a1f 458 "dsdd", m_id, m_name, iOldStatus, m_status);
5039dede
AK
459
460 DbgPrintf(6, _T("Condition %d \"%s\" deactivated"),
c42b4551 461 m_id, m_name);
5039dede
AK
462 }
463 else
464 {
465 DbgPrintf(6, _T("Condition %d \"%s\" still inactive"),
c42b4551
VK
466 m_id, m_name);
467 lockProperties();
db091a1f 468 if (m_status != m_inactiveStatus)
5039dede 469 {
db091a1f 470 m_status = m_inactiveStatus;
3d48d1b4 471 setModified(MODIFY_RUNTIME);
5039dede 472 }
c42b4551 473 unlockProperties();
5039dede
AK
474 }
475 }
476 else
477 {
9fddfb91 478 if (!m_isActive)
5039dede
AK
479 {
480 // Activate condition
c42b4551 481 lockProperties();
db091a1f 482 m_status = m_activeStatus;
9fddfb91 483 m_isActive = TRUE;
3d48d1b4 484 setModified(MODIFY_RUNTIME);
c42b4551 485 unlockProperties();
5039dede 486
9fddfb91
VK
487 PostEvent(m_activationEventCode,
488 (m_sourceObject == 0) ? g_dwMgmtNode : m_sourceObject,
db091a1f 489 "dsdd", m_id, m_name, iOldStatus, m_status);
5039dede
AK
490
491 DbgPrintf(6, _T("Condition %d \"%s\" activated"),
c42b4551 492 m_id, m_name);
5039dede
AK
493 }
494 else
495 {
496 DbgPrintf(6, _T("Condition %d \"%s\" still active"),
c42b4551
VK
497 m_id, m_name);
498 lockProperties();
db091a1f 499 if (m_status != m_activeStatus)
5039dede 500 {
db091a1f 501 m_status = m_activeStatus;
3d48d1b4 502 setModified(MODIFY_RUNTIME);
5039dede 503 }
c42b4551 504 unlockProperties();
5039dede
AK
505 }
506 }
507 }
508 else
509 {
510 nxlog_write(MSG_COND_SCRIPT_EXECUTION_ERROR, EVENTLOG_ERROR_TYPE,
c42b4551 511 "dss", m_id, m_name, m_script->getErrorText());
5039dede 512
c42b4551 513 lockProperties();
db091a1f 514 if (m_status != STATUS_UNKNOWN)
5039dede 515 {
db091a1f 516 m_status = STATUS_UNKNOWN;
3d48d1b4 517 setModified(MODIFY_RUNTIME);
5039dede 518 }
c42b4551 519 unlockProperties();
5039dede
AK
520 }
521 free(ppValueList);
522
523 // Cause parent object(s) to recalculate it's status
db091a1f 524 if (iOldStatus != m_status)
5039dede 525 {
db091a1f
VK
526 lockParentList(false);
527 for(int i = 0; i < m_parentList->size(); i++)
528 m_parentList->get(i)->calculateCompoundStatus();
529 unlockParentList();
5039dede
AK
530 }
531}
532
9fddfb91
VK
533/**
534 * Determine DCI cache size required by condition object
535 */
9f06d008 536int ConditionObject::getCacheSizeForDCI(UINT32 itemId, bool noLock)
5039dede 537{
967893bb 538 UINT32 i;
5039dede
AK
539 int nSize = 0;
540
9fddfb91 541 if (!noLock)
c42b4551 542 lockProperties();
9fddfb91 543 for(i = 0; i < m_dciCount; i++)
5039dede 544 {
9fddfb91 545 if (m_dciList[i].id == itemId)
5039dede 546 {
9fddfb91 547 switch(m_dciList[i].function)
5039dede
AK
548 {
549 case F_LAST:
550 nSize = 1;
551 break;
552 case F_DIFF:
553 nSize = 2;
554 break;
555 default:
9fddfb91 556 nSize = m_dciList[i].polls;
5039dede
AK
557 break;
558 }
559 break;
560 }
561 }
9fddfb91 562 if (!noLock)
c42b4551 563 unlockProperties();
5039dede
AK
564 return nSize;
565}
950b229b
VK
566
567/**
568 * Serialize object to JSON
569 */
570json_t *ConditionObject::toJson()
571{
572 json_t *root = NetObj::toJson();
573 json_t *inputs = json_array();
574 for(int i = 0; i < m_dciCount; i++)
575 {
576 json_t *dci = json_object();
577 json_object_set_new(dci, "id", json_integer(m_dciList[i].id));
578 json_object_set_new(dci, "nodeId", json_integer(m_dciList[i].nodeId));
579 json_object_set_new(dci, "function", json_integer(m_dciList[i].function));
580 json_object_set_new(dci, "polls", json_integer(m_dciList[i].polls));
581 json_array_append_new(inputs, dci);
582 }
583 json_object_set_new(root, "inputs", inputs);
584 json_object_set_new(root, "script", json_string_t(m_scriptSource));
585 json_object_set_new(root, "activationEventCode", json_integer(m_activationEventCode));
586 json_object_set_new(root, "deactivationEventCode", json_integer(m_deactivationEventCode));
587 json_object_set_new(root, "sourceObject", json_integer(m_sourceObject));
588 json_object_set_new(root, "activeStatus", json_integer(m_activeStatus));
589 json_object_set_new(root, "inactiveStatus", json_integer(m_inactiveStatus));
590 json_object_set_new(root, "isActive", json_boolean(m_isActive));
591 json_object_set_new(root, "lastPoll", json_integer(m_lastPoll));
592 return root;
593}