新增逻辑

This commit is contained in:
yan.y 2024-01-16 23:03:48 +08:00
parent d5ddab8043
commit c2da1433a2
14 changed files with 276 additions and 39 deletions

View File

@ -13,8 +13,10 @@ import com.ifish.entity.DeviceUser;
*/ */
public interface DeviceUserDao { public interface DeviceUserDao {
public DeviceUser getUniqueByProperty(Criterion... criterions); DeviceUser getUniqueByProperty(Criterion... criterions);
public List<DeviceUser> getListByProperty(Criterion... criterions); List<DeviceUser> getListByProperty(Criterion... criterions);
void saveDeviceUser(DeviceUser deviceUser);
} }

View File

@ -35,4 +35,8 @@ public class DeviceUserDaoImpl extends HibernateBaseDao<DeviceUser, PriId> imple
return this.findByProperty(criterions); return this.findByProperty(criterions);
} }
@Override
public void saveDeviceUser(DeviceUser deviceUser) {
this.getSession().save(deviceUser);
}
} }

View File

@ -1,6 +1,7 @@
package com.ifish.entity; package com.ifish.entity;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date;
/** /**
* @ClassName: tbl_device_user * @ClassName: tbl_device_user
@ -9,31 +10,75 @@ import java.io.Serializable;
*/ */
public class DeviceUser implements Serializable{ public class DeviceUser implements Serializable{
private static final long serialVersionUID = 6845058501114939424L; private static final long serialVersionUID = -6056043883433438749L;
//ID //ID
private PriId priId = new PriId(); private DeviceUserId priId = new DeviceUserId();
//1主控制权0被分享者 //1主控制权0被分享者
private String isMaster; private String isMaster;
//显示名称 //显示名称
private String showName; private String showName;
//保存的显示图标
private String customIconName;
//保存的显示名称
private String customShowName;
//修改时间
private Date updateTime;
//创建时间
private Date createTime;
public PriId getPriId() { public DeviceUser() {}
public DeviceUser(DeviceUserId priId, String isMaster, String showName) {
this.priId = priId;
this.isMaster = isMaster;
this.showName = showName;
this.customIconName = "";
this.customShowName = "";
this.updateTime = new Date();
this.createTime = new Date();
}
public DeviceUserId getPriId() {
return priId; return priId;
} }
public void setPriId(PriId priId) { public void setPriId(DeviceUserId priId) {
this.priId = priId; this.priId = priId;
} }
public String getIsMaster() {
return isMaster;
}
public void setIsMaster(String isMaster) {
this.isMaster = isMaster;
}
public String getShowName() { public String getShowName() {
return showName; return showName;
} }
public void setShowName(String showName) { public void setShowName(String showName) {
this.showName = showName; this.showName = showName;
} }
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getIsMaster() {
return isMaster;
}
public void setIsMaster(String isMaster) {
this.isMaster = isMaster;
}
public String getCustomIconName() {
return customIconName;
}
public void setCustomIconName(String customIconName) {
this.customIconName = customIconName;
}
public String getCustomShowName() {
return customShowName;
}
public void setCustomShowName(String customShowName) {
this.customShowName = customShowName;
}
} }

View File

@ -0,0 +1,47 @@
package com.ifish.entity;
import java.io.Serializable;
public class DeviceUserId implements Serializable {
private static final long serialVersionUID = 1L;
private Integer userId;
private Integer deviceId;
public DeviceUserId() {}
public DeviceUserId(Integer userId, Integer deviceId) {
this.userId = userId;
this.deviceId = deviceId;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getDeviceId() {
return deviceId;
}
public void setDeviceId(Integer deviceId) {
this.deviceId = deviceId;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof DeviceUserId){
DeviceUserId pk=(DeviceUserId)obj;
if(this.userId.equals(pk.userId)&&this.deviceId.equals(pk.deviceId)){
return true;
}
}
return false;
}
@Override
public int hashCode() {
return super.hashCode();
}
}

View File

@ -3,12 +3,15 @@
<hibernate-mapping package="com.ifish.entity"> <hibernate-mapping package="com.ifish.entity">
<class name="DeviceUser" table="tbl_device_user" dynamic-insert="true" dynamic-update="true"> <class name="DeviceUser" table="tbl_device_user" dynamic-insert="true" dynamic-update="true">
<meta attribute="sync-DAO">false</meta> <meta attribute="sync-DAO">false</meta>
<cache usage="read-write" region="myCache" /> <composite-id name="priId" class="com.ifish.entity.DeviceUserId">
<composite-id name="priId" class="com.ifish.entity.PriId">
<key-property name="userId" column="user_id" type="java.lang.Integer" /> <key-property name="userId" column="user_id" type="java.lang.Integer" />
<key-property name="deviceId" column="device_id" type="java.lang.Integer" /> <key-property name="deviceId" column="device_id" type="java.lang.Integer" />
</composite-id> </composite-id>
<property name="isMaster" column="is_master" type="string" length="1" /> <property name="isMaster" column="is_master" type="string" length="1" />
<property name="showName" column="show_name" type="string" length="50" /> <property name="showName" column="show_name" type="string" length="50" />
<property name="customIconName" column="custom_icon_name" type="string" length="100"/>
<property name="customShowName" column="custom_show_name" type="string" length="100"/>
<property name="createTime" column="create_time" type="timestamp" />
<property name="updateTime" column="update_time" type="timestamp" />
</class> </class>
</hibernate-mapping> </hibernate-mapping>

View File

@ -1,5 +1,6 @@
package com.ifish.service; package com.ifish.service;
import com.ifish.entity.DeviceUser;
import com.ifish.entity.PushList; import com.ifish.entity.PushList;
import com.ifish.entity.User; import com.ifish.entity.User;
@ -10,7 +11,12 @@ import com.ifish.entity.User;
* *
*/ */
public interface UserService { public interface UserService {
public User findById(Integer userId); User findById(Integer userId);
PushList save(PushList pushList);
DeviceUser findUserDevice(Integer userId, Integer deviceId);
void saveDeviceUser(DeviceUser deviceUser);
public PushList save(PushList pushList);
} }

View File

@ -78,9 +78,6 @@ public class DeviceServiceImpl implements DeviceService {
ipStr.append("."+v); ipStr.append("."+v);
} }
} }
String userId = new String(model.getUserId(), StandardCharsets.UTF_8);
log.info("用户登陆信息 :::::: mac : {}, ip : {}, userId : {}", stcMac, ipStr, userId);
//版本 //版本
Integer version = model.getVersion() & 0xff; Integer version = model.getVersion() & 0xff;
//电子厂 //电子厂

View File

@ -1,5 +1,9 @@
package com.ifish.serviceImpl; package com.ifish.serviceImpl;
import com.ifish.dao.DeviceUserDao;
import com.ifish.entity.DeviceUser;
import com.ifish.enums.BooleanEnum;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -30,6 +34,9 @@ public class UserServiceImpl implements UserService {
@Autowired @Autowired
private PushListDao pushListDao; private PushListDao pushListDao;
@Autowired
private DeviceUserDao deviceUserDao;
@Override @Override
public User findById(Integer userId) { public User findById(Integer userId) {
return this.userDao.findById(userId); return this.userDao.findById(userId);
@ -40,5 +47,13 @@ public class UserServiceImpl implements UserService {
return pushListDao.save(pushList); return pushListDao.save(pushList);
} }
@Override
public DeviceUser findUserDevice(Integer userId, Integer deviceId) {
return this.deviceUserDao.getUniqueByProperty(Restrictions.eq("priId.userId", userId), Restrictions.eq("priId.deviceId", deviceId));
}
@Override
public void saveDeviceUser(DeviceUser deviceUser) {
this.deviceUserDao.saveDeviceUser(deviceUser);
}
} }

View File

@ -206,6 +206,61 @@ public class SomeServer {
else{ else{
log.error("error warn_type:{},macAddress:{}",warnType,strSrc); log.error("error warn_type:{},macAddress:{}",warnType,strSrc);
} }
} //设备登录服务器
else if(message instanceof OrderFunctionCode1_1) {
OrderFunctionCode1_1 receive = (OrderFunctionCode1_1) message;
byte[] srcBytes = receive.getSrc();
byte[] destBytes = receive.getDest();
String strSrc = ByteUtil.bytesToHexString(srcBytes);
StringBuilder userId = new StringBuilder();
for (byte b : receive.getUserId()) {
char character = (char) b;
userId.append(character);
}
//服务器回复设备登录请求
BackFunctionCode1 model = OrderModel.BackFunctionCode1(destBytes, srcBytes);
//直接往本次session写回去
session.write(model);
//保存连接
sessions_cz.put(strSrc, session);
remoteAddress.put(session.getRemoteAddress().toString(), strSrc);
//设备重新连接上则移除延时推送的任务
JobGroup jobGroup = new JobGroup();
jobGroup.setJobName(strSrc);
jobGroup.setTriggerName(strSrc);
scheduleJob.deleteJob(jobGroup);
try {
log.info("设备登录服务器 ********** mac : " + strSrc + " 用户ID : " + userId);
if (limitCaches.get(strSrc) == 0L) {
//更新设备信息
deviceService.update(receive);
limitCaches.put(strSrc, 1L);
}
if (!"".contentEquals(userId) && userId.length() > 0) {
Device device = deviceService.getUniqueByProperty("macAddress", strSrc);
if (device != null) {
int user = Integer.parseInt(userId.toString());
DeviceUser userDevice = userService.findUserDevice(user, device.getDeviceId());
if (userDevice != null) {
DeviceUser deviceUser = null;
//新增关联关系
DeviceUserId priId = new DeviceUserId();
if (device.getHardwareType() != null && device.getHardwareType().equals("3a")) {
deviceUser = new DeviceUser(priId, BooleanEnum.YES.getKey(), "加热器"+(int)(Math.random()*9000+1000));
} else {
deviceUser = new DeviceUser(priId, BooleanEnum.YES.getKey(), "鱼缸"+(int)(Math.random()*9000+1000));
}
//关联设备
userService.saveDeviceUser(deviceUser);
log.info("设备绑定成功 *********** mac : " + strSrc + " 用户ID : " + userId + " 设备名称 : " + deviceUser.getShowName());
}
}
}
} catch (Exception e) {
e.printStackTrace();
log.error("save device login error:macAddress:{},error msg:{}",strSrc,e.toString());
}
} }
//设备登录服务器 //设备登录服务器
else if(message instanceof OrderFunctionCode1) { else if(message instanceof OrderFunctionCode1) {
@ -226,11 +281,10 @@ public class SomeServer {
jobGroup.setTriggerName(strSrc); jobGroup.setTriggerName(strSrc);
scheduleJob.deleteJob(jobGroup); scheduleJob.deleteJob(jobGroup);
try { try {
String stcMac = ByteUtil.bytesToHexString(model.getSrc()); if (limitCaches.get(strSrc) == 0L) {
if (limitCaches.get(stcMac) == 0L) {
//更新设备信息 //更新设备信息
deviceService.update(receive); deviceService.update(receive);
limitCaches.put(stcMac, 1L); limitCaches.put(strSrc, 1L);
} }
// if(device==null){ // if(device==null){

View File

@ -4,6 +4,7 @@ import java.nio.charset.Charset;
import java.util.Date; import java.util.Date;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.alibaba.fastjson.JSONObject;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
@ -21,6 +22,7 @@ import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession; import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoder; import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput; import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.springframework.beans.BeanUtils;
/** /**
@ -150,7 +152,7 @@ public class ServerDecode implements ProtocolDecoder {
break; break;
} }
//长度为24设备发登录请求 //长度为24设备发登录请求
case 24:{ case 24: {
if(check_code==1){ if(check_code==1){
functionCode1(buf, out); functionCode1(buf, out);
} else { } else {
@ -158,6 +160,14 @@ public class ServerDecode implements ProtocolDecoder {
} }
break; break;
} }
case 34: {
if(check_code==1){
functionCode1_34(buf, out);
} else {
out.write(bytes);
}
break;
}
//长度为25手机发登录请求 //长度为25手机发登录请求
case 25:{ case 25:{
if(check_code==0){ if(check_code==0){
@ -285,6 +295,44 @@ public class ServerDecode implements ProtocolDecoder {
login_ip[i] = buf.get(); login_ip[i] = buf.get();
} }
model.setLogin_ip(login_ip); model.setLogin_ip(login_ip);
byte[] crc16_code = model.getCrc16_code();
for (int i = 0; i < crc16_code.length; i++) {
crc16_code[i] = buf.get();
}
out.write(model);
}
/**
* 设备登陆服务器
* @param buf
* @param out
*/
private void functionCode1_34(IoBuffer buf, ProtocolDecoderOutput out) {
OrderFunctionCode1_1 model = new OrderFunctionCode1_1();
log.info("login bytes : " + buf.getHexDump());
//15字节数据包头
model.setType(buf.get());
model.setCheck_code(buf.get());
byte[] src = model.getSrc();
for (int i = 0; i < src.length; i++) {
src[i] = buf.get();
}
model.setSrc(src);
byte[] dest = model.getDest();
for (int i = 0; i < dest.length; i++) {
dest[i] = buf.get();
}
model.setDest(dest);
model.setRemote_len(buf.get());
//登陆信息
model.setVendor(buf.get());
model.setHardware_type(buf.get());
model.setVersion(buf.get());
byte[] login_ip = model.getLogin_ip();
for (int i = 0; i < login_ip.length; i++) {
login_ip[i] = buf.get();
}
model.setLogin_ip(login_ip);
byte[] userId = model.getUserId(); byte[] userId = model.getUserId();
for (int i = 0; i < userId.length; i++) { for (int i = 0; i < userId.length; i++) {
userId[i] = buf.get(); userId[i] = buf.get();

View File

@ -20,7 +20,6 @@ public class OrderFunctionCode1 extends HeadModel implements Serializable {
private byte version; private byte version;
//登陆IP //登陆IP
private byte[] login_ip = new byte[4]; private byte[] login_ip = new byte[4];
private byte[] userId = new byte[2];
public byte getVendor() { public byte getVendor() {
@ -54,12 +53,4 @@ public class OrderFunctionCode1 extends HeadModel implements Serializable {
public void setLogin_ip(byte[] login_ip) { public void setLogin_ip(byte[] login_ip) {
this.login_ip = login_ip; this.login_ip = login_ip;
} }
public byte[] getUserId() {
return userId;
}
public void setUserId(byte[] userId) {
this.userId = userId;
}
} }

View File

@ -0,0 +1,25 @@
package com.ifish.socketNew.model.send;
import com.ifish.socketNew.model.HeadModel;
import java.io.Serializable;
/**
* 新增字段
* @author guogw
*
*/
public class OrderFunctionCode1_1 extends OrderFunctionCode1 implements Serializable {
private static final long serialVersionUID = 1L;
//用户ID
private byte[] userId = new byte[10];
public byte[] getUserId() {
return userId;
}
public void setUserId(byte[] userId) {
this.userId = userId;
}
}

View File

@ -1,6 +1,6 @@
c3p0.driverClassName=com.mysql.jdbc.Driver c3p0.driverClassName=com.mysql.jdbc.Driver
#c3p0.url=jdbc\:mysql\://localhost\:3306/myfishdb?characterEncoding\=UTF-8 #c3p0.url=jdbc\:mysql\://localhost\:3306/myfishdb?characterEncoding\=UTF-8
c3p0.url=jdbc\:mysql\://139.196.24.156\:3306/myfishdb?characterEncoding\=UTF-8 c3p0.url=jdbc\:mysql\://10.174.60.12\:3306/myfishdb?characterEncoding\=UTF-8
#c3p0.username=ifish #c3p0.username=ifish
#c3p0.password=ifish7pwd #c3p0.password=ifish7pwd
c3p0.username=root c3p0.username=root

View File

@ -3,6 +3,6 @@
#\u6d4b\u8bd5\u4e91 #\u6d4b\u8bd5\u4e91
ifish.ip=10.174.60.12 ifish.ip=10.174.60.12
#\u6b63\u5f0f\u4e91 #\u6b63\u5f0f\u4e91
#ifish.ip=120.55.190.56 #ifish.ip=10.46.65.60
#\u670d\u52a1\u5668\u7aef\u53e3 #\u670d\u52a1\u5668\u7aef\u53e3
ifish.port=9955 ifish.port=9955