nantum.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. const Nantum = require('@hw/edge-sdks');
  3. const { API } = Nantum;
  4. const logger = require('../logger');
  5. const { company, nantumUrl } = require('../config');
  6. const { request } = API({ company, logger });
  7. async function getVen(clientCertificateFingerprint) {
  8. const results = await request({
  9. uri: `${nantumUrl}/oadr_vens`,
  10. query: {
  11. client_certificate_fingerprint: clientCertificateFingerprint,
  12. },
  13. });
  14. return results[0];
  15. }
  16. async function createVen(ven) {
  17. await request({
  18. method: 'POST',
  19. uri: `${nantumUrl}/oadr_vens`,
  20. body: ven,
  21. });
  22. }
  23. async function updateVen(id, newProperties) {
  24. await request({
  25. method: 'PUT',
  26. uri: `${nantumUrl}/oadr_vens/${id}`,
  27. body: newProperties,
  28. });
  29. }
  30. async function getEventResponse(ven, eventId, modificationNumber) {
  31. const results = await request({
  32. uri: `${nantumUrl}/oadr_event_responses`,
  33. query: {
  34. ven_id: ven._id,
  35. event_id: eventId,
  36. modification_number: modificationNumber,
  37. },
  38. });
  39. return results[0];
  40. }
  41. async function createEventResponse(eventResponse) {
  42. await request({
  43. method: 'POST',
  44. uri: `${nantumUrl}/oadr_event_responses`,
  45. body: eventResponse,
  46. });
  47. }
  48. async function updateEventResponse(id, newProperties) {
  49. await request({
  50. method: 'PUT',
  51. uri: `${nantumUrl}/oadr_event_responses/${id}`,
  52. body: newProperties,
  53. });
  54. }
  55. async function markEventAsSeen(ven, eventId, modificationNumber) {
  56. //TODO: potentially racy. Consider breaking out `seen_events` into its own document.
  57. const existing = ven.seen_events || [];
  58. await request({
  59. method: 'PUT',
  60. uri: `${nantumUrl}/oadr_vens/${ven._id}`,
  61. body: {
  62. seen_events: [
  63. ...existing,
  64. {
  65. event_id: eventId,
  66. modification_number: modificationNumber,
  67. },
  68. ],
  69. },
  70. });
  71. }
  72. async function getEvents() {
  73. return await request({
  74. uri: `${nantumUrl}/oadr_events`,
  75. query: {},
  76. });
  77. }
  78. async function sendReportReadings(ven, readings) {
  79. logger.info(
  80. 'received report readings',
  81. ven._id,
  82. JSON.stringify(readings, null, 2),
  83. );
  84. }
  85. module.exports = {
  86. getEvents,
  87. getEventResponse,
  88. createEventResponse,
  89. updateEventResponse,
  90. markEventAsSeen,
  91. getVen,
  92. updateVen,
  93. createVen,
  94. sendReportReadings,
  95. };