package com.ics.common.utils.device;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;


public class SignUtils {

    /**
     * MD5加密
     *
     * @param strData
     * @return 返回加密字符
     */
    public static String getMD5(String strData) {
        return DigestUtils.md5DigestAsHex(strData.getBytes());
    }

    /**
     * 签名
     *
     * @param appID appID
     * @param key   key
     * @param time  时间戳
     * @return 返回签名
     */
    public static String sign(String appID, String key, Long time) {
        String sign1 = getMD5(time + "|" + key);
        return getMD5(sign1 + "|" + appID + "|" + key);
    }



    public static void lock(String lockId){
        String appID = "8888";
        String key = "cootoo";
        Long time = System.currentTimeMillis();

        //http://111.23.208.229:8088/cootoo/openLock
        JSONObject on =new JSONObject();
        on.put("appID",appID);
        on.put("requestID",time);
        on.put("peopleID","20249001");
        on.put("lockID",lockId);
        on.put("time",time);
        on.put("sign",sign(appID,key,time));

        Map<String, String > heads = new HashMap<>();

        System.out.println("ip为："+"222.67.48.244");

        String body = HttpRequest.post("http://222.67.48.244:8888/cootoo/openLock")
                .headerMap(heads, false)
                .body(on.toString()).timeout(30 * 1000).execute().body();
        System.out.println(body);
    }

    public static void main(String[] args) {
        lock("240");
    }

}
