22 changed files with 5 additions and 896 deletions
-
4.env
-
2README.md
-
2package.json
-
21src/constant/command.type.js
-
13src/constant/err.type.js
-
101src/controller/gateway.controller.js
-
12src/controller/terminal.controller.js
-
80src/middleware/gateway.middleware.js
-
2src/model/account.model.js
-
29src/model/com.model.js
-
42src/model/gateway.model.js
-
28src/model/role.model.js
-
74src/model/terminal.model.js
-
61src/router/gateway.route.js
-
11src/router/terminal.route.js
-
21src/service/com.service.js
-
150src/service/gateway.service.js
-
75src/service/terminal.service.js
-
58src/tcp/controller/tcp.controller.js
-
40src/tcp/forwardCommand.js
-
5src/tcp/server.js
-
70src/test/tcpClient.js
@ -1 +1 @@ |
|||||
# 智能太阳能服务端 Nodejs -> Koa |
|
||||
|
# 鸡管家养殖系统 |
@ -1,21 +0,0 @@ |
|||||
module.exports = { |
|
||||
// 重置网关名称 网关 -> 服务器
|
|
||||
RESET_THE_GATEWAY_NAME: "ReceiptSetTheGatewayName", |
|
||||
// 设置网关名称 服务器 -> 网关
|
|
||||
SET_THE_GATEWAY_NAME: "SetTheGatewayName", |
|
||||
// 全部注册 服务器 -> 网关
|
|
||||
REGISTER_ALL_JUNCTION_BOXES: "CommandRegisterAllJunctionBoxes", |
|
||||
// 全部注册 网关 -> 服务器
|
|
||||
RECEIPT_ALL_JUNCTION_BOXES: "ReceiptAllJunctionBoxes", |
|
||||
// 全部查询 服务器 -> 网关
|
|
||||
QUERY_ALL_JUNCTION_BOXES: "CommandQueryAllJunctionBoxes", |
|
||||
// 全部查询 网关 -> 服务器
|
|
||||
RECEIPT_QUERY_ALL_JUNCTION_BOXES: "ReceiptQueryAllJunctionBoxes", |
|
||||
// 单个控制 服务器 -> 网关
|
|
||||
CONTROL_JUNCTION_BOX_SWITCH: "CommandControlJunctionBoxSwitch", |
|
||||
// 单个控制 网关 -> 服务器
|
|
||||
RECEIPT_CONTROL_JUNCTION_BOX_SWITCH: "ReceiptControlJunctionBoxSwitch", |
|
||||
// 上传检测数据 网关 -> 服务器
|
|
||||
JUNCTION_BOX_SWITCH_STATE_RECEIPT: |
|
||||
"command,getJunctionBoxSwitchsStateReceipt", |
|
||||
}; |
|
@ -1,101 +0,0 @@ |
|||||
const { |
|
||||
getGatewayInfo, |
|
||||
addGatewayOne, |
|
||||
updateGatewayInfo, |
|
||||
getAllOnlineGateWayInService, |
|
||||
saveOtherWatchDataList, |
|
||||
getTerminalInfoListByGatewayListService, |
|
||||
} = require('../service/gateway.service') |
|
||||
const Response = require('../utils/response') |
|
||||
const Request = require('../utils/request') |
|
||||
const { |
|
||||
addGatewayOneError, |
|
||||
saveTerminalInfoError, |
|
||||
} = require('../constant/err.type') |
|
||||
const { SET_THE_GATEWAY_NAME } = require('../constant/command.type') |
|
||||
|
|
||||
const createClientToSendData = require('../tcp/client') |
|
||||
|
|
||||
class GatewayController { |
|
||||
async addOrUpdateSingleGateway(ctx, next) { |
|
||||
const { TerminalName, TerminalID } = ctx.request.body |
|
||||
// 查询该网关是否存在
|
|
||||
const gatewayInfo = await getGatewayInfo({ gateway_number: TerminalID }) |
|
||||
let res = null |
|
||||
if (gatewayInfo) { |
|
||||
// 存在该网关则修改信息
|
|
||||
if (gatewayInfo.gateway_name != TerminalName) { |
|
||||
res = await updateGatewayInfo(TerminalName, TerminalID) |
|
||||
} |
|
||||
} else { |
|
||||
// 不存在则新增
|
|
||||
res = await addGatewayOne(TerminalName, TerminalID) |
|
||||
} |
|
||||
// 向网关发送5.3 设置网关名称指令
|
|
||||
createClientToSendData( |
|
||||
Request(SET_THE_GATEWAY_NAME, { TerminalName, TerminalID }), |
|
||||
) |
|
||||
ctx.body = Response(0, '网关信息更新完毕', res) |
|
||||
} |
|
||||
|
|
||||
async getAllOnlineGateWay(ctx, next) { |
|
||||
const { pageIndex, pageSize } = ctx.request.query |
|
||||
const res = await getAllOnlineGateWayInService( |
|
||||
pageIndex && parseInt(pageIndex), |
|
||||
pageSize && parseInt(pageSize), |
|
||||
) |
|
||||
ctx.body = Response(0, '查询成功', res) |
|
||||
} |
|
||||
|
|
||||
async addOrUpdateWatchData(ctx, next) { |
|
||||
const { TerminalName, TerminalID, States } = ctx.request.body |
|
||||
// 查询该网关是否存在
|
|
||||
try { |
|
||||
const gatewayInfo = await getGatewayInfo({ gateway_number: TerminalID }) |
|
||||
let gatewayRes = null |
|
||||
if (gatewayInfo) { |
|
||||
// 存在该网关则修改信息
|
|
||||
if (gatewayInfo.gateway_name != TerminalName) { |
|
||||
gatewayRes = await updateGatewayInfo(TerminalName, TerminalID) |
|
||||
} |
|
||||
} else { |
|
||||
// 不存在则新增
|
|
||||
gatewayRes = await addGatewayOne(TerminalName, TerminalID) |
|
||||
} |
|
||||
} catch (error) { |
|
||||
ctx.app.emit('error', addGatewayOneError, ctx) |
|
||||
} |
|
||||
try { |
|
||||
const res = saveOtherWatchDataList(TerminalID, States) |
|
||||
ctx.body = Response(0, '更新网关及接线盒信息完毕', res) |
|
||||
} catch (error) { |
|
||||
ctx.app.emit('error', saveTerminalInfoError, ctx) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
async registerAll(ctx, next) { |
|
||||
const { TerminalName, TerminalID, Command } = ctx.request.body |
|
||||
// 控制网关
|
|
||||
createClientToSendData(Command, { TerminalName, TerminalID }) |
|
||||
ctx.body = Response(0, '发送全部注册指令成功', null) |
|
||||
} |
|
||||
|
|
||||
async queryAll(ctx, next) { |
|
||||
const { TerminalName, TerminalID, Command } = ctx.request.body |
|
||||
// 控制网关
|
|
||||
createClientToSendData(Command, { TerminalName, TerminalID }) |
|
||||
ctx.body = Response(0, '发送全部查询指令成功', null) |
|
||||
} |
|
||||
|
|
||||
async getTerminalInfoListByGatewayList(ctx, next) { |
|
||||
const { gateway_id, terminal_status, pageIndex } = ctx.request.query |
|
||||
const res = await getTerminalInfoListByGatewayListService( |
|
||||
gateway_id, |
|
||||
terminal_status, |
|
||||
pageIndex, |
|
||||
) |
|
||||
ctx.body = Response(0, '查询接线盒信息成功', res) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
module.exports = new GatewayController() |
|
@ -1,12 +0,0 @@ |
|||||
const { updateTerminalSwitch } = require('../service/terminal.service') |
|
||||
const Response = require('../utils/response') |
|
||||
|
|
||||
class TerminalController { |
|
||||
async updateTerminalSwitchState(ctx, next) { |
|
||||
const { switch_state, terminal_id } = ctx.request.body |
|
||||
const res = updateTerminalSwitch(switch_state, terminal_id) |
|
||||
ctx.body = Response(0, '更新接线盒通断状态成功', res) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
module.exports = new TerminalController() |
|
@ -1,80 +0,0 @@ |
|||||
const { |
|
||||
gatewayFormatError, |
|
||||
gatewayNameExistedError, |
|
||||
switchStateReceiptCommandError, |
|
||||
registerAllCommandError, |
|
||||
queryAllCommandError, |
|
||||
} = require("../constant/err.type"); |
|
||||
const { getGatewayInfo } = require("../service/gateway.service"); |
|
||||
|
|
||||
const { |
|
||||
JUNCTION_BOX_SWITCH_STATE_RECEIPT, |
|
||||
QUERY_ALL_JUNCTION_BOXES, |
|
||||
REGISTER_ALL_JUNCTION_BOXES, |
|
||||
} = require("../constant/command.type"); |
|
||||
|
|
||||
// 检测网关参数是否传递正确
|
|
||||
const gatewayValidator = async (ctx, next) => { |
|
||||
if (ctx.request.body) { |
|
||||
const { TerminalName, TerminalID } = ctx.request.body; |
|
||||
// 合法性
|
|
||||
if (!TerminalID || !TerminalName) { |
|
||||
ctx.app.emit("error", gatewayFormatError, ctx); |
|
||||
return; |
|
||||
} |
|
||||
} else { |
|
||||
ctx.app.emit("error", gatewayFormatError, ctx); |
|
||||
return; |
|
||||
} |
|
||||
await next(); |
|
||||
}; |
|
||||
|
|
||||
const isGatewayNameExisted = async (ctx, next) => { |
|
||||
const { TerminalName, TerminalID } = ctx.request.body; |
|
||||
const isGatewayNameExisted = await getGatewayInfo({ |
|
||||
gateway_name: TerminalName, |
|
||||
}); |
|
||||
if (isGatewayNameExisted) { |
|
||||
// 网关名称重复
|
|
||||
if (TerminalID != isGatewayNameExisted.gateway_number) { |
|
||||
ctx.app.emit("error", gatewayNameExistedError, ctx); |
|
||||
return; |
|
||||
} |
|
||||
} |
|
||||
await next(); |
|
||||
}; |
|
||||
|
|
||||
const verifyWatchCommandTrue = async (ctx, next) => { |
|
||||
const { Command } = ctx.request.body; |
|
||||
if (Command != JUNCTION_BOX_SWITCH_STATE_RECEIPT) { |
|
||||
ctx.app.emit("error", switchStateReceiptCommandError, ctx); |
|
||||
return; |
|
||||
} |
|
||||
await next(); |
|
||||
}; |
|
||||
|
|
||||
const verifyRegisterCommandTrue = async (ctx, next) => { |
|
||||
const { Command } = ctx.request.body; |
|
||||
if (Command != REGISTER_ALL_JUNCTION_BOXES) { |
|
||||
ctx.app.emit("error", registerAllCommandError, ctx); |
|
||||
return; |
|
||||
} |
|
||||
await next(); |
|
||||
}; |
|
||||
|
|
||||
const verifyQueryCommandTrue = async (ctx, next) => { |
|
||||
const { Command } = ctx.request.body; |
|
||||
if (Command != QUERY_ALL_JUNCTION_BOXES) { |
|
||||
ctx.app.emit("error", queryAllCommandError, ctx); |
|
||||
return; |
|
||||
} |
|
||||
await next(); |
|
||||
}; |
|
||||
|
|
||||
module.exports = { |
|
||||
gatewayValidator, |
|
||||
isGatewayNameExisted, |
|
||||
verifyWatchCommandTrue, |
|
||||
verifyRegisterCommandTrue, |
|
||||
verifyQueryCommandTrue, |
|
||||
}; |
|
@ -1,29 +0,0 @@ |
|||||
const { DataTypes } = require("sequelize"); |
|
||||
|
|
||||
const seq = require("../db/seq"); |
|
||||
|
|
||||
const Com = seq.define( |
|
||||
"solar_com", |
|
||||
{ |
|
||||
// id 会被sequelize自动创建, 管理
|
|
||||
com_number: { |
|
||||
type: DataTypes.STRING, |
|
||||
allowNull: false, |
|
||||
unique: true, |
|
||||
comment: "com口编号", |
|
||||
}, |
|
||||
gateway_id: { |
|
||||
type: DataTypes.STRING, |
|
||||
allowNull: false, |
|
||||
comment: "网关id", |
|
||||
}, |
|
||||
}, |
|
||||
{ |
|
||||
tableName: "solar_com", |
|
||||
} |
|
||||
); |
|
||||
|
|
||||
// 强制同步数据库(创建数据表)
|
|
||||
// Com.sync({ force: true });
|
|
||||
|
|
||||
module.exports = Com; |
|
@ -1,42 +0,0 @@ |
|||||
const { DataTypes } = require("sequelize"); |
|
||||
|
|
||||
const seq = require("../db/seq"); |
|
||||
|
|
||||
const Gateway = seq.define( |
|
||||
"solar_gateway", |
|
||||
{ |
|
||||
// id 会被sequelize自动创建, 管理
|
|
||||
gateway_name: { |
|
||||
type: DataTypes.STRING(64), |
|
||||
allowNull: false, |
|
||||
unique: true, |
|
||||
comment: "网关名称", |
|
||||
}, |
|
||||
is_online: { |
|
||||
type: DataTypes.BOOLEAN, |
|
||||
allowNull: false, |
|
||||
defaultValue: 1, |
|
||||
comment: "是否在线,0掉线,1在线", |
|
||||
}, |
|
||||
gateway_number: { |
|
||||
type: DataTypes.STRING(64), |
|
||||
allowNull: false, |
|
||||
unique: true, |
|
||||
comment: "网关编号", |
|
||||
}, |
|
||||
is_deleted: { |
|
||||
type: DataTypes.BOOLEAN, |
|
||||
defaultValue: 0, |
|
||||
allowNull: false, |
|
||||
comment: "是否在物理层面移除掉了该设备,0未移除,1已移除", |
|
||||
}, |
|
||||
}, |
|
||||
{ |
|
||||
tableName: "solar_gateway", |
|
||||
} |
|
||||
); |
|
||||
|
|
||||
// 强制同步数据库(创建数据表)
|
|
||||
// Gateway.sync({ force: true });
|
|
||||
|
|
||||
module.exports = Gateway; |
|
@ -1,28 +0,0 @@ |
|||||
const { DataTypes } = require("sequelize"); |
|
||||
|
|
||||
const seq = require("../db/seq"); |
|
||||
|
|
||||
const Role = seq.define( |
|
||||
"solar_role", |
|
||||
{ |
|
||||
// id 会被sequelize自动创建, 管理
|
|
||||
role_id: { |
|
||||
type: DataTypes.INTEGER, |
|
||||
allowNull: false, |
|
||||
comment: "角色id", |
|
||||
}, |
|
||||
gateway_id: { |
|
||||
type: DataTypes.INTEGER, |
|
||||
allowNull: false, |
|
||||
comment: "网关id", |
|
||||
}, |
|
||||
}, |
|
||||
{ |
|
||||
tableName: "solar_role", |
|
||||
} |
|
||||
); |
|
||||
|
|
||||
// 强制同步数据库(创建数据表)
|
|
||||
// Role.sync({ force: true });
|
|
||||
|
|
||||
module.exports = Role; |
|
@ -1,74 +0,0 @@ |
|||||
const { DataTypes } = require("sequelize"); |
|
||||
|
|
||||
const seq = require("../db/seq"); |
|
||||
|
|
||||
const Terminal = seq.define( |
|
||||
"solar_terminal", |
|
||||
{ |
|
||||
device_id: { |
|
||||
type: DataTypes.INTEGER, |
|
||||
allowNull: false, |
|
||||
unique: true, |
|
||||
comment: "接线盒的设备ID", |
|
||||
}, |
|
||||
switch_state: { |
|
||||
type: DataTypes.BOOLEAN, |
|
||||
allowNull: false, |
|
||||
comment: "设备的开关状态,默认0关闭,1开启", |
|
||||
defaultValue: 0, |
|
||||
}, |
|
||||
temperuature: { |
|
||||
type: DataTypes.INTEGER, |
|
||||
allowNull: false, |
|
||||
comment: "设备的温度,单位℃,精确到整数", |
|
||||
defaultValue: 0, |
|
||||
}, |
|
||||
current: { |
|
||||
type: DataTypes.FLOAT(10, 1), |
|
||||
allowNull: false, |
|
||||
comment: "设备的电流,单位A,精确到小数点后一位", |
|
||||
defaultValue: 0, |
|
||||
}, |
|
||||
voltage: { |
|
||||
type: DataTypes.FLOAT(10, 1), |
|
||||
allowNull: false, |
|
||||
comment: "设备的电压,单位V,精确到小数点后一位", |
|
||||
defaultValue: 0, |
|
||||
}, |
|
||||
online_or_not: { |
|
||||
type: DataTypes.BOOLEAN, |
|
||||
allowNull: false, |
|
||||
comment: "接线盒是否在线,默认0不在线,1在线", |
|
||||
defaultValue: 1, |
|
||||
}, |
|
||||
registration_info_match: { |
|
||||
type: DataTypes.BOOLEAN, |
|
||||
allowNull: false, |
|
||||
comment: "注册信息是否正确,默认0不正确,1正确", |
|
||||
defaultValue: 1, |
|
||||
}, |
|
||||
gateway_id: { |
|
||||
type: DataTypes.STRING, |
|
||||
allowNull: false, |
|
||||
comment: "网关id", |
|
||||
}, |
|
||||
com_id: { |
|
||||
type: DataTypes.STRING, |
|
||||
allowNull: false, |
|
||||
comment: "对应那个COM口", |
|
||||
}, |
|
||||
rank: { |
|
||||
type: DataTypes.INTEGER, |
|
||||
allowNull: false, |
|
||||
comment: "当前终端在当前COM链条中的位置", |
|
||||
}, |
|
||||
}, |
|
||||
{ |
|
||||
tableName: "solar_terminal", |
|
||||
} |
|
||||
); |
|
||||
|
|
||||
// 强制同步数据库(创建数据表)
|
|
||||
// Terminal.sync({ force: true });
|
|
||||
|
|
||||
module.exports = Terminal; |
|
@ -1,61 +0,0 @@ |
|||||
const Router = require('koa-router') |
|
||||
|
|
||||
const router = new Router({ prefix: '/gateway' }) |
|
||||
|
|
||||
const { |
|
||||
addOrUpdateSingleGateway, |
|
||||
getAllOnlineGateWay, |
|
||||
addOrUpdateWatchData, |
|
||||
registerAll, |
|
||||
queryAll, |
|
||||
getTerminalInfoListByGatewayList, |
|
||||
} = require('../controller/gateway.controller') |
|
||||
const { auth } = require('../middleware/auth.middleware') |
|
||||
const { |
|
||||
gatewayValidator, |
|
||||
isGatewayNameExisted, |
|
||||
verifyWatchCommandTrue, |
|
||||
verifyRegisterCommandTrue, |
|
||||
verifyQueryCommandTrue, |
|
||||
} = require('../middleware/gateway.middleware') |
|
||||
|
|
||||
router.post( |
|
||||
'/addOrUpdateSingleGateway', |
|
||||
auth, |
|
||||
gatewayValidator, |
|
||||
isGatewayNameExisted, |
|
||||
addOrUpdateSingleGateway, |
|
||||
) |
|
||||
router.get('/list', auth, getAllOnlineGateWay) |
|
||||
|
|
||||
router.post( |
|
||||
'/addOrUpdateWatchData', |
|
||||
auth, |
|
||||
gatewayValidator, |
|
||||
verifyWatchCommandTrue, |
|
||||
addOrUpdateWatchData, |
|
||||
) |
|
||||
|
|
||||
router.get( |
|
||||
'/getTerminalInfoListByGatewayId', |
|
||||
auth, |
|
||||
getTerminalInfoListByGatewayList, |
|
||||
) |
|
||||
|
|
||||
router.post( |
|
||||
'/allregister', |
|
||||
auth, |
|
||||
gatewayValidator, |
|
||||
verifyRegisterCommandTrue, |
|
||||
registerAll, |
|
||||
) |
|
||||
|
|
||||
router.post( |
|
||||
'/allquery', |
|
||||
auth, |
|
||||
gatewayValidator, |
|
||||
verifyQueryCommandTrue, |
|
||||
queryAll, |
|
||||
) |
|
||||
|
|
||||
module.exports = router |
|
@ -1,11 +0,0 @@ |
|||||
const Router = require('koa-router') |
|
||||
|
|
||||
const router = new Router({ prefix: '/terminal' }) |
|
||||
|
|
||||
const { auth } = require('../middleware/auth.middleware') |
|
||||
|
|
||||
const { |
|
||||
updateTerminalSwitchState, |
|
||||
} = require('../controller/terminal.controller') |
|
||||
router.post('/updateState', auth, updateTerminalSwitchState) |
|
||||
module.exports = router |
|
@ -1,21 +0,0 @@ |
|||||
const Com = require("../model/com.model"); |
|
||||
class ComService { |
|
||||
async addComInfo(com_number, gateway_id) { |
|
||||
// 查询没有则添加
|
|
||||
const whereOpt = {}; |
|
||||
com_number && Object.assign(whereOpt, { com_number }); |
|
||||
gateway_id && Object.assign(whereOpt, { gateway_id }); |
|
||||
const findRes = await Com.findOne({ |
|
||||
attributes: ["com_number", "gateway_id"], |
|
||||
where: whereOpt, |
|
||||
}); |
|
||||
if (!findRes) { |
|
||||
const createRes = Com.create({ |
|
||||
com_number, |
|
||||
gateway_id, |
|
||||
}); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
module.exports = new ComService(); |
|
@ -1,150 +0,0 @@ |
|||||
const Gateway = require('../model/gateway.model') |
|
||||
const { |
|
||||
updateGatewayInfoError, |
|
||||
getGatewayInfoError, |
|
||||
addGatewayOneError, |
|
||||
getAllOnlineGateWayError, |
|
||||
} = require('../constant/err.type') |
|
||||
|
|
||||
const { addComInfo } = require('./com.service') |
|
||||
const { addTerminal } = require('./terminal.service') |
|
||||
const Terminal = require('../model/terminal.model') |
|
||||
|
|
||||
class GatewayService { |
|
||||
async getGatewayInfo({ id, gateway_name, gateway_number }) { |
|
||||
try { |
|
||||
const whereOpt = {} |
|
||||
id && Object.assign(whereOpt, { id }) |
|
||||
gateway_name && Object.assign(whereOpt, { gateway_name }) |
|
||||
gateway_number && Object.assign(whereOpt, { gateway_number }) |
|
||||
const res = await Gateway.findOne({ |
|
||||
attributes: ['id', 'gateway_name', 'gateway_number'], |
|
||||
where: whereOpt, |
|
||||
}) |
|
||||
return res ? res.dataValues : null |
|
||||
} catch (error) { |
|
||||
return ctx.app.exit('error', getGatewayInfoError, ctx) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
async addGatewayOne(TerminalName, TerminalID) { |
|
||||
try { |
|
||||
const res = await Gateway.create({ |
|
||||
gateway_name: TerminalName, |
|
||||
gateway_number: TerminalID, |
|
||||
}) |
|
||||
return res ? res.dataValues : null |
|
||||
} catch (error) { |
|
||||
return ctx.app.exit('error', addGatewayOneError, ctx) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
async updateGatewayInfo(TerminalName, TerminalID) { |
|
||||
try { |
|
||||
const res = await Gateway.update( |
|
||||
{ |
|
||||
gateway_name: TerminalName, |
|
||||
}, |
|
||||
{ |
|
||||
where: { |
|
||||
gateway_number: TerminalID, |
|
||||
}, |
|
||||
}, |
|
||||
) |
|
||||
return res |
|
||||
} catch (error) { |
|
||||
return ctx.app.exit('error', updateGatewayInfoError, ctx) |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
async getAllOnlineGateWayInService(pageIndex = 1, pageSize = 10) { |
|
||||
try { |
|
||||
let count = await Gateway.count({ |
|
||||
where: { |
|
||||
is_online: 1, |
|
||||
}, |
|
||||
}) |
|
||||
if (count != 0) { |
|
||||
const gatewayList = await Gateway.findAll({ |
|
||||
where: { |
|
||||
is_online: 1, |
|
||||
}, |
|
||||
limit: pageSize, |
|
||||
offset: (pageIndex - 1) * pageSize, |
|
||||
}) |
|
||||
const res = gatewayList.map(item => item.dataValues) |
|
||||
return { |
|
||||
pageInfo: { pageIndex, pageSize, total: count }, |
|
||||
list: res, |
|
||||
} |
|
||||
} |
|
||||
} catch (error) { |
|
||||
return ctx.app.exit('error', getAllOnlineGateWayError, ctx) |
|
||||
} |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
async saveOtherWatchDataList(gateway_id, states) { |
|
||||
// const { SwitchChannel, JunctionBoxs } = states || {};
|
|
||||
// 存储com口编号和网关编号的对应关系
|
|
||||
states && |
|
||||
states.map(async item => { |
|
||||
const { SwitchChannel: com_number, JunctionBoxs: terminalList } = |
|
||||
item || {} |
|
||||
const comResult = addComInfo(com_number, gateway_id) |
|
||||
const rank = 1 |
|
||||
terminalList && |
|
||||
terminalList.map(t => { |
|
||||
const terminalResult = addTerminal({ |
|
||||
...t, |
|
||||
com_number, |
|
||||
rank, |
|
||||
gateway_id, |
|
||||
}) |
|
||||
}) |
|
||||
}) |
|
||||
return null |
|
||||
} |
|
||||
|
|
||||
async getTerminalInfoListByGatewayListService( |
|
||||
gateway_id, |
|
||||
terminal_status = '0', |
|
||||
pageIndex = 1, |
|
||||
pageSize = 10, |
|
||||
) { |
|
||||
try { |
|
||||
let count = await Terminal.count({ |
|
||||
where: { |
|
||||
gateway_id, |
|
||||
}, |
|
||||
}) |
|
||||
if (count != 0) { |
|
||||
// 需要根据terminal_status转换为对应的查询条件
|
|
||||
let obj = { gateway_id } |
|
||||
if (terminal_status == '1') { |
|
||||
obj['online_or_not'] = 1 |
|
||||
} else { |
|
||||
if (terminal_status == '2') { |
|
||||
obj['online_or_not'] = 0 |
|
||||
} |
|
||||
if (terminal_status == '3') { |
|
||||
obj['registration_info_match'] = 0 |
|
||||
} |
|
||||
} |
|
||||
console.log(pageIndex) |
|
||||
const terminalList = await Terminal.findAll({ |
|
||||
where: obj, |
|
||||
limit: pageSize, |
|
||||
offset: (parseInt(pageIndex) - 1) * pageSize, |
|
||||
}) |
|
||||
const res = terminalList.map(item => item.dataValues) |
|
||||
return { |
|
||||
pageInfo: { pageIndex, pageSize, total: count }, |
|
||||
list: res || [], |
|
||||
} |
|
||||
} |
|
||||
} catch (error) {} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
module.exports = new GatewayService() |
|
@ -1,75 +0,0 @@ |
|||||
const Terminal = require('../model/terminal.model') |
|
||||
|
|
||||
const createClientToSendData = require('../tcp/client') |
|
||||
class TerminalService { |
|
||||
async addTerminal({ |
|
||||
DeviceID: device_id, |
|
||||
SwitchState: switch_state, |
|
||||
Temperuature: temperuature, |
|
||||
Current: current, |
|
||||
Voltage: voltage, |
|
||||
OnlineOrNot: online_or_not, |
|
||||
RegistrationInfoMatch: registration_info_match, |
|
||||
com_number: com_id, |
|
||||
rank, |
|
||||
gateway_id, |
|
||||
}) { |
|
||||
const findResult = await Terminal.findOne({ |
|
||||
where: { |
|
||||
device_id, |
|
||||
}, |
|
||||
}) |
|
||||
if (findResult) { |
|
||||
const whereOpt = {} |
|
||||
switch_state && Object.assign(whereOpt, { switch_state }) |
|
||||
temperuature && Object.assign(whereOpt, { temperuature }) |
|
||||
current && Object.assign(whereOpt, { current }) |
|
||||
voltage && Object.assign(whereOpt, { voltage }) |
|
||||
online_or_not && Object.assign(whereOpt, { online_or_not }) |
|
||||
registration_info_match && |
|
||||
Object.assign(whereOpt, { registration_info_match }) |
|
||||
com_id && Object.assign(whereOpt, { com_id }) |
|
||||
gateway_id && Object.assign(whereOpt, { gateway_id }) |
|
||||
rank && Object.assign(whereOpt, { rank }) |
|
||||
const res = await Terminal.update(whereOpt, { |
|
||||
where: { |
|
||||
device_id, |
|
||||
}, |
|
||||
}) |
|
||||
return res |
|
||||
} else { |
|
||||
const res = await Terminal.create({ |
|
||||
device_id, |
|
||||
switch_state, |
|
||||
temperuature, |
|
||||
current, |
|
||||
voltage, |
|
||||
online_or_not, |
|
||||
registration_info_match, |
|
||||
com_id, |
|
||||
rank, |
|
||||
gateway_id, |
|
||||
}) |
|
||||
return res.dataValues |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
async updateTerminalSwitch(switch_state, terminal_id) { |
|
||||
const res = await Terminal.update( |
|
||||
{ |
|
||||
switch_state, |
|
||||
}, |
|
||||
{ |
|
||||
where: { |
|
||||
id: terminal_id, |
|
||||
}, |
|
||||
}, |
|
||||
) |
|
||||
// 需要向网关发送通断指令。
|
|
||||
// TODO
|
|
||||
// createClientToSendData({})
|
|
||||
return res |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
module.exports = new TerminalService() |
|
@ -1,58 +0,0 @@ |
|||||
const { |
|
||||
getGatewayInfo, |
|
||||
addGatewayOne, |
|
||||
updateGatewayInfo, |
|
||||
saveOtherWatchDataList, |
|
||||
} = require("../../service/gateway.service"); |
|
||||
const Response = require("../../utils/response"); |
|
||||
const { |
|
||||
saveTerminalInfoError, |
|
||||
addGatewayOneError, |
|
||||
} = require("../../constant/err.type"); |
|
||||
|
|
||||
class TcpController { |
|
||||
// 5.1 终端建立TCP连接后上报终端名称
|
|
||||
async addOrUpdateSingleGateway({ TerminalName, TerminalID }) { |
|
||||
// 查询该网关是否存在
|
|
||||
const gatewayInfo = await getGatewayInfo({ gateway_number: TerminalID }); |
|
||||
let res = null; |
|
||||
if (gatewayInfo) { |
|
||||
// 存在该网关则修改信息
|
|
||||
if (gatewayInfo.gateway_name != TerminalName) { |
|
||||
res = await updateGatewayInfo(TerminalName, TerminalID); |
|
||||
} |
|
||||
} else { |
|
||||
// 不存在则新增
|
|
||||
res = await addGatewayOne(TerminalName, TerminalID); |
|
||||
} |
|
||||
return Response(0, "网关信息更新完毕", res); |
|
||||
} |
|
||||
|
|
||||
// 5.2 5min主动向服务器上传一次监测数据
|
|
||||
async addOrUpdateWatchData({ TerminalName, TerminalID, States }) { |
|
||||
// 查询该网关是否存在
|
|
||||
try { |
|
||||
const gatewayInfo = await getGatewayInfo({ gateway_number: TerminalID }); |
|
||||
let gatewayRes = null; |
|
||||
if (gatewayInfo) { |
|
||||
// 存在该网关则修改信息
|
|
||||
if (gatewayInfo.gateway_name != TerminalName) { |
|
||||
gatewayRes = await updateGatewayInfo(TerminalName, TerminalID); |
|
||||
} |
|
||||
} else { |
|
||||
// 不存在则新增
|
|
||||
gatewayRes = await addGatewayOne(TerminalName, TerminalID); |
|
||||
} |
|
||||
} catch (error) { |
|
||||
return addGatewayOneError; |
|
||||
} |
|
||||
try { |
|
||||
const res = saveOtherWatchDataList(TerminalID, States); |
|
||||
return Response(0, "更新网关及接线盒信息完毕", res); |
|
||||
} catch (error) { |
|
||||
return saveTerminalInfoError; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
module.exports = new TcpController(); |
|
@ -1,40 +0,0 @@ |
|||||
const { |
|
||||
addOrUpdateSingleGateway, |
|
||||
addOrUpdateWatchData, |
|
||||
} = require("./controller/tcp.controller"); |
|
||||
const { |
|
||||
JUNCTION_BOX_SWITCH_STATE_RECEIPT, |
|
||||
RESET_THE_GATEWAY_NAME, |
|
||||
RECEIPT_ALL_JUNCTION_BOXES, |
|
||||
RECEIPT_CONTROL_JUNCTION_BOX_SWITCH, |
|
||||
RECEIPT_QUERY_ALL_JUNCTION_BOXES, |
|
||||
} = require("../constant/command.type"); |
|
||||
|
|
||||
const forwardCommand = (command, data) => { |
|
||||
switch (command) { |
|
||||
case JUNCTION_BOX_SWITCH_STATE_RECEIPT: |
|
||||
// 5.2 5min主动向服务器上传一次监测数据
|
|
||||
addOrUpdateWatchData(data); |
|
||||
break; |
|
||||
case RECEIPT_ALL_JUNCTION_BOXES: |
|
||||
// 5.4 全部注册
|
|
||||
addOrUpdateWatchData(data); |
|
||||
break; |
|
||||
case RECEIPT_QUERY_ALL_JUNCTION_BOXES: |
|
||||
addOrUpdateWatchData(data); |
|
||||
break; |
|
||||
case RECEIPT_CONTROL_JUNCTION_BOX_SWITCH: |
|
||||
addOrUpdateWatchData(data); |
|
||||
break; |
|
||||
case RESET_THE_GATEWAY_NAME: |
|
||||
// 5.3 设置网关名称
|
|
||||
addOrUpdateSingleGateway(data); |
|
||||
break; |
|
||||
default: |
|
||||
// 5.1 终端建立TCP连接后上报终端名称
|
|
||||
addOrUpdateSingleGateway(data); |
|
||||
break; |
|
||||
} |
|
||||
}; |
|
||||
|
|
||||
module.exports = forwardCommand; |
|
@ -1,70 +0,0 @@ |
|||||
// 模拟网关
|
|
||||
|
|
||||
const createClientToSendData = require("../tcp/client"); |
|
||||
|
|
||||
const addGateway = () => { |
|
||||
const data = { |
|
||||
MessageID: "xxxafdafd-adfasdfasd-fdafasdfas-fdadffasd", |
|
||||
TimePoint: 123124, |
|
||||
TerminalName: "北京北京昌平一号", |
|
||||
TerminalID: "AJKM-MBK3-9AQR-1OFD", |
|
||||
}; |
|
||||
createClientToSendData(data); |
|
||||
}; |
|
||||
|
|
||||
const getJunctionBoxSwitchsStateReceipt = () => { |
|
||||
const data = { |
|
||||
MessageID: "xxxafdafd-adfasdfasd-fdafasdfas-fdadffasd", |
|
||||
TimePoint: 123124, |
|
||||
TerminalName: "北京北京昌平一2号", |
|
||||
TerminalID: "AJKM-MBK3-9AQR-1OFD", |
|
||||
Command: "command,getJunctionBoxSwitchsStateReceipt", |
|
||||
States: [ |
|
||||
{ |
|
||||
SwitchChannel: 3, |
|
||||
JunctionBoxs: [ |
|
||||
{ |
|
||||
DeviceID: 12, |
|
||||
SwitchState: true, |
|
||||
Temperuature: 18, |
|
||||
Current: 3.1, |
|
||||
Voltage: 55.2, |
|
||||
OnlineOrNot: true, |
|
||||
RegistrationInfoMatch: true, |
|
||||
}, |
|
||||
], |
|
||||
}, |
|
||||
{ |
|
||||
SwitchChannel: 1, |
|
||||
JunctionBoxs: [ |
|
||||
{ |
|
||||
DeviceID: 13, |
|
||||
SwitchState: true, |
|
||||
Temperuature: 18, |
|
||||
Current: 3.1, |
|
||||
Voltage: 55.2, |
|
||||
OnlineOrNot: true, |
|
||||
RegistrationInfoMatch: true, |
|
||||
}, |
|
||||
], |
|
||||
}, |
|
||||
], |
|
||||
}; |
|
||||
createClientToSendData(data); |
|
||||
}; |
|
||||
|
|
||||
// 5.3 设置网关名称
|
|
||||
const setTheGatewayName = () => { |
|
||||
const data = { |
|
||||
MessageID: "xxxafdafd-adfasdfasd-fdafasdfas-fdadffasd", |
|
||||
TimePoint: 123124, |
|
||||
TerminalName: "北京北京昌平shi号", |
|
||||
TerminalID: "AJKM-MBK3-9AQR-1OFD", |
|
||||
Command: "SetTheGatewayName", |
|
||||
}; |
|
||||
createClientToSendData(data); |
|
||||
}; |
|
||||
|
|
||||
// setTheGatewayName();
|
|
||||
// getJunctionBoxSwitchsStateReceipt();
|
|
||||
// addGateway();
|
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue