1 changed files with 174 additions and 0 deletions
@ -0,0 +1,174 @@ |
|||
package com.thing; |
|||
import cn.hutool.http.HttpUtil; |
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.http.*; |
|||
import org.springframework.web.client.RestTemplate; |
|||
|
|||
import java.io.ByteArrayOutputStream; |
|||
import java.io.PrintStream; |
|||
import java.io.UnsupportedEncodingException; |
|||
import java.net.URLEncoder; |
|||
import java.nio.ByteBuffer; |
|||
import java.nio.charset.StandardCharsets; |
|||
import java.security.Key; |
|||
import java.security.MessageDigest; |
|||
import java.util.Base64; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
import javax.crypto.Cipher; |
|||
import javax.crypto.spec.SecretKeySpec; |
|||
|
|||
/** |
|||
* @author: yu |
|||
* @date: 2024/12/21 12:05 |
|||
* @description: |
|||
*/ |
|||
public class ShineMonitorOpenApi { |
|||
|
|||
private static String OPEN_API_URI = "http://api.shinemonitor.com/public/"; |
|||
|
|||
private static final Map<String,String> CSLTokenCache = new HashMap<>(); |
|||
|
|||
/** 认证接口. */ |
|||
//获取token |
|||
public static String auth() throws UnsupportedEncodingException { |
|||
|
|||
String usr = "2022青原区村级电站"; /* 平台分配的账号, 密码, 厂家标识. */ |
|||
String pwd = "12345678"; |
|||
String companyKey = "wFeaA_Wd2R966GEQ"; |
|||
String source = "0"; |
|||
String _app_id_ = "com.demo.test"; |
|||
String _app_version_ = "3.6.2.1"; |
|||
String _app_client_ = "android"; |
|||
String salt = System.currentTimeMillis() + ""; /* 盐值. */ |
|||
String sha1Pwd = sha1ToLowerCase(pwd.getBytes()); /* SHA-1(pwd). */ |
|||
String action = "&action=auth&usr=" + URLEncoder.encode(usr, StandardCharsets.UTF_8.toString()) /* 注意: 中文需URLEncoder.encode. */ + "&company-key=" + companyKey |
|||
+ "&source=" + source + "&_app_id_=" + _app_id_ + "&_app_version_=" + _app_version_ + "&_app_client_=" + _app_client_; |
|||
String sign = sha1ToLowerCase((salt + sha1Pwd + action).getBytes()); /* SHA-1(slat + SHA-1(pwd) + action). */ |
|||
String request = OPEN_API_URI + "?sign=" + sign + "&salt=" + salt + action; |
|||
System.out.println(request); |
|||
return request; |
|||
} |
|||
|
|||
|
|||
/** 注册电站业主账号接口. */ |
|||
private static void reg() |
|||
{ |
|||
String usr = "plant-user-01"; |
|||
String pwd = "plant-user-01"; |
|||
String mobile = "15889700000"; |
|||
String email = "eybond@eybond.com"; |
|||
String pn = "0123456789ABCD"; |
|||
String sn = pn; |
|||
String companyKey = "0123456789ABCDEF"; |
|||
String source = "0"; |
|||
String _app_id_ = "com.demo.test"; |
|||
String _app_version_ = "3.6.2.1"; |
|||
String _app_client_ = "android"; |
|||
String salt = System.currentTimeMillis() + ""; /* 盐值. */ |
|||
String pwdSha1 = sha1ToLowerCase(pwd.getBytes()); /* SHA-1(PWD). */ |
|||
String pnSha1 = sha1ToLowerCase(pn.getBytes()); /* SHA-1(PN). */ |
|||
byte[] rc4 = rc4enc(pnSha1.getBytes(), pwdSha1.getBytes(), 0, pwdSha1.getBytes().length); /* RC4(SHA-1(PN), SHA-1(PWD)). */ |
|||
String action = "&action=reg&usr=" + usr /* 注意: 中文需URLEncoder.encode. */ + "&pwd=" + byte2HexStr(rc4, 0, rc4.length).toLowerCase(); |
|||
action += "&mobile=" + mobile + "&email=" + email + "&sn=" + sn + "&company-key=" + companyKey + "&source=" |
|||
+ source + "&_app_id_=" + _app_id_ + "&_app_version_=" + _app_version_ + "&_app_client_=" + _app_client_; |
|||
String sign = sha1ToLowerCase((salt + pwdSha1 + action).getBytes()); |
|||
String request = OPEN_API_URI + "?sign=" + sign + "&salt=" + salt + action; |
|||
System.out.println(request); |
|||
} |
|||
|
|||
/** 认证通过后的业务API接口. */ |
|||
public static String authPassed(String act) |
|||
{ |
|||
//String secret = CSLTokenCache.tokenMap.get("plantSecret"); /* 认证通过后的secret与token. */ |
|||
// String token = CSLTokenCache.tokenMap.get("plantToken"); |
|||
// |
|||
String secret = "8c8858760519b53426e7bd09d35412004ec46646"; /* 认证通过后的secret与token. */ |
|||
String token = "9bb029922c7bd51d07bf93535627b004ec6a29e3608bfce72aaf816e84183640"; |
|||
String source = "0"; |
|||
String _app_id_ = "com.demo.test"; |
|||
String _app_version_ = "3.6.2.1"; |
|||
String _app_client_ = "android"; |
|||
String salt = System.currentTimeMillis() + ""; /* 盐值. */ |
|||
String action = "&action="+ act + "&source=" + source + "&_app_id_=" |
|||
+ _app_id_ + "&_app_version_=" + _app_version_ + "&_app_client_=" + _app_client_; |
|||
String sign = sha1ToLowerCase((salt + secret + token + action).getBytes()); /* SHA-1(slat + secret + token + action). */ |
|||
String request = OPEN_API_URI + "?sign=" + sign + "&salt=" + salt + "&token=" + token + action; |
|||
System.out.println(request); |
|||
return request; |
|||
} |
|||
|
|||
/** 将字节流转换成十六进制字符串(紧凑格式, 无空格). */ |
|||
private static String byte2HexStr(byte by[], int ofst, int len) |
|||
{ |
|||
if (len < 1) |
|||
return ""; |
|||
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
|||
PrintStream ps = new PrintStream(bos); |
|||
for (int i = ofst; i < ofst + len; i++) |
|||
ps.printf("%02X", by[i]); |
|||
return bos.toString(); |
|||
} |
|||
|
|||
/** SHA-1摘要算法. */ |
|||
private static String sha1ToLowerCase(byte[] by) |
|||
{ |
|||
try |
|||
{ |
|||
byte dat[] = MessageDigest.getInstance("SHA-1").digest(by); |
|||
return byte2HexStr(dat, 0, dat.length).toLowerCase(); |
|||
} catch (Exception e) |
|||
{ |
|||
e.printStackTrace(); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
/** RC4加密. */ |
|||
public static byte[] rc4enc(byte key[], byte[] org, int ofst, int len) |
|||
{ |
|||
try |
|||
{ |
|||
Key k = new SecretKeySpec(key, "RC4"); |
|||
Cipher cip = Cipher.getInstance("RC4"); |
|||
cip.init(Cipher.ENCRYPT_MODE, k); |
|||
return cip.doFinal(org, ofst, len); |
|||
} catch (Exception e) |
|||
{ |
|||
e.printStackTrace(); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
public static void main(String[] args) throws UnsupportedEncodingException { |
|||
/* String auth = auth(); |
|||
String authUrl = HttpUtil.get(auth); |
|||
JSONObject jsonObject = JSONObject.parseObject(authUrl); |
|||
JSONObject jsonObject1 = jsonObject.getJSONObject("dat"); |
|||
String secret = jsonObject1.getString("secret"); |
|||
String token = jsonObject1.getString("token"); |
|||
|
|||
CSLTokenCache.put("plantSecret", secret); |
|||
CSLTokenCache.put("plantToken", token);*/ |
|||
|
|||
//电站 |
|||
String url1 = authPassed("queryPlants&orderBy=ascPlantName&page=0&pagesize=10"); |
|||
// 查询数采器 |
|||
String url2 = authPassed("queryCollectors&plantid=416002&page=0&pagesize=10"); |
|||
//数据采集器下的设备 |
|||
// String url3 = authPassed("queryCollectorDevices&pn=L0022362555526"); |
|||
//查询逆变器 |
|||
// String url4 = authPassed("queryDeviceRateActiveOutputPower&pn=L0022362555526&devcode=734&devaddr=2&sn=6T2289044210"); |
|||
//查询设备 |
|||
// String url = authPassed("queryDevices&plantid=425771&page=1&pagesize=10"); |
|||
String url = authPassed("queryDevices&page=0&plantid=416002&pn=L0022362347633&pagesize=10&devtype=0x0400"); |
|||
String url4 = authPassed("queryDeviceLastRawData&pn=L0022362347633&devcode=734&devaddr=1&sn=ES2280121217"); |
|||
String url54 = authPassed("queryDeviceEnergyTotal&pn=L0022362347633&devcode=734&devaddr=1&sn=ES2280121217"); |
|||
|
|||
String result = HttpUtil.get(url); |
|||
System.out.println(result); |
|||
|
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue