作者 钟来

提交鱼儿乐数据到飞书

  1 +package com.ruoyi.quartz.task;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.google.gson.JsonObject;
  5 +import com.ruoyi.common.utils.FeishuUtil;
  6 +import com.ruoyi.common.utils.GsonConstructor;
  7 +import com.zhonglai.luhui.dao.service.PublicService;
  8 +import org.slf4j.Logger;
  9 +import org.slf4j.LoggerFactory;
  10 +import org.springframework.beans.factory.annotation.Autowired;
  11 +import org.springframework.stereotype.Component;
  12 +
  13 +import java.util.Date;
  14 +import java.util.List;
  15 +import java.util.Map;
  16 +
  17 +@Component("feiShuSubData")
  18 +public class FeiShuSubData {
  19 + private final Logger logger = LoggerFactory.getLogger(this.getClass());
  20 +
  21 + @Autowired
  22 + private PublicService publicService;
  23 +
  24 + /**
  25 + * 提交鱼儿乐集团-》南京养殖数据总表-》硬件抄表
  26 + * https://e1l9o8boha.feishu.cn/base/OcUYbxkuCakxYlsbrEUcXyqKnbf?table=tblSfMTyLc8OCPVX&view=vewbRhrhFU
  27 + */
  28 + public void subPenggongData()
  29 + {
  30 + List<Map<String,Object>> list = publicService.getObjectListBySQL("SELECT id,data_state FROM `liu_yu_le`.`device_info` WHERE user_login_name='15968860065' AND device_type=0");
  31 + subFeishuLuhuiList(list);
  32 + }
  33 +
  34 + private void subFeishuLuhuiList(List<Map<String,Object>> list)
  35 + {
  36 + if(null != list && list.size() != 0)
  37 + {
  38 + JSONObject field = new JSONObject();
  39 + field.put("记录时间",System.currentTimeMillis());
  40 + for (Map<String,Object> map:list)
  41 + {
  42 + String id = (String) map.get("id");
  43 + String data_state = (String) map.get("data_state");
  44 + if(null != data_state && !"".equals(data_state))
  45 + {
  46 +
  47 + JsonObject jsonObject = GsonConstructor.get().fromJson(data_state, JsonObject.class);
  48 + switch (id)
  49 + {
  50 + case "865019079529917_1_1":
  51 + if(jsonObject.has("41"))
  52 + {
  53 + field.put("8号池ORP值",jsonObject.get("41").getAsDouble());
  54 + }
  55 + break;
  56 + case "865019079529917_1_2":
  57 + if(jsonObject.has("105"))
  58 + {
  59 + field.put("8号池浊度",jsonObject.get("105").getAsDouble());
  60 + }
  61 + break;
  62 + case "865019079529917_1_3":
  63 + if(jsonObject.has("7"))
  64 + {
  65 + field.put("8号池氨氮",jsonObject.get("7").getAsDouble());
  66 + }
  67 + break;
  68 + case "865019079529917_1_4":
  69 + if(jsonObject.has("8"))
  70 + {
  71 + field.put("8号池PH",jsonObject.get("8").getAsDouble());
  72 + }
  73 + break;
  74 + case "865019079530196_1_1":
  75 + if(jsonObject.has("0"))
  76 + {
  77 + field.put("微滤机水温",jsonObject.get("0").getAsDouble());
  78 + }
  79 + if(jsonObject.has("1"))
  80 + {
  81 + field.put("微滤机溶氧",jsonObject.get("1").getAsDouble());
  82 + }
  83 + break;
  84 + case "865019079530196_1_2":
  85 + if(jsonObject.has("0"))
  86 + {
  87 + field.put("7号池水温",jsonObject.get("0").getAsDouble());
  88 + }
  89 + if(jsonObject.has("1"))
  90 + {
  91 + field.put("7号池溶氧",jsonObject.get("1").getAsDouble());
  92 + }
  93 + break;
  94 + case "865019079530196_1_3":
  95 + if(jsonObject.has("0"))
  96 + {
  97 + field.put("8号池水温",jsonObject.get("0").getAsDouble());
  98 + }
  99 + if(jsonObject.has("1"))
  100 + {
  101 + field.put("8号池溶氧",jsonObject.get("1").getAsDouble());
  102 + }
  103 + break;
  104 + }
  105 +
  106 + }
  107 + }
  108 + subFeishuLuhui(field);
  109 + }
  110 + }
  111 +
  112 + private static void subFeishuLuhui(JSONObject field)
  113 + {
  114 + FeishuUtil.subFeishuTables(FeishuUtil.gettenant_access_token("cli_a88a14d3b279d01c","Z3hpYKHeR1yR2aiv6Rp0mcnwKvehkzmT"),"OcUYbxkuCakxYlsbrEUcXyqKnbf","tblSfMTyLc8OCPVX",field);
  115 + }
  116 +}
  1 +package com.ruoyi.common.utils;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.google.gson.JsonObject;
  5 +import com.ruoyi.common.utils.html.HttpUtils;
  6 +import okhttp3.Request;
  7 +import okhttp3.Response;
  8 +import org.slf4j.Logger;
  9 +import org.slf4j.LoggerFactory;
  10 +
  11 +import java.io.IOException;
  12 +import java.util.HashMap;
  13 +import java.util.Map;
  14 +
  15 +public class FeishuUtil {
  16 + private static final Logger logger = LoggerFactory.getLogger(FeishuUtil.class);
  17 +
  18 + private static Map<String, TenantAccessToken> tenant_access_token_map = new HashMap<>();
  19 +
  20 + public static String gettenant_access_token(String yy_app_id,String yy_app_secret) {
  21 + if(tenant_access_token_map.containsKey(yy_app_id))
  22 + {
  23 + TenantAccessToken tenant_access_token = tenant_access_token_map.get(yy_app_id);
  24 + // 判断token是否未超时
  25 + if (tenant_access_token.getTenant_access_token() != null && System.currentTimeMillis() < tenant_access_token.getTokenExpireTime()) {
  26 + return tenant_access_token.getTenant_access_token();
  27 + }
  28 + }
  29 +
  30 + String url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal";
  31 + try {
  32 + Response response = HttpUtils.postJsonBody(url, builder -> builder.addHeader("Content-Type", "application/json; charset=utf-8"), jsonObject -> {
  33 + jsonObject.put("app_id", yy_app_id); // 替换为实际的 app_id
  34 + jsonObject.put("app_secret", yy_app_secret); // 替换为实际的 app_secret
  35 + });
  36 + String str = response.body().string();
  37 + JSONObject responseMap = JSONObject.parseObject(str, JSONObject.class);
  38 + if (responseMap != null && responseMap.getInteger("code")==0) {
  39 + TenantAccessToken tenant_access_token = new TenantAccessToken();
  40 + tenant_access_token.setTenant_access_token(responseMap.getString("tenant_access_token"));
  41 + // 设置token过期时间,假设token有效期为7200秒
  42 + tenant_access_token.setTokenExpireTime(System.currentTimeMillis() + responseMap.getInteger("expire") * 1000);
  43 + tenant_access_token_map.put(yy_app_id, tenant_access_token);
  44 + return tenant_access_token.getTenant_access_token();
  45 + }else {
  46 + throw new RuntimeException("Failed to get tenant_access_token: " + str);
  47 + }
  48 + } catch (IOException e) {
  49 + throw new RuntimeException("Failed to get tenant_access_token: " ,e);
  50 + }
  51 + }
  52 +
  53 + /**
  54 + * HTTP URL
  55 + * https://open.feishu.cn/open-apis/bitable/v1/apps/:app_token/tables/:table_id/records
  56 + * HTTP Method
  57 + * POST
  58 + * 接口频率限制
  59 + * 50 次/秒
  60 + *
  61 + * 请求头 :Authorization、Content-Type
  62 + */
  63 + public static void subFeishuTables(String tenant_access_token,String app_token,String table_id, JSONObject field )
  64 + {
  65 + String url = "https://open.feishu.cn/open-apis/bitable/v1/apps/"+app_token+"/tables/"+table_id+"/records";
  66 +
  67 + JSONObject params = new JSONObject();
  68 + params.put("fields",field);
  69 +
  70 + try {
  71 + Response response = HttpUtils.postJsonBody(url, builder -> {
  72 + builder.addHeader("Content-Type", "application/json");
  73 + builder.addHeader("Authorization", "Bearer "+tenant_access_token);
  74 + }, jsonObject -> {
  75 + jsonObject.put("fields", field);
  76 + });
  77 + String str = response.body().string();
  78 + JSONObject responseMap = JSONObject.parseObject(str, JSONObject.class);
  79 + if (responseMap != null && responseMap.getInteger("code")!=0)
  80 + {
  81 + logger.error("提交飞书表数据返回失败:{}",str);
  82 + }
  83 + } catch (IOException e) {
  84 + logger.error("提交飞书表数据异常",e);
  85 + }
  86 + }
  87 +}
1 -package com.zhonglai.luhui.api.controller.test.dto; 1 +package com.ruoyi.common.utils;
2 2
3 public class TenantAccessToken { 3 public class TenantAccessToken {
4 private String tenant_access_token; 4 private String tenant_access_token;
@@ -175,9 +175,10 @@ public class HttpUtils { @@ -175,9 +175,10 @@ public class HttpUtils {
175 Response response = okHttpClient.newCall(request).execute(); 175 Response response = okHttpClient.newCall(request).execute();
176 try { 176 try {
177 if (!response.isSuccessful()) { 177 if (!response.isSuccessful()) {
178 - throw new IOException("Unexpected code " + response); 178 + throw new IOException("Unexpected code " );
179 } 179 }
180 } catch (IOException e) { 180 } catch (IOException e) {
  181 + System.out.println(response.body().string());
181 response.body().close(); 182 response.body().close();
182 } 183 }
183 return response; 184 return response;