package com.ruoyi.monitorInterface.chenganshengbang.service.impl;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.ConfigService;
import com.ruoyi.monitorInterface.chenganshengbang.service.WaterPressureService;
import com.ruoyi.monitorInterface.sennor.service.impl.SennorServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WaterPressureServiceImpl implements WaterPressureService {

    private static final Logger log = LoggerFactory.getLogger(WaterPressureServiceImpl.class);

    @Resource
    private ConfigService configService;

    public void waterPressureMessageReceiver(String data){
        log.info("WaterPressure 接到数据 ==>{}",data);
        JSONObject object;
        String res;
        try {
            object = JSONObject.parseObject(data);
            res = HttpRequest.post(configService.getKey("centerMonitorURL")+"device/saveWaterPressureDeviceData")
                    .header("Content-Type", "application/json")
                    .body(object.toJSONString())
                    .execute().body();
            log.info("数据推送成功! data ===> {} , result ===> {}",data, res);
        }catch(Exception e){
            e.printStackTrace();
            log.error("数据推送失败! data ===> {} , result ===> {}",data, e.getMessage());
        }
    }

    public String command(String data){
        log.info("WaterPressure-command 接到指令 ==>{}",data);
        JSONObject object;
        String res = null;
        try {
            object = JSONObject.parseObject(data);
            res = HttpRequest.post(configService.getKey("commandURL"))
                    .header("Content-Type", "application/json")
                    .body(object.toJSONString())
                    .execute().body();
            log.info("指令下发成功! data ===> {} , result ===> {}",data, res);
            return res;
        }catch(Exception e){
            e.printStackTrace();
            log.error("指令下发失败! data ===> {} , result ===> {}",data, e.getMessage());
            return res;
        }
    }
}
