AiLockingKeyServiceImpl.java
8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.glsx.smart.service.impl;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.glsx.plat.common.utils.SnowFlake;
import com.glsx.plat.core.web.R;
import com.glsx.smart.dao.entity.SmartKeyCommandLog;
import com.glsx.smart.dao.entity.SmartVehicleBlueKey;
import com.glsx.smart.dao.entity.SmartVehicleBlueKeyExample;
import com.glsx.smart.exception.BusinessException;
import com.glsx.smart.exception.ResultEnum;
import com.glsx.smart.pojo.request.*;
import com.glsx.smart.pojo.response.SmartCarKeyResponse;
import com.glsx.smart.service.AiLockingKeyService;
import com.glsx.smart.service.SmartKeyCommandLogService;
import com.glsx.smart.service.SmartVehicleBlueKeyService;
import com.glsx.smart.utils.AesEncryptUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* <p>Description:智能车钥匙service
* <p>Project: chejia-smart
*
* @author fengzhi
* @date 2021-04-19
*/
@Service
@Slf4j
public class AiLockingKeyServiceImpl implements AiLockingKeyService {
@Autowired
private SmartKeyCommandLogService smartKeyCommandLogService;
@Autowired
private SmartVehicleBlueKeyService smartVehicleBlueKeyService;
@Autowired
private AesEncryptUtils aesEncryptUtils;
//sn查询/新增车钥匙
@Override
public R getLockingKey(SmartCarKeyRequest request) {
SmartCarKeyResponse response = new SmartCarKeyResponse();
SmartVehicleBlueKeyExample example = getExampleBysn(Long.valueOf(request.getUserId()),request.getSn());
List<SmartVehicleBlueKey> smartVehicleBlueKeys = smartVehicleBlueKeyService.selectByExample(example);
if(CollectionUtils.isEmpty(smartVehicleBlueKeys)){
//如果sn,userid没有数据,生成只含有crep钥匙信息返回
SmartVehicleBlueKey newKey = new SmartVehicleBlueKey();
//雪花算法生成crep,对称加密
String crep = getCrepCode(request);
String crep_encrypt = "";
//加密
try {
crep_encrypt = aesEncryptUtils.encrypt(crep);
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException(ResultEnum.ENCRYPT_EXCEP_1);
}
//保存,crep用明文存储
newKey.setSn(request.getSn());
newKey.setUserId(Long.valueOf(request.getUserId()));
newKey.setCrepCode(crep);
newKey.setCreateTime(new Date());
newKey.setAutoSenseStatus(0);
newKey.setIsDelete(0);
smartVehicleBlueKeyService.insert(newKey);
//返回,crep用密文传输
BeanUtils.copyProperties(newKey,response);
response.setCrepCode(crep_encrypt);
return R.ok().data(response);
}else {
SmartVehicleBlueKey carkey = smartVehicleBlueKeys.get(0);
String crepCode = carkey.getCrepCode();
if(StringUtils.isNotEmpty(crepCode)){
//如果sn,userid有对应的crep,返回钥匙信息
BeanUtils.copyProperties(carkey,response);
//将crep和密钥加密
String secretKey = carkey.getSecretKey();
String crep_encrypt = "";
String secret_key_encrypt = "";
try {
crep_encrypt = aesEncryptUtils.encrypt(crepCode);
if(StringUtils.isNotEmpty(secretKey))
secret_key_encrypt = aesEncryptUtils.encrypt(secretKey);
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException(ResultEnum.ENCRYPT_EXCEP_2);
}
response.setCrepCode(crep_encrypt);
response.setSecretKey(secret_key_encrypt);
return R.ok().data(response);
}else {
//如果sn,userid没有对应的crep。生成只含有crep钥匙信息返回
//雪花算法生成crep,对称加密
String crep = getCrepCode(request);
String crep_encrypt = "";
//加密
try {
crep_encrypt = aesEncryptUtils.encrypt(crep);
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException(ResultEnum.ENCRYPT_EXCEP_3);
}
//更新,crep用明文存储
carkey.setSn(request.getSn());
carkey.setUserId(Long.valueOf(request.getUserId()));
carkey.setCrepCode(crep);
carkey.setCreateTime(new Date());
carkey.setAutoSenseStatus(0);
carkey.setIsDelete(0);
smartVehicleBlueKeyService.updateByPrimaryKey(carkey);
//返回,crep用密文传输
BeanUtils.copyProperties(carkey,response);
response.setCrepCode(crep_encrypt);
return R.ok().data(response);
}
}
}
//智能车钥匙解绑
@Override
public R removeLockingKey(SmartCarKeyRmRequest request) {
SmartVehicleBlueKey smartVehicleBlueKey = new SmartVehicleBlueKey();
smartVehicleBlueKey.setIsDelete(1);
smartVehicleBlueKeyService.updateByExampleSelective(smartVehicleBlueKey, getExampleBysn(Long.valueOf(request.getUserId()), request.getSn()));
return R.ok();
}
//车钥匙操作指令上报
@Override
public R LogLockingKeyCommand(SmartCarKeyCommandRequest carKeyCommandRequest) {
SmartKeyCommandLog logData = new SmartKeyCommandLog();
logData.setUserId(Long.valueOf(carKeyCommandRequest.getUserId()));
String crep = "";
try {
crep = aesEncryptUtils.decrypt(carKeyCommandRequest.getCrepCode());
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException(ResultEnum.ENCRYPT_EXCEP_4);
}
logData.setCrep(crep);
logData.setKeyCommand(carKeyCommandRequest.getKeyCommond());
logData.setCreateTime(new Date());
smartKeyCommandLogService.insert(logData);
return R.ok();
}
//车钥匙修改昵称
@Override
public R setLockingKeyName(SmartCarKeyNameRequest request) {
SmartVehicleBlueKey smartVehicleBlueKey = new SmartVehicleBlueKey();
smartVehicleBlueKey.setKeyName(request.getKeyName());
smartVehicleBlueKeyService.updateByExampleSelective(smartVehicleBlueKey, getExampleBysn(Long.valueOf(request.getUserId()), request.getSn()));
return R.ok();
}
//车钥匙修改自动感应
@Override
public R setLockingKeyAutoSense(SmartCarKeyAutoSenseRequest request) {
SmartVehicleBlueKey smartVehicleBlueKey = new SmartVehicleBlueKey();
smartVehicleBlueKey.setAutoSenseStatus(Integer.valueOf(request.getAutoSenseStatus()));
smartVehicleBlueKeyService.updateByExampleSelective(smartVehicleBlueKey, getExampleBysn(Long.valueOf(request.getUserId()), request.getSn()));
return R.ok();
}
//智能车钥匙注册成功后的数据保存
@Override
public R saveLockingKeyRegisterInfo(SmartCarKeyRegisterRequest request) {
//解密
String crep = "";
String keySecret = "";
try {
crep = aesEncryptUtils.decrypt(request.getCrepCode());
keySecret = aesEncryptUtils.decrypt(request.getKeyLock());
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException(ResultEnum.ENCRYPT_EXCEP_5);
}
SmartVehicleBlueKey smartVehicleBlueKey = new SmartVehicleBlueKey();
smartVehicleBlueKey.setMobileBlueMac(request.getMobileMac());
smartVehicleBlueKey.setSmartLockMac(request.getSmartLockMac());
//明文存储
smartVehicleBlueKey.setCrepCode(crep);
smartVehicleBlueKey.setSecretKey(keySecret);
SmartVehicleBlueKeyExample example = new SmartVehicleBlueKeyExample();
example.createCriteria().andUserIdEqualTo(Long.valueOf(request.getUserId())).andCrepCodeEqualTo(crep).andIsDeleteEqualTo(0);
smartVehicleBlueKeyService.updateByExampleSelective(smartVehicleBlueKey,example);
return R.ok();
}
//查询车钥匙example,根据用户ID和sn查
private SmartVehicleBlueKeyExample getExampleBysn(Long userId, String sn) {
SmartVehicleBlueKeyExample example = new SmartVehicleBlueKeyExample();
example.createCriteria().andUserIdEqualTo(userId).andSnEqualTo(sn).andIsDeleteEqualTo(0);
return example;
}
//获取crep(雪花算法+userid)
private String getCrepCode(SmartCarKeyRequest request) {
long snowId = SnowFlake.nextId();
long userId = Long.parseLong(request.getUserId());
StringBuilder sb = new StringBuilder();
return sb.append(snowId).append(userId).toString();
}
}