基于上面(一)中讲解,启动的时候,每一个厂家的协议会不同,因此会启动不同的线程和端口来处理不同的厂家业务。
- new Thread(new BlueChipsServer(2004)).start();
- new Thread(new pinzhiServer(2002)).start();
- 如果有更多厂家依次创建
启动线程后进行数据获取和数据状态更新,因为netty是异步操作,由于设备返回的协议信息比较少,无法唯一确定下发数据情况,所以对有些必须要设备回复消息后才能进行第二步操作的,采用异步转同步的模式,增加锁
synchronized和记录当前下发消息的唯一标识
package com.openapi.protocol;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.utils.HttpClient;
import com.common.utils.HttpClientUtil;
import com.openapi.apidomain.EtlDO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class TaskDistribution {
private final static Logger log = Logger.getLogger(TaskDistribution.class);
@Value("${eems.serviceIp}")
private String serviceIp;
@Autowired
BlueChips blueChips;
public static Map<String, String> SendMap = new HashMap<String, String>();
public static long GetExpireDuration() {
long expire = 63072000;
expire += System.currentTimeMillis()/1000;
return expire;
}
public int ReadTask() throws URISyntaxException {
//读取ETL数据
String data = HttpClientUtil.httpPostRequest(serviceIp+"/web/getIssuedData");
//将ETLjson封装成map
Map<String, Object> resultMap = JSON.parseObject(data);
String code = resultMap.get("code").toString();
String listData = resultMap.get("data").toString();
Map<String, Object> Data = JSON.parseObject(listData);
if (code.equals("200")) {
int total = Integer.parseInt(Data.get("etlNum").toString());
String dataMsg = Data.get("etlList").toString();
log.info(" total: " + total + " dataMsg: " + dataMsg);
JSONArray etl_info = JSON.parseArray(dataMsg);
//返回给eems 更新数据下发状态
List<Map<String, Object>> list = new ArrayList<>();
for (int i=0; i< total; i++){
JSONObject jsonObject = etl_info.getJSONObject(i);
int status_code = TaskDistribution(jsonObject);
Map<String, Object> params = new HashMap<>();
params.put("id", Integer.parseInt(jsonObject.getString("id").toString()));
params.put("status_code", status_code);
list.add(params);
log.info( "第" + i + "个" + "id= " + jsonObject.getString("id").toString() + " status_code " + status_code );
}
//组装更新数据状态
if(list.size()>0) {
String Pdata = HttpClient.httpPostRequest(serviceIp+"/web/updateDataStatus", JSONObject.toJSONString(list));
log.info("更新etl数据状态请求 返回结果 Pdata:" + Pdata);
}
if(total<1){
log.info("没有数据需要处理了!");
return 0;
}
}else {
//系统错误
log.error("系统错误!" + resultMap.get("msg").toString());
return 0;
}
return 1;
}
/***************************************************************
* 0:待处理数据
* 1:正在处理
* 2:下发成功
* 3:下发失败
* 4:设备离线
* **************************************************************/
public int TaskDistribution(JSONObject jsonObject){
int ret = BlueChips.RET_DELIVERY_FAILED;
//根据厂商的唯一编号(manufacturer_uuid) 分别启动不同的进程来处理数据
System.out.println(" TaskDistribution jsonObject:" + jsonObject);
String manufacturer_uuid = jsonObject.getString("manufacturerUuid").toString();
EtlDO etldo = new EtlDO();
etldo.setId(Long.parseLong(jsonObject.getString("id").toString()));
etldo.setHardwareNum(jsonObject.getString("hardwareNum").toString());
etldo.setUserUuid(jsonObject.getString("userUuid").toString());
etldo.setName(jsonObject.getString("name").toString());
etldo.setDepartmentName(jsonObject.getString("departmentName").toString());
if(jsonObject.getString("companyId") != null) {
etldo.setCompanyId(jsonObject.getString("companyId").toString());
}else{
etldo.setCompanyId("");//随机写入一个默认值,防止平台传入空值导致系统错误
}
if(jsonObject.getString("companyName") != null) {
etldo.setCompanyName(jsonObject.getString("companyName").toString());
}else{
etldo.setCompanyName("");//随机写入一个默认值,防止平台传入空值导致系统错误
}
if(jsonObject.getString("propertyId") != null) {
etldo.setPropertyId(jsonObject.getString("propertyId").toString());
}else{
etldo.setPropertyId("");//随机写入一个默认值,防止平台传入空值导致系统错误
}
if(jsonObject.getString("imgAddress") != null) {
etldo.setImgAddress(jsonObject.getString("imgAddress").toString());
}else{
etldo.setImgAddress("");//随机写入一个默认值,防止平台传入空值导致系统错误
}
if(jsonObject.getString("type") != null) {
etldo.setType(Integer.parseInt(jsonObject.getString("type").toString()));
}else{
etldo.setType(1000);//随机写入一个默认值,防止平台传入空值导致系统错误
}
if(jsonObject.getString("operationType") != null) {
etldo.setOperationType(Integer.parseInt(jsonObject.getString("operationType").toString()));
}else {
etldo.setOperationType(1001);//随机写入一个默认值,防止平台传入空值导致系统错误
}
if(jsonObject.getString("cardId") != null) {
etldo.setCardId(jsonObject.getString("cardId").toString());
}else {
etldo.setCardId("");//随机写入一个默认值,防止平台传入空值导致系统错误
}
if(jsonObject.getString("manufacturerUuid") != null) {
etldo.setManufacturerUuid(jsonObject.getString("manufacturerUuid").toString());
}else {
etldo.setManufacturerUuid("");//随机写入一个默认值,防止平台传入空值导致系统错误
}
//蓝筹科技
if(manufacturer_uuid.equals("4")){
synchronized (TaskDistribution.class) {//采用异步转同步等待消息返回 再执行下一条
BlueChips blueChips = new BlueChips();
//1: 添加&编辑 2: 删除,3:按照公司删除, 4:恢复出厂配合
//0: 二维码; 1:人脸;2:物业卡
if (0 == etldo.getType()) {//二维码
List<String> cardlist = new ArrayList<>();
cardlist.add(etldo.getUserUuid());
SendMap.put(etldo.getHardwareNum(),etldo.getUserUuid());
if (1 == etldo.getOperationType()) {//1: 添加&编辑
ret = blueChips.UserOperation(etldo.getHardwareNum(), blueChips.JST_SAVE_USER, cardlist, GetExpireDuration() + "");
if (blueChips.RET_SUCESS != ret) {
log.error("添加用户二维码失败 失败码:" + ret + " 单条数据: " + jsonObject);
SendMap.values().removeIf(value -> value.contains(BlueChips.ReverseOrder(etldo.getUserUuid())));
return ret;
}
} else if (2 == etldo.getOperationType()) {//2: 删除
ret = blueChips.UserOperation(etldo.getHardwareNum(), blueChips.JST_DEL_USER, cardlist, GetExpireDuration() + "");
if (blueChips.RET_SUCESS != ret) {
log.error("删除用户二维码失败 失败码:" + ret + " 单条数据: " + jsonObject);
SendMap.values().removeIf(value -> value.contains(BlueChips.ReverseOrder(etldo.getUserUuid())));
return ret;
}
}else{
log.error("二维码类型未知 operationType:" + etldo.getOperationType());
SendMap.values().removeIf(value -> value.contains(etldo.getUserUuid()));
return blueChips.RET_DELIVERY_FAILED;
}
try {
log.info("二维码数据下发 等待设备返回消息!");
TaskDistribution.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (1 == etldo.getType()) {//人脸
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> params = new HashMap<>();
params.put("uid", etldo.getUserUuid());
params.put("name", etldo.getName());
params.put("oid", etldo.getCompanyId());
params.put("org", etldo.getCompanyName());
params.put("dept", etldo.getDepartmentName());
params.put("img", etldo.getImgAddress());
params.put("card", blueChips.ReverseOrder(etldo.getCardId()));
params.put("expire", GetExpireDuration());
list.add(params);
SendMap.put(etldo.getHardwareNum(),etldo.getUserUuid());
if (1 == etldo.getOperationType()) {//1: 添加&编辑
ret = blueChips.AndroidUserOperation(etldo.getHardwareNum(), blueChips.JST_ANDROID_SAVE_FACE, list);
if (blueChips.RET_SUCESS != ret) {
log.error("添加用户人脸数据失败 失败码:" + ret + " 单条数据: " + jsonObject);
SendMap.values().removeIf(value -> value.contains(BlueChips.ReverseOrder(etldo.getUserUuid())));
return ret;
}
} else if (2 == etldo.getOperationType()) {//2: 删除
ret = blueChips.AndroidUserOperation(etldo.getHardwareNum(), blueChips.JST_ANDROID_DEL_FACE, list);
if (blueChips.RET_SUCESS != ret) {
SendMap.values().removeIf(value -> value.contains(BlueChips.ReverseOrder(etldo.getUserUuid())));
log.error("删除用户人脸数据失败 失败码:" + ret + " 单条数据: " + jsonObject);
return ret;
}
} else if (3 == etldo.getOperationType()) {//3:按照公司删除 删除公司(160)
if (1 == etldo.getType()) {
ret = blueChips.DeleteCompany(etldo.getHardwareNum(), etldo.getCompanyId());
if (blueChips.RET_SUCESS != ret) {
SendMap.values().removeIf(value -> value.contains(BlueChips.ReverseOrder(etldo.getUserUuid())));
log.error("按照公司删除失败 ret:" + ret + " 单条数据: " + jsonObject);
return ret;
}
}
}else{
log.error("人脸设备类型未知 operationType:" + etldo.getOperationType());
SendMap.values().removeIf(value -> value.contains(etldo.getUserUuid()));
return blueChips.RET_DELIVERY_FAILED;
}
try {
log.info("人脸数据下发,等待设备回复!");
TaskDistribution.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (2 == etldo.getType()) {//物业卡
List<String> cardlist = new ArrayList<>();
cardlist.add(BlueChips.ReverseOrder(etldo.getCardId()));
SendMap.put(etldo.getHardwareNum(),BlueChips.ReverseOrder(etldo.getCardId()));
if (1 == etldo.getOperationType()) {//1: 添加&编辑
ret = blueChips.CardOperation(etldo.getHardwareNum(), blueChips.JST_SAVE_CARD, cardlist, GetExpireDuration() + "");
if (blueChips.RET_SUCESS != ret) {
log.error("添加用户二维码或者卡数据失败 失败码:" + ret + " 单条数据: " + jsonObject);
SendMap.values().removeIf(value -> value.contains(BlueChips.ReverseOrder(etldo.getCardId())));
return ret;
}
} else if (2 == etldo.getOperationType()) {//2: 删除
ret = blueChips.CardOperation(etldo.getHardwareNum(), blueChips.JST_DEL_CARD, cardlist, GetExpireDuration() + "");
if (blueChips.RET_SUCESS != ret) {
log.error("删除用户二维码或者卡数据失败 失败码:" + ret + " 单条数据: " + jsonObject);
SendMap.values().removeIf(value -> value.contains(BlueChips.ReverseOrder(etldo.getCardId())));
return ret;
}
}else{
log.error("物业卡类型未知 operationType:" + etldo.getOperationType());
SendMap.values().removeIf(value -> value.contains(BlueChips.ReverseOrder(etldo.getCardId())));
return blueChips.RET_DELIVERY_FAILED;
}
try {
log.info("物业卡数据下发,等待设备回复!");
TaskDistribution.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
if (4 == etldo.getOperationType()) {
log.info("恢复出厂配置 暂时没有处理");
return blueChips.RET_DELIVERY_FAILED;
}
log.error("ETL 蓝筹科技 下发数据 类型错误:" + etldo.getType());
return blueChips.RET_DELIVERY_FAILED;
}
}
}else if(manufacturer_uuid.equals("1")){//平冶
synchronized (TaskDistribution.class) {//采用异步转同步等待消息返回 再执行下一条
pinzhi pz = new pinzhi();
//1: 添加&编辑 2: 删除,3:按照公司删除, 4:恢复出厂配合
//0: 二维码; 1:人脸;2:物业卡
if (0 == etldo.getType()) {//平治目前二维码是在线验证模式
log.error("平治设备暂时不支持离线开门: " + etldo.getType());
return ret;
} else if (2 == etldo.getType()) {
if (1 == etldo.getOperationType()) {
ret= pz.AddwhitelistSingle(etldo.getHardwareNum(), BlueChips.ReverseOrder(etldo.getCardId()));
if (pz.RET_SUCESS != ret) {
log.error("平治添加卡数据失败 失败码:" + ret);
return ret;
}
} else if (2 == etldo.getOperationType()) {
ret = pz.DeleteWhitelistSingle(etldo.getHardwareNum(), BlueChips.ReverseOrder(etldo.getCardId()));
if (pz.RET_SUCESS != ret) {
log.error("平治删除卡数据失败 失败码:" + ret);
return ret;
}
} else {
log.error("平治设备操作类型未知 operationType: " + etldo.getOperationType());
return ret;
}
try {
log.info("物业卡数据下发,等待设备回复!");
TaskDistribution.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
log.error("平治设备类型未知 type: " + etldo.getType());
return ret;
}
}
}else {
log.error("设备厂商不存在 manufacturer_uuid: " + manufacturer_uuid);
return ret;
}
return ret;
}
}