Commit 2011043d by yixinjj

修改任务下发指令

1 parent 36572713
...@@ -167,7 +167,7 @@ export class DeviceService implements OnModuleInit { ...@@ -167,7 +167,7 @@ export class DeviceService implements OnModuleInit {
JOIN log_config c ON c.device_id = d.device_id JOIN log_config c ON c.device_id = d.device_id
WHERE c.log_saved_days >= TIMESTAMPDIFF(DAY, d.last_log_remove_time, NOW());` WHERE c.log_saved_days >= TIMESTAMPDIFF(DAY, d.last_log_remove_time, NOW());`
for (const { device_id, log_saved_days } of results) { for (const { device_id, log_saved_days } of results) {
await this.wsGateway.send({ event: WsMessageType.Private, device_id, data: { command: Command.RemoveLog, payload: { days: log_saved_days } } }) await this.wsGateway.send({ event: WsMessageType.Private, device_id, data: { command: Command.RemoveLog, days: log_saved_days } })
} }
return { code: RESPONSE_CODE.SUCCESS, message: '每日日志删除任务完成' } return { code: RESPONSE_CODE.SUCCESS, message: '每日日志删除任务完成' }
} }
......
...@@ -113,7 +113,7 @@ export class WsGateway implements WsTypes.WebSocketGateway<Server, Observable<Ws ...@@ -113,7 +113,7 @@ export class WsGateway implements WsTypes.WebSocketGateway<Server, Observable<Ws
* 发送消息 * 发送消息
* @param payload * @param payload
*/ */
public send(payload: WsTypes.MessageBody<WsTypes.WsMessageData<Command>, WsMessageType>): Promise<CommonTypes.IResData> { public send(payload: WsTypes.MessageBody<Command, WsMessageType>): Promise<CommonTypes.IResData> {
return this.wsService[{ [WsMessageType.Private]: 'sendPrivateMessage', [WsMessageType.System]: 'sendPublicMessage' }[payload.event]](payload) return this.wsService[{ [WsMessageType.Private]: 'sendPrivateMessage', [WsMessageType.System]: 'sendPublicMessage' }[payload.event]](payload)
} }
} }
import { HttpException, HttpStatus, Injectable, Inject } from '@nestjs/common' import { HttpException, HttpStatus, Injectable, Inject } from '@nestjs/common'
import { Server, Socket } from 'socket.io' import { Server, Socket } from 'socket.io'
import { Logger } from '@nestjs/common' import { Logger } from '@nestjs/common'
import { RESPONSE_CODE, WsAccess, Events, WsMessageType, WsStatus } from '../../common/enums' import { RESPONSE_CODE, WsAccess, Events, WsMessageType, WsStatus, Command } from '../../common/enums'
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter' import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'
@Injectable() @Injectable()
...@@ -96,7 +96,7 @@ export class WsService { ...@@ -96,7 +96,7 @@ export class WsService {
* @param messagePath 发布地址 * @param messagePath 发布地址
* @param response 响应数据 * @param response 响应数据
*/ */
async sendPublicMessage(payload: WsTypes.MessageBody<WsTypes.WsMessageData<string>>): Promise<CommonTypes.IResData> { async sendPublicMessage(payload: WsTypes.MessageBody<Command>): Promise<CommonTypes.IResData> {
const res = this.server?.emit(WsMessageType.System, payload.data) const res = this.server?.emit(WsMessageType.System, payload.data)
if (!res) { if (!res) {
Logger.log('Websocket Send Private Error->', payload) Logger.log('Websocket Send Private Error->', payload)
...@@ -112,7 +112,7 @@ export class WsService { ...@@ -112,7 +112,7 @@ export class WsService {
* @param response 响应数据 * @param response 响应数据
* @param deviceId 接收者设备号 * @param deviceId 接收者设备号
*/ */
async sendPrivateMessage(payload: WsTypes.MessageBody<WsTypes.WsMessageData<string>>): Promise<CommonTypes.IResData> { async sendPrivateMessage(payload: WsTypes.MessageBody<Command>): Promise<CommonTypes.IResData> {
if (!payload.device_id) throw new HttpException({ status: HttpStatus.BAD_REQUEST, message: '请求参数device_id 必传', error: 'deviceId is required' }, HttpStatus.BAD_REQUEST) if (!payload.device_id) throw new HttpException({ status: HttpStatus.BAD_REQUEST, message: '请求参数device_id 必传', error: 'deviceId is required' }, HttpStatus.BAD_REQUEST)
const client = this.allClients?.get(this.clientIds.get(payload.device_id)) const client = this.allClients?.get(this.clientIds.get(payload.device_id))
const res = client?.emit(WsMessageType.Private, payload.data) const res = client?.emit(WsMessageType.Private, payload.data)
......
...@@ -8,10 +8,9 @@ declare namespace WsTypes { ...@@ -8,10 +8,9 @@ declare namespace WsTypes {
type MessageBody<U, T = WsMsgType> = { type MessageBody<U, T = WsMsgType> = {
event: T event: T
data: U data: MessageData<U>
} & (T extends 'private' ? CommonTypes.IDeviceId : Partial<CommonTypes.IDeviceId>) } & (T extends 'private' ? CommonTypes.IDeviceId : Partial<CommonTypes.IDeviceId>)
interface IWsResponse<T, U> extends CommonTypes.IDeviceId { interface IWsResponse<T, U> extends CommonTypes.IDeviceId {
/** /**
* 状态码 * 状态码
...@@ -20,14 +19,11 @@ declare namespace WsTypes { ...@@ -20,14 +19,11 @@ declare namespace WsTypes {
message: U message: U
} }
type LoginValidate<T> = { isValidate: boolean; errMsg: T } type LoginValidate<T> = { isValidate: boolean; errMsg: T }
type MessageData<T, U = CommonTypes.TObj> = { type MessageData<T> = {
command: T | string command: T
payload: U days?: number
id?: string
interval?: number
} }
type WsMessageData<T> = MessageData<T>
type WsConnError = CommonTypes.IDeviceId & { message: string } type WsConnError = CommonTypes.IDeviceId & { message: string }
} }
...@@ -41,7 +41,8 @@ POST http://localhost:3000/broadcast/cmd HTTP/1.1 ...@@ -41,7 +41,8 @@ POST http://localhost:3000/broadcast/cmd HTTP/1.1
"event":"private", "event":"private",
"device_id":"1000000", "device_id":"1000000",
"data":{ "data":{
"command":"startPolling", "command":"stopPolling",
"interval":1000,
"force":true "force":true
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!