SensorUtil.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. package cz.hsrs.db.util;
  2. import java.sql.ResultSet;
  3. import java.sql.SQLException;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import cz.hsrs.db.model.Phenomenon;
  7. import cz.hsrs.db.model.Sensor;
  8. import cz.hsrs.db.model.composite.AggregateObservation;
  9. import cz.hsrs.db.model.composite.ObservationValue;
  10. import cz.hsrs.db.model.composite.UnitSensor;
  11. import cz.hsrs.db.model.composite.UnitSensorObservation;
  12. import cz.hsrs.db.pool.SQLExecutor;
  13. /**
  14. * Class provides methods for publishing observations
  15. * @author jezekjan
  16. *
  17. */
  18. public class SensorUtil extends TrackUtil {
  19. /**
  20. * Empty constructor
  21. */
  22. public SensorUtil() {
  23. super();
  24. }
  25. /**
  26. * Method gets list of sensors connected to given unit
  27. * @param unit_id - identifier of unit
  28. * @return list of UnitSensor objects represents sensors connected to given unit
  29. * @throws SQLException
  30. */
  31. @SuppressWarnings("unchecked")
  32. public List<UnitSensor> getUnitsSensors(long unit_id) throws SQLException {
  33. String queryObservations = "SELECT *"
  34. + " FROM sensors, units_to_sensors "
  35. + "WHERE units_to_sensors.unit_id = " + unit_id
  36. + " AND units_to_sensors.sensor_id = sensors.sensor_id;";
  37. ResultSet res = stmt.executeQuery(queryObservations);
  38. return (List<UnitSensor>) generateObjectList(new UnitSensor(), res);
  39. }
  40. /**
  41. * Method gets list of sensors connected to given unit
  42. * @param unit_id - identifier of unit
  43. * @return list of Sensor objects represents sensors connected to given unit
  44. * @throws SQLException
  45. */
  46. @SuppressWarnings("unchecked")
  47. public List<Sensor> getSensors(long unit_id) throws SQLException {
  48. String queryObservations = "SELECT *"
  49. + " FROM sensors, units_to_sensors"
  50. + " WHERE units_to_sensors.unit_id = " + unit_id
  51. + " AND units_to_sensors.sensor_id = sensors.sensor_id ORDER BY sensors.sensor_id;";
  52. ResultSet res = stmt.executeQuery(queryObservations);
  53. return (List<Sensor>) generateObjectList(new Sensor(), res);
  54. }
  55. /**
  56. *
  57. * @return
  58. * @throws SQLException
  59. */
  60. @SuppressWarnings("unchecked")
  61. public List<Sensor> getSensors() throws SQLException {
  62. String queryObservations = "SELECT * FROM sensors ORDER BY sensors.sensor_id;";
  63. ResultSet res = stmt.executeQuery(queryObservations);
  64. return (List<Sensor>) generateObjectList(new Sensor(), res);
  65. }
  66. /**
  67. * Method selects list of sensor types stored in DB
  68. * @return
  69. * @throws SQLException
  70. */
  71. public List<Sensor> getSensorTypes() throws SQLException{
  72. String query = "select distinct(sensor_type) sensor_type from sensors order by sensor_type;";
  73. ResultSet res = stmt.executeQuery(query);
  74. List<Sensor> senTypeList = new LinkedList<Sensor>();
  75. if(res != null) {
  76. while(res.next()) {
  77. senTypeList.add(new Sensor(res.getString("sensor_type")));
  78. }
  79. }
  80. return senTypeList;
  81. }
  82. /**
  83. * Method gets description of sensor by given identifier
  84. * @param sensor_id - identifier of sensor
  85. * @return Sensor object with all attributes
  86. * @throws SQLException
  87. */
  88. public Sensor getSensorById(long sensor_id) throws SQLException {
  89. String queryObservations = "SELECT * FROM sensors "
  90. + "WHERE sensor_id = " + sensor_id + ";";
  91. ResultSet res = stmt.executeQuery(queryObservations);
  92. if (res.next()) {
  93. return (Sensor) (new Sensor()).getDBObject(res);
  94. } else {
  95. throw new SQLException("Sensor " + sensor_id + " not found!");
  96. }
  97. }
  98. /**
  99. * Method checks if there is sensor in DB, given by Id or sensor name
  100. * @param sensorId - id of sensor
  101. * @param sensorName - name of sensor
  102. * @return Sensor object from DB or null if there is not sensor with given id nor name
  103. * @throws SQLException
  104. */
  105. public Sensor getSensorByIdOrName(Long sensorId, String sensorName) throws SQLException {
  106. if(sensorId == null && sensorName != null){
  107. String query = "SELECT * FROM sensors WHERE sensor_name = '" + sensorName + "';";
  108. ResultSet res = stmt.executeQuery(query);
  109. if (res.next()) {
  110. return (Sensor) (new Sensor()).getDBObject(res);
  111. } else {
  112. return null;
  113. }
  114. }
  115. else if(sensorId != null && sensorName == null){
  116. String query = "SELECT * FROM sensors WHERE sensor_id = " + sensorId + ";";
  117. ResultSet res = stmt.executeQuery(query);
  118. if (res.next()) {
  119. return (Sensor) (new Sensor()).getDBObject(res);
  120. } else {
  121. return null;
  122. }
  123. } else{
  124. throw new SQLException("Either sensor_id or sensor_name must be given!");
  125. }
  126. }
  127. /**
  128. * Method selects Sensor by given sensor_name, sensor_type and phenomenon_id
  129. * @param sensorName
  130. * @param sensorType
  131. * @param phenId
  132. * @return Sensor object from DB if there is in DB, null if there is not
  133. * @throws SQLException
  134. */
  135. public Sensor getSensorByNameAndTypeAndPhen(String sensorName, String sensorType, String phenId) throws SQLException {
  136. String query = "SELECT * FROM sensors WHERE sensor_name = '" + sensorName + "'"
  137. + " AND sensor_type = '"+sensorType+"' AND phenomenon_id = '"+phenId+"';";
  138. ResultSet res = stmt.executeQuery(query);
  139. if (res.next()) {
  140. return (Sensor) (new Sensor()).getDBObject(res);
  141. } else {
  142. return null;
  143. }
  144. }
  145. /**
  146. * Method gets next value of sensor_id sequence
  147. * @return next value of sensor_id or null if there is not possible to get next value
  148. * @throws SQLException
  149. */
  150. public Long getNextSensorId() throws SQLException{
  151. boolean exists = true;
  152. Long newId = null;
  153. while(exists){
  154. try{
  155. String selectId = "SELECT nextval('sensors_sensor_id_seq'::regclass);";
  156. ResultSet resId = SQLExecutor.getInstance().executeQuery(selectId);
  157. if(resId.next()){
  158. newId = resId.getLong(1);
  159. } else{
  160. throw new SQLException("Sensor can't get new ID!");
  161. }
  162. Sensor isSame = this.getSensorByIdOrName(newId, null);
  163. if(isSame == null){
  164. exists = false;
  165. }
  166. } catch(SQLException e){
  167. throw new SQLException("Sensor can't get new ID!");
  168. }
  169. }
  170. return newId;
  171. }
  172. /**
  173. * Method detects if given sensor is connected to given unit
  174. * @param unit_id - identifier of unit
  175. * @param sensor_id - identifier of sensor
  176. * @return true if given sensor is connected to given unit, false if not
  177. * @throws SQLException
  178. */
  179. public boolean hasSensor(long unit_id, long sensor_id) throws SQLException {
  180. String query = "SELECT * FROM units_to_sensors"
  181. + " WHERE units_to_sensors.unit_id = " + unit_id
  182. + " AND units_to_sensors.sensor_id = " + sensor_id;
  183. ResultSet res = stmt.executeQuery(query);
  184. return res.next();
  185. }
  186. /**
  187. * Method gets list of observations for given unit-sensor pair and given time stamp interval
  188. * @param unit_id - identifier of unit
  189. * @param sensor_id - identifier of sensor
  190. * @param from - begin time stamp of time range, optional
  191. * @param to - end time stamp of time range, optional
  192. * @return list of ObservationValue objects represents observations for given unit-sensor pair for given time stamps
  193. * @throws SQLException
  194. */
  195. @SuppressWarnings("unchecked")
  196. public List<ObservationValue> getSensorObservations(long unit_id, long sensor_id, String from, String to) throws SQLException {
  197. if (from == null && to == null) {
  198. return getSensorObservations(unit_id, sensor_id);
  199. } else if (to == null && from != null) {
  200. return getSensorObservationsFrom(unit_id, sensor_id, from);
  201. } else if (to != null && from == null) {
  202. return getSensorObservationsTo(unit_id, sensor_id, to);
  203. } else {
  204. String query = "SELECT gid, observed_value, time_stamp"
  205. + " FROM observations"
  206. + " WHERE unit_id = " + unit_id
  207. + " AND sensor_id = " + sensor_id
  208. + " AND time_stamp > '" + from + "'"
  209. + " AND time_stamp < '" + to + "'";
  210. ResultSet res = stmt.executeQuery(query);
  211. return (List<ObservationValue>) generateObjectList(new ObservationValue(), res);
  212. }
  213. }
  214. /**
  215. * Method gets list of observations for given unit-sensor pair and given time stamp interval and trunc parameter
  216. * @param unit_id - identifier of unit
  217. * @param sensor_id - identifier of sensor
  218. * @param from - begin time stamp of time range, mandatory
  219. * @param to - end time stamp of time range, mandatory
  220. * @param trunc - truncate parameter
  221. * @return list of AggregateObservation objects represents observations for given unit-sensor pair for given time stamp and truncate
  222. * @throws SQLException
  223. */
  224. @SuppressWarnings("unchecked")
  225. public List<AggregateObservation> getSensorObservationsTrunc(long unit_id, long sensor_id, String from, String to, String trunc) throws SQLException {
  226. String queryObservations = "SELECT"
  227. + " avg(observed_value) as avg_value,"
  228. + " date_trunc('" + trunc + "', time_stamp) AS dtrunc,"
  229. + " count(*) AS count "
  230. + " FROM observations WHERE unit_id = " + unit_id
  231. + " AND sensor_id = " + sensor_id
  232. + " AND time_stamp >= '" + from + "'"
  233. + " AND time_stamp <= '" + to + "'"
  234. + " GROUP BY dtrunc ORDER BY dtrunc DESC;";
  235. ResultSet res = stmt.executeQuery(queryObservations);
  236. return (List<AggregateObservation>) generateObjectList(new AggregateObservation(), res);
  237. }
  238. /**
  239. * Method gets list of observations for given unit-sensor pair from given timestamp
  240. * @param unit_id - identifier of unit
  241. * @param sensor_id - identifier of sensor
  242. * @param from - timestamp represents beginning of given time range
  243. * @return list of ObservationValue objects represents observations for given unit-sensor pair from given time stamp
  244. * @throws SQLException
  245. */
  246. @SuppressWarnings("unchecked")
  247. protected List<ObservationValue> getSensorObservationsFrom(long unit_id, long sensor_id, String from) throws SQLException {
  248. String queryObservations = "SELECT gid, observed_value, time_stamp"
  249. + " FROM observations WHERE unit_id = " + unit_id
  250. + " AND sensor_id = " + sensor_id
  251. + " AND time_stamp > '" + from + "'";
  252. ResultSet res = stmt.executeQuery(queryObservations);
  253. return (List<ObservationValue>) generateObjectList(new ObservationValue(), res);
  254. }
  255. /**
  256. * Method gets list of observations for given unit-sensor pair till given end timestamp
  257. * @param unit_id - identifier of unit
  258. * @param sensor_id - identifier of sensor
  259. * @param to - timestamp represents end of given time range
  260. * @return list of ObservationValue objects represents observations for given unit-sensor pair until given time stamp
  261. * @throws SQLException
  262. */
  263. @SuppressWarnings("unchecked")
  264. protected List<ObservationValue> getSensorObservationsTo(long unit_id, long sensor_id, String to) throws SQLException {
  265. String queryObservations = "SELECT gid, observed_value, time_stamp"
  266. + " FROM observations WHERE unit_id = " + unit_id
  267. + " AND sensor_id = " + sensor_id
  268. + " AND time_stamp < '" + to + "'";
  269. ResultSet res = stmt.executeQuery(queryObservations);
  270. return (List<ObservationValue>) generateObjectList(new ObservationValue(), res);
  271. }
  272. /**
  273. * Method gets list of observations for given unit-sensor pair
  274. * @param unit_id - identifier of unit
  275. * @param sensor_id - identifier of sensor
  276. * @return list of ObservationValue objects represents all observations for given unit-sensor pair
  277. * @throws SQLException
  278. */
  279. @SuppressWarnings("unchecked")
  280. public List<ObservationValue> getSensorObservations(long unit_id, long sensor_id) throws SQLException {
  281. String queryObservations = "SELECT gid, observed_value, time_stamp"
  282. + " FROM observations WHERE unit_id = " + unit_id
  283. + " AND sensor_id = " + sensor_id + ";";
  284. ResultSet res = stmt.executeQuery(queryObservations);
  285. return (List<ObservationValue>) generateObjectList(new ObservationValue(), res);
  286. }
  287. /**
  288. * Method gets all Phenomenons in DB
  289. * @return
  290. * @throws SQLException
  291. */
  292. @SuppressWarnings("unchecked")
  293. public List<Phenomenon> getPhenomenons() throws SQLException {
  294. String queryObservations = "SELECT * FROM phenomenons ORDER BY phenomenon_id;";
  295. ResultSet res = stmt.executeQuery(queryObservations);
  296. if (res.next()) {
  297. return (List<Phenomenon>) generateObjectList(new Phenomenon(), res);
  298. } else
  299. return null;
  300. }
  301. /**
  302. * Select phenomenon by given ID
  303. * @param id - phenomenonId of phenomenon to select
  304. * @return Phenomenon object if there is phenomenon in DB or null if there is not
  305. * @throws SQLException
  306. */
  307. public Phenomenon getPhenomenonById(String id) throws SQLException {
  308. String queryObservations = "SELECT * FROM phenomenons"
  309. + " WHERE phenomenon_id = '" + id + "';";
  310. ResultSet res = stmt.executeQuery(queryObservations);
  311. if (res.next()) {
  312. return new Phenomenon(res);
  313. } else
  314. return null;
  315. }
  316. /**
  317. * Select phenomenon by given name
  318. * @param phenName - name of phenomenon to select
  319. * @return Phenomenon object if there is phenomenon in DB or null if there is not
  320. * @throws SQLException
  321. */
  322. public Phenomenon getPhenomenonByName(String phenName) throws SQLException{
  323. String query = "SELECT * FROM phenomenons WHERE phenomenon_name='"+phenName+"';";
  324. ResultSet res = SQLExecutor.getInstance().executeQuery(query);
  325. if(res.next()){
  326. return new Phenomenon(res);
  327. } else{
  328. return null;
  329. }
  330. }
  331. /**
  332. * Method get next value of phenomenonId sequence
  333. * @return next value of phenomenonId, null if there is not possible to get next value
  334. * @throws SQLException
  335. */
  336. public String getNextPhenomenonId() throws SQLException{
  337. boolean exists = true;
  338. String newId = null;
  339. while(exists){
  340. try{
  341. String selectId = "SELECT nextval('phenomenons_id_seq'::regclass);";
  342. ResultSet res = SQLExecutor.getInstance().executeQuery(selectId);
  343. if(res.next()){
  344. newId = res.getString(1);
  345. } else{
  346. throw new SQLException("Phenomenon can't get new ID!");
  347. }
  348. Phenomenon isSame = getPhenomenonById(newId);
  349. if(isSame == null){
  350. exists = false;
  351. }
  352. } catch(SQLException e){
  353. throw new SQLException("Phenomenon can't get new ID!");
  354. }
  355. }
  356. return newId;
  357. }
  358. /**
  359. * Method checks if there is same phenomenon already in the DB
  360. * @param phen Phenomenon to be checked
  361. * @return Phenomenon object that is already in the DB, null if there is not any
  362. * @throws SQLException
  363. */
  364. public Phenomenon isPhenomenonInDB(Phenomenon phen) throws SQLException{
  365. if(phen.internalGetPhenomenonId() != null){
  366. Phenomenon phenDB = getPhenomenonById(phen.internalGetPhenomenonId());
  367. return phenDB;
  368. } else if(phen.getPhenomenonName() != null && phen.getUnit() != null){
  369. Phenomenon phenDB = getPhenomenonByName(phen.getPhenomenonName());
  370. if(phenDB == null){
  371. return null;
  372. } else{
  373. if(phen.internalGetPhenomenonId() == null){
  374. phen.internalSetPhenomenonId(phenDB.internalGetPhenomenonId());
  375. }
  376. return phenDB;
  377. }
  378. } else {
  379. return null;
  380. }
  381. }
  382. /**
  383. * Method checks if there is already same sensor in the DB
  384. * @param sen Sensor to be checked
  385. * @return Sensor object that is already in the DB, null if there is not any
  386. * @throws SQLException
  387. */
  388. public Sensor isSensorInDB(Sensor sen) throws SQLException{
  389. if(sen.getSensorId() != 0){
  390. return getSensorByIdOrName(sen.getSensorId(), null);
  391. } else if(sen.getSensorName() != null
  392. && sen.getSensorType() != null
  393. && sen.getPhenomenon() != null)
  394. {
  395. Sensor senDB = getSensorByIdOrName(null, sen.getSensorName());
  396. if(senDB != null){
  397. Sensor sensorSame = getSensorByNameAndTypeAndPhen(
  398. sen.getSensorName(), sen.getSensorType(),
  399. sen.getPhenomenon().internalGetPhenomenonId());
  400. if(sensorSame == null){
  401. throw new SQLException("Sensor with given name is already registered!");
  402. } else {
  403. return sensorSame;
  404. }
  405. } else {
  406. return null;
  407. }
  408. }
  409. else{
  410. return null;
  411. }
  412. }
  413. /**
  414. * Method try tests if there is same Sensor in the DB
  415. * @param sen Sensor object to be compared
  416. * @return Sensor object from DB if there is same as given, null if there is not same
  417. * @throws SQLException
  418. */
  419. public Sensor isSameSensorInDB(Sensor sen) throws SQLException{
  420. if(sen.getSensorId() != 0){
  421. return getSensorByIdOrName(sen.getSensorId(), null);
  422. } else if(sen.getSensorName() != null
  423. && sen.getSensorType() != null
  424. && sen.getPhenomenon() != null)
  425. {
  426. Sensor senDB = getSensorByIdOrName(null, sen.getSensorName());
  427. if(senDB != null){
  428. Sensor sensorSame = getSensorByNameAndTypeAndPhen(
  429. sen.getSensorName(), sen.getSensorType(),
  430. sen.getPhenomenon().internalGetPhenomenonId());
  431. if(sensorSame == null){
  432. throw new SQLException("Sensor with given name is already registered!");
  433. } else {
  434. return sensorSame;
  435. }
  436. } else {
  437. return null;
  438. }
  439. } else{
  440. return null;
  441. }
  442. }
  443. /**
  444. * Method checks if given sensor is paired with given unit
  445. * @param sensorId - id of sensor
  446. * @param unitId - id of unit
  447. * @return true if sensor is paired, false if not
  448. * @throws SQLException
  449. */
  450. public boolean isSensorPairedToUnit(long sensorId, long unitId) throws SQLException{
  451. String query = "SELECT sensor_id, unit_id FROM units_to_sensors WHERE unit_id ="+unitId+" AND sensor_id ="+sensorId+";";
  452. ResultSet res = SQLExecutor.getInstance().executeQuery(query);
  453. return res.next();
  454. }
  455. /**
  456. * Method inserts new sensor in DB
  457. * @param sen - Sensor to be inserted
  458. * @return either (1) the row count for SQL DML statements or (2) 0 for SQL statements that return nothing
  459. * @throws SQLException
  460. */
  461. public int insertSensor(Sensor sen) throws SQLException{
  462. String ins = "INSERT INTO sensors (sensor_id, sensor_name, sensor_type, phenomenon_id)"
  463. + " VALUES ("+sen.getSensorId()
  464. +", '"+sen.getSensorName()
  465. +"', '"+sen.getSensorType()
  466. +"', '"+sen.getPhenomenon().internalGetPhenomenonId()+"');";
  467. return SQLExecutor.executeUpdate(ins);
  468. }
  469. /**
  470. * Method pairs unit to sensor
  471. * @param unitId - id of unit
  472. * @param sensorId - id of sensor
  473. * @return either (1) the row count for SQL DML statements or (2) 0 for SQL statements that return nothing
  474. * @throws SQLException
  475. */
  476. public int pairUnitToSensor(long unitId, long sensorId) throws SQLException{
  477. String insUS = "INSERT INTO units_to_sensors(unit_id, sensor_id) VALUES("+unitId+", "+sensorId+");";
  478. return SQLExecutor.executeUpdate(insUS);
  479. }
  480. /**
  481. * Method inserts new Phenomenon in DB
  482. * @param phen Phenomenon object to be inserted, all attributes must by given
  483. * @return either (1) the row count for SQL DML statements or (2) 0 for SQL statements that return nothing
  484. * @throws SQLException
  485. */
  486. public int insertNewPhenomenon(Phenomenon phen) throws SQLException{
  487. String ins = "INSERT INTO phenomenons (phenomenon_id, phenomenon_name, unit)"
  488. + " VALUES ('"+phen.internalGetPhenomenonId()
  489. +"', '"+phen.getPhenomenonName()
  490. +"', '"+phen.getUnit()+"');";
  491. return SQLExecutor.executeUpdate(ins);
  492. }
  493. /**
  494. * Method gets last observation for given unit-sensor pair
  495. * @param unitId - identifier of unit
  496. * @param sensorId - identifier of sensor
  497. * @return ResultSet object represents last observation for given unit-sensor pair
  498. * @throws SQLException
  499. */
  500. @SuppressWarnings("unchecked")
  501. public List<ObservationValue> getSensorLastObservation(long unitId, long sensorId) throws SQLException {
  502. String query = "SELECT time_stamp, gid, observed_value"
  503. + " FROM units_to_sensors uts"
  504. + " LEFT JOIN observations o ON uts.last_obs = o.time_stamp"
  505. + " WHERE uts.unit_id = "+unitId
  506. + " AND uts.sensor_id = "+sensorId
  507. + " AND uts.sensor_id = o.sensor_id;";
  508. ResultSet res = stmt.executeQuery(query);
  509. return (List<ObservationValue>) generateObjectList(new ObservationValue(), res);
  510. }
  511. /**
  512. * Method gets list of last observations from all connected sensors for given unit
  513. * @param unitId - identifier of unit
  514. * @return list of UnitSensorObservation objects represents last observations from all connected sensors to given unit
  515. * @throws SQLException
  516. */
  517. @SuppressWarnings("unchecked")
  518. public List<UnitSensorObservation> getUnitSensorsLastObservations(long unitId) throws SQLException{
  519. String query = "SELECT time_stamp, gid, observed_value, o.sensor_id, o.unit_id"
  520. + " FROM units_to_sensors uts"
  521. + " LEFT JOIN observations o ON uts.last_obs = o.time_stamp"
  522. + " WHERE uts.unit_id = " + unitId
  523. + " AND uts.sensor_id = o.sensor_id;";
  524. ResultSet res = stmt.executeQuery(query);
  525. return (List<UnitSensorObservation>) generateObjectList(new UnitSensorObservation(), res);
  526. }
  527. /**
  528. * Method gets list of last observations from all connected sensors to all units belonging to given group
  529. * @param groupName - name of group
  530. * @return list of UnitSensorObservation objects represents last observations from all connected sensors to all units
  531. * belonging to given group
  532. * @throws SQLException
  533. */
  534. @SuppressWarnings("unchecked")
  535. public List<UnitSensorObservation> getUnitsSensorsLastObservations(String groupName) throws SQLException{
  536. String query = "SELECT time_stamp, gid, observed_value, o.sensor_id, o.unit_id"
  537. + " FROM groups g, units_to_groups utg, units_to_sensors uts"
  538. + " LEFT JOIN observations o ON uts.last_obs = o.time_stamp"
  539. + " WHERE g.group_name = '"+groupName+"'"
  540. + " AND g.id = utg.group_id"
  541. + " AND utg.unit_id = uts.unit_id"
  542. + " AND uts.unit_id = o.unit_id"
  543. + " AND uts.sensor_id = o.sensor_id"
  544. + " ORDER BY uts.unit_id, uts.sensor_id;";
  545. ResultSet res = stmt.executeQuery(query);
  546. return (List<UnitSensorObservation>) generateObjectList(new UnitSensorObservation(), res);
  547. }
  548. /**
  549. * Method gets list of last observations of given connected sensor of all units belonging to given group.
  550. * @param groupName - name of group
  551. * @param sensorId - identifier of sensor
  552. * @return list of UnitSensorObservation objects represents last observations for given connected sensors to all units
  553. * belonging to given group
  554. * @throws SQLException
  555. */
  556. @SuppressWarnings("unchecked")
  557. public List<UnitSensorObservation> getUnitsSensorsLastObservations(String groupName, long sensorId) throws SQLException{
  558. String query = "SELECT time_stamp, gid, observed_value, o.sensor_id, o.unit_id"
  559. + " FROM groups g, units_to_groups utg, units_to_sensors uts"
  560. + " LEFT JOIN observations o ON uts.last_obs = o.time_stamp"
  561. + " WHERE g.group_name = '"+groupName+"'"
  562. + " AND g.id = utg.group_id"
  563. + " AND utg.unit_id = uts.unit_id"
  564. + " AND uts.sensor_id = "+sensorId
  565. + " AND uts.unit_id = o.unit_id"
  566. + " AND uts.sensor_id = o.sensor_id"
  567. + " ORDER BY uts.unit_id, uts.sensor_id;";
  568. ResultSet res = stmt.executeQuery(query);
  569. return (List<UnitSensorObservation>) generateObjectList(new UnitSensorObservation(), res);
  570. }
  571. }