clobot-socket-manager
v1.0.13
Published
> Clobot Socket Manager (TM websocket handler) > <br/> > <br/>
Downloads
44
Readme
ClobotRosManager
Clobot Socket Manager (TM websocket handler)
목차
Getting Started
Installation
npm install clobot-socket-manager
or
yarn add clobot-socket-manager
Syntax
import ClobotSocketManager, {
ClobotSocketOption,
SocketUrlVO,
} from "clobot-ros-manager";
const options: ClobotSocketOption = {
url,
maxRetries: 0,
publisher: "APP",
};
const serviceUrl = {
ROBOT_CMD: "/clobot_msgs/robot/robot_cmd",
CHANGE_REQUEST: "/clobot_msgs/page/change_request",
CONFIG_SAVE: "/clobot_msgs/robot/config_save",
LOAD_PAGE: "/clobot_msgs/page/load_page",
};
const topicUrl = {
ROBOT_STATUS: "/clobot_msgs/robot/robot_status",
ALERT_MESSAGE: "/clobot_msgs/robot/alert_message",
LOADING_PROGRESS: "/clobot_msgs/robot/loading_progress",
EMERGENCY: "/clobot_msgs/robot/emergency",
};
this.socketUrl = new SocketUrlVO(serviceUrl, topicUrl);
this.socketManager = new ClobotSocketManager(options, this.socketUrl);
options (optional)
- Type:
Object
- Check out the available options.
- Type:
socketUrl (optional)
- Type: SocketUrlVO
- Check out the available socketUrl.
Example
import ClobotSocketManager, {
ClobotSocketOption,
SocketStatus,
ClobotSocketMessage,
SocketUrlVO,
} from "clobot-socket-manager";
const serviceUrl = {
ROBOT_CMD: "/clobot_msgs/robot/robot_cmd",
CHANGE_REQUEST: "/clobot_msgs/page/change_request",
CONFIG_SAVE: "/clobot_msgs/robot/config_save",
LOAD_PAGE: "/clobot_msgs/page/load_page",
};
const topicUrl = {
ROBOT_STATUS: "/clobot_msgs/robot/robot_status",
ALERT_MESSAGE: "/clobot_msgs/robot/alert_message",
LOADING_PROGRESS: "/clobot_msgs/robot/loading_progress",
EMERGENCY: "/clobot_msgs/robot/emergency",
};
export default class SocketManager {
public socketManager: ClobotSocketManager;
private socketUrl: SocketUrlVO;
constructor(url: string) {
const options: ClobotSocketOption = {
url,
maxRetries: 0,
publisher: "APP",
};
this.socketUrl = new SocketUrlVO(serviceUrl, topicUrl);
this.socketManager = new ClobotSocketManager(options, this.socketUrl);
// Socket Service handler
const { CHANGE_REQUEST, ROBOT_CMD } = this.socketUrl.service;
this.socketManager.event.on(CHANGE_REQUEST, this.handleChanageRequest);
this.socketManager.event.on(ROBOT_CMD, this.handleRobotCmd);
// Socket Topic handler
const { ALERT_MESSAGE, ROBOT_STATUS } = this.socketUrl.topic;
this.socketManager.event.on(ALERT_MESSAGE, this.handleAlertMessage);
this.socketManager.event.on(ROBOT_STATUS, this.handleRobotStatus);
// Socket status handler
this.socketManager.event.on("socket", this.handleSocket);
this.socketManager.event.on("error", this.handleError);
}
private handleSocket = (state: SocketStatus) => {
switch (state) {
case "onSuccess":
console.log("# onSuccess");
break;
case "onDisconnect":
console.log("# onDisconnect");
break;
case "onClose":
console.log("# onClose");
break;
case "onFail":
console.log("# onFail");
break;
case "onTimeOut":
console.log("# onTimeOut");
break;
case "onConnecting":
console.log("# on connecting");
break;
}
};
private handleChanageRequest = (data: ClobotSocketMessage) => {
console.log("handle change request", data);
};
private handleRobotCmd = (data: ClobotSocketMessage) => {
console.log("handle robot cmd", data);
};
private handleAlertMessage = (data: ClobotSocketMessage) => {
console.log("handle robot cmd", data);
};
private handleRobotStatus = (data: ClobotSocketMessage) => {
console.log("handle robot status", data);
};
private handleError = (error: any) => {
console.log("ERROR", error);
};
}
Options
- url
TM Socket에 연동할 URL 정보
- Type:
String
- Default:
false
- maxRetries
Socket 연동 실패할 경우 다시 연동 시도 할 카운터 수 - "0"의 경우 붙을 때까지 연동 시도
- Type:
Number
- Default:
false
- publisher
Socket event 발송 주체 TM의 경우 publisher : 'TM' 고정 Front의 경우 상황에 따라 'UI' or 'APP' 사용
- Type:
'UI' or 'APP'
- Default:
false
socketUrl
- Service
- CHANGE_REQUEST: "< Enter your project's 'CHANGE_REQUEST' URL >"
- CONFIG_SAVE: "< Enter your project's 'CONFIG_SAVE' URL >"
- LOAD_PAGE: "< Enter your project's 'LOAD_PAGE' URL >"
- ROBOT_CMD: "< Enter your project's 'ROBOT_CMD' URL >"
- Topic
- ROBOT_STATUS: "< Enter your project's 'ROBOT_STATUS' URL >"
- ALERT_MESSAGE: "< Enter your project's 'ALERT_MESSAGE' URL >"
- LOADING_PROGRESS: "< Enter your project's 'LOADING_PROGRESS' URL >"
- EMERGENCY: "< Enter your project's 'EMERGENCY' URL >"
Methods
UI or APP -> TM
changeRequest(data : any, timeout : number)
- type : service
- name : Service.CHANGE_REQUEST ( enum )
- path : /clobot_msgs/page/change_request ( path )
socketManager.changeRequest(
{
page_id: "page_id",
comp_id: "comp_id",
content: {},
},
10000 // timeout - default 10s
);
robotCmd(data :any, timeout :number)
- type : service
- name : Service.ROBOT_CMD ( enum )
- path : /clobot_msgs/robot/robot_cmd ( path )
socketManager.robotCmd(
{
page_id: "page_id",
comp_id: "comp_id",
content: {},
},
10000 // timeout - default 10s
);
Event
TM -> UI or APP
Socket Event
SocketStatus
- onDisconnect : 연결이 존재하다가 끊어졌을 떄 상태
- onClose : 연결이 종료되었을 떄 상태
- onFail : 연결 실패 상태
- onSuccess : 연결 성공 상태
- onConnecting : 연결 시도중 상태
- onTimeOut : 요청중 타임아웃 되었을 때 상태
import ClobotSocketManager, { SocketStatus } from "clobot-clobot-manager";
socketManager.event.on("socket", (status: SocketStatus) => {});
Service Event
Service
* 현재 정의되어 있는 Service Keys
- ROBOT_CMD,
- CHANGE_REQUEST,
- LOADING_PROGRESS,
- EMERGENCY,
Topic Event
Topic
* 현재 정의되어 있는 Topic Keys
- ROBOT_STATUS,
- ALERT_MESSAGE,
- CONFIG_SAVE,
- LOAD_PAGE,