¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.fzzy.protocol.fzzydatabase; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.fzzy.api.data.GatewayDeviceProtocol; |
| | | import com.fzzy.api.data.GatewayDeviceType; |
| | | import com.fzzy.api.utils.BytesUtil; |
| | | import com.fzzy.api.utils.DateUtil; |
| | | import com.fzzy.api.utils.NumberUtil; |
| | | import com.fzzy.async.fzzy40.Fzzy40CommonService; |
| | | import com.fzzy.data.ConfigData; |
| | | import com.fzzy.gateway.GatewayUtils; |
| | | import com.fzzy.gateway.api.GatewayDeviceReportService; |
| | | import com.fzzy.gateway.api.GatewayRemoteManager; |
| | | import com.fzzy.gateway.api.GatewaySyncGranService; |
| | | import com.fzzy.gateway.data.BaseReqData; |
| | | import com.fzzy.gateway.data.BaseResp; |
| | | import com.fzzy.gateway.data.GrainCableData; |
| | | import com.fzzy.gateway.data.WeatherWebDto; |
| | | import com.fzzy.gateway.entity.GatewayDevice; |
| | | import com.fzzy.gateway.hx2023.ScConstant; |
| | | import com.fzzy.gateway.hx2023.data.*; |
| | | import com.fzzy.gateway.service.GatewayConfService; |
| | | import com.fzzy.gateway.service.GatewayDeviceTestPR; |
| | | import com.fzzy.protocol.ProtocolUtils; |
| | | import com.fzzy.protocol.bhzn.cmd.ReMessageBuilder; |
| | | import com.fzzy.protocol.data.THDto; |
| | | import com.fzzy.protocol.youxian1.client.ClientEngine; |
| | | import com.ld.io.api.InvokeResult; |
| | | import io.netty.channel.Channel; |
| | | import com.fzzy.async.fzzy40.entity.Fz40Grain; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 游ä»ååºåè®®-ç²®æ
ééå
¥å£ |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class FzzyDatabaseGatewayGrainService implements GatewaySyncGranService { |
| | | @Resource |
| | | private GatewayConfService confService; |
| | | @Resource |
| | | private GatewayRemoteManager gatewayRemoteManager; |
| | | @Resource |
| | | private ConfigData configData; |
| | | @Resource |
| | | private Fzzy40CommonService fzzy40CommonService; |
| | | @Resource |
| | | private GatewayDeviceTestPR gatewayDeviceTestPR; |
| | | |
| | | @Override |
| | | public String getGrainProtocol() { |
| | | return GatewayDeviceProtocol.GRAIN_FZZY_DATABASE.getCode(); |
| | | } |
| | | |
| | | /** |
| | | * @param reqData |
| | | * @return |
| | | */ |
| | | @Override |
| | | public synchronized BaseResp syncGrain(BaseReqData reqData) { |
| | | |
| | | BaseResp resp = new BaseResp(); |
| | | |
| | | GatewayDevice device = reqData.getDevice(); |
| | | |
| | | if (null == device) { |
| | | resp.setCode(500); |
| | | resp.setMsg("ç³»ç»æªè·åå°ä¸è¡è¿æ¥è®¾å¤ä¿¡æ¯ï¼æ æ³æ§è¡"); |
| | | log.error("----------------ç³»ç»æªè·åå°ä¸è¡è¿æ¥è®¾å¤ä¿¡æ¯,æ æ³æ§è¡---------"); |
| | | return resp; |
| | | } |
| | | try { |
| | | //仿°æ®åºæ¥è¯¢ææ°çæ°æ®ï¼éè¿mqttæ¨éä¸å» |
| | | //reqData = new BaseReqData(); |
| | | reqData.setProductId(device.getProductId()); |
| | | reqData.setDeviceName(device.getDeviceName()); |
| | | reqData.setDeviceId(device.getDeviceId()); |
| | | reqData.setDayTime(new Date()); |
| | | reqData.setDevice(device); |
| | | Date start = DateUtil.getCurZero(reqData.getDayTime()); |
| | | Date end = DateUtil.getNextZero(reqData.getDayTime()); |
| | | //è·åå°æ°æ® |
| | | String depotIdSys = device.getDepotIdSys(); |
| | | if (StringUtils.isEmpty(depotIdSys)) { |
| | | log.error("--------设å¤--{}-æªé
置系ç»ç¸å
³ä»åºç¼ç ï¼æ æ³æ§è¡å½åæä½", device.getDeviceName()); |
| | | return new BaseResp(BaseResp.CODE_500, "æªé
置系ç»ç¸å
³ä»åºç¼ç ï¼æ æ³æ§è¡å½åæä½"); |
| | | } |
| | | |
| | | List<Fz40Grain> listGrain = fzzy40CommonService.listGrain(depotIdSys, start, end); |
| | | if (null == listGrain || listGrain.isEmpty()) { |
| | | log.error("---------设å¤-{}-ç³»ç»ä»åºç¼ç -{}-æªåæ¥å°ç²®æ
ä¿¡æ¯ï¼è¯·ç¡®è®¤å½åæ¡ä»¶ä¸æ¯å¦ææ°æ®", device.getDeviceName(), device.getDepotIdSys()); |
| | | return new BaseResp(BaseResp.CODE_500, "æªåæ¥å°ç²®æ
ä¿¡æ¯ï¼è¯·ç¡®è®¤å½åæ¡ä»¶ä¸æ¯å¦ææ°æ®"); |
| | | } |
| | | |
| | | //è·åç¬¬ä¸æ¡æ°æ®æ§è¡æ¨é |
| | | Fz40Grain lastData = listGrain.get(0); |
| | | log.info(lastData.toString()); |
| | | lastData.setReceiveDate(new Date()); |
| | | GrainData grain = fz40Grain2GrainData(reqData, lastData, device); |
| | | //æ¨é |
| | | Thread.sleep(3000); |
| | | doPushGrain(reqData,grain); |
| | | } catch (Exception e) { |
| | | log.error("ç²®æ
æ£æµå¼å¸¸ï¼{}", e); |
| | | resp.setCode(500); |
| | | resp.setMsg("å¹³å°------>>>>æ§å¶æï¼åéç²®æ
æ£æµå½ä»¤ï¼" + e.getMessage()); |
| | | return resp; |
| | | } |
| | | return resp; |
| | | } |
| | | |
| | | |
| | | private GrainData fz40Grain2GrainData(BaseReqData reqData ,Fz40Grain grainData, GatewayDevice device) { |
| | | GrainCableData cableData = GatewayUtils.getCableData(device); |
| | | GrainData grain = new GrainData(); |
| | | //å±è¡å |
| | | int cableZ = cableData.getCableZ(); |
| | | int cableY = cableData.getCableY(); |
| | | int cableX = cableData.getCableX(); |
| | | |
| | | //温度éå |
| | | String[] attr = grainData.getPoints().split(","); |
| | | grain.setMessageId(ScConstant.getMessageId()); |
| | | grain.setDeviceId(device.getDeviceId()); |
| | | grain.setTimestamp(System.currentTimeMillis() + ""); |
| | | |
| | | ClientHeaders headers = new ClientHeaders(); |
| | | headers.setDeviceName(device.getDeviceName()); |
| | | headers.setProductId(device.getProductId()); |
| | | headers.setOrgId(device.getOrgId()); |
| | | headers.setMsgId(reqData.getMessageId()); |
| | | grain.setHeaders(headers); |
| | | |
| | | |
| | | GrainOutPut outPut = new GrainOutPut(); |
| | | |
| | | |
| | | double max = ReMessageBuilder.MAX_TEMP, min = ReMessageBuilder.MIN_TEMP, sumT = 0.0, sumNum = 0; |
| | | |
| | | List<GrainTemp> temperature = new ArrayList<>(); |
| | | //æ ¹å· |
| | | int cableNum = 1, position = 0; |
| | | |
| | | double curTemp; |
| | | int x = 0, y = 0, z = 0; |
| | | for (int i = 0; i < attr.length; i++) { |
| | | curTemp = Double.valueOf(attr[i]); |
| | | position = i; |
| | | z = i % cableZ + 1; |
| | | x = i / (cableZ * cableY); |
| | | y = x * (cableZ * cableY); |
| | | y = (i - y) / cableZ; |
| | | //æ ¹å· |
| | | cableNum = (i / cableZ) + 1; |
| | | |
| | | temperature.add(new GrainTemp(cableNum + "", z + "", curTemp + "", position + "")); |
| | | |
| | | if (curTemp > max && curTemp < 40) { |
| | | max = curTemp; |
| | | } |
| | | if (curTemp < min && curTemp > 3) { |
| | | min = curTemp; |
| | | } |
| | | if(curTemp > 3 && curTemp < 40){ |
| | | sumT += curTemp; |
| | | sumNum++; |
| | | } |
| | | } |
| | | |
| | | if (sumNum == 0) { |
| | | sumNum = 1; |
| | | log.warn("---å½åç²®æ
ééå¼å¸¸--"); |
| | | } |
| | | //è¿æ»¤æ¯è¾ç¨çæå¤§æå°å¼ |
| | | if (max == ReMessageBuilder.MAX_TEMP) { |
| | | max = 0.0; |
| | | } |
| | | if (min == ReMessageBuilder.MIN_TEMP) { |
| | | min = 0.0; |
| | | } |
| | | |
| | | outPut.setTemperature(temperature); |
| | | outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + ""); |
| | | outPut.setMinTemperature(min + ""); |
| | | outPut.setMaxTemperature(max + ""); |
| | | List<GrainTH> ths = new ArrayList<>(); |
| | | |
| | | |
| | | //è·å温湿度 |
| | | ths.add(new GrainTH(grainData.getTempIn() != null ? grainData.getTempIn() + "" : "", grainData.getHumidityIn() != null ? grainData.getHumidityIn() + "" : "", "1")); |
| | | outPut.setTemperatureAndhumidity(ths); |
| | | grain.setOutput(JSONObject.toJSONString(outPut)); |
| | | |
| | | GatewayDevice gatewayDeviceWeather = GatewayUtils.getCacheByDeviceTypeOne(GatewayDeviceType.TYPE_09.getCode()); |
| | | |
| | | //ç³»ç»æ°è±¡ç«ä¿¡æ¯ |
| | | WeatherWebDto weather = WeatherWebDto.contextMap.get("default"); |
| | | |
| | | //æ°è±¡ä¿¡æ¯ |
| | | GrainWeather weatherStation = new GrainWeather(); |
| | | weatherStation.setMessageId(ScConstant.getMessageId()); |
| | | weatherStation.setMessgeId(weatherStation.getMessageId()); |
| | | |
| | | if (null != gatewayDeviceWeather) { |
| | | weatherStation.setId(gatewayDeviceWeather.getDeviceId()); |
| | | } else { |
| | | weatherStation.setId(device.getDeviceId()); |
| | | } |
| | | weatherStation.setAirPressure(weather.getPressure()); |
| | | weatherStation.setHumidity(weather.getHumidity().replaceAll("%","")); |
| | | weatherStation.setPm(weather.getAir_pm25()); |
| | | weatherStation.setRadiation("0"); |
| | | weatherStation.setRainfallAmount(weather.getWea()); |
| | | weatherStation.setTemperature(weather.getTem()); |
| | | weatherStation.setWindDirection(weather.getWin()); |
| | | weatherStation.setWindPower(weather.getWin_meter()); |
| | | weatherStation.setWindSpeed(weather.getWin_speed()); |
| | | |
| | | grain.setWeatherStation(JSONObject.toJSONString(weatherStation)); |
| | | |
| | | //å°è£
å¥½çæ°æ® |
| | | log.info("---ç²®æ
ä¿¡æ¯å°è£
宿-å¼å§æ§è¡æ¨é"); |
| | | |
| | | reqData.setData(JSONObject.toJSONString(grain)); |
| | | return grain; |
| | | } |
| | | |
| | | private void doPushGrain(BaseReqData reqData,GrainData grainData) { |
| | | |
| | | GatewayDeviceReportService reportService = gatewayRemoteManager.getDeviceReportService(reqData.getDevice().getPushProtocol()); |
| | | if (null == reportService) { |
| | | log.error("------------ç²®æ
æ¨é失败ï¼ç³»ç»ä¸åå¨å½ååè®®æ§è¡ç±»----{}", reqData.getDevice().getDeviceName()); |
| | | return; |
| | | } |
| | | reportService.reportGrainData(reqData); |
| | | reqData.setData(reportService.grainData2GatewayApiInfoKafka(grainData,reqData.getDevice()).getData()); |
| | | reportService.reportGrainDataByKafka(reqData); |
| | | } |
| | | @Override |
| | | public BaseResp syncGrainTh(BaseReqData reqData) { |
| | | return new BaseResp(); |
| | | } |
| | | |
| | | @Override |
| | | public BaseResp syncConf(BaseReqData reqData) { |
| | | return new BaseResp(); |
| | | } |
| | | |
| | | @Override |
| | | public BaseResp writeConf(BaseReqData reqData) { |
| | | return new BaseResp(); |
| | | } |
| | | |
| | | @Override |
| | | public BaseResp initCable(BaseReqData reqData) { |
| | | return new BaseResp(); |
| | | } |
| | | |
| | | @Override |
| | | public BaseResp disconnect(BaseReqData reqData) { |
| | | return new BaseResp(); |
| | | } |
| | | |
| | | @Override |
| | | public BaseResp transparent(BaseReqData reqData) { |
| | | return new BaseResp(); |
| | | } |
| | | } |