View Javadoc
1   package org.metricshub.ipmi.client.model;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * IPMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import org.metricshub.ipmi.core.coding.commands.sdr.GetSensorReadingResponseData;
26  import org.metricshub.ipmi.core.coding.commands.sdr.record.CompactSensorRecord;
27  import org.metricshub.ipmi.core.coding.commands.sdr.record.EntityId;
28  import org.metricshub.ipmi.core.coding.commands.sdr.record.FullSensorRecord;
29  import org.metricshub.ipmi.core.coding.commands.sdr.record.SensorRecord;
30  
31  /**
32   * Wraps Sensor information:
33   * <ul>
34   * <li>The Sensor record which can be either {@link CompactSensorRecord} or {@link FullSensorRecord}. Note that the compact sensor record is
35   * used to discover and collect the state of the devices, where the full sensor record is used to collect the devices reading values, i.e.
36   * temperature, power consumption, voltage, ...etc.</li>
37   * <li>The sensor reading response data wrapping the reading value</li>
38   * <li>The states of the sensor record formatted as the following: <em>$sensorName=$state|$sensorName=$state|...|$sensorName=$state</em></li>
39   * </ul>
40   */
41  public class Sensor {
42  
43  	private SensorRecord sensorRecord;
44  	private GetSensorReadingResponseData data;
45  	private String states;
46  
47  	public Sensor(SensorRecord sensorRecord, GetSensorReadingResponseData data, String states) {
48  		this.sensorRecord = sensorRecord;
49  		this.data = data;
50  		this.states = states;
51  	}
52  
53  	public SensorRecord getRecord() {
54  		return sensorRecord;
55  	}
56  
57  	public GetSensorReadingResponseData getData() {
58  		return data;
59  	}
60  
61  	public boolean isCompact() {
62  		return sensorRecord instanceof CompactSensorRecord;
63  	}
64  
65  	public boolean isFull() {
66  		return sensorRecord instanceof FullSensorRecord;
67  	}
68  
69  	/**
70  	 * Cast the current record to a {@link CompactSensorRecord}
71  	 *
72  	 * @return {@link CompactSensorRecord} instance
73  	 */
74  	private CompactSensorRecord getCompactSensorRecord() {
75  		return (CompactSensorRecord) sensorRecord;
76  	}
77  
78  	/**
79  	 * Cast the current record to a {@link FullSensorRecord}
80  	 *
81  	 * @return {@link FullSensorRecord} instance
82  	 */
83  	private FullSensorRecord getFullSensorRecord() {
84  		return (FullSensorRecord) sensorRecord;
85  	}
86  
87  	/**
88  	 * Get the {@link EntityId} from the current <code>SensorRecord record;</code> instance
89  	 *
90  	 * @return {@link EntityId} instance
91  	 */
92  	public EntityId getEntityId() {
93  
94  		if (isCompact()) {
95  			return getCompactSensorRecord().getEntityId();
96  		} else if (isFull()) {
97  			return getFullSensorRecord().getEntityId();
98  		}
99  		return null;
100 	}
101 
102 	/**
103 	 * Get the device identifier
104 	 *
105 	 * @return {@link Byte} value or <code>null</code> if the type of the sensor cannot be detected.
106 	 */
107 	public Byte getDeviceId() {
108 		if (isCompact()) {
109 			return getCompactSensorRecord().getEntityInstanceNumber();
110 		} else if (isFull()) {
111 			return getFullSensorRecord().getEntityInstanceNumber();
112 		}
113 		return null;
114 	}
115 
116 	/**
117 	 * Get the name of the sensor record
118 	 *
119 	 * @return {@link String} value
120 	 */
121 	public String getName() {
122 		if (isCompact()) {
123 			return getCompactSensorRecord().getName();
124 		} else if (isFull()) {
125 			return getFullSensorRecord().getName();
126 		}
127 		return null;
128 	}
129 
130 	/**
131 	 * @return state values formatted as: <em>$sensorName=$state|$sensorName=$state...|$sensorName=$state</em>
132 	 */
133 	public String getStates() {
134 		return states;
135 	}
136 }