22794981ff55f302a8f9460d11cdea3cabc3143a
[public/netxms.git] / src / java / client / netxms-client / src / main / java / org / netxms / client / datacollection / DciValue.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2010 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 package org.netxms.client.datacollection;
20
21 import java.util.Date;
22 import org.netxms.base.NXCPMessage;
23 import org.netxms.client.constants.Severity;
24
25 /**
26 * DCI value
27 */
28 public abstract class DciValue
29 {
30 private long id; // DCI id
31 private long nodeId; // related node object id
32 private long templateDciId; // related template DCI ID
33 private String name; // name
34 private String description; // description
35 private String value; // value
36 private int source; // data source (agent, SNMP, etc.)
37 private int dataType;
38 private int status; // status (active, disabled, etc.)
39 private int errorCount;
40 private int dcObjectType; // Data collection object type (item, table, etc.)
41 private Date timestamp;
42 private Threshold activeThreshold;
43
44 /**
45 * Factory method to create correct DciValue subclass from NXCP message.
46 *
47 * @param nodeId owning node ID
48 * @param msg NXCP message
49 * @param base Base variable ID for value object
50 * @return DciValue object
51 */
52 public static DciValue createFromMessage(long nodeId, NXCPMessage msg, long base)
53 {
54 int type = msg.getFieldAsInt32(base + 8);
55 switch(type)
56 {
57 case DataCollectionObject.DCO_TYPE_ITEM:
58 return new SimpleDciValue(nodeId, msg, base);
59 case DataCollectionObject.DCO_TYPE_TABLE:
60 return new TableDciValue(nodeId, msg, base);
61 default:
62 return null;
63 }
64 }
65
66 /**
67 * Constructor for creating DciValue from NXCP message
68 *
69 * @param nodeId owning node ID
70 * @param msg NXCP message
71 * @param base Base variable ID for value object
72 */
73 protected DciValue(long nodeId, NXCPMessage msg, long base)
74 {
75 long fieldId = base;
76
77 this.nodeId = nodeId;
78 id = msg.getFieldAsInt64(fieldId++);
79 name = msg.getFieldAsString(fieldId++);
80 description = msg.getFieldAsString(fieldId++);
81 source = msg.getFieldAsInt32(fieldId++);
82 dataType = msg.getFieldAsInt32(fieldId++);
83 value = msg.getFieldAsString(fieldId++);
84 timestamp = msg.getFieldAsDate(fieldId++);
85 status = msg.getFieldAsInt32(fieldId++);
86 dcObjectType = msg.getFieldAsInt32(fieldId++);
87 errorCount = msg.getFieldAsInt32(fieldId++);
88 templateDciId = msg.getFieldAsInt64(fieldId++);
89 if (msg.getFieldAsBoolean(fieldId++))
90 activeThreshold = new Threshold(msg, fieldId);
91 else
92 activeThreshold = null;
93 }
94
95 /**
96 * Constructor for creating DCIValue from last values message
97 *
98 * @param id
99 * @param value
100 * @param dataType
101 * @param status
102 */
103 public DciValue(long id, String value, int dataType, int status)
104 {
105 this.id = id;
106 this.value = value;
107 this.dataType = dataType;
108 this.status = status;
109 }
110
111 /**
112 * Returns formated DCI value or string with format error and correct type of DCI value;
113 *
114 * @param formatString the string into which will be placed DCI value
115 * @return
116 */
117 public String format(String formatString)
118 {
119 return new DataFormatter(formatString, dataType).format(value);
120 }
121
122 /**
123 * @return the id
124 */
125 public long getId()
126 {
127 return id;
128 }
129
130 /**
131 * @return the name
132 */
133 public String getName()
134 {
135 return name;
136 }
137
138 /**
139 * @return the description
140 */
141 public String getDescription()
142 {
143 return description;
144 }
145
146 /**
147 * @return the value
148 */
149 public String getValue()
150 {
151 return value;
152 }
153
154 /**
155 * @return the source
156 */
157 public int getSource()
158 {
159 return source;
160 }
161
162 /**
163 * @return the dataType
164 */
165 public int getDataType()
166 {
167 return dataType;
168 }
169
170 /**
171 * @return the status
172 */
173 public int getStatus()
174 {
175 return status;
176 }
177
178 /**
179 * @return the timestamp
180 */
181 public Date getTimestamp()
182 {
183 return timestamp;
184 }
185
186 /**
187 * @return the nodeId
188 */
189 public long getNodeId()
190 {
191 return nodeId;
192 }
193
194 /**
195 * @return the activeThreshold
196 */
197 public Threshold getActiveThreshold()
198 {
199 return activeThreshold;
200 }
201
202 /**
203 * @return the dcObjectType
204 */
205 public int getDcObjectType()
206 {
207 return dcObjectType;
208 }
209
210 /**
211 * @return the errorCount
212 */
213 public int getErrorCount()
214 {
215 return errorCount;
216 }
217
218 /**
219 * @return the templateDciId
220 */
221 public final long getTemplateDciId()
222 {
223 return templateDciId;
224 }
225
226 /**
227 * Get severity of active threshold
228 *
229 * @return severity of active threshold or NORMAL if there are no active thresholds
230 */
231 public Severity getThresholdSeverity()
232 {
233 return (activeThreshold != null) ? activeThreshold.getCurrentSeverity() : Severity.NORMAL;
234 }
235 }