Skip to content

Commit

Permalink
基本完成 RESTFul API 的设计
Browse files Browse the repository at this point in the history
  • Loading branch information
bitkylin committed Apr 24, 2017
1 parent 8e8cd90 commit 0f93bae
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cc.bitky.clustermanage.db.mongoops;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Repository;

@Repository
public class RoutineTableOps {
private final MongoOperations operations;

@Autowired
public RoutineTableOps(MongoOperations operations) {
this.operations = operations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cc.bitky.clustermanage.db.mongoops;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Repository;

@Repository
public class SettingOps {
private final MongoOperations operations;

@Autowired
public SettingOps(MongoOperations operations) {
this.operations = operations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

import cc.bitky.clustermanage.db.bean.routineinfo.HistoryInfo;
import cc.bitky.clustermanage.db.bean.routineinfo.RoutineTables;
import cc.bitky.clustermanage.db.repository.RoutimeTableRepository;
import cc.bitky.clustermanage.db.repository.RoutineTableRepository;
import cc.bitky.clustermanage.server.message.tcp.TcpMsgResponseDeviceStatus;

@Repository
public class DbRoutinePresenter {
private final RoutimeTableRepository routimeTableRepository;
private final RoutineTableRepository routineTableRepository;

@Autowired
public DbRoutinePresenter(RoutimeTableRepository routimeTableRepository) {
this.routimeTableRepository = routimeTableRepository;
public DbRoutinePresenter(RoutineTableRepository routineTableRepository) {
this.routineTableRepository = routineTableRepository;
}

/**
Expand All @@ -23,13 +23,13 @@ public DbRoutinePresenter(RoutimeTableRepository routimeTableRepository) {
* @param employeeObjectId 员工的 ObjectId
* @param tcpMsgResponseDeviceStatus 设备状态包
*/
public void updateRoutineById(String employeeObjectId, TcpMsgResponseDeviceStatus tcpMsgResponseDeviceStatus) {
RoutineTables routineTables = routimeTableRepository.findOne(employeeObjectId);
void updateRoutineById(String employeeObjectId, TcpMsgResponseDeviceStatus tcpMsgResponseDeviceStatus) {
RoutineTables routineTables = routineTableRepository.findOne(employeeObjectId);
if (routineTables == null) {
routineTables = new RoutineTables();
routineTables.setId(employeeObjectId);
}
routineTables.getHistoryInfos().add(HistoryInfo.newInstance(tcpMsgResponseDeviceStatus.getTime(), tcpMsgResponseDeviceStatus.getStatus()));
routimeTableRepository.save(routineTables);
routineTableRepository.save(routineTables);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cc.bitky.clustermanage.db.presenter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cc.bitky.clustermanage.db.bean.KySetting;
import cc.bitky.clustermanage.db.repository.SettingRepository;
import cc.bitky.clustermanage.server.bean.ServerWebMessageHandler.Card;

@Repository
public class DbSettingPresenter {
private final SettingRepository settingRepository;

@Autowired
public DbSettingPresenter(SettingRepository settingRepository) {
this.settingRepository = settingRepository;
}

/**
* 将卡号集合保存在数据库的 Setting 文档中
*
* @param cardArray 卡号集合
* @param card 卡号类型枚举
* @return 是否保存成功
*/
boolean saveCardArray(long[] cardArray, Card card) {
KySetting kySetting = settingRepository.findOne("1");
if (kySetting == null) {
kySetting = new KySetting();
kySetting.setId(1);
}
switch (card) {
case FREE:
kySetting.setFreeCardList(cardArray);
break;
case CONFIRM:
kySetting.setConfirmCardList(cardArray);
break;
}
settingRepository.save(kySetting);
return true;
}

/**
* 获取卡号的集合
*
* @return 卡号的集合
*/
long[] getCardArray(Card card) {
KySetting kyKySettings = settingRepository.findOne("1");
if (kyKySettings == null) return new long[0];
long[] longs;
switch (card) {
case FREE:
longs = kyKySettings.getFreeCardList();
return longs == null ? new long[0] : longs;
case CONFIRM:
longs = kyKySettings.getConfirmCardList();
return longs == null ? new long[0] : longs;
}
return new long[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@
import cc.bitky.clustermanage.db.bean.Device;
import cc.bitky.clustermanage.db.bean.DeviceGroup;
import cc.bitky.clustermanage.db.bean.Employee;
import cc.bitky.clustermanage.db.bean.KySetting;
import cc.bitky.clustermanage.db.repository.DeviceGroupRepository;
import cc.bitky.clustermanage.db.repository.SettingRepository;
import cc.bitky.clustermanage.server.bean.ServerWebMessageHandler.Card;
import cc.bitky.clustermanage.server.message.IMessage;
import cc.bitky.clustermanage.server.message.tcp.TcpMsgResponseDeviceStatus;

@Repository
public class KyDbPresenter {
private final DeviceGroupRepository deviceGroupRepository;

private final DbEmployeePresenter dbEmployeePresenter;
private final SettingRepository settingRepository;
private final DbSettingPresenter dbSettingPresenter;
private final DbDevicePresenter dbDevicePresenter;
private final DbRoutinePresenter dbRoutinePresenter;

int groupSize;
private int groupSize;

private Logger logger = LoggerFactory.getLogger(KyDbPresenter.class);

@Autowired
public KyDbPresenter(DbRoutinePresenter dbRoutinePresenter, DbEmployeePresenter dbEmployeePresenter, DeviceGroupRepository deviceGroupRepository, DbDevicePresenter dbDevicePresenter, SettingRepository settingRepository) {
public KyDbPresenter(DbRoutinePresenter dbRoutinePresenter, DbEmployeePresenter dbEmployeePresenter
, DeviceGroupRepository deviceGroupRepository, DbDevicePresenter dbDevicePresenter
, DbSettingPresenter dbSettingPresenter) {
this.dbRoutinePresenter = dbRoutinePresenter;
this.deviceGroupRepository = deviceGroupRepository;
this.settingRepository = settingRepository;
this.dbSettingPresenter = dbSettingPresenter;
this.dbDevicePresenter = dbDevicePresenter;
this.dbEmployeePresenter = dbEmployeePresenter;
}
Expand Down Expand Up @@ -131,19 +131,6 @@ public List<Device> getDevices(int groupId, int boxId) {
return dbDevicePresenter.getDevices(groupId, boxId);
}

/**
* 从数据库中获取万能卡号的数组
*
* @return 万能卡号的数组
*/
public long[] obtainFreeCardArray() {
List<KySetting> kyKySettings = settingRepository.findAll();
if (kyKySettings.size() == 0) return new long[0];
long[] longs = kyKySettings.get(kyKySettings.size() - 1).getFreeCardList();

return longs == null ? new long[0] : longs;
}

/**
* 设备初始化: 根据员工卡号获取员工信息
*
Expand All @@ -154,4 +141,25 @@ public Employee obtainDeviceByEmployeeCard(long cardNumber) {
String employeeObjectId = dbDevicePresenter.obtainEmployeeObjectIdByCardNum(cardNumber);
return dbEmployeePresenter.ObtainEmployeeByObjectId(employeeObjectId);
}

/**
* 获取卡号的集合
*
* @return 卡号的集合
*/
public long[] getCardArray(Card card) {

return dbSettingPresenter.getCardArray(card);
}

/**
* 将卡号保存到数据库
*
* @param freecards 卡号的数组
* @param card 卡号类型
* @return 是否保存成功
*/
public boolean saveCardNumber(long[] freecards, Card card) {
return dbSettingPresenter.saveCardArray(freecards, card);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

import cc.bitky.clustermanage.db.bean.routineinfo.RoutineTables;

public interface RoutimeTableRepository extends MongoRepository<RoutineTables, String> {
public interface RoutineTableRepository extends MongoRepository<RoutineTables, String> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;

import cc.bitky.clustermanage.db.presenter.KyDbPresenter;
import cc.bitky.clustermanage.server.bean.ServerWebMessageHandler.Card;
import cc.bitky.clustermanage.server.message.IMessage;
import cc.bitky.clustermanage.server.message.web.WebMsgDeployFreeCardNumber;
import cc.bitky.clustermanage.server.message.web.WebMsgGrouped;
Expand Down Expand Up @@ -42,7 +43,7 @@ private boolean sendMsgToTcp(List<IMessage> messages) {
* @return 万能卡号获取并写入 TCP 成功
*/
boolean deployFreeCard(int groupId, int deviceId) {
long[] freeCards = kyDbPresenter.obtainFreeCardArray();
long[] freeCards = kyDbPresenter.getCardArray(Card.FREE);
IMessage CardMsg = new WebMsgDeployFreeCardNumber(groupId, deviceId, freeCards);
return deployGroupedMessage(CardMsg);
}
Expand Down Expand Up @@ -84,4 +85,24 @@ private boolean deployGroupedMessage(IMessage message) {
boolean deployDeviceMsg(IMessage message) {
return deployGroupedMessage(message);
}

/**
* 从数据库中获取卡号的集合
*
* @return 卡号的集合
*/
long[] getCardArray(Card card) {
return kyDbPresenter.getCardArray(card);
}

/**
* 将卡号保存到数据库
*
* @param freecards 卡号的数组
* @param card 卡号类型
* @return 是否保存成功
*/
boolean saveCardNumber(long[] freecards, Card card) {
return kyDbPresenter.saveCardNumber(freecards, card);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ServerWebMessageHandler(KyDbPresenter kyDbPresenter) {
}

/**
* 从数据库中获取万能卡号并写入 Netty 的 Handler
* 从数据库中获取万能卡号,并写入 Netty 的 Handler
*
* @param groupId 设备组 Id
* @param deviceId 设备 Id
Expand Down Expand Up @@ -71,4 +71,39 @@ public boolean deployDeviceMsg(IMessage message) {
void setKyServerCenterHandler(KyServerCenterHandler kyServerCenterHandler) {
this.kyServerCenterHandler = kyServerCenterHandler;
}


/**
* 从数据库中获取万能卡号的集合
*
* @return 万能卡号的集合
*/
public long[] obtainFreeCards() {
return kyServerCenterHandler.getCardArray(Card.FREE);
}

/**
* 从数据库中获取确认卡号的集合
*
* @return 确认卡号的集合
*/
public long[] obtainConfirmCards() {
return kyServerCenterHandler.getCardArray(Card.CONFIRM);
}

/**
* 将卡号保存到数据库
*
* @param freecards 卡号的数组
* @param card 卡号类型
* @return 是否保存成功
*/
public boolean saveCardNumber(long[] freecards, Card card) {
return kyServerCenterHandler.saveCardNumber(freecards, card);
}

public enum Card {
FREE,
CONFIRM
}
}
Loading

0 comments on commit 0f93bae

Please sign in to comment.