石墨消解仪后端用nodejs编写,与嵌入式端交互和前端交互均用ws
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
8.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. const Environment = require("../model/environment.model");
  2. const { isIntTime } = require("../utils/common");
  3. const moment = require("moment");
  4. const { Op } = require("sequelize");
  5. class EnvironmentService {
  6. async getEnvironmentByCoopId(house_id, coop_id) {
  7. // 目前没有栋舍概念 暂且注释,取平均值
  8. const res = await Environment.findAll({
  9. where: {
  10. coop_id,
  11. house_id,
  12. },
  13. order: [["log_time", "DESC"]],
  14. });
  15. const arr = res.map((item) => item.dataValues);
  16. if (arr && arr.length > 0) {
  17. const len = arr.length;
  18. let tempTotal = 0;
  19. let humidityTotal = 0;
  20. let co2Total = 0;
  21. arr.map((i) => {
  22. tempTotal += parseInt(i.temperature);
  23. humidityTotal += parseInt(i.humidity);
  24. co2Total += parseInt(i.co2);
  25. });
  26. return {
  27. ...arr[0],
  28. temperature: (tempTotal / len).toFixed(2),
  29. humidity: (humidityTotal / len).toFixed(2),
  30. co2: (co2Total / len).toFixed(2),
  31. };
  32. }
  33. return null;
  34. }
  35. async addNewLog(
  36. house_id,
  37. coop_id,
  38. temperature,
  39. humidity,
  40. co2,
  41. nh3,
  42. illumination,
  43. wind_speed,
  44. h2s
  45. ) {
  46. // 判断当前时间是否为正点 如果是整点则记录
  47. // if (isIntTime()) {
  48. const log_time = moment().format("YYYY-MM-DD HH:mm:ss");
  49. const res = await Environment.create({
  50. house_id,
  51. coop_id,
  52. temperature,
  53. humidity,
  54. co2,
  55. nh3,
  56. illumination,
  57. wind_speed,
  58. h2s,
  59. log_time,
  60. });
  61. return res ? res.dataValues : null;
  62. // }
  63. // return null;
  64. }
  65. async getEnvironmentHistoryList(house_id, coop_id, time_id, indicator_id) {
  66. let whereObj = {
  67. house_id,
  68. coop_id,
  69. };
  70. // 需要根据id进行筛选 7天、全部、24小时
  71. if (time_id == "1") {
  72. // 24小时
  73. Object.assign(whereObj, {
  74. log_time: {
  75. [Op.lt]: new Date(),
  76. [Op.gt]: new Date(new Date() - 24 * 60 * 60 * 1000),
  77. },
  78. });
  79. }
  80. if (time_id == "2") {
  81. Object.assign(whereObj, {
  82. log_time: {
  83. [Op.lt]: new Date(),
  84. [Op.gt]: new Date(new Date() - 7 * 24 * 60 * 60 * 1000),
  85. },
  86. });
  87. }
  88. const res = await Environment.findAll({
  89. where: whereObj,
  90. });
  91. const arr = res.map((item) => item.dataValues);
  92. // 对数据进行处理
  93. let timeList = [];
  94. let temperatureList = [];
  95. let humidityList = [];
  96. let co2List = [];
  97. let nh3List = [];
  98. let illuminationList = [];
  99. let windSpeedList = [];
  100. let h2sList = [];
  101. if (time_id == "1") {
  102. // 24小时的数据 需要展示该小时的平均数据 默认展示7条 前端可以滚动展示
  103. let obj = {};
  104. arr.map((item) => {
  105. const hour = moment(item.log_time).format("HH");
  106. if (obj[hour]) {
  107. obj[hour] = [...obj[hour], item];
  108. } else {
  109. obj[hour] = [item];
  110. }
  111. });
  112. timeList = Object.keys(obj)?.map((item) => item + ":00");
  113. let dataObj = [];
  114. Object.values(obj)?.map((item) => {
  115. const len = item.length;
  116. if (len != 0) {
  117. let tempTotal = 0;
  118. let humidityTotal = 0;
  119. let co2Total = 0;
  120. let nh3Total = 0;
  121. let illuminationTotal = 0;
  122. let windSpeedTotal = 0;
  123. let h2sTotal = 0;
  124. item.map((i) => {
  125. tempTotal += parseInt(i.temperature);
  126. humidityTotal += parseInt(i.humidity);
  127. co2Total += parseInt(i.co2);
  128. nh3Total += parseInt(i.nh3);
  129. illuminationTotal += parseInt(i.illumination);
  130. windSpeedTotal += parseInt(i.wind_speed);
  131. h2sTotal += parseInt(i.h2s);
  132. });
  133. dataObj.push({
  134. temperature: (tempTotal / len).toFixed(2),
  135. humidity: (humidityTotal / len).toFixed(2),
  136. co2: (co2Total / len).toFixed(2),
  137. nh3: (nh3Total / len).toFixed(2),
  138. illumination: (illuminationTotal / len).toFixed(2),
  139. wind_speed: (windSpeedTotal / len).toFixed(2),
  140. h2s: (h2sTotal / len).toFixed(2),
  141. });
  142. }
  143. });
  144. temperatureList = dataObj.map((item) => item.temperature);
  145. humidityList = dataObj.map((item) => item.humidity);
  146. co2List = dataObj.map((item) => item.co2);
  147. nh3List = dataObj.map((item) => item.nh3);
  148. illuminationList = dataObj.map((item) => item.illumination);
  149. windSpeedList = dataObj.map((item) => item.wind_speed);
  150. h2sList = dataObj.map((item) => item.h2s);
  151. } else {
  152. // 7天和全部的按照 每天进行汇总,计算出每天的平均并返回 默认展示7条 前端可以滚动展示
  153. // TODO
  154. let obj = {};
  155. arr.map((item) => {
  156. const hour = moment(item.log_time).format("YYYY-MM-DD");
  157. if (obj[hour]) {
  158. obj[hour] = [...obj[hour], item];
  159. } else {
  160. obj[hour] = [item];
  161. }
  162. });
  163. timeList = Object.keys(obj);
  164. let dataObj = [];
  165. Object.values(obj)?.map((item) => {
  166. const len = item.length;
  167. if (len != 0) {
  168. let tempTotal = 0;
  169. let humidityTotal = 0;
  170. let co2Total = 0;
  171. let nh3Total = 0;
  172. let illuminationTotal = 0;
  173. let windSpeedTotal = 0;
  174. let h2sTotal = 0;
  175. item.map((i) => {
  176. tempTotal += parseInt(i.temperature);
  177. humidityTotal += parseInt(i.humidity);
  178. co2Total += parseInt(i.co2);
  179. nh3Total += parseInt(i.nh3);
  180. illuminationTotal += parseInt(i.illumination);
  181. windSpeedTotal += parseInt(i.wind_speed);
  182. h2sTotal += parseInt(i.h2s);
  183. });
  184. dataObj.push({
  185. temperature: (tempTotal / len).toFixed(2),
  186. humidity: (humidityTotal / len).toFixed(2),
  187. co2: (co2Total / len).toFixed(2),
  188. nh3: (nh3Total / len).toFixed(2),
  189. illumination: (illuminationTotal / len).toFixed(2),
  190. wind_speed: (windSpeedTotal / len).toFixed(2),
  191. h2s: (h2sTotal / len).toFixed(2),
  192. });
  193. }
  194. });
  195. temperatureList = dataObj.map((item) => item.temperature);
  196. humidityList = dataObj.map((item) => item.humidity);
  197. co2List = dataObj.map((item) => item.co2);
  198. nh3List = dataObj.map((item) => item.nh3);
  199. illuminationList = dataObj.map((item) => item.illumination);
  200. windSpeedList = dataObj.map((item) => item.wind_speed);
  201. h2sList = dataObj.map((item) => item.h2s);
  202. }
  203. // 根据所选监测数据进行筛选
  204. if (indicator_id == "1") {
  205. return {
  206. yData: temperatureList,
  207. xData: timeList,
  208. };
  209. }
  210. if (indicator_id == "2") {
  211. return {
  212. yData: nh3List,
  213. xData: timeList,
  214. };
  215. }
  216. if (indicator_id == "3") {
  217. return {
  218. yData: humidityList,
  219. xData: timeList,
  220. };
  221. }
  222. if (indicator_id == "4") {
  223. return {
  224. yData: co2List,
  225. xData: timeList,
  226. };
  227. }
  228. if (indicator_id == "5") {
  229. return {
  230. yData: illuminationList,
  231. xData: timeList,
  232. };
  233. }
  234. if (indicator_id == "6") {
  235. return {
  236. yData: windSpeedList,
  237. xData: timeList,
  238. };
  239. }
  240. if (indicator_id == "7") {
  241. return {
  242. yData: h2sList,
  243. xData: timeList,
  244. };
  245. }
  246. return null;
  247. }
  248. // 因为目前设备并没有鸡场、鸡舍相关概念 默认为0,没有的数据默认为0
  249. async reportEnvironmentData(env, positionM, log_time, coop_id, house_id) {
  250. // position_index, positionM, log_time;
  251. if (!env) {
  252. return;
  253. }
  254. const p = env.map(async (item) => {
  255. const res = await Environment.create({
  256. coop_id: parseInt(coop_id),
  257. house_id: parseInt(house_id),
  258. temperature: item.temperature,
  259. humidity: item.humidity,
  260. co2: item.co2,
  261. nh3: 0,
  262. illumination: 0,
  263. wind_speed: 0,
  264. h2s: 0,
  265. position_index: item.position_index,
  266. positionM,
  267. log_time,
  268. });
  269. return res;
  270. });
  271. const real = await Promise.all(p);
  272. return real;
  273. }
  274. // 获取环境实时监测数据
  275. async getRealTimeData(plies_number) {
  276. const res = await Environment.findAll();
  277. const arr = res?.map((item) => item.dataValues);
  278. return arr;
  279. }
  280. }
  281. module.exports = new EnvironmentService();