石墨消解仪后端用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.

378 lines
11 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
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 { getCoopIdByUUID } = require("./coop.service");
  3. const { getHouseIdByUUID } = require("./house.service");
  4. const { isIntTime } = require("../utils/common");
  5. const moment = require("moment");
  6. const { Op } = require("sequelize");
  7. class EnvironmentService {
  8. async getEnvironmentByCoopId(house_id, coop_id) {
  9. // 目前没有栋舍概念 暂且注释,取平均值
  10. const res = await Environment.findAll({
  11. where: {
  12. coop_id,
  13. house_id,
  14. },
  15. order: [["log_time", "DESC"]],
  16. });
  17. const arr = res.map((item) => item.dataValues);
  18. if (arr && arr.length > 0) {
  19. const len = arr.length;
  20. let tempTotal = 0;
  21. let humidityTotal = 0;
  22. let co2Total = 0;
  23. arr.map((i) => {
  24. tempTotal += parseFloat(i.temperature);
  25. humidityTotal += parseFloat(i.humidity);
  26. co2Total += parseFloat(i.co2);
  27. });
  28. return {
  29. ...arr[0],
  30. temperature: (tempTotal / len).toFixed(2),
  31. humidity: (humidityTotal / len).toFixed(2),
  32. co2: (co2Total / len).toFixed(2),
  33. };
  34. }
  35. return null;
  36. }
  37. async addNewLog(
  38. house_id,
  39. coop_id,
  40. temperature,
  41. humidity,
  42. co2,
  43. nh3,
  44. illumination,
  45. wind_speed,
  46. h2s
  47. ) {
  48. // 判断当前时间是否为正点 如果是整点则记录
  49. // if (isIntTime()) {
  50. const log_time = moment().format("YYYY-MM-DD HH:mm:ss");
  51. const res = await Environment.create({
  52. house_id,
  53. coop_id,
  54. temperature,
  55. humidity,
  56. co2,
  57. nh3,
  58. illumination,
  59. wind_speed,
  60. h2s,
  61. log_time,
  62. });
  63. return res ? res.dataValues : null;
  64. // }
  65. // return null;
  66. }
  67. async getEnvironmentHistoryList(house_id, coop_id, time_id, indicator_id) {
  68. let whereObj = {
  69. house_id,
  70. coop_id,
  71. };
  72. // 需要根据id进行筛选 7天、全部、24小时
  73. if (time_id == "1") {
  74. // 24小时
  75. Object.assign(whereObj, {
  76. log_time: {
  77. [Op.lt]: new Date(),
  78. [Op.gt]: new Date(new Date() - 24 * 60 * 60 * 1000),
  79. },
  80. });
  81. }
  82. if (time_id == "2") {
  83. Object.assign(whereObj, {
  84. log_time: {
  85. [Op.lt]: new Date(),
  86. [Op.gt]: new Date(new Date() - 7 * 24 * 60 * 60 * 1000),
  87. },
  88. });
  89. }
  90. const res = await Environment.findAll({
  91. where: whereObj,
  92. });
  93. const arr = res.map((item) => item.dataValues);
  94. // 对数据进行处理
  95. let timeList = [];
  96. let temperatureList = [];
  97. let humidityList = [];
  98. let co2List = [];
  99. let nh3List = [];
  100. let illuminationList = [];
  101. let windSpeedList = [];
  102. let h2sList = [];
  103. if (time_id == "1") {
  104. // 24小时的数据 需要展示该小时的平均数据 默认展示7条 前端可以滚动展示
  105. let obj = {};
  106. arr.map((item) => {
  107. const hour = moment(item.log_time).format("HH");
  108. if (obj[hour]) {
  109. obj[hour] = [...obj[hour], item];
  110. } else {
  111. obj[hour] = [item];
  112. }
  113. });
  114. timeList = Object.keys(obj)?.map((item) => item + ":00");
  115. let dataObj = [];
  116. Object.values(obj)?.map((item) => {
  117. const len = item.length;
  118. if (len != 0) {
  119. let tempTotal = 0;
  120. let humidityTotal = 0;
  121. let co2Total = 0;
  122. let nh3Total = 0;
  123. let illuminationTotal = 0;
  124. let windSpeedTotal = 0;
  125. let h2sTotal = 0;
  126. item.map((i) => {
  127. tempTotal += parseFloat(i.temperature);
  128. humidityTotal += parseFloat(i.humidity);
  129. co2Total += parseFloat(i.co2);
  130. nh3Total += parseFloat(i.nh3);
  131. illuminationTotal += parseFloat(i.illumination);
  132. windSpeedTotal += parseFloat(i.wind_speed);
  133. h2sTotal += parseFloat(i.h2s);
  134. });
  135. dataObj.push({
  136. temperature: (tempTotal / len).toFixed(2),
  137. humidity: (humidityTotal / len).toFixed(2),
  138. co2: (co2Total / len).toFixed(2),
  139. nh3: (nh3Total / len).toFixed(2),
  140. illumination: (illuminationTotal / len).toFixed(2),
  141. wind_speed: (windSpeedTotal / len).toFixed(2),
  142. h2s: (h2sTotal / len).toFixed(2),
  143. });
  144. }
  145. });
  146. temperatureList = dataObj.map((item) => item.temperature);
  147. humidityList = dataObj.map((item) => item.humidity);
  148. co2List = dataObj.map((item) => item.co2);
  149. nh3List = dataObj.map((item) => item.nh3);
  150. illuminationList = dataObj.map((item) => item.illumination);
  151. windSpeedList = dataObj.map((item) => item.wind_speed);
  152. h2sList = dataObj.map((item) => item.h2s);
  153. } else {
  154. // 7天和全部的按照 每天进行汇总,计算出每天的平均并返回 默认展示7条 前端可以滚动展示
  155. // TODO
  156. let obj = {};
  157. arr.map((item) => {
  158. const hour = moment(item.log_time).format("YYYY-MM-DD");
  159. if (obj[hour]) {
  160. obj[hour] = [...obj[hour], item];
  161. } else {
  162. obj[hour] = [item];
  163. }
  164. });
  165. timeList = Object.keys(obj);
  166. let dataObj = [];
  167. Object.values(obj)?.map((item) => {
  168. const len = item.length;
  169. if (len != 0) {
  170. let tempTotal = 0;
  171. let humidityTotal = 0;
  172. let co2Total = 0;
  173. let nh3Total = 0;
  174. let illuminationTotal = 0;
  175. let windSpeedTotal = 0;
  176. let h2sTotal = 0;
  177. item.map((i) => {
  178. tempTotal += parseFloat(i.temperature);
  179. humidityTotal += parseFloat(i.humidity);
  180. co2Total += parseFloat(i.co2);
  181. nh3Total += parseFloat(i.nh3);
  182. illuminationTotal += parseFloat(i.illumination);
  183. windSpeedTotal += parseFloat(i.wind_speed);
  184. h2sTotal += parseFloat(i.h2s);
  185. });
  186. dataObj.push({
  187. temperature: (tempTotal / len).toFixed(2),
  188. humidity: (humidityTotal / len).toFixed(2),
  189. co2: (co2Total / len).toFixed(2),
  190. nh3: (nh3Total / len).toFixed(2),
  191. illumination: (illuminationTotal / len).toFixed(2),
  192. wind_speed: (windSpeedTotal / len).toFixed(2),
  193. h2s: (h2sTotal / len).toFixed(2),
  194. });
  195. }
  196. });
  197. temperatureList = dataObj.map((item) => item.temperature);
  198. humidityList = dataObj.map((item) => item.humidity);
  199. co2List = dataObj.map((item) => item.co2);
  200. nh3List = dataObj.map((item) => item.nh3);
  201. illuminationList = dataObj.map((item) => item.illumination);
  202. windSpeedList = dataObj.map((item) => item.wind_speed);
  203. h2sList = dataObj.map((item) => item.h2s);
  204. }
  205. // 根据所选监测数据进行筛选
  206. if (indicator_id == "1") {
  207. return {
  208. yData: temperatureList,
  209. xData: timeList,
  210. };
  211. }
  212. if (indicator_id == "2") {
  213. return {
  214. yData: nh3List,
  215. xData: timeList,
  216. };
  217. }
  218. if (indicator_id == "3") {
  219. return {
  220. yData: humidityList,
  221. xData: timeList,
  222. };
  223. }
  224. if (indicator_id == "4") {
  225. return {
  226. yData: co2List,
  227. xData: timeList,
  228. };
  229. }
  230. if (indicator_id == "5") {
  231. return {
  232. yData: illuminationList,
  233. xData: timeList,
  234. };
  235. }
  236. if (indicator_id == "6") {
  237. return {
  238. yData: windSpeedList,
  239. xData: timeList,
  240. };
  241. }
  242. if (indicator_id == "7") {
  243. return {
  244. yData: h2sList,
  245. xData: timeList,
  246. };
  247. }
  248. return null;
  249. }
  250. // 因为目前设备并没有鸡场、鸡舍相关概念 默认为0,没有的数据默认为0
  251. async reportEnvironmentData(env, positionM, log_time, henhouseId, farmId) {
  252. try {
  253. // 根据uuid查询各自的id
  254. const coopInfo = await getCoopIdByUUID(henhouseId);
  255. const houseInfo = await getHouseIdByUUID(farmId);
  256. // position_index, positionM, log_time;
  257. if (!env || !coopInfo || !houseInfo) {
  258. return;
  259. }
  260. const p = env.map(async (item) => {
  261. // 查询传过来的coop_id和house_id
  262. if (item.layerIndex && item.lineIndex) {
  263. const res = await Environment.create({
  264. coop_id: coopInfo.id,
  265. house_id: houseInfo.id,
  266. temperature: item.temperature,
  267. humidity: item.humidity,
  268. co2: item.co2,
  269. nh3: 0,
  270. illumination: 0,
  271. wind_speed: 0,
  272. h2s: 0,
  273. position_index: item.position_index,
  274. layer_index: item.layerIndex,
  275. line_index: item.lineIndex,
  276. positionM,
  277. log_time,
  278. });
  279. return res;
  280. }
  281. });
  282. const real = await Promise.all(p);
  283. return real;
  284. } catch (error) {
  285. console.log(error);
  286. }
  287. }
  288. // 获取环境实时监测数据
  289. async getRealTimeData(house_id, coop_id, plies_number) {
  290. const res = await Environment.findAll({
  291. where: {
  292. house_id,
  293. coop_id,
  294. },
  295. order: [["log_time", "DESC"]],
  296. });
  297. const arr = res?.map((item) => item.dataValues);
  298. let filterArr = [];
  299. // 对arr进行筛选 将旧数据甩出
  300. arr.map((item) => {
  301. let temp = filterArr.filter(
  302. (it) =>
  303. it.line_index == item.line_index &&
  304. it.layer_index == item.layer_index &&
  305. it.positionM == item.positionM
  306. );
  307. if (temp && temp.length > 0) {
  308. } else {
  309. filterArr.push(item);
  310. }
  311. });
  312. let lastArr = [];
  313. filterArr.map((item) => {
  314. const layer_index = item.layer_index;
  315. if (plies_number == "1") {
  316. if (layer_index == 1) {
  317. // 第一层
  318. const temp = lastArr.filter((i) => i.index == item.line_index);
  319. if (temp && temp.length > 0) {
  320. lastArr.map((itl) => {
  321. if (itl.index == item.line_index) {
  322. itl.data?.push(item);
  323. }
  324. });
  325. } else {
  326. lastArr.push({
  327. index: item.line_index,
  328. data: [item],
  329. });
  330. }
  331. }
  332. }
  333. if (plies_number == "2") {
  334. if (layer_index == 2) {
  335. // 第二层
  336. const temp = lastArr.filter((i) => i.index == item.line_index);
  337. if (temp && temp.length > 0) {
  338. lastArr.map((itl) => {
  339. if (itl.index == item.line_index) {
  340. itl.data?.push(item);
  341. }
  342. });
  343. } else {
  344. lastArr.push({
  345. index: item.line_index,
  346. data: [item],
  347. });
  348. }
  349. }
  350. }
  351. if (plies_number == "3") {
  352. if (layer_index == 3) {
  353. // 第三层
  354. const temp = lastArr.filter((i) => i.index == item.line_index);
  355. if (temp && temp.length > 0) {
  356. lastArr.map((itl) => {
  357. if (itl.index == item.line_index) {
  358. itl.data?.push(item);
  359. }
  360. });
  361. } else {
  362. lastArr.push({
  363. index: item.line_index,
  364. data: [item],
  365. });
  366. }
  367. }
  368. }
  369. });
  370. return lastArr;
  371. }
  372. }
  373. module.exports = new EnvironmentService();