gr-device-init
Version:
provide methods and models to connect to server
181 lines (135 loc) • 3.83 kB
Markdown
# GR Device Init
version 1.2.0
Server should use websocket transports
Of course, for first time user 'testtoken' to connect to socket
I did this to control linux-based robots with Raspberry Pi or Banana pi.
Also, It can work on Windows-based devices.
# Updates
added 'this.robotObject.updateState' to update state for robotObject more useful
## Main idea:
> - We have deployed socket server
> - Should just create connections for main and robot (or client/user and robot).
> - Don't need to think about logic on server
> - RobotController has several controllers and update his state by time.
## Getting started
This library provides simple controller and connection to connect to the GRobotics server - this server has some methods to controll devices using socket.
It is a device and user's part.
You can see examples in 'example' folder.
At first, install npm package:
```sh
run npm i gr-device-init
```
## Create user connection
```
const { User, newGroboticsClientConnection } = require("gr-device-init");
```
Create new user that should be pass into connection
```
const user = new User({
id: 'USER_ID',
setUserStatus(st) {
console.log('user accepted', st);
},
robotMessageHandler(state) {
console.log('state', state);
},
deviceMessageHandler(userId, msg) {
console.log('received from device', msg);
}
});
```
Now we can create connection with our user on this robot. Also, robot should get access to this user.
```
const connection = newGroboticsClientConnection(user, 'https://grobotics-socket-server.herokuapp.com/', 'testtoken');
```
Before send commands to robot, we should init user
```
connection.initUser('MY_ROBOT_ID');
```
Also, user can has a few robots
So, now we are possible to send command!
```
connection.send('MY_ROBOT_ID', 'ROBOT_DEVICE_ID', 'my message')
```
## Create robot connection
At first, let's create robot controller
```
const {
createRobotController,
DeviceController,
newGroboticsConnection,
} = require("gr-device-init");
const robotController = createRobotController(robotObject, "MY_ROBOT_ID");
```
Where `robotObject` is something to work with inner state as middleware, for example:
```
const robotObject = {
init() {
console.log("init robots");
},
validate() {
// This method needs to validate command to robot. Just return true if does not need.
return true;
},
initUser() {
console.log("initUser");
},
serverConnected() {
console.log("serverConnected");
},
serverDisconnected() {
console.log("serverDisconnected");
},
};
```
Now create some controller with device
```
const testDevice = {
id: "TEST_DEVICE",
messagesIdsMap: {
serverConnected: "serverConnected",
serverDisconnected: "serverDisconnected",
initUser: "initUser",
},
initDevice(args) {
console.log("init in testDevice");
},
attachMsgFromDevice(cb) {
let count = 0;
setInterval(() => {
cb(count++)
}, 100);
},
sendMessage(msg) {
console.log("send testDevice", msg);
// here should be validation for message 'initUser'
// just return true or false.
// At this moment, each device should return true to create access for user
},
};
```
```
const testController = new DeviceController({
id: "TEST",
device: testDevice,
});
```
### Important!!
#### Add controllers before create connections
```
robotController.addController(testController);
robotController.addController(testController1);
robotController.addController(testController2);
....
```
After adding controllers, we should connect robotController to server, like this:
```
const connection = newGroboticsConnection(
"SOCKET SERVER URL",
"/api/str/",
"MY_ROBOT_ID",
"testtoken"
);
connection(robotController);
```
#### Now, we can work and send command from user to robot!