Skip to content

Commit

Permalink
cleanup example, update + cleanup examples, spelling receive
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles-Schleich committed Sep 3, 2024
1 parent e1a391b commit ed9a126
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 211 deletions.
2 changes: 1 addition & 1 deletion examples/ros2_teleop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"bytebuffer": "^5.0.1",
"jscdr": "^0.0.3",
"tslog": "^4.9.3",
"zenoh": "file:../../esm/"
"@ZettaScaleLabs/zenoh-ts": "file:../../"
}
}
47 changes: 2 additions & 45 deletions examples/ros2_teleop/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CDRReader, CDRWriter } from "jscdr";
// import { ByteBuffer } from "bytebuffer";
import { Session, Config, Sample } from "zenoh";
import { Session, Config, Sample } from "@ZettaScaleLabs/zenoh-ts";


import { BatteryState, LaserScan, Log, Twist, Vector3 } from "./ros2_types";
import ByteBuffer from "bytebuffer";
Expand Down Expand Up @@ -120,22 +121,6 @@ const battery_subscriber = async function (sample: Sample): Promise<void> {
console.log("battery Topic:", key_expr);
await session.declare_subscriber(key_expr, battery_subscriber);
console.log("After Subscriber:");
//////////////////////////////////////////////////////////////////
// Camera subscription (as motion-JPEG via WebService plugin) //
//////////////////////////////////////////////////////////////////

// If your robot has a camera and zcapture installed (from zenoh-demos/computer-vision/zcam/):
// the zcapture must be started with "-k <scope>/camera", and the zenoh router must have the WebServer plugin running
// if (document.getElementById("camera_img") != null) {
// // update Camera label
// let elem = document.getElementById("camera_label");
// elem.innerHTML = "Camera ( " + sub_scope + "camera )";

// // Set "camera_img" element's src to the same URL host, but with port 8080 (WebServer plugin)
// // and with path: "<scope>/camera?_method=SUB"
// img_url = remote_api.replace(":8000", ":8080") + sub_scope + "camera?_method=SUB";
// document.getElementById("camera_img").src = img_url;
// }

///////////////////////////////
// Lidar subscription //
Expand Down Expand Up @@ -270,31 +255,3 @@ const logs_callback = async function (sample: Sample): Promise<void> {
};
console.log("Lidar Sub:", key_expr);
session.declare_subscriber(key_expr, logs_callback);

//////////////////////////////////
// Logs subscription (MQTT) //
//////////////////////////////////
// var mqtt_logs_source = null;

// // Test if Server-Source Event is supported
// if (typeof (EventSource) !== "undefined") {
// // the key expression to subscribe (no scope used by MQTT bridge as only 1)
// var key_expr = TOPIC_MQTT;

// console.log("Subscribe to EventSource: " + remote_api + key_expr);
// ros2_logs_source = new EventSource(remote_api + key_expr);
// ros2_logs_source.addEventListener("PUT", function (e) {
// console.log("Received sample: " + e.data);
// // The zenoh REST API sends JSON objects
// // that includes "key", "value", "encoding" and "time" (same than a result to GET)
// let sample = JSON.parse(e.data)
// // Add it to "rosout_logs" HTML element
// let elem = document.getElementById("rosout_logs");
// elem.innerHTML += "MQTT: on " + sample.key + " : " + JSON.stringify(sample.value) + "<br>";
// // Auto-scroll to the bottom
// elem.scrollTop = elem.scrollHeight;
// }, false);

// } else {
// document.getElementById("rosout_logs").innerHTML = "Sorry, your browser does not support server-sent events...";
// }
17 changes: 14 additions & 3 deletions examples/ros2_teleop/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@
resolved "https://registry.npmjs.org/@types/uuid/-/uuid-10.0.0.tgz"
integrity sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==

"@ZettaScaleLabs/zenoh-ts@file:../../":
version "0.0.2"
resolved "file:../.."
dependencies:
channel-ts "^0.1.2"
cpx "^1.5.0"
fp-ts "^2.16.5"
object-inspect "^1.13.1"
request "^2.79.0"
text-encoding "^0.7.0"
ts-log "^2.2.5"
tslog "^4.9.3"
uuid "^10.0.0"

bytebuffer@^5.0.1, [email protected]:
version "5.0.1"
resolved "https://registry.npmjs.org/bytebuffer/-/bytebuffer-5.0.1.tgz"
Expand Down Expand Up @@ -175,6 +189,3 @@ vite@^5.2.12:
rollup "^4.13.0"
optionalDependencies:
fsevents "~2.3.3"

"zenoh@file:../../esm/":
resolved "file:../../esm"
111 changes: 15 additions & 96 deletions examples/zenoh-ts/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,104 +1,24 @@
import "./style.css";
import "./webpage.ts";
import {main_thr} from "./z_sub_thr.ts";
import {main_get} from "./z_get.ts";
import { main_ping } from "./z_ping.ts";
import { main_pong } from "./z_pong.ts";
import { main_sub } from "./z_sub.ts";
import { main_pub } from "./z_pub.ts";
import { main_queryable } from "./z_queryable.ts";

import {
Session,
Config,
Query,
Sample,
KeyExpr,
Publisher,
Subscriber,
Receiver,
RecvErr,
Queryable,
} from "@ZettaScaleLabs/zenoh-ts";;

async function queryable_callback(query: Query) {
console.log(" Query Receieved in Callback", query);
query.reply(query.key_expr(), [65, 66, 67, 50]);
console.log(" ");
}

async function main() {
main_thr();

// const subscriber_callback = async function (sample: Sample): Promise<void> {
// console.log(" cb demo 1 : Key_expr ", sample.keyexpr());
// console.log(" cb demo 1 : Value ", sample.payload());
// };

// const session = await Session.open(Config.new("ws/127.0.0.1:10000"));
// KeyExpr
// let key_exp = KeyExpr.new("demo/put");

// Session put / del / get
// await session.put("demo/put", [65, 66, 67, 49]);
// await session.put(key_exp, [65, 66, 67, 50]);
// await session.delete("demo/delete");

// console.log("Issue Get");
// let receiver: Receiver = await session.get("test/queryable/**?p1=s1;p2=s2");
// let stop = false;
// while (!stop) {
// let reply = await receiver.receive();

// if (reply == RecvErr.Disconnected) {
// console.log("All Replies Receved");
// stop = true;
// } else if (reply == RecvErr.MalformedReply) {
// console.log("MalformedReply");
// } else {
// console.log("Reply Value ", reply.result());
// };
// }

// // subscribers
// let callback_subscriber: Subscriber = await session.declare_subscriber("demo/pub", subscriber_callback);
// await sleep(1000 * 3);
// callback_subscriber.undeclare()
// console.log("undeclare callback_subscriber");
// main_thr();
// main_ping();
// main_pong();
// main_sub();
// main_pub();
// main_queryable();
main_get();

// let poll_subscriber: Subscriber = await session.declare_subscriber("demo/pub");
// let value = await poll_subscriber.receive();
// console.log("poll_subscriber", value);
// console.log(await poll_subscriber.receive());
// poll_subscriber.undeclare()

// // // publisher
// let publisher: Publisher = await session.declare_publisher("demo/pub/1");
// await publisher.put("This is typescript string");
// await publisher.put(new String("This is typescript String ()"));
// await publisher.put([65, 66, 67, 49]);
// With encoding and attachment
// await publisher.put(
// [65, 66, 67, 49],
// Encoding.APPLICATION_JSON(),
// [12, 234, 5],
// );
// await publisher.undeclare();

// queryable
// console.log("declare queryable");
// let queryable: Queryable = await session.declare_queryable(
// "demo/test/queryable",
// true,
// );
// let query = await queryable.receive();
// if (query instanceof Query) {
// console.log(query.selector())
// console.log(query.selector().parameters())
// query.reply("demo/test/queryable", "Demo Test 1234")
// // query.reply_err("Demo Test 1234")
// // query.reply_del("test/queryable");
// }
// console.log("declare queryableend ");

// Declare a Queryable with a Callback, this will continue to run until the queryable falls out of scope
// let queryable_with_callback: Queryable = await session.declare_queryable("demo/test/queryable", true, queryable_callback);

// Loop to spin and keep alive
var count = 0;
while (true) {
var seconds = 100;
Expand All @@ -115,10 +35,9 @@ main()
throw e;
});

function executeAsync(func: any) {
setTimeout(func, 0);
}

function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}


9 changes: 1 addition & 8 deletions examples/zenoh-ts/src/z_delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ import "./webpage.ts";

import { Config, Session } from "@ZettaScaleLabs/zenoh-ts";

async function main() {
export async function main_delete() {
const session = await Session.open(Config.new("ws/127.0.0.1:10000"));
await session.delete("demo/delete");
await session.close();
}

main()
.then(() => console.log("Done"))
.catch((e) => {
console.log(e);
throw e;
});
20 changes: 11 additions & 9 deletions examples/zenoh-ts/src/z_get.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { Result } from "../../../dist/session";
import "./style.css";
import "./webpage.ts";

import { Config, Receiver, RecvErr, Session } from "@ZettaScaleLabs/zenoh-ts";

async function main() {
export async function main_get() {
const session = await Session.open(Config.new("ws/127.0.0.1:10000"));
console.log("Issue Get");
let receiver: Receiver = await session.get("test/queryable/**");
let res_receiver: Result<Receiver, String> = await session.get("test/queryable/**");

let receiver: Receiver;
if (res_receiver instanceof Receiver) {
receiver = res_receiver;
} else {
console.log(res_receiver)
return;
}

let stop = false;

Expand All @@ -23,10 +32,3 @@ async function main() {
}
}
}

main()
.then(() => console.log("Done"))
.catch((e) => {
console.log(e);
throw e;
});
8 changes: 1 addition & 7 deletions examples/zenoh-ts/src/z_ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./webpage.ts";

import { Encoding, CongestionControl, Config, Session } from "@ZettaScaleLabs/zenoh-ts";

export async function main() {
export async function main_ping() {
const session = await Session.open(Config.new("ws/127.0.0.1:10000"));

let sub = await session.declare_subscriber("test/pong");
Expand Down Expand Up @@ -64,9 +64,3 @@ function elapsed_ms(startTime: Date) {
return timeDiff;
}

main()
.then(() => console.log("Done"))
.catch((e) => {
console.log(e);
throw e;
});
8 changes: 1 addition & 7 deletions examples/zenoh-ts/src/z_pong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./webpage.ts";

import { Encoding, CongestionControl, Sample, Config, Session } from "@ZettaScaleLabs/zenoh-ts";

export async function main() {
export async function main_pong() {
const session = await Session.open(Config.new("ws/127.0.0.1:10000"));

let pub = await session.declare_publisher(
Expand All @@ -30,9 +30,3 @@ function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

main()
.then(() => console.log("Done"))
.catch((e) => {
console.log(e);
throw e;
});
9 changes: 1 addition & 8 deletions examples/zenoh-ts/src/z_pub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./webpage.ts";

import { Encoding, CongestionControl, Config, KeyExpr, Publisher, Session } from "@ZettaScaleLabs/zenoh-ts";

export async function main() {
export async function main_pub() {
const session = await Session.open(Config.new("ws/127.0.0.1:10000"));

let key_expr = KeyExpr.new("demo/ping");
Expand All @@ -28,10 +28,3 @@ export async function main() {
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

main()
.then(() => console.log("Done"))
.catch((e) => {
console.log(e);
throw e;
});
9 changes: 1 addition & 8 deletions examples/zenoh-ts/src/z_queryable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Config, KeyExpr, Query, Queryable, Session, ZBytes } from "@ZettaScaleL

type Option<T> = T | null;

export async function main() {
export async function main_queryable() {
const session = await Session.open(Config.new("ws/127.0.0.1:10000"));

let key_expr = KeyExpr.new("demo/test/queryable");
Expand Down Expand Up @@ -33,10 +33,3 @@ export async function main() {
query = await queryable.receive();
}
}

main()
.then(() => console.log("Done"))
.catch((e) => {
console.log(e);
throw e;
});
9 changes: 1 addition & 8 deletions examples/zenoh-ts/src/z_sub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./webpage.ts";

import { Sample, Config, Subscriber, Session } from "@ZettaScaleLabs/zenoh-ts";

export async function main() {
export async function main_sub() {
const session = await Session.open(Config.new("ws/127.0.0.1:10000"));

const subscriber_callback = async function (sample: Sample): Promise<void> {
Expand Down Expand Up @@ -34,10 +34,3 @@ export async function main() {
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

main()
.then(() => console.log("Done"))
.catch((e) => {
console.log(e);
throw e;
});
Loading

0 comments on commit ed9a126

Please sign in to comment.