npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gr-device-init

v1.1.4

Published

provide methods and models to connect to server

Downloads

20

Readme

GR Device Init

version 1.1.2

Server is deployed on https://grobotics-socket-server.herokuapp.com - you can use this to create connections for user and robot.

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:

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(
  "https://grobotics-socket-server.herokuapp.com/",
  "MY_ROBOT_ID",
  "testtoken"
);

connection(robotController);

Now, we can work and send command from user to robot!