Selaa lähdekoodia

1.注释sdk包下文件;
2.注释自动发送数据逻辑。

Yumin 6 vuotta sitten
vanhempi
commit
fefa70c360

+ 271 - 271
src/main/java/cn/minbb/iot/sdk/RASRsdk.java

@@ -1,271 +1,271 @@
-package cn.minbb.iot.sdk;
-
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.ByteArrayEntity;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.util.EntityUtils;
-
-import javax.crypto.Mac;
-import javax.crypto.spec.SecretKeySpec;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.UnsupportedEncodingException;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.util.Base64;
-import java.util.Map;
-import java.util.Random;
-import java.util.TreeMap;
-import java.util.concurrent.TimeUnit;
-
-/**
- * 实时语音识别 SDK
- */
-public class RASRsdk {
-    private static String secret_key, secretid, appid, engine_model_type, res_type, result_text_format, voice_format, filepath, template_name;
-    private static int cutlength;
-
-    private static String generateUrl(String serverUrl, Map<String, String> mapReq) {
-        StringBuilder strBuilder = new StringBuilder(serverUrl);
-
-        if (mapReq.containsKey("appid")) {
-            strBuilder.append(mapReq.get("appid"));
-        }
-
-        strBuilder.append('?');
-
-        // to make that all the parameters are sorted by ASC order
-        TreeMap<String, String> sortedMap = new TreeMap(mapReq);
-
-        for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
-            if (entry.getKey().equals("appid")) {
-                continue;
-            }
-            strBuilder.append(entry.getKey());
-            strBuilder.append('=');
-            strBuilder.append(entry.getValue());
-            strBuilder.append('&');
-        }
-
-        if (mapReq.size() > 0) {
-            strBuilder.setLength(strBuilder.length() - 1);
-        }
-
-        return strBuilder.toString();
-    }
-
-    private static String base64_hmac_sha1(String value, String keyStr) {
-        String encoded = "";
-        String type = "HmacSHA1";
-        try {
-            byte[] key = (keyStr).getBytes("UTF-8");
-            byte[] Sequence = (value).getBytes("UTF-8");
-
-            Mac HMAC = Mac.getInstance(type);
-            SecretKeySpec secretKey = new SecretKeySpec(key, type);
-
-            HMAC.init(secretKey);
-            byte[] Hash = HMAC.doFinal(Sequence);
-
-            encoded = Base64.getEncoder().encodeToString(Hash);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return encoded;
-    }
-
-    /*
-     * UNIXEpoch 时间�?
-     */
-    private static String toUNIXEpoch() {
-        long unixTime = System.currentTimeMillis() / 1000L;
-        return unixTime + "";
-    }
-
-    /*
-     * ExpiredUNIXEpoch 时间�?
-     */
-    private static String toExpiredUNIXEpoch() {
-        long unixTime = System.currentTimeMillis() / 1000L + 24 * 60 * 60;
-        return unixTime + "";
-    }
-
-    private static String getNonce() {
-        Random random = new Random(System.currentTimeMillis());
-        return random.nextInt(10000) + "";
-    }
-
-    private static String createSign(String serverUrl, String secretKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
-        String strToBeEncoded = "POST" + serverUrl.substring(7);
-        //System.out.println("String to be encoded: " + strToBeEncoded);
-        return base64_hmac_sha1(strToBeEncoded, secretKey);
-    }
-
-    private static String getRandomString(int length) {
-        //定义一个字符串(A-Z,a-z,0-9)即62位;
-        String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
-        //由Random生成随机数
-        Random random = new Random();
-        StringBuffer sb = new StringBuffer();
-        //长度为几就循环几次
-        for (int i = 0; i < length; ++i) {
-            //产生0-61的数字
-            int number = random.nextInt(62);
-            //将产生的数字通过length次承载到sb中
-            sb.append(str.charAt(number));
-        }
-        //将承载的字符转换成字符串
-        return sb.toString();
-    }
-
-    public static int setConfig(
-            String secret_key,
-            String secretid,
-            String appid,
-            String engine_model_type,
-            String res_type,
-            String result_text_format,
-            String voice_format,
-            String filepath,
-            int cutlength
-    ) {
-        return RASRsdk.setConfig(secret_key, secretid, appid, engine_model_type, res_type, result_text_format, voice_format, filepath, cutlength, "");
-    }
-
-    public static int setConfig(
-            String secret_key,
-            String secretid,
-            String appid,
-            String engine_model_type,
-            String res_type,
-            String result_text_format,
-            String voice_format,
-            String filepath,
-            int cutlength,
-            String template_name
-
-    ) {
-
-        if (secret_key.length() <= 0) {
-            System.out.println("secret_key can not be empty!");
-            return -1;
-        }
-        if (secretid.length() <= 0) {
-            System.out.println("secretid can not be empty!");
-            return -1;
-        }
-        if (appid.length() <= 0) {
-            System.out.println("appid can not be empty!");
-            return -1;
-        }
-        if (engine_model_type.length() <= 0 || (engine_model_type.compareTo("8k_0") != 0 && engine_model_type.compareTo("16k_0") != 0)) {
-            System.out.println("engine_model_type is not vailed!");
-            return -1;
-        }
-        if (res_type.length() <= 0 || (res_type.compareTo("0") != 0 && res_type.compareTo("1") != 0)) {
-            System.out.println("res_type is not vailed!");
-            return -1;
-        }
-        if (result_text_format.length() <= 0 || (result_text_format.compareTo("0") != 0 && result_text_format.compareTo("1") != 0 &&
-                result_text_format.compareTo("2") != 0 && result_text_format.compareTo("3") != 0)) {
-            System.out.println("result_text_format is not vailed!");
-            return -1;
-        }
-        if (voice_format.length() <= 0 || (voice_format.compareTo("1") != 0 && voice_format.compareTo("4") != 0 && voice_format.compareTo("6") != 0)) {
-            System.out.println("voice_format is not vailed!");
-            return -1;
-        }
-        if (filepath.length() <= 0) {
-            System.out.println("filepath can not be empty!");
-            return -1;
-        }
-        if (cutlength < 0 || cutlength > 200000) {
-            System.out.println("cutlength is not vailed!");
-            return -1;
-        }
-        RASRsdk.secret_key = secret_key;
-        RASRsdk.secretid = secretid;
-        RASRsdk.appid = appid;
-        RASRsdk.engine_model_type = engine_model_type;
-        RASRsdk.res_type = res_type;
-        RASRsdk.result_text_format = result_text_format;
-        RASRsdk.voice_format = voice_format;
-        RASRsdk.filepath = filepath;
-        RASRsdk.cutlength = cutlength;
-        RASRsdk.template_name = template_name;
-
-        return 0;
-    }
-
-    public static int sendVoice() {
-        Map<String, String> reqMap = new TreeMap();
-        reqMap.put("appid", appid);
-        reqMap.put("secretid", secretid);
-        reqMap.put("projectid", "1013976");
-        if (template_name.compareTo("") != 0) {
-            reqMap.put("template_name", template_name);
-        }
-        reqMap.put("sub_service_type", "1");
-        reqMap.put("engine_model_type", engine_model_type);
-        reqMap.put("res_type", res_type);
-        reqMap.put("result_text_format", result_text_format);
-        reqMap.put("voice_id", getRandomString(16));
-        reqMap.put("timeout", "20000");
-        reqMap.put("source", "0");
-        reqMap.put("voice_format", voice_format);
-        reqMap.put("timestamp", toUNIXEpoch());
-        reqMap.put("expired", toExpiredUNIXEpoch());
-        reqMap.put("nonce", getNonce());
-        FileInputStream fileInputStream = null;
-        long starttime = System.currentTimeMillis();
-        try {
-            fileInputStream = new FileInputStream(new File(filepath));
-            byte[] dataPacket = new byte[cutlength];
-            CloseableHttpClient httpclient = HttpClients.createDefault();
-            int seq = 0, end = 0, n = 0;
-            while (true) {
-                n = fileInputStream.read(dataPacket);
-                if (n == cutlength || (n < cutlength && end == 0)) {
-                    if (n < cutlength) {
-                        n = (n >> 2) << 2;
-                        end = 1;
-                    }
-                    //System.out.println("n :" + n);
-                    reqMap.put("seq", seq + "");
-                    reqMap.put("end", end + "");
-                    //1、serverUrl
-                    String _url = "http://aai.qcloud.com/asr/v1/";
-                    String serverUrl = generateUrl(_url, reqMap);
-                   //System.out.println("Server URL: " + serverUrl);
-                    //2、设置header
-                    String authinfo = createSign(serverUrl, secret_key);
-                    //System.out.println("签名: " + authinfo);
-                    HttpPost hpost = new HttpPost(serverUrl);
-                    hpost.setHeader("Authorization", authinfo);
-                    hpost.setHeader("Content-Type", "application/octet-stream");
-                    hpost.setEntity(new ByteArrayEntity(dataPacket));
-                    CloseableHttpResponse response = httpclient.execute(hpost);
-                    try {
-                        //System.out.println("----------------------------------------");
-                        //System.out.println(response.getStatusLine());
-                        System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
-                    } finally {
-                        response.close();
-                    }
-                    seq++;
-                } else {
-                    break;
-                }
-                TimeUnit.MILLISECONDS.sleep(10);
-            }
-            httpclient.close();
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        long endtime = System.currentTimeMillis();
-        System.out.println("程序运行时间为:" + (endtime - starttime) / 1000 + "秒!");
-        return 0;
-    }
-}
+//package cn.minbb.iot.sdk;
+//
+//import org.apache.http.client.methods.CloseableHttpResponse;
+//import org.apache.http.client.methods.HttpPost;
+//import org.apache.http.entity.ByteArrayEntity;
+//import org.apache.http.impl.client.CloseableHttpClient;
+//import org.apache.http.impl.client.HttpClients;
+//import org.apache.http.util.EntityUtils;
+//
+//import javax.crypto.Mac;
+//import javax.crypto.spec.SecretKeySpec;
+//import java.io.File;
+//import java.io.FileInputStream;
+//import java.io.UnsupportedEncodingException;
+//import java.security.InvalidKeyException;
+//import java.security.NoSuchAlgorithmException;
+//import java.util.Base64;
+//import java.util.Map;
+//import java.util.Random;
+//import java.util.TreeMap;
+//import java.util.concurrent.TimeUnit;
+//
+///**
+// * 实时语音识别 SDK
+// */
+//public class RASRsdk {
+//    private static String secret_key, secretid, appid, engine_model_type, res_type, result_text_format, voice_format, filepath, template_name;
+//    private static int cutlength;
+//
+//    private static String generateUrl(String serverUrl, Map<String, String> mapReq) {
+//        StringBuilder strBuilder = new StringBuilder(serverUrl);
+//
+//        if (mapReq.containsKey("appid")) {
+//            strBuilder.append(mapReq.get("appid"));
+//        }
+//
+//        strBuilder.append('?');
+//
+//        // to make that all the parameters are sorted by ASC order
+//        TreeMap<String, String> sortedMap = new TreeMap(mapReq);
+//
+//        for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
+//            if (entry.getKey().equals("appid")) {
+//                continue;
+//            }
+//            strBuilder.append(entry.getKey());
+//            strBuilder.append('=');
+//            strBuilder.append(entry.getValue());
+//            strBuilder.append('&');
+//        }
+//
+//        if (mapReq.size() > 0) {
+//            strBuilder.setLength(strBuilder.length() - 1);
+//        }
+//
+//        return strBuilder.toString();
+//    }
+//
+//    private static String base64_hmac_sha1(String value, String keyStr) {
+//        String encoded = "";
+//        String type = "HmacSHA1";
+//        try {
+//            byte[] key = (keyStr).getBytes("UTF-8");
+//            byte[] Sequence = (value).getBytes("UTF-8");
+//
+//            Mac HMAC = Mac.getInstance(type);
+//            SecretKeySpec secretKey = new SecretKeySpec(key, type);
+//
+//            HMAC.init(secretKey);
+//            byte[] Hash = HMAC.doFinal(Sequence);
+//
+//            encoded = Base64.getEncoder().encodeToString(Hash);
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//        return encoded;
+//    }
+//
+//    /*
+//     * UNIXEpoch 时间�?
+//     */
+//    private static String toUNIXEpoch() {
+//        long unixTime = System.currentTimeMillis() / 1000L;
+//        return unixTime + "";
+//    }
+//
+//    /*
+//     * ExpiredUNIXEpoch 时间�?
+//     */
+//    private static String toExpiredUNIXEpoch() {
+//        long unixTime = System.currentTimeMillis() / 1000L + 24 * 60 * 60;
+//        return unixTime + "";
+//    }
+//
+//    private static String getNonce() {
+//        Random random = new Random(System.currentTimeMillis());
+//        return random.nextInt(10000) + "";
+//    }
+//
+//    private static String createSign(String serverUrl, String secretKey) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
+//        String strToBeEncoded = "POST" + serverUrl.substring(7);
+//        //System.out.println("String to be encoded: " + strToBeEncoded);
+//        return base64_hmac_sha1(strToBeEncoded, secretKey);
+//    }
+//
+//    private static String getRandomString(int length) {
+//        //定义一个字符串(A-Z,a-z,0-9)即62位;
+//        String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
+//        //由Random生成随机数
+//        Random random = new Random();
+//        StringBuffer sb = new StringBuffer();
+//        //长度为几就循环几次
+//        for (int i = 0; i < length; ++i) {
+//            //产生0-61的数字
+//            int number = random.nextInt(62);
+//            //将产生的数字通过length次承载到sb中
+//            sb.append(str.charAt(number));
+//        }
+//        //将承载的字符转换成字符串
+//        return sb.toString();
+//    }
+//
+//    public static int setConfig(
+//            String secret_key,
+//            String secretid,
+//            String appid,
+//            String engine_model_type,
+//            String res_type,
+//            String result_text_format,
+//            String voice_format,
+//            String filepath,
+//            int cutlength
+//    ) {
+//        return RASRsdk.setConfig(secret_key, secretid, appid, engine_model_type, res_type, result_text_format, voice_format, filepath, cutlength, "");
+//    }
+//
+//    public static int setConfig(
+//            String secret_key,
+//            String secretid,
+//            String appid,
+//            String engine_model_type,
+//            String res_type,
+//            String result_text_format,
+//            String voice_format,
+//            String filepath,
+//            int cutlength,
+//            String template_name
+//
+//    ) {
+//
+//        if (secret_key.length() <= 0) {
+//            System.out.println("secret_key can not be empty!");
+//            return -1;
+//        }
+//        if (secretid.length() <= 0) {
+//            System.out.println("secretid can not be empty!");
+//            return -1;
+//        }
+//        if (appid.length() <= 0) {
+//            System.out.println("appid can not be empty!");
+//            return -1;
+//        }
+//        if (engine_model_type.length() <= 0 || (engine_model_type.compareTo("8k_0") != 0 && engine_model_type.compareTo("16k_0") != 0)) {
+//            System.out.println("engine_model_type is not vailed!");
+//            return -1;
+//        }
+//        if (res_type.length() <= 0 || (res_type.compareTo("0") != 0 && res_type.compareTo("1") != 0)) {
+//            System.out.println("res_type is not vailed!");
+//            return -1;
+//        }
+//        if (result_text_format.length() <= 0 || (result_text_format.compareTo("0") != 0 && result_text_format.compareTo("1") != 0 &&
+//                result_text_format.compareTo("2") != 0 && result_text_format.compareTo("3") != 0)) {
+//            System.out.println("result_text_format is not vailed!");
+//            return -1;
+//        }
+//        if (voice_format.length() <= 0 || (voice_format.compareTo("1") != 0 && voice_format.compareTo("4") != 0 && voice_format.compareTo("6") != 0)) {
+//            System.out.println("voice_format is not vailed!");
+//            return -1;
+//        }
+//        if (filepath.length() <= 0) {
+//            System.out.println("filepath can not be empty!");
+//            return -1;
+//        }
+//        if (cutlength < 0 || cutlength > 200000) {
+//            System.out.println("cutlength is not vailed!");
+//            return -1;
+//        }
+//        RASRsdk.secret_key = secret_key;
+//        RASRsdk.secretid = secretid;
+//        RASRsdk.appid = appid;
+//        RASRsdk.engine_model_type = engine_model_type;
+//        RASRsdk.res_type = res_type;
+//        RASRsdk.result_text_format = result_text_format;
+//        RASRsdk.voice_format = voice_format;
+//        RASRsdk.filepath = filepath;
+//        RASRsdk.cutlength = cutlength;
+//        RASRsdk.template_name = template_name;
+//
+//        return 0;
+//    }
+//
+//    public static int sendVoice() {
+//        Map<String, String> reqMap = new TreeMap();
+//        reqMap.put("appid", appid);
+//        reqMap.put("secretid", secretid);
+//        reqMap.put("projectid", "1013976");
+//        if (template_name.compareTo("") != 0) {
+//            reqMap.put("template_name", template_name);
+//        }
+//        reqMap.put("sub_service_type", "1");
+//        reqMap.put("engine_model_type", engine_model_type);
+//        reqMap.put("res_type", res_type);
+//        reqMap.put("result_text_format", result_text_format);
+//        reqMap.put("voice_id", getRandomString(16));
+//        reqMap.put("timeout", "20000");
+//        reqMap.put("source", "0");
+//        reqMap.put("voice_format", voice_format);
+//        reqMap.put("timestamp", toUNIXEpoch());
+//        reqMap.put("expired", toExpiredUNIXEpoch());
+//        reqMap.put("nonce", getNonce());
+//        FileInputStream fileInputStream = null;
+//        long starttime = System.currentTimeMillis();
+//        try {
+//            fileInputStream = new FileInputStream(new File(filepath));
+//            byte[] dataPacket = new byte[cutlength];
+//            CloseableHttpClient httpclient = HttpClients.createDefault();
+//            int seq = 0, end = 0, n = 0;
+//            while (true) {
+//                n = fileInputStream.read(dataPacket);
+//                if (n == cutlength || (n < cutlength && end == 0)) {
+//                    if (n < cutlength) {
+//                        n = (n >> 2) << 2;
+//                        end = 1;
+//                    }
+//                    //System.out.println("n :" + n);
+//                    reqMap.put("seq", seq + "");
+//                    reqMap.put("end", end + "");
+//                    //1、serverUrl
+//                    String _url = "http://aai.qcloud.com/asr/v1/";
+//                    String serverUrl = generateUrl(_url, reqMap);
+//                   //System.out.println("Server URL: " + serverUrl);
+//                    //2、设置header
+//                    String authinfo = createSign(serverUrl, secret_key);
+//                    //System.out.println("签名: " + authinfo);
+//                    HttpPost hpost = new HttpPost(serverUrl);
+//                    hpost.setHeader("Authorization", authinfo);
+//                    hpost.setHeader("Content-Type", "application/octet-stream");
+//                    hpost.setEntity(new ByteArrayEntity(dataPacket));
+//                    CloseableHttpResponse response = httpclient.execute(hpost);
+//                    try {
+//                        //System.out.println("----------------------------------------");
+//                        //System.out.println(response.getStatusLine());
+//                        System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
+//                    } finally {
+//                        response.close();
+//                    }
+//                    seq++;
+//                } else {
+//                    break;
+//                }
+//                TimeUnit.MILLISECONDS.sleep(10);
+//            }
+//            httpclient.close();
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//        long endtime = System.currentTimeMillis();
+//        System.out.println("程序运行时间为:" + (endtime - starttime) / 1000 + "秒!");
+//        return 0;
+//    }
+//}

+ 269 - 269
src/main/java/cn/minbb/iot/sdk/SASRsdk.java

@@ -1,269 +1,269 @@
-package cn.minbb.iot.sdk;
-
-import cn.minbb.iot.data.Response;
-import com.alibaba.fastjson.JSONObject;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.crypto.Mac;
-import javax.crypto.spec.SecretKeySpec;
-import java.io.*;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.net.URLEncoder;
-import java.util.Base64;
-import java.util.Map;
-import java.util.Random;
-import java.util.TreeMap;
-
-/**
- * 一句话语音识别 SDK
- */
-public class SASRsdk {
-    private static Logger logger = LoggerFactory.getLogger(SASRsdk.class);
-
-    private static String SecretId, SecretKey, EngSerViceType, SourceType, VoiceFormat, fileURI;
-
-    public static String formSignstr(String serverUrl, Map<String, String> mapReq) {
-        StringBuilder strBuilder = new StringBuilder(serverUrl);
-
-        // to make that all the parameters are sorted by ASC order
-        TreeMap<String, String> sortedMap = new TreeMap(mapReq);
-
-        for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
-            strBuilder.append(entry.getKey());
-            strBuilder.append('=');
-            strBuilder.append(entry.getValue());
-            strBuilder.append('&');
-        }
-
-        if (mapReq.size() > 0) {
-            strBuilder.setLength(strBuilder.length() - 1);
-        }
-
-        //System.out.println("sign str: " + strBuilder);
-
-        return strBuilder.toString();
-    }
-
-    public static String formPostbody(Map<String, String> mapReq) {
-        StringBuilder stringBuilder = new StringBuilder();
-        // to make that all the parameters are sorted by ASC order
-        TreeMap<String, String> sortedMap = new TreeMap(mapReq);
-        for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
-            try {
-                stringBuilder.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
-                stringBuilder.append('=');
-                stringBuilder.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
-                stringBuilder.append('&');
-            } catch (UnsupportedEncodingException e) {
-                e.printStackTrace();
-            }
-        }
-        return stringBuilder.toString();
-    }
-
-    public static String base64_hmac_sha1(String value, String keyStr) {
-        String encoded = "";
-        String type = "HmacSHA1";
-        try {
-            byte[] key = (keyStr).getBytes("UTF-8");
-            byte[] Sequence = (value).getBytes("UTF-8");
-
-            Mac HMAC = Mac.getInstance(type);
-            SecretKeySpec secretKey = new SecretKeySpec(key, type);
-
-            HMAC.init(secretKey);
-            byte[] Hash = HMAC.doFinal(Sequence);
-
-            encoded = Base64.getEncoder().encodeToString(Hash);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return encoded;
-    }
-
-    /*
-     * 获得unix时间戳
-     */
-    public static String toUNIXEpoch() {
-        long unixTime = System.currentTimeMillis() / 1000L;
-        return unixTime + "";
-    }
-
-    /*
-     * 生成随机nonce
-     */
-    public static String toUNIXNonce() {
-        long unixTime = System.currentTimeMillis() / 1000L;
-        String str = unixTime + "";
-        String nonce = str.substring(0, 4);
-        return nonce;
-    }
-
-    public static String createSign(String signStr, String secretKey) {
-        return base64_hmac_sha1(signStr, secretKey);
-    }
-
-    private static String getRandomString(int length) {
-        //定义一个字符串(A-Z,a-z,0-9)即62位;
-        String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
-        //由Random生成随机数
-        Random random = new Random();
-        StringBuffer sb = new StringBuffer();
-        //长度为几就循环几次
-        for (int i = 0; i < length; ++i) {
-            //产生0-61的数字
-            int number = random.nextInt(62);
-            //将产生的数字通过length次承载到sb中
-            sb.append(str.charAt(number));
-        }
-        //将承载的字符转换成字符串
-        return sb.toString();
-    }
-
-    public static int setConfig(
-            String SecretId,
-            String SecretKey,
-            String EngSerViceType,
-            String SourceType,
-            String VoiceFormat,
-            String fileURI
-    ) {
-        if (SecretId.length() <= 0) {
-            System.out.println("SecretId can not be empty!");
-            return -1;
-        }
-        if (SecretKey.length() <= 0) {
-            System.out.println("SecretKey can not be empty!");
-            return -1;
-        }
-        if (EngSerViceType.length() <= 0 || (EngSerViceType.compareTo("8k") != 0 && EngSerViceType.compareTo("16k") != 0)) {
-            System.out.println("EngSerViceTyp is not valied !");
-            return -1;
-        }
-        if (SourceType.length() <= 0 || (SourceType.compareTo("0") != 0 && SourceType.compareTo("1") != 0)) {
-            System.out.println("SourceType is not valied !");
-            return -1;
-        }
-        if (VoiceFormat.length() <= 0 || (VoiceFormat.compareTo("mp3") != 0 && VoiceFormat.compareTo("wav") != 0)) {
-            System.out.println("VoiceFormat is not valied !");
-            return -1;
-        }
-        if (fileURI.length() <= 0) {
-            System.out.println("fileURI can not be empty!");
-            return -1;
-        }
-        SASRsdk.SecretId = SecretId;
-        SASRsdk.SecretKey = SecretKey;
-        SASRsdk.EngSerViceType = EngSerViceType;
-        SASRsdk.SourceType = SourceType;
-        SASRsdk.VoiceFormat = VoiceFormat;
-        SASRsdk.fileURI = fileURI;
-        return 0;
-    }
-
-    public static int sendVoice() {
-        Map<String, String> reqMap = new TreeMap();
-        reqMap.put("Action", "SentenceRecognition");
-        reqMap.put("SecretId", SecretId);
-        reqMap.put("Timestamp", toUNIXEpoch());
-        reqMap.put("Nonce", toUNIXNonce());
-        reqMap.put("Version", "2018-05-22");
-        reqMap.put("ProjectId", "0");
-        reqMap.put("SubServiceType", "2");
-        reqMap.put("EngSerViceType", EngSerViceType);
-        reqMap.put("SourceType", SourceType);
-        if (SourceType.compareTo("0") == 0) {
-            try {
-                String Url = URLEncoder.encode(fileURI, "UTF-8");
-                reqMap.put("Url", Url);
-            } catch (UnsupportedEncodingException e) {
-                e.printStackTrace();
-            }
-        } else if (SourceType.compareTo("1") == 0) {
-            FileInputStream fileInputStream = null;
-            try {
-                fileInputStream = new FileInputStream(new File(fileURI));
-                int datalen = fileInputStream.available();
-                byte[] dataPacket = new byte[datalen];
-                int n = fileInputStream.read(dataPacket);
-                //System.out.println("n :"+n);
-                String Data = Base64.getEncoder().encodeToString(dataPacket);
-                String DataLen = datalen + "";
-                // System.out.println("data len: "+DataLen);
-                reqMap.put("Data", Data);
-                reqMap.put("DataLen", DataLen);
-
-
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        } else {
-            return -3;
-        }
-        reqMap.put("VoiceFormat", VoiceFormat);
-        String UsrAudioKey = getRandomString(16);
-        reqMap.put("UsrAudioKey", UsrAudioKey);
-        String _url = "POSTaai.tencentcloudapi.com/?";
-        String signstr = formSignstr(_url, reqMap);
-        // System.out.println("signstr: " + signstr);
-        String signing = createSign(signstr, SecretKey);
-        // System.out.println("签名: " + signing);
-        String tmppostdata = formPostbody(reqMap);
-        String sign = "";
-        try {
-            sign = URLEncoder.encode(signing, "UTF-8");
-        } catch (UnsupportedEncodingException e) {
-            e.printStackTrace();
-        }
-        StringBuilder postdata = new StringBuilder(tmppostdata);
-        postdata.append("Signature=");
-        postdata.append(sign);
-        String post = postdata.toString();
-        //System.out.println("post : "+post);
-        String serverUrl = "https://aai.tencentcloudapi.com";
-
-        HttpURLConnection con = null;
-        StringBuilder sbResult = new StringBuilder();
-        try {
-            URL url = new URL(serverUrl);
-            con = (HttpURLConnection) url.openConnection();
-            con.setRequestMethod("POST");
-            con.setDoOutput(true);
-            con.setDoInput(true);
-            con.setUseCaches(false);
-            con.setRequestProperty("Host", "aai.tencentcloudapi.com");
-            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
-            con.setRequestProperty("Charset", "utf-8");
-
-            // 往服务器写入数据
-            OutputStream out = con.getOutputStream();
-            out.write(post.getBytes());
-            out.flush();
-
-            // 接收服务器返回的数据
-            InputStream in = con.getInputStream();
-            BufferedReader br = new BufferedReader(new InputStreamReader(in));
-            String line;// 每一行的数据
-            while ((line = br.readLine()) != null) {
-                sbResult.append(line);
-            }
-            // 数据解析
-            String data = sbResult.toString();
-            Response response = JSONObject.parseObject(JSONObject.parseObject(data).get("Response").toString(), Response.class);
-            // TODO: 2019.04.03
-            logger.info("SDK收到解析结果 = {}", data);
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        } finally {
-            if (con != null) {
-                con.disconnect();
-                con = null;
-            }
-        }
-
-        return 0;
-    }
-
-}
+//package cn.minbb.iot.sdk;
+//
+//import cn.minbb.iot.data.Response;
+//import com.alibaba.fastjson.JSONObject;
+//import org.slf4j.Logger;
+//import org.slf4j.LoggerFactory;
+//
+//import javax.crypto.Mac;
+//import javax.crypto.spec.SecretKeySpec;
+//import java.io.*;
+//import java.net.HttpURLConnection;
+//import java.net.URL;
+//import java.net.URLEncoder;
+//import java.util.Base64;
+//import java.util.Map;
+//import java.util.Random;
+//import java.util.TreeMap;
+//
+///**
+// * 一句话语音识别 SDK
+// */
+//public class SASRsdk {
+//    private static Logger logger = LoggerFactory.getLogger(SASRsdk.class);
+//
+//    private static String SecretId, SecretKey, EngSerViceType, SourceType, VoiceFormat, fileURI;
+//
+//    public static String formSignstr(String serverUrl, Map<String, String> mapReq) {
+//        StringBuilder strBuilder = new StringBuilder(serverUrl);
+//
+//        // to make that all the parameters are sorted by ASC order
+//        TreeMap<String, String> sortedMap = new TreeMap(mapReq);
+//
+//        for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
+//            strBuilder.append(entry.getKey());
+//            strBuilder.append('=');
+//            strBuilder.append(entry.getValue());
+//            strBuilder.append('&');
+//        }
+//
+//        if (mapReq.size() > 0) {
+//            strBuilder.setLength(strBuilder.length() - 1);
+//        }
+//
+//        //System.out.println("sign str: " + strBuilder);
+//
+//        return strBuilder.toString();
+//    }
+//
+//    public static String formPostbody(Map<String, String> mapReq) {
+//        StringBuilder stringBuilder = new StringBuilder();
+//        // to make that all the parameters are sorted by ASC order
+//        TreeMap<String, String> sortedMap = new TreeMap(mapReq);
+//        for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
+//            try {
+//                stringBuilder.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
+//                stringBuilder.append('=');
+//                stringBuilder.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
+//                stringBuilder.append('&');
+//            } catch (UnsupportedEncodingException e) {
+//                e.printStackTrace();
+//            }
+//        }
+//        return stringBuilder.toString();
+//    }
+//
+//    public static String base64_hmac_sha1(String value, String keyStr) {
+//        String encoded = "";
+//        String type = "HmacSHA1";
+//        try {
+//            byte[] key = (keyStr).getBytes("UTF-8");
+//            byte[] Sequence = (value).getBytes("UTF-8");
+//
+//            Mac HMAC = Mac.getInstance(type);
+//            SecretKeySpec secretKey = new SecretKeySpec(key, type);
+//
+//            HMAC.init(secretKey);
+//            byte[] Hash = HMAC.doFinal(Sequence);
+//
+//            encoded = Base64.getEncoder().encodeToString(Hash);
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//        return encoded;
+//    }
+//
+//    /*
+//     * 获得unix时间戳
+//     */
+//    public static String toUNIXEpoch() {
+//        long unixTime = System.currentTimeMillis() / 1000L;
+//        return unixTime + "";
+//    }
+//
+//    /*
+//     * 生成随机nonce
+//     */
+//    public static String toUNIXNonce() {
+//        long unixTime = System.currentTimeMillis() / 1000L;
+//        String str = unixTime + "";
+//        String nonce = str.substring(0, 4);
+//        return nonce;
+//    }
+//
+//    public static String createSign(String signStr, String secretKey) {
+//        return base64_hmac_sha1(signStr, secretKey);
+//    }
+//
+//    private static String getRandomString(int length) {
+//        //定义一个字符串(A-Z,a-z,0-9)即62位;
+//        String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
+//        //由Random生成随机数
+//        Random random = new Random();
+//        StringBuffer sb = new StringBuffer();
+//        //长度为几就循环几次
+//        for (int i = 0; i < length; ++i) {
+//            //产生0-61的数字
+//            int number = random.nextInt(62);
+//            //将产生的数字通过length次承载到sb中
+//            sb.append(str.charAt(number));
+//        }
+//        //将承载的字符转换成字符串
+//        return sb.toString();
+//    }
+//
+//    public static int setConfig(
+//            String SecretId,
+//            String SecretKey,
+//            String EngSerViceType,
+//            String SourceType,
+//            String VoiceFormat,
+//            String fileURI
+//    ) {
+//        if (SecretId.length() <= 0) {
+//            System.out.println("SecretId can not be empty!");
+//            return -1;
+//        }
+//        if (SecretKey.length() <= 0) {
+//            System.out.println("SecretKey can not be empty!");
+//            return -1;
+//        }
+//        if (EngSerViceType.length() <= 0 || (EngSerViceType.compareTo("8k") != 0 && EngSerViceType.compareTo("16k") != 0)) {
+//            System.out.println("EngSerViceTyp is not valied !");
+//            return -1;
+//        }
+//        if (SourceType.length() <= 0 || (SourceType.compareTo("0") != 0 && SourceType.compareTo("1") != 0)) {
+//            System.out.println("SourceType is not valied !");
+//            return -1;
+//        }
+//        if (VoiceFormat.length() <= 0 || (VoiceFormat.compareTo("mp3") != 0 && VoiceFormat.compareTo("wav") != 0)) {
+//            System.out.println("VoiceFormat is not valied !");
+//            return -1;
+//        }
+//        if (fileURI.length() <= 0) {
+//            System.out.println("fileURI can not be empty!");
+//            return -1;
+//        }
+//        SASRsdk.SecretId = SecretId;
+//        SASRsdk.SecretKey = SecretKey;
+//        SASRsdk.EngSerViceType = EngSerViceType;
+//        SASRsdk.SourceType = SourceType;
+//        SASRsdk.VoiceFormat = VoiceFormat;
+//        SASRsdk.fileURI = fileURI;
+//        return 0;
+//    }
+//
+//    public static int sendVoice() {
+//        Map<String, String> reqMap = new TreeMap();
+//        reqMap.put("Action", "SentenceRecognition");
+//        reqMap.put("SecretId", SecretId);
+//        reqMap.put("Timestamp", toUNIXEpoch());
+//        reqMap.put("Nonce", toUNIXNonce());
+//        reqMap.put("Version", "2018-05-22");
+//        reqMap.put("ProjectId", "0");
+//        reqMap.put("SubServiceType", "2");
+//        reqMap.put("EngSerViceType", EngSerViceType);
+//        reqMap.put("SourceType", SourceType);
+//        if (SourceType.compareTo("0") == 0) {
+//            try {
+//                String Url = URLEncoder.encode(fileURI, "UTF-8");
+//                reqMap.put("Url", Url);
+//            } catch (UnsupportedEncodingException e) {
+//                e.printStackTrace();
+//            }
+//        } else if (SourceType.compareTo("1") == 0) {
+//            FileInputStream fileInputStream = null;
+//            try {
+//                fileInputStream = new FileInputStream(new File(fileURI));
+//                int datalen = fileInputStream.available();
+//                byte[] dataPacket = new byte[datalen];
+//                int n = fileInputStream.read(dataPacket);
+//                //System.out.println("n :"+n);
+//                String Data = Base64.getEncoder().encodeToString(dataPacket);
+//                String DataLen = datalen + "";
+//                // System.out.println("data len: "+DataLen);
+//                reqMap.put("Data", Data);
+//                reqMap.put("DataLen", DataLen);
+//
+//
+//            } catch (Exception e) {
+//                e.printStackTrace();
+//            }
+//        } else {
+//            return -3;
+//        }
+//        reqMap.put("VoiceFormat", VoiceFormat);
+//        String UsrAudioKey = getRandomString(16);
+//        reqMap.put("UsrAudioKey", UsrAudioKey);
+//        String _url = "POSTaai.tencentcloudapi.com/?";
+//        String signstr = formSignstr(_url, reqMap);
+//        // System.out.println("signstr: " + signstr);
+//        String signing = createSign(signstr, SecretKey);
+//        // System.out.println("签名: " + signing);
+//        String tmppostdata = formPostbody(reqMap);
+//        String sign = "";
+//        try {
+//            sign = URLEncoder.encode(signing, "UTF-8");
+//        } catch (UnsupportedEncodingException e) {
+//            e.printStackTrace();
+//        }
+//        StringBuilder postdata = new StringBuilder(tmppostdata);
+//        postdata.append("Signature=");
+//        postdata.append(sign);
+//        String post = postdata.toString();
+//        //System.out.println("post : "+post);
+//        String serverUrl = "https://aai.tencentcloudapi.com";
+//
+//        HttpURLConnection con = null;
+//        StringBuilder sbResult = new StringBuilder();
+//        try {
+//            URL url = new URL(serverUrl);
+//            con = (HttpURLConnection) url.openConnection();
+//            con.setRequestMethod("POST");
+//            con.setDoOutput(true);
+//            con.setDoInput(true);
+//            con.setUseCaches(false);
+//            con.setRequestProperty("Host", "aai.tencentcloudapi.com");
+//            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+//            con.setRequestProperty("Charset", "utf-8");
+//
+//            // 往服务器写入数据
+//            OutputStream out = con.getOutputStream();
+//            out.write(post.getBytes());
+//            out.flush();
+//
+//            // 接收服务器返回的数据
+//            InputStream in = con.getInputStream();
+//            BufferedReader br = new BufferedReader(new InputStreamReader(in));
+//            String line;// 每一行的数据
+//            while ((line = br.readLine()) != null) {
+//                sbResult.append(line);
+//            }
+//            // 数据解析
+//            String data = sbResult.toString();
+//            Response response = JSONObject.parseObject(JSONObject.parseObject(data).get("Response").toString(), Response.class);
+//            // TODO: 2019.04.03
+//            logger.info("SDK收到解析结果 = {}", data);
+//        } catch (Exception e) {
+//            throw new RuntimeException(e);
+//        } finally {
+//            if (con != null) {
+//                con.disconnect();
+//                con = null;
+//            }
+//        }
+//
+//        return 0;
+//    }
+//
+//}

+ 3 - 5
src/main/java/cn/minbb/iot/task/AutoConfig.java

@@ -1,10 +1,8 @@
 package cn.minbb.iot.task;
 
 import cn.minbb.iot.config.Const;
-import cn.minbb.iot.model.DeviceData;
 import cn.minbb.iot.service.MqttGateway;
 import cn.minbb.iot.util.Application;
-import com.alibaba.fastjson.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -32,8 +30,8 @@ public class AutoConfig {
         int random1 = 1 + ((int) (new Random().nextFloat() * (10 - 1)));
         mqttGateway.sendToMqtt(Application.getCurrentStringTime(), Const.MQTT_TOPIC_TIME, 2);
         int random2 = 1 + ((int) (new Random().nextFloat() * (10 - 1)));
-        mqttGateway.sendToMqtt(JSONObject.toJSONString(
-                new DeviceData("3447.9" + random1 + "043 N", "11339.2" + random2 + "603 E",
-                        Application.getCurrentStringTime())), Const.MQTT_TOPIC_CAR_DATA, 2);
+//        mqttGateway.sendToMqtt(JSONObject.toJSONString(
+//                new DeviceData("3447.9" + random1 + "043 N", "11339.2" + random2 + "603 E",
+//                        Application.getCurrentStringTime())), Const.MQTT_TOPIC_CAR_DATA, 2);
     }
 }