YinShiController.java 14.5 KB
package com.sincre.springboot.controller;


import com.alibaba.fastjson.JSON;
import com.sincre.springboot.ApiModel.YinShiResResult;
import com.sincre.springboot.ApiPlatform.YinShiServiceConfig;
import com.sincre.springboot.common.MD5;
import com.sincre.springboot.common.ResponseCode;
import com.sincre.springboot.common.ServerResponse;
import com.sincre.springboot.utils.ApiHelper;
import com.sincre.springboot.utils.CacheHelper;
import com.sincre.springboot.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/YinShi")
@Api(value = "YinShiController", tags = "萤石对接接口")
public class YinShiController {

    @ApiOperation(value = "用于管理员获取accessToken")
    @GetMapping("/token")
    public ServerResponse GetYinShiToken() {

        CacheHelper.GetYinShiToken();

        return ServerResponse.createBySuccessMessage(ResponseCode.SUCCESS.getDesc());
    }

    @ApiOperation(value = "增加子账号")
    @GetMapping("/addChildAccount")
    public ServerResponse addChildAccount(@RequestParam String accountName, @RequestParam String password) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/create";
        Map<String, Object> map = new HashMap<>();
        //子账户密码,LowerCase(MD5(AppKey#密码明文))
        password = YinShiServiceConfig.appKey + "#" + password;
        try {
            password = MD5.md5(password, "").toLowerCase();
        } catch (Exception ex) {
            ex.getStackTrace();
        }
        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountName", accountName);
        map.put("password", password);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        System.out.println(result);
        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "获取单个子账户信息")
    @ApiImplicitParam(name="accountId",value = "子账号标识",required = true,dataType = "String")
    @GetMapping("getChildAccount")
    public ServerResponse getChildAccount(@RequestParam String accountId) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/get";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);
        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "获取子账号信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageIndex", value = "分页起始页,从1开始",required = true),
            @ApiImplicitParam(name="pageSize", value = "分页大小,默认为10,最大为50")
    })
    @GetMapping("getChildAccountList")
    public ServerResponse getChildAccountList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam(defaultValue = "10") Integer pageSize) {

        pageIndex = pageIndex - 1;
        String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/list";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("pageStart", pageIndex);
        map.put("pageSize", pageSize);
        String result = ApiHelper.doPost(url, new HashMap<String,String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "修改当前子账户密码")
    @GetMapping("updateChildPassword")
    public ServerResponse updateChildPassword(@RequestParam("accountId") String accountId, @RequestParam("newPassword") String newPassword, @RequestParam("oldPassword") String oldPassword) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/updatePassword";
        Map<String, Object> map = new HashMap<>();

        oldPassword = YinShiServiceConfig.appKey + "#" + oldPassword;
        newPassword = YinShiServiceConfig.appKey + "#" + newPassword;
        try {
            oldPassword = MD5.md5(oldPassword, "").toLowerCase();
            newPassword = MD5.md5(newPassword, "").toLowerCase();
        } catch (Exception ex) {
            ex.getStackTrace();
        }
        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        map.put("oldPassword", oldPassword);
        map.put("newPassword", newPassword);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);
        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "删除指定子账户")
    @GetMapping("deleteChildAccount")
    public ServerResponse deleteChildAccount(@RequestParam String accountId) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/account/delete";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);
        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }


    @ApiOperation(value = "设置子账户的授权策略")
    @GetMapping("policySet")
    public ServerResponse policySet(@RequestParam String accountId, @RequestParam String policy) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/policy/set";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        map.put("policy", policy);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "增加子账户授权策略中的授权语句")
    @GetMapping("policyAdd")
    public ServerResponse policyAdd(@RequestParam String accountId, @RequestParam String statement) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/statement/add";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        map.put("statement", statement);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "删除子账户授权策略中指定设备的所有授权语句")
    @GetMapping("policyDelete")
    public ServerResponse policyDelete(@RequestParam String accountId, @RequestParam String deviceSerial) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/statement/delete";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        map.put("deviceSerial", deviceSerial);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "获取B模式子账户accessToken")
    @GetMapping("getChildAccountToken")
    public ServerResponse getChildAccountToken(@RequestParam String accountId) {

        String url = YinShiServiceConfig.HostUrl + "lapp/ram/token/get";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("accountId", accountId);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }



    @ApiOperation(value = "添加设备到账号下")
    @ApiImplicitParams({
            @ApiImplicitParam(name="deviceSerial",value = "设备序列号",required = true),
            @ApiImplicitParam(name="validateCode",value = "备验证码,设备机身上的六位大写字母,如OLZKQU",required = true)
    })
    @GetMapping("addAccountDevice")
    public ServerResponse addAccountDevice(@RequestParam String deviceSerial,@RequestParam String validateCode) {

        String url = YinShiServiceConfig.HostUrl + "lapp/device/add";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("deviceSerial", deviceSerial);
        map.put("validateCode", validateCode);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "修改设备的名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name="deviceSerial",value = "设备序列号",required = true),
            @ApiImplicitParam(name="deviceName",value = "设备名称,长度不大于50字节,不能包含特殊字符",required = true)
    })
    @GetMapping("updateDeviceName")
    public ServerResponse updateDeviceName(@RequestParam String deviceName,@RequestParam String deviceSerial) {

        String url = YinShiServiceConfig.HostUrl + "lapp/device/name/update";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("deviceSerial", deviceSerial);
        map.put("deviceName", deviceName);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "删除设备")
    @ApiImplicitParam(name="deviceSerial",value = "设备序列号",required = true)
    @GetMapping("deleteDevice")
    public ServerResponse deleteDevice(@RequestParam String deviceSerial) {

        String url = YinShiServiceConfig.HostUrl + "lapp/device/delete";
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("deviceSerial", deviceSerial);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "设备抓拍图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name="channelNo",value = "通道号,IPC设备填写1",required = true),
            @ApiImplicitParam(name="deviceSerial",value = "设备序列号",required = true)
    })
    @GetMapping("deviceCapture")
    public ServerResponse deviceCapture(@RequestParam Integer channelNo,@RequestParam String deviceSerial) {

        String url = YinShiServiceConfig.HostUrl + "lapp/device/capture";
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("deviceSerial", deviceSerial);
        map.put("channelNo",channelNo);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }







    @ApiOperation(value = "获取设备列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageIndex", value = "分页起始页,从1开始",required = true),
            @ApiImplicitParam(name="pageSize", value = "分页大小,默认为10,最大为50")
    })
    @GetMapping("getDeviceList")
    public ServerResponse getDeviceList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam(defaultValue = "10") Integer pageSize) {

        pageIndex = pageIndex - 1;
        String url = YinShiServiceConfig.HostUrl + "lapp/device/list";
        Map<String, Object> map = new HashMap<>();

        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("pageStart", pageIndex);
        map.put("pageSize", pageSize);
        String result = ApiHelper.doPost(url, new HashMap<String,String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }

    @ApiOperation(value = "获取单个设备信息")
    @ApiImplicitParam(name="deviceSerial",value = "设备序列号",required = true)
    @GetMapping("getDeviceInfo")
    public ServerResponse getDeviceInfo(@RequestParam String deviceSerial) {

        String url = YinShiServiceConfig.HostUrl + "lapp/device/info";
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", YinShiServiceConfig.AccessToken);
        map.put("deviceSerial", deviceSerial);
        String result = ApiHelper.doPost(url, new HashMap<String, String>(), map);

        YinShiResResult yinShiResResult = JSON.parseObject(result,YinShiResResult.class);

        return ResultUtils.getInstance().returnResultYingshi(yinShiResResult);
    }
}