Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

346 linhas
8.4 KiB

  1. var RESLEVIS_API_BASE = "/frontend/api/reslevis";
  2. function reslevisRequest(path, options) {
  3. var url = RESLEVIS_API_BASE + path;
  4. var baseHeaders = {
  5. "Accept": "application/json"
  6. };
  7. var headers = baseHeaders;
  8. if (options && options.headers) {
  9. headers = {};
  10. var k1;
  11. for (k1 in baseHeaders) {
  12. if (Object.prototype.hasOwnProperty.call(baseHeaders, k1)) {
  13. headers[k1] = baseHeaders[k1];
  14. }
  15. }
  16. var k2;
  17. for (k2 in options.headers) {
  18. if (Object.prototype.hasOwnProperty.call(options.headers, k2)) {
  19. headers[k2] = options.headers[k2];
  20. }
  21. }
  22. }
  23. var fetchOptions = {};
  24. if (options) {
  25. var k3;
  26. for (k3 in options) {
  27. if (Object.prototype.hasOwnProperty.call(options, k3)) {
  28. fetchOptions[k3] = options[k3];
  29. }
  30. }
  31. }
  32. fetchOptions.headers = headers;
  33. console.log("API URL", url);
  34. console.log("API options", fetchOptions);
  35. return fetch(url, fetchOptions).then(function (response) {
  36. console.log("HTTP status", response.status, response.statusText);
  37. var hdrs = {};
  38. response.headers.forEach(function (value, key) {
  39. hdrs[key] = value;
  40. });
  41. console.log("HTTP headers", hdrs);
  42. return response.text().then(function (body) {
  43. console.log("RAW body", body);
  44. if (!response.ok) {
  45. throw new Error("HTTP " + response.status + " " + response.statusText + " - " + body);
  46. }
  47. if (!body) {
  48. return null;
  49. }
  50. var ct = response.headers.get("content-type") || "";
  51. if (ct.indexOf("application/json") === -1) {
  52. throw new Error("Expected JSON but got Content-Type=" + ct + " body=" + body);
  53. }
  54. try {
  55. return JSON.parse(body);
  56. } catch (e) {
  57. throw new Error("JSON parse error: " + e.message + " body=" + body);
  58. }
  59. });
  60. });
  61. }
  62. /* ===== Gateways ===== */
  63. function getGateways() {
  64. return reslevisRequest("/getGateways", { method: "GET" });
  65. }
  66. function postGateway(item) {
  67. return reslevisRequest("/postGateway", {
  68. method: "POST",
  69. headers: { "Content-Type": "application/json" },
  70. body: JSON.stringify(item)
  71. });
  72. }
  73. function updateGateway(item) {
  74. return reslevisRequest("/updateGateway", {
  75. method: "PUT",
  76. headers: { "Content-Type": "application/json" },
  77. body: JSON.stringify(item)
  78. });
  79. }
  80. function removeGateway(id) {
  81. return reslevisRequest("/removeGateway/" + encodeURIComponent(id), { method: "DELETE" });
  82. }
  83. /* ===== Buildings ===== */
  84. function getBuildings() {
  85. return reslevisRequest("/getBuildings", { method: "GET" });
  86. }
  87. function postBuilding(item) {
  88. return reslevisRequest("/postBuilding", {
  89. method: "POST",
  90. headers: { "Content-Type": "application/json" },
  91. body: JSON.stringify(item)
  92. });
  93. }
  94. function updateBuilding(item) {
  95. return reslevisRequest("/updateBuilding", {
  96. method: "PUT",
  97. headers: { "Content-Type": "application/json" },
  98. body: JSON.stringify(item)
  99. });
  100. }
  101. function removeBuilding(id) {
  102. return reslevisRequest("/removeBuilding/" + encodeURIComponent(id), { method: "DELETE" });
  103. }
  104. /* ===== Plans ===== */
  105. function getPlans() {
  106. return reslevisRequest("/getPlans", { method: "GET" });
  107. }
  108. function postPlan(item) {
  109. return reslevisRequest("/postPlan", {
  110. method: "POST",
  111. headers: { "Content-Type": "application/json" },
  112. body: JSON.stringify(item)
  113. });
  114. }
  115. function updatePlan(item) {
  116. return reslevisRequest("/updatePlan", {
  117. method: "PUT",
  118. headers: { "Content-Type": "application/json" },
  119. body: JSON.stringify(item)
  120. });
  121. }
  122. function removePlan(id) {
  123. return reslevisRequest("/removePlan/" + encodeURIComponent(id), { method: "DELETE" });
  124. }
  125. /* ===== Zones ===== */
  126. function getZones() {
  127. return reslevisRequest("/getZones", { method: "GET" });
  128. }
  129. function postZone(item) {
  130. return reslevisRequest("/postZone", {
  131. method: "POST",
  132. headers: { "Content-Type": "application/json" },
  133. body: JSON.stringify(item)
  134. });
  135. }
  136. function updateZone(item) {
  137. return reslevisRequest("/updateZone", {
  138. method: "PUT",
  139. headers: { "Content-Type": "application/json" },
  140. body: JSON.stringify(item)
  141. });
  142. }
  143. function removeZone(id) {
  144. return reslevisRequest("/removeZone/" + encodeURIComponent(id), { method: "DELETE" });
  145. }
  146. /* ===== Trackers ===== */
  147. function getTrackers() {
  148. return reslevisRequest("/getTrackers", { method: "GET" });
  149. }
  150. function postTracker(item) {
  151. return reslevisRequest("/postTracker", {
  152. method: "POST",
  153. headers: { "Content-Type": "application/json" },
  154. body: JSON.stringify(item)
  155. });
  156. }
  157. function updateTracker(item) {
  158. return reslevisRequest("/updateTracker", {
  159. method: "PUT",
  160. headers: { "Content-Type": "application/json" },
  161. body: JSON.stringify(item)
  162. });
  163. }
  164. function removeTracker(id) {
  165. return reslevisRequest("/removeTracker/" + encodeURIComponent(id), { method: "DELETE" });
  166. }
  167. /* ===== Operators ===== */
  168. function getOperators() {
  169. return reslevisRequest("/getOperators", { method: "GET" });
  170. }
  171. function postOperator(item) {
  172. return reslevisRequest("/postOperator", {
  173. method: "POST",
  174. headers: { "Content-Type": "application/json" },
  175. body: JSON.stringify(item)
  176. });
  177. }
  178. function updateOperator(item) {
  179. return reslevisRequest("/updateOperator", {
  180. method: "PUT",
  181. headers: { "Content-Type": "application/json" },
  182. body: JSON.stringify(item)
  183. });
  184. }
  185. function removeOperator(id) {
  186. return reslevisRequest("/removeOperator/" + encodeURIComponent(id), { method: "DELETE" });
  187. }
  188. /* ===== Subjects ===== */
  189. function getSubjects() {
  190. return reslevisRequest("/getSubjects", { method: "GET" });
  191. }
  192. function postSubject(item) {
  193. return reslevisRequest("/postSubject", {
  194. method: "POST",
  195. headers: { "Content-Type": "application/json" },
  196. body: JSON.stringify(item)
  197. });
  198. }
  199. function updateSubject(item) {
  200. return reslevisRequest("/updateSubject", {
  201. method: "PUT",
  202. headers: { "Content-Type": "application/json" },
  203. body: JSON.stringify(item)
  204. });
  205. }
  206. function removeSubject(id) {
  207. return reslevisRequest("/removeSubject/" + encodeURIComponent(id), { method: "DELETE" });
  208. }
  209. /* ===== Alarms ===== */
  210. function getAlarms() {
  211. return reslevisRequest("/getAlarms", { method: "GET" });
  212. }
  213. function postAlarm(item) {
  214. return reslevisRequest("/postAlarm", {
  215. method: "POST",
  216. headers: { "Content-Type": "application/json" },
  217. body: JSON.stringify(item)
  218. });
  219. }
  220. function updateAlarm(item) {
  221. return reslevisRequest("/updateAlarm", {
  222. method: "PUT",
  223. headers: { "Content-Type": "application/json" },
  224. body: JSON.stringify(item)
  225. });
  226. }
  227. function removeAlarm(id) {
  228. return reslevisRequest("/removeAlarm/" + encodeURIComponent(id), { method: "DELETE" });
  229. }
  230. /* ===== Tracks ===== */
  231. function getTracks() {
  232. return reslevisRequest("/getTracks", { method: "GET" });
  233. }
  234. function postTrack(item) {
  235. return reslevisRequest("/postTrack", {
  236. method: "POST",
  237. headers: { "Content-Type": "application/json" },
  238. body: JSON.stringify(item)
  239. });
  240. }
  241. function updateTrack(item) {
  242. return reslevisRequest("/updateTrack", {
  243. method: "PUT",
  244. headers: { "Content-Type": "application/json" },
  245. body: JSON.stringify(item)
  246. });
  247. }
  248. function removeTrack(id) {
  249. return reslevisRequest("/removeTrack/" + encodeURIComponent(id), { method: "DELETE" });
  250. }
  251. /* ===== Export global ===== */
  252. window.getGateways = getGateways;
  253. window.postGateway = postGateway;
  254. window.updateGateway = updateGateway;
  255. window.removeGateway = removeGateway;
  256. window.getBuildings = getBuildings;
  257. window.postBuilding = postBuilding;
  258. window.updateBuilding = updateBuilding;
  259. window.removeBuilding = removeBuilding;
  260. window.getPlans = getPlans;
  261. window.postPlan = postPlan;
  262. window.updatePlan = updatePlan;
  263. window.removePlan = removePlan;
  264. window.getZones = getZones;
  265. window.postZone = postZone;
  266. window.updateZone = updateZone;
  267. window.removeZone = removeZone;
  268. window.getTrackers = getTrackers;
  269. window.postTracker = postTracker;
  270. window.updateTracker = updateTracker;
  271. window.removeTracker = removeTracker;
  272. window.getOperators = getOperators;
  273. window.postOperator = postOperator;
  274. window.updateOperator = updateOperator;
  275. window.removeOperator = removeOperator;
  276. window.getSubjects = getSubjects;
  277. window.postSubject = postSubject;
  278. window.updateSubject = updateSubject;
  279. window.removeSubject = removeSubject;
  280. window.getAlarms = getAlarms;
  281. window.postAlarm = postAlarm;
  282. window.updateAlarm = updateAlarm;
  283. window.removeAlarm = removeAlarm;
  284. window.getTracks = getTracks;
  285. window.postTrack = postTrack;
  286. window.updateTrack = updateTrack;
  287. window.removeTrack = removeTrack;