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

trackle-library-nodejs

v1.2.0

Published

## Table of contents * [installation](#installation) * [methods](#implemented-methods) * [Get a Device ID and a private key](#get-a-device-id-and-a-private-key) * [example](#example) * [Attention](#Attention) ## Installation `npm install trackle-library

Downloads

85

Readme

NodeJS Trackle Wrapper

Table of contents

Installation

npm install trackle-library-nodejs

Get a Device ID and a private key

  • Create an account on Trackle Cloud (https://trackle.cloud/)
  • Open "My Devices" section from the drawer
  • Click the button "Claim a device"
  • Select the link "I don't have a device id", then Continue
  • The Device Id will be shown on the screen and the private key file will be download with name <device_id>.der where <device_id> is Device ID taken from Trackle.

Implemented methods

To see the implemented methods you can check on this link example or import trackle from the installed library and check it there, with the library you also will import

  • Log_Level
  • Function_PermissionDef
  • Data_TypeDef
  • Ota_Method
  • Connection_Type
  • Event_Type
  • Event_Flags this are all enum declareted types that will help you with the values that function expects

Example

import fs from "fs";

import trackle, {
  Log_Level,
  Function_PermissionDef,
  Data_TypeDef,
  Connection_Type,
  Event_Type,
  Event_Flags
} from "trackle-library-nodejs";

// Define callbacks

const getMillis = () => {
  const date = new Date();
  const ms: number = date.getTime();
  return ms;
};

const setLog = (msg: string, level: number, category: string): void => {
  if (category.length === 0) {
    console.log(`[${level}] [] ${msg}`);
  } else {
    console.log(`[${level}] [${category}] ${msg}`);
  }
};

// Post callbacks declaration (creating the functions that will be called from user in the online platform)

const post1 = (name: string): number => {
  console.log("called post1 : " + name);
  console.log("Post1 - Pippo");
  return 1;
}

const post2 = (name: string): number => {
  if (name) {
    console.log("Post2 - Pluto");
    return 0;
  }

  return -1;
};

const postSuccess = (name: string): number => {
  console.log(`Richiamata callback postSuccess, arg: ${name}`);
  return 1;
}

const postFailure = (name: string): number => {
  console.log(`Richiamata callback postFailure, arg: ${name}`);
  return -1;
};

// Get callback declaration (creating the functions that will be called from user in the online platform)

const getString = (name: string): string => {
  console.log("Called getString : " + name);
  if (name) {
    console.log("getString - ");
    return "Hello";
  }

  return "World";
};

const getChar = (name: string): string => {
  console.log("Called getChar : " + name);
  if (name) {
    console.log("getChar - ");
    return "C";
  }

  return "a";
};

const getInt = (name: string): number => {
  console.log("Called getInt : " + name);
  if (name) {
    console.log("getInt - ");
    return 5678;
  }

  return 1234;
};

const getBoolean = (name: string): boolean => {
  console.log("Called getBoolean : " + name);
  if (name) {
    console.log("getBoolean - ");
    return true;
  }

  return false;
};

const getDouble = (name: string): number => {
  console.log("Called getDouble : " + name);
  if (name) {
    console.log("getDouble - ");
    return 1.4563333;
  }

  return 0.4567812;
};

const getJson = (name: string): string => {
  console.log("Called getJson : " + name);
  if (name) {
    console.log("getJson - ");
    return JSON.stringify({ name: "pippo", surname: "pluto" });
  }

  return JSON.stringify({ name: "Hello", surname: "World" });
};

const systemTimeCallback = (
  time: number,
  param: number,
  data: Buffer | undefined
): void => {
  console.log(time);

};

const completedPublishCallback = (
  error: number,
  callbackData?: number
): void => {
  console.log("msg key: " + callbackData)
  console.log("error: " + error)

};

const UpdateStateCallback = (
  function_key: string,
  arg: string
): number => {
  console.log("key value: " + function_key)
  console.log("arg: " + arg)
  return -1;

};

const ConnectionStatusCallback = (
  status: number,
) => {
  if (firstConnect && status === 2) {
    trackle.publish(
      "First",
      "Hello",
      30,
      Event_Type.PUBLIC,
      Event_Flags.WITH_ACK,
      1
    );
    firstConnect = false;
  }
};

const systemRebootCallback = (data: string) => {
  console.log("Reboot request ignored!");

};

let prevPubMillis = 0;
let msg_key = 1;
let firstConnect = true;
const PRIVATE_KEY = fs.readFileSync("Path_To_Your_.der_Key_file");

trackle.setMillis(getMillis);
trackle.setLogLevel(Log_Level.TRACKLE_INFO);
trackle.setLogCallback(setLog);

// Set cloud credentials
trackle.setDeviceId("Your_Device_ID");
trackle.setKeys(PRIVATE_KEY);

trackle.setFirmwareVersion(1);

trackle.setConnectionType(Connection_Type.CONNECTION_TYPE_WIFI);

// set system Callbacks

trackle.setSystemTimeCallback(systemTimeCallback);
trackle.setSystemRebootCallback(systemRebootCallback);
trackle.setCompletedPublishCallback(completedPublishCallback);
trackle.setUpdateStateCallback(UpdateStateCallback);

// Registering POST functions callable from cloud

trackle.post(
  "post1",
  post1,
  Function_PermissionDef.ALL_USERS
);
trackle.post(
  "post2",
  post2,
  Function_PermissionDef.ALL_USERS
);
trackle.post(
  "postSuccess",
  postSuccess,
  Function_PermissionDef.ALL_USERS
);
trackle.post(
  "postFailure",
  postFailure,
  Function_PermissionDef.ALL_USERS
);


// Registering GET functions callable from cloud

trackle.get("getInt", getInt, Data_TypeDef.VAR_INT);
trackle.get("getString", getString, Data_TypeDef.VAR_STRING);
trackle.get("getDouble", getDouble, Data_TypeDef.VAR_DOUBLE);
trackle.get("getChar", getChar, Data_TypeDef.VAR_CHAR);
trackle.get("getJson", getJson, Data_TypeDef.VAR_JSON);
trackle.get("getBool", getBoolean, Data_TypeDef.VAR_BOOLEAN);

trackle.setConnectionStatusCallback(ConnectionStatusCallback)

// Connection to trackle
trackle.connect();

console.log("Startup completed. Running...");
console.log("Starting Loop...");

setInterval(async () => {
  trackle.loop();

  // Every 5 seconds make a publish

  if (getMillis() - prevPubMillis > 5000) {

    trackle.publish(
      "greetings",
      "Hello, World!",
      30,
      Event_Type.PUBLIC,
      Event_Flags.WITH_ACK,
      msg_key
    );
    msg_key++;
    prevPubMillis = new Date().getTime();
  }
}, 10);

Attention

The POST and GET methods are implemented to be synchronous, so if you want to implement a JavaScript function with them, you should NOT return a Promise.