diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ExcludeController.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ExcludeController.java index af6f29a..4684bf9 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ExcludeController.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/controller/ExcludeController.java @@ -1,7 +1,11 @@ package com.aiit.xiuos.controller; +import cn.hutool.json.JSONUtil; import com.aiit.xiuos.Utils.MyUtils; +import com.aiit.xiuos.model.GZJCData; import com.aiit.xiuos.scheduled.TaskScheduled; +import com.aiit.xiuos.service.GZJCService; import com.aiit.xiuos.service.impl.ProtocolServiceImpl; +import com.aiit.xiuos.socket.GZJCWebSocketServer; import com.aiit.xiuos.socket.WebSocketServer; import com.aiit.xiuos.tdengine.TDengineJDBCUtil; import com.alibaba.fastjson.JSONArray; @@ -9,9 +13,11 @@ import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; - import javax.servlet.http.HttpServletRequest; import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + @RestController @RequestMapping("/exclude") @Slf4j @@ -20,7 +26,10 @@ public class ExcludeController { ProtocolServiceImpl protocolService; @Autowired TaskScheduled taskScheduled; - + @Autowired + GZJCWebSocketServer GZJCWebSocketServer; + @Autowired + GZJCService GZJCService; @Autowired WebSocketServer webSocketServer; @PostMapping("/addjxsd") @@ -40,6 +49,16 @@ public class ExcludeController { } + + @PostMapping("/addgzjc") + public void addGZJC(@RequestBody GZJCData gzjcData, HttpServletRequest request) throws ParseException { + GZJCWebSocketServer.sendInfo(JSONUtil.toJsonStr(gzjcData)); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date date = sdf.parse(MyUtils.getTime()); + gzjcData.setTime(date); + GZJCService.insertData(gzjcData); + } + @GetMapping("/executeAlarmTask") public void executeTask(){ try { diff --git a/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/GZJCTaskScheduled.java b/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/GZJCTaskScheduled.java index 387bc6b..2f20565 100644 --- a/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/GZJCTaskScheduled.java +++ b/xiuosiot-backend/src/main/java/com/aiit/xiuos/scheduled/GZJCTaskScheduled.java @@ -27,91 +27,91 @@ public class GZJCTaskScheduled { @Autowired GZJCService GZJCService; - @Scheduled(cron = "0 0 */1 * * ?")//every hour - public void insertHaierData() throws ParseException { - GZJCData GZJCData =mockData(); - GZJCService.insertData(GZJCData); - - } - - @Scheduled(cron = "0 */1 * * * ?")//every minute - public void sendWebsocket() throws ParseException, IOException { - GZJCData GZJCData = mockData(); - JSONObject jsonObject = JSONUtil.parseObj(GZJCData,false,true); - GZJCWebSocketServer.sendInfo(jsonObject.toString()); - } - - public GZJCData mockData() throws ParseException { - GZJCData GZJCData =new GZJCData(); - Random rand = new Random(); - DecimalFormat df = new DecimalFormat("#.00"); //保留两位数,若数据库已截取,java可不作保留 - //生成在max与min之间的随机数 - // int randNumber = rand.nextInt(MAX - MIN + 1) + MIN; - - double o3 =rand.nextInt(10)+30+Math.random(); //O3 - GZJCData.setO3(new BigDecimal(df.format(o3))); - - double co2 =rand.nextInt(100)+200+Math.random(); //co2 - GZJCData.setCo2(new BigDecimal(df.format(co2))); - - double so2=0.0; //so2 - GZJCData.setSo2(new BigDecimal(df.format(so2))); - - double no2=0.0; //no2 - GZJCData.setNo2(new BigDecimal(df.format(no2))); - - double nh3=0.0; //nh3 - GZJCData.setNh3(new BigDecimal(df.format(nh3))); - - double tvoc = rand.nextInt(1)+Math.random(); //tvoc - GZJCData.setTvoc(new BigDecimal(df.format(tvoc))); - - double ch2o =0.0; //ch2o - GZJCData.setCh2o(new BigDecimal(df.format(ch2o))); - - double c2h5oh = rand.nextInt(10)+Math.random(); //c2h5oh - GZJCData.setC2h5oh(new BigDecimal(df.format(c2h5oh))); - - double ch4 = 0.0; //ch4 - GZJCData.setCh4(new BigDecimal(df.format(ch4))); - - double o2 = rand.nextInt(10)+20+Math.random(); //o2 - GZJCData.setO2(new BigDecimal(df.format(o2))); - - double aqs = rand.nextInt(30)+200+Math.random(); //aqs - GZJCData.setAqs(new BigDecimal(df.format(aqs))); - - double humidness = rand.nextInt(30)+30+Math.random(); //湿度 - GZJCData.setHumidness(new BigDecimal(df.format(humidness))); - - double temperature =rand.nextInt(10)+15+Math.random(); //温度 - GZJCData.setTemperature(new BigDecimal(df.format(temperature))); - - double pm1_0=rand.nextInt(20)+Math.random(); //pm1.0 ug/m3 - GZJCData.setPm1d0(new BigDecimal(df.format(pm1_0))); - - double pm2_5=rand.nextInt(20)+30+Math.random(); //pm2.5 ug/m3 - GZJCData.setPm2d5(new BigDecimal(df.format(pm2_5))); - - double pm10=rand.nextInt(20)+50+Math.random(); //pm10 ug/m3 - GZJCData.setPm10(new BigDecimal(df.format(pm10))); - - double windspeed = Math.random(); //风速m/s - GZJCData.setWindspeed(new BigDecimal(df.format(windspeed))); - - double winddirection = rand.nextInt(30)+200; //风向 - GZJCData.setWinddirection(new BigDecimal(df.format(winddirection))); - - double airpressure=rand.nextInt(100)+900; //气压 - GZJCData.setAirpressure(new BigDecimal(df.format(airpressure))); - - double noise =rand.nextInt(70)+30+Math.random(); //噪音 pd - GZJCData.setNoise(new BigDecimal(df.format(noise))); - - - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Date date = sdf.parse(MyUtils.getTime()); - GZJCData.setTime(date); - return GZJCData; - } +// @Scheduled(cron = "0 0 */1 * * ?")//every hour +// public void insertHaierData() throws ParseException { +// GZJCData GZJCData =mockData(); +// GZJCService.insertData(GZJCData); +// +// } +// +// @Scheduled(cron = "0 */1 * * * ?")//every minute +// public void sendWebsocket() throws ParseException, IOException { +// GZJCData GZJCData = mockData(); +// JSONObject jsonObject = JSONUtil.parseObj(GZJCData,false,true); +// GZJCWebSocketServer.sendInfo(jsonObject.toString()); +// } +// +// public GZJCData mockData() throws ParseException { +// GZJCData GZJCData =new GZJCData(); +// Random rand = new Random(); +// DecimalFormat df = new DecimalFormat("#.00"); //保留两位数,若数据库已截取,java可不作保留 +// //生成在max与min之间的随机数 +// // int randNumber = rand.nextInt(MAX - MIN + 1) + MIN; +// +// double o3 =rand.nextInt(10)+30+Math.random(); //O3 +// GZJCData.setO3(new BigDecimal(df.format(o3))); +// +// double co2 =rand.nextInt(100)+200+Math.random(); //co2 +// GZJCData.setCo2(new BigDecimal(df.format(co2))); +// +// double so2=0.0; //so2 +// GZJCData.setSo2(new BigDecimal(df.format(so2))); +// +// double no2=0.0; //no2 +// GZJCData.setNo2(new BigDecimal(df.format(no2))); +// +// double nh3=0.0; //nh3 +// GZJCData.setNh3(new BigDecimal(df.format(nh3))); +// +// double tvoc = rand.nextInt(1)+Math.random(); //tvoc +// GZJCData.setTvoc(new BigDecimal(df.format(tvoc))); +// +// double ch2o =0.0; //ch2o +// GZJCData.setCh2o(new BigDecimal(df.format(ch2o))); +// +// double c2h5oh = rand.nextInt(10)+Math.random(); //c2h5oh +// GZJCData.setC2h5oh(new BigDecimal(df.format(c2h5oh))); +// +// double ch4 = 0.0; //ch4 +// GZJCData.setCh4(new BigDecimal(df.format(ch4))); +// +// double o2 = rand.nextInt(10)+20+Math.random(); //o2 +// GZJCData.setO2(new BigDecimal(df.format(o2))); +// +// double aqs = rand.nextInt(30)+200+Math.random(); //aqs +// GZJCData.setAqs(new BigDecimal(df.format(aqs))); +// +// double humidness = rand.nextInt(30)+30+Math.random(); //湿度 +// GZJCData.setHumidness(new BigDecimal(df.format(humidness))); +// +// double temperature =rand.nextInt(10)+15+Math.random(); //温度 +// GZJCData.setTemperature(new BigDecimal(df.format(temperature))); +// +// double pm1_0=rand.nextInt(20)+Math.random(); //pm1.0 ug/m3 +// GZJCData.setPm1d0(new BigDecimal(df.format(pm1_0))); +// +// double pm2_5=rand.nextInt(20)+30+Math.random(); //pm2.5 ug/m3 +// GZJCData.setPm2d5(new BigDecimal(df.format(pm2_5))); +// +// double pm10=rand.nextInt(20)+50+Math.random(); //pm10 ug/m3 +// GZJCData.setPm10(new BigDecimal(df.format(pm10))); +// +// double windspeed = Math.random(); //风速m/s +// GZJCData.setWindspeed(new BigDecimal(df.format(windspeed))); +// +// double winddirection = rand.nextInt(30)+200; //风向 +// GZJCData.setWinddirection(new BigDecimal(df.format(winddirection))); +// +// double airpressure=rand.nextInt(100)+900; //气压 +// GZJCData.setAirpressure(new BigDecimal(df.format(airpressure))); +// +// double noise =rand.nextInt(70)+30+Math.random(); //噪音 pd +// GZJCData.setNoise(new BigDecimal(df.format(noise))); +// +// +// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); +// Date date = sdf.parse(MyUtils.getTime()); +// GZJCData.setTime(date); +// return GZJCData; +// } }