package com.sunwin.metro.source;

import com.alibaba.fastjson.JSONObject;
import com.sunwin.metro.bean.*;
import com.sunwin.metro.utils.DateUtils;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;

import java.util.Calendar;
import java.util.Date;

import static com.sunwin.metro.utils.StringUtils.m2;

/**
 * @program: sunwin_metro
 * @description: 模拟生成设备数据源
 * @author: xuYJ
 * @create: 2021-12-01 15:32
 **/
public class CreateMetroMes extends RichSourceFunction<String> {
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    @Override
    public void run(SourceContext<String> ctx) throws Exception {

        //生成固定变化值
        BasTefMes basTefMes = new BasTefMes();
        basTefMes.setAi_comm_word(m2(220 + Math.random() * 10));
        basTefMes.setAi_faxlet(m2(80 + Math.random() * 10));
        basTefMes.setAi_y_vibr(m2(20 + Math.random() * 5));
        basTefMes.setDi_commokst(false);
        basTefMes.setDi_vfflt(false);

        BasTvfMes basTvfMes = new BasTvfMes();
        basTvfMes.setAi_x_vibr(m2(20 + Math.random() * 5));
        basTvfMes.setAi_faxlet(m2(80 + Math.random() * 10));
        basTvfMes.setAi_y_vibr(m2(20 + Math.random() * 5));
        basTvfMes.setDi_vfflt(false);
        basTvfMes.setDi_commokst(false);

        UpsMes upsMes = new UpsMes();
        upsMes.setDp36(m2(100 + Math.random() * 5));
        upsMes.setDp32(m2(20 + Math.random() * 3));
        upsMes.setDp29(m2(380 + Math.random() * 10));
        upsMes.setDp23(false);
        upsMes.setDp30(false);


        UpsXjyMes upsMesXyj = new UpsXjyMes();
        upsMesXyj.setWd(m2(60 + Math.random() * 2));
        upsMesXyj.setZdl(m2(20 + Math.random() * 2));
        upsMesXyj.setZdy(m2(380 + Math.random() * 2));


        BasAtMes basAtMes = new BasAtMes();
        basAtMes.setAi_fltno(10 + (int) (Math.random() * 2));
        basAtMes.setAi_lr(m2(1000 + Math.random() * 5));
        basAtMes.setDi_commokst(false);
        basAtMes.setDi_m0vl1(false);

        int one = 0;
        int two = 0;
        int three = 0;
        int four = 0;

        while (true) {
            int minute = getMinute(new Date());
            //生成6/9个点 随机 向上/向下
            if (minute % 5 == 0) {
                if (one < 6) {
                    //6点连续向上
                    System.out.println("6点连续向上");
                    JSONObject jsonObject = new JSONObject();
                    basTefMes.setAi_comm_word(m2(basTefMes.getAi_comm_word() + Math.random() * 10));
                    basTefMes.setAi_faxlet(m2(basTefMes.getAi_faxlet() + Math.random() * 10));
                    basTefMes.setAi_y_vibr(m2(basTefMes.getAi_y_vibr() + Math.random() * 5));
                    basTefMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    basTefMes.setEventTime(new Date());
                    jsonObject.put("隧道风机", JSONObject.toJSONString(basTefMes));
                    basTvfMes.setAi_x_vibr(m2(basTvfMes.getAi_x_vibr() + Math.random() * 5));
                    basTvfMes.setAi_faxlet(m2(basTvfMes.getAi_faxlet() + Math.random() * 10));
                    basTvfMes.setAi_y_vibr(m2(basTvfMes.getAi_y_vibr() + Math.random() * 5));
                    basTvfMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    basTvfMes.setEventTime(new Date());
                    jsonObject.put("排热风机", JSONObject.toJSONString(basTvfMes));
                    upsMes.setDp36(m2(upsMes.getDp36() + Math.random() * 5));
                    upsMes.setDp32(m2(upsMes.getDp32() + Math.random() * 3));
                    upsMes.setDp29(m2(upsMes.getDp29() + Math.random() * 10));
                    upsMes.setEventTime(new Date());
                    upsMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("不间断电源", JSONObject.toJSONString(upsMes));
                    upsMesXyj.setWd(m2(upsMesXyj.getWd() + Math.random() * 5));
                    upsMesXyj.setZdl(m2(upsMesXyj.getZdl() + Math.random() * 3));
                    upsMesXyj.setZdy(m2(upsMesXyj.getZdy() + Math.random() * 10));
                    upsMesXyj.setEventTime(new Date());
                    upsMesXyj.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("ups巡检仪", JSONObject.toJSONString(upsMesXyj));
                    basAtMes.setAi_fltno(basAtMes.getAi_fltno() + (int) (Math.random() * 10));
                    basAtMes.setAi_lr(m2(basAtMes.getAi_lr() + Math.random() * 10));
                    basAtMes.setEventTime(new Date());
                    basAtMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("雨水泵", JSONObject.toJSONString(basAtMes));
                    ctx.collect(jsonObject.toString());
                    one++;
                }
                Thread.sleep(20000L);
            } else if (minute % 6 == 0) {
                if (two < 9) {
                    //9点连续向下
                    System.out.println("9点连续向下");
                    JSONObject jsonObject = new JSONObject();
                    basTefMes.setAi_comm_word(m2(basTefMes.getAi_comm_word() - Math.random() * 10));
                    basTefMes.setAi_faxlet(m2(basTefMes.getAi_faxlet() - Math.random() * 10));
                    basTefMes.setAi_y_vibr(m2(basTefMes.getAi_y_vibr() - Math.random() * 5));
                    basTefMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    basTefMes.setEventTime(new Date());
                    jsonObject.put("隧道风机", JSONObject.toJSONString(basTefMes));
                    basTvfMes.setAi_x_vibr(m2(basTvfMes.getAi_x_vibr() - Math.random() * 5));
                    basTvfMes.setAi_faxlet(m2(basTvfMes.getAi_faxlet() - Math.random() * 10));
                    basTvfMes.setAi_y_vibr(m2(basTvfMes.getAi_y_vibr() - Math.random() * 5));
                    basTvfMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    basTvfMes.setEventTime(new Date());
                    jsonObject.put("排热风机", JSONObject.toJSONString(basTvfMes));
                    upsMes.setDp36(m2(upsMes.getDp36() - Math.random() * 5));
                    upsMes.setDp32(m2(upsMes.getDp32() - Math.random() * 3));
                    upsMes.setDp29(m2(upsMes.getDp29() - Math.random() * 10));
                    upsMes.setEventTime(new Date());
                    upsMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("不间断电源", JSONObject.toJSONString(upsMes));
                    upsMesXyj.setWd(m2(upsMesXyj.getWd() - Math.random() * 5));
                    upsMesXyj.setZdl(m2(upsMesXyj.getZdl() - Math.random() * 3));
                    upsMesXyj.setZdy(m2(upsMesXyj.getZdy() - Math.random() * 10));
                    upsMesXyj.setEventTime(new Date());
                    upsMesXyj.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("ups巡检仪", JSONObject.toJSONString(upsMesXyj));
                    basAtMes.setAi_fltno(basAtMes.getAi_fltno() - (int) (Math.random() * 10));
                    basAtMes.setAi_lr(m2(basAtMes.getAi_lr() - Math.random() * 10));
                    basAtMes.setEventTime(new Date());
                    basAtMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("雨水泵", JSONObject.toJSONString(basAtMes));
                    two++;
                    ctx.collect(jsonObject.toString());
                }
                Thread.sleep(20000L);
            } else if (minute % 7 == 0) {
                if (three < 9) {
                    //9点连续向上
                    System.out.println("9点连续向上");
                    JSONObject jsonObject = new JSONObject();
                    basTefMes.setAi_comm_word(m2(basTefMes.getAi_comm_word() + Math.random() * 10));
                    basTefMes.setAi_faxlet(m2(basTefMes.getAi_faxlet() + Math.random() * 10));
                    basTefMes.setAi_y_vibr(m2(basTefMes.getAi_y_vibr() + Math.random() * 5));
                    basTefMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    basTefMes.setEventTime(new Date());
                    jsonObject.put("隧道风机", JSONObject.toJSONString(basTefMes));
                    basTvfMes.setAi_x_vibr(m2(basTvfMes.getAi_x_vibr() + Math.random() * 5));
                    basTvfMes.setAi_faxlet(m2(basTvfMes.getAi_faxlet() + Math.random() * 10));
                    basTvfMes.setAi_y_vibr(m2(basTvfMes.getAi_y_vibr() + Math.random() * 5));
                    basTvfMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    basTvfMes.setEventTime(new Date());
                    jsonObject.put("排热风机", JSONObject.toJSONString(basTvfMes));
                    upsMes.setDp36(m2(upsMes.getDp36() + Math.random() * 5));
                    upsMes.setDp32(m2(upsMes.getDp32() + Math.random() * 3));
                    upsMes.setDp29(m2(upsMes.getDp29() + Math.random() * 10));
                    upsMes.setEventTime(new Date());
                    upsMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("不间断电源", JSONObject.toJSONString(upsMes));
                    upsMesXyj.setWd(m2(upsMesXyj.getWd() + Math.random() * 5));
                    upsMesXyj.setZdl(m2(upsMesXyj.getZdl() + Math.random() * 3));
                    upsMesXyj.setZdy(m2(upsMesXyj.getZdy() + Math.random() * 10));
                    upsMesXyj.setEventTime(new Date());
                    upsMesXyj.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("ups巡检仪", JSONObject.toJSONString(upsMesXyj));
                    basAtMes.setAi_fltno(basAtMes.getAi_fltno() + (int) (Math.random() * 10));
                    basAtMes.setAi_lr(m2(basAtMes.getAi_lr() + Math.random() * 10));
                    basAtMes.setEventTime(new Date());
                    basAtMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("雨水泵", JSONObject.toJSONString(basAtMes));
                    ctx.collect(jsonObject.toString());
                    three++;
                }
                Thread.sleep(20000L);
            } else if (minute % 8 == 0) {
                if (four < 6) {
                    //6点连续向下
                    System.out.println("6点连续向下");
                    JSONObject jsonObject = new JSONObject();
                    basTefMes.setAi_comm_word(m2(basTefMes.getAi_comm_word() - Math.random() * 10));
                    basTefMes.setAi_faxlet(m2(basTefMes.getAi_faxlet() - Math.random() * 10));
                    basTefMes.setAi_y_vibr(m2(basTefMes.getAi_y_vibr() - Math.random() * 5));
                    basTefMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    basTefMes.setEventTime(new Date());
                    jsonObject.put("隧道风机", JSONObject.toJSONString(basTefMes));
                    basTvfMes.setAi_x_vibr(m2(basTvfMes.getAi_x_vibr() - Math.random() * 5));
                    basTvfMes.setAi_faxlet(m2(basTvfMes.getAi_faxlet() - Math.random() * 10));
                    basTvfMes.setAi_y_vibr(m2(basTvfMes.getAi_y_vibr() - Math.random() * 5));
                    basTvfMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    basTvfMes.setEventTime(new Date());
                    jsonObject.put("排热风机", JSONObject.toJSONString(basTvfMes));
                    upsMes.setDp36(m2(upsMes.getDp36() - Math.random() * 5));
                    upsMes.setDp32(m2(upsMes.getDp32() - Math.random() * 3));
                    upsMes.setDp29(m2(upsMes.getDp29() - Math.random() * 10));
                    upsMes.setEventTime(new Date());
                    upsMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("不间断电源", JSONObject.toJSONString(upsMes));
                    upsMesXyj.setWd(m2(upsMesXyj.getWd() - Math.random() * 5));
                    upsMesXyj.setZdl(m2(upsMesXyj.getZdl() - Math.random() * 3));
                    upsMesXyj.setZdy(m2(upsMesXyj.getZdy() - Math.random() * 10));
                    upsMesXyj.setEventTime(new Date());
                    upsMesXyj.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("ups巡检仪", JSONObject.toJSONString(upsMesXyj));
                    basAtMes.setAi_fltno(basAtMes.getAi_fltno() - (int) (Math.random() * 10));
                    basAtMes.setAi_lr(m2(basAtMes.getAi_lr() - Math.random() * 10));
                    basAtMes.setEventTime(new Date());
                    basAtMes.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                    jsonObject.put("雨水泵", JSONObject.toJSONString(basAtMes));
                    four++;
                    ctx.collect(jsonObject.toString());
                }
                Thread.sleep(20000L);
            } else {
                //System.out.println("生成随机值");
                //生成随机值
                JSONObject jsonObject = new JSONObject();
                //生成区间隧道风机
                BasTefMes basTefMes2 = new BasTefMes();
                basTefMes2.setAi_comm_word(m2(220 + Math.random() * 10));
                basTefMes2.setAi_faxlet(m2(80 + Math.random() * 10));
                basTefMes2.setAi_y_vibr(m2(20 + Math.random() * 5));
                basTefMes2.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                basTefMes2.setDi_commokst(Math.random() * 100 > 98);
                basTefMes2.setDi_vfflt(Math.random() * 100 > 98);
                basTefMes2.setEventTime(new Date());
                jsonObject.put("隧道风机", JSONObject.toJSONString(basTefMes2));
                //生成排热风机
                BasTvfMes basTvfMes2 = new BasTvfMes();
                basTvfMes2.setAi_x_vibr(m2(20 + Math.random() * 5));
                basTvfMes2.setAi_faxlet(m2(80 + Math.random() * 10));
                basTvfMes2.setAi_y_vibr(m2(20 + Math.random() * 5));
                basTvfMes2.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                basTvfMes2.setDi_vfflt(Math.random() * 100 > 98);
                basTvfMes2.setDi_commokst(Math.random() * 100 > 98);
                basTvfMes2.setEventTime(new Date());
                jsonObject.put("排热风机", JSONObject.toJSONString(basTvfMes2));
                //生成ups
                UpsMes upsMes2 = new UpsMes();
                upsMes2.setDp36(m2(100 + Math.random() * 5));
                upsMes2.setDp32(m2(20 + Math.random() * 3));
                upsMes2.setDp29(m2(380 + Math.random() * 10));
                upsMes2.setDp23(Math.random() * 100 > 98);
                upsMes2.setDp30(Math.random() * 100 > 98);
                upsMes2.setEventTime(new Date());
                upsMes2.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                jsonObject.put("不间断电源", JSONObject.toJSONString(upsMes2));
                //生成ups巡检仪
                UpsXjyMes upsMesXyj2 = new UpsXjyMes();
                upsMesXyj2.setWd(m2(60 + Math.random() * 5));
                upsMesXyj2.setZdl(m2(20 + Math.random() * 3));
                upsMesXyj2.setZdy(m2(380 + Math.random() * 10));
                upsMesXyj2.setEventTime(new Date());
                upsMesXyj2.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                jsonObject.put("ups巡检仪", JSONObject.toJSONString(upsMesXyj2));
                //生成雨水泵
                BasAtMes basAtMes2 = new BasAtMes();
                basAtMes2.setAi_fltno(10 + (int) (Math.random() * 10));
                basAtMes2.setAi_lr(m2(1000 + Math.random() * 10));
                basAtMes2.setDi_commokst(Math.random() * 100 > 98);
                basAtMes2.setDi_m0vl1(Math.random() * 100 > 98);
                basAtMes2.setEventTime(new Date());
                basAtMes2.setTime(DateUtils.dateToLongStringNoSep(new Date()));
                jsonObject.put("雨水泵", JSONObject.toJSONString(basAtMes2));
                ctx.collect(jsonObject.toString());
                one = 0;
                two = 0;
                three = 0;
                four = 0;
                Thread.sleep(20000L);
            }
        }
    }

    @Override
    public void cancel() {

    }
}
