Skip to content

Package: KuraAssetChannel

KuraAssetChannel

nameinstructionbranchcomplexitylinemethod
KuraAssetChannel()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getError()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getMode()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getName()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getTimestamp()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getType()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getValue()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
readJsonNode(JsonNode)
M: 90 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 26 C: 0
0%
M: 1 C: 0
0%
setError(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setMode(KuraAssetChannelMode)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setName(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setTimestamp(Long)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setType(Class)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
setValue(Object)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
writeJsonNode(JsonGenerator)
M: 68 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.service.device.call.kura.model.asset;
14:
15: import java.io.IOException;
16:
17: import org.eclipse.kapua.KapuaException;
18: import org.eclipse.kapua.KapuaIllegalArgumentException;
19: import org.eclipse.kapua.service.device.call.kura.model.type.KuraObjectTypeConverter;
20: import org.eclipse.kapua.service.device.call.kura.model.type.KuraObjectValueConverter;
21:
22: import com.fasterxml.jackson.core.JsonGenerator;
23: import com.fasterxml.jackson.databind.JsonNode;
24:
25: /**
26: * Kura channel asset definition
27: *
28: * @since 1.0.0
29: */
30: public class KuraAssetChannel {
31:
32: private String name;
33: private KuraAssetChannelMode mode;
34: private Class<?> type;
35: private Object value;
36: private Long timestamp;
37: private String error;
38:
39: /**
40: * Gets the name of the channel.
41: *
42: * @return The name of the channel.
43: *
44: * @since 1.0.0
45: */
46: public String getName() {
47: return name;
48: }
49:
50: /**
51: * Sets the name of the channel.
52: *
53: * @param name
54: *
55: * @since 1.0.0
56: */
57: public void setName(String name) {
58: this.name = name;
59: }
60:
61: /**
62: * Gets the channel mode.
63: *
64: * @return The channel mode.
65: *
66: * @since 1.0.0
67: */
68: public KuraAssetChannelMode getMode() {
69: return mode;
70: }
71:
72: /**
73: * Sets the channel mode.
74: * A {@link KuraAssetChannel} can have modes available from {@link KuraAssetChannelMode}.
75: *
76: * @param mode
77: * The channel mode to set.
78: *
79: * @since 1.0.0
80: */
81: public void setMode(KuraAssetChannelMode mode) {
82: this.mode = mode;
83: }
84:
85: /**
86: * Gets the channel type.
87: * This is the type returned by {@link #getValue()}.
88: *
89: * @return The channel value type.
90: *
91: * @since 1.0.0
92: */
93: public Class<?> getType() {
94: return type;
95: }
96:
97: /**
98: * Sets the channel type.
99: * This type must be coherent with the value given to {@link #setValue(Object)}.
100: * If not errors will occur during the interaction with the device.
101: *
102: * @param type
103: * The channel type.
104: *
105: * @since 1.0.0
106: */
107: public void setType(Class<?> type) {
108: this.type = type;
109: }
110:
111: /**
112: * Gets the value of the channel.
113: * Depending on the {@link KuraAssetChannelMode} this can be a value {@link KuraAssetChannelMode#READ} from the channel or
114: * to {@link KuraAssetChannelMode#WRITE} into the channel.
115: * This is mutually exclusive with {@link #getError()}
116: *
117: * @return The value of the channel.
118: *
119: * @since 1.0.0
120: */
121: public Object getValue() {
122: return value;
123: }
124:
125: /**
126: * Sets the value of the channel.
127: * Depending on the {@link KuraAssetChannelMode} this can be a value {@link KuraAssetChannelMode#READ} from the channel or
128: * to {@link KuraAssetChannelMode#WRITE} into the channel.
129: *
130: * @param value
131: * The value of the channel to set.
132: *
133: * @since 1.0.0
134: */
135: public void setValue(Object value) {
136: this.value = value;
137: }
138:
139: /**
140: * Gets the timestamp in millisecond of the time when the value was read from the channel.
141: *
142: * @return The timestamp in millisecond of the time when the value was read from the channel.
143: *
144: * @since 1.0.0
145: */
146: public Long getTimestamp() {
147: return timestamp;
148: }
149:
150: /**
151: * Sets timestamp in millisecond of the time when the value was read from the channel.
152: *
153: * @param timestamp
154: * The timestamp in millisecond of the time when the value was read from the channel.
155: *
156: * @since 1.0.0
157: */
158: public void setTimestamp(Long timestamp) {
159: this.timestamp = timestamp;
160: }
161:
162: /**
163: * Gets the error message for this channel
164: * When reading from or writing to a channel, if any error occurs it will be reported here.
165: * This is mutually exclusive with {@link #getValue()}
166: *
167: * @return The error message, if error has occurred.
168: *
169: * @since 1.0.0
170: */
171: public String getError() {
172: return error;
173: }
174:
175: /**
176: * Sets the error message for this channel.
177: * This must be set if error has occurred during reading from/wrtiting to
178: *
179: * @param error
180: * The error message.
181: */
182: public void setError(String error) {
183: this.error = error;
184: }
185:
186: /**
187: * Parse a {@link JsonNode} that represent the {@link KuraAssetChannel} object.
188: *
189: * @param jsonKuraAssetChannel
190: * The {@link JsonNode} to parse
191: * @return The parsed {@link KuraAssetChannel} result.
192: *
193: * @throws KapuaException
194: *
195: * @since 1.0.0
196: */
197: public static KuraAssetChannel readJsonNode(JsonNode jsonKuraAssetChannel) throws KapuaException {
198: KuraAssetChannel kuraAssetChannel = new KuraAssetChannel();
199:
200: // Name
201: JsonNode jsonName = jsonKuraAssetChannel.get("name");
202:• if (jsonName != null) {
203: kuraAssetChannel.setName(jsonName.asText());
204: }
205:
206: // Type
207: JsonNode jsonType = jsonKuraAssetChannel.get("type");
208:• if (jsonType != null) {
209: try {
210: kuraAssetChannel.setType(KuraObjectTypeConverter.fromString(jsonType.asText()));
211: } catch (ClassNotFoundException e) {
212: throw new KapuaIllegalArgumentException("channel.type", jsonType.asText());
213: }
214: }
215:
216: // Mode
217: JsonNode jsonMode = jsonKuraAssetChannel.get("mode");
218:• if (jsonMode != null) {
219: kuraAssetChannel.setMode(KuraAssetChannelMode.valueOf(jsonMode.asText()));
220: }
221:
222: // Timestamp
223: JsonNode jsonTimestamp = jsonKuraAssetChannel.get("timestamp");
224:• if (jsonTimestamp != null) {
225: kuraAssetChannel.setTimestamp(jsonTimestamp.asLong());
226: }
227:
228: // Value
229: JsonNode jsonValue = jsonKuraAssetChannel.get("value");
230:• if (jsonValue != null) {
231: try {
232: kuraAssetChannel.setValue(KuraObjectValueConverter.fromString(jsonValue.asText(), kuraAssetChannel.getType()));
233: } catch (ClassNotFoundException e) {
234: throw new KapuaIllegalArgumentException("channel.value", jsonValue.asText());
235: }
236: }
237:
238: // Error
239: JsonNode jsonError = jsonKuraAssetChannel.get("error");
240:• if (jsonError != null) {
241: kuraAssetChannel.setError(jsonError.asText());
242: }
243:
244: return kuraAssetChannel;
245: }
246:
247: /**
248: * Serialize {@code this} {@link KuraAssetChannel} into json using the given {@link JsonGenerator}.
249: *
250: * @param jsonGenerator
251: * The {@link JsonGenerator} to put serialized {@link KuraAssetChannel}.
252: * @throws IOException
253: * @since 1.0.0
254: */
255: public void writeJsonNode(JsonGenerator jsonGenerator) throws IOException {
256: jsonGenerator.writeStartObject();
257:
258: // Name
259: String name = getName();
260:• if (name != null) {
261: jsonGenerator.writeStringField("name", name);
262: }
263:
264: // Type
265: Class<?> type = getType();
266:• if (type != null) {
267: jsonGenerator.writeStringField("type", KuraObjectTypeConverter.toString(type));
268: }
269:
270: // Mode
271: KuraAssetChannelMode mode = getMode();
272:• if (mode != null) {
273: jsonGenerator.writeStringField("mode", mode.name());
274: }
275:
276: // Timestamp
277: Long timestamp = getTimestamp();
278:• if (timestamp != null && timestamp > 0) {
279: jsonGenerator.writeNumberField("timestamp", timestamp);
280: }
281:
282: // Value
283: Object value = getValue();
284:• if (value != null) {
285: jsonGenerator.writeStringField("value", KuraObjectValueConverter.toString(value));
286: }
287:
288: // Error
289: String error = getError();
290:• if (error != null) {
291: jsonGenerator.writeStringField("error", error);
292: }
293:
294: jsonGenerator.writeEndObject();
295: }
296: }