Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 | 1x 1x 1x 1x 1x 1x 1x 42x 6x 4x 21x 21x 1x 21x 4x 21x 21x 21x 37x 21x 27x 21x 21x 21x 21x 21x 21x 21x 21x 21x 2x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 6x 6x 6x 6x 5x 5x 5x 5x 1x 1x 5x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 16x 16x 16x 16x 30x 30x 30x 16x 2x 2x 2x 2x 16x 1x 1x 1x 1x 1x 1x 30x 30x 30x 30x 30x 29x 29x 29x 29x 29x 29x 4x 30x 30x 29x 16x 16x 16x 3x 3x 3x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1x 1x 16x 16x 16x 16x 16x 3x 16x 16x 16x 16x 5x 5x 1x 8x 8x 8x 8x 7x 6x 6x 6x 6x 6x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 24x 16x 16x 16x 24x 5x 5x 5x 24x 24x 3x 3x 2x 1x 1x 1x 19x 15x 4x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 6x 6x 6x | import { Injectable } from '@angular/core';
import { Subject, Subscription } from 'rxjs';
import { take } from 'rxjs/operators';
import { AppConstants } from '@unpispas/upp-defs';
import { GenericUtils } from '@unpispas/upp-defs';
import { httpService } from './http';
import { adhocService } from './adhoc';
/**
* Defines the possible WebSocket connection types.
*/
export type WsConnType = 'WSR' | 'WSL';
/*****************************/
/* ENABLE VERBOSE LOGGING */
/*****************************/
/**
* @internal
*/
class CustomLogger {
private _verbose = false;
addinfo(message: string){
Iif (this._verbose){
console.info(message);
}
}
addwarn(message: string){
Iif (this._verbose){
console.warn(message);
}
}
}
/*****************************/
/* SERVICE ABSTRACT CLASS */
/*****************************/
/**
* @internal
*/
abstract class _Service extends CustomLogger {
protected host: string | null = null;
protected port: number | null = null;
abstract Connect(host: string | null, port: number | null, name: string | null) : Promise <WSConnection | null>;
}
/*****************************/
/* CONNECTION ABSTRACT CLASS */
/*****************************/
/**
* Abstract base class for managing WebSocket connections.
* This class provides a framework for establishing and managing WebSocket connections,
* with built-in support for event handling and logging capabilities.
*/
export abstract class WSConnection extends CustomLogger {
protected _service: _Service | null = null;
/**
* @description Gets or sets the associated service for this WebSocket connection.
* @type {_Service | null}
*/
get service() : _Service | null {
return this._service;
}
set service(value: _Service | null){
this._service = value;;
}
protected _version: string | null = null;
/**
* @description Gets or sets the WebSocket connection version.
* @type {string | null}
*/
get version(): string | null {
return this._version;
}
set version(value: string | null){
this._version = value;
}
protected _svcname: string | null = null;
/**
* @description Gets or sets the service name for this WebSocket connection.
* @type {string | null}
*/
get svcname(): string | null {
return this._svcname;
}
set svcname(value: string | null){
this._svcname = value;
}
private _contype: WsConnType | null = null;
/**
* @description Gets the WebSocket connection type.
* This property is read-only and indicates whether the connection is local or remote.
* @type {WsConnType | null}
*/
get contype(): WsConnType | null {
return this._contype;
}
protected _onDisconnected = new Subject<void> ();
/** Emits when the WebSocket connection is disconnected. */
public OnDisconnected = this._onDisconnected.asObservable();
protected _onMessageSent = new Subject<any> ();
/** Emits when a message is sent via the WebSocket connection. */
public OnMessageSent = this._onMessageSent.asObservable();
protected _onMessageRecv = new Subject<any> ();
/** Emits when a message is received via the WebSocket connection. */
public OnMessageRecv = this._onMessageRecv.asObservable();
/**
* Constructs a new `WSConnection` instance.
* Initializes the connection with the specified type and service name.
* @param type The WebSocket connection type (e.g., 'WSR' for remote or 'WSL' for local).
* @param name The name of the service associated with this WebSocket connection.
*/
constructor(type: WsConnType, name: string){
super();
this._svcname = name;
this._contype = type;
}
/**
* Disconnects the WebSocket connection and emits the disconnect event.
*/
Disconnect(){
this._onDisconnected.next();
}
/**
* Sends a message through the WebSocket connection.
* @param module The target module.
* @param message The message to send.
* @returns A promise resolving to any response from the server.
*/
abstract Send(module: string, message: any): Promise <any>;
}
/*****************************/
/* INPUT ABSTRACT CLASS */
/*****************************/
/**
* Abstract base class for WebSocket input handling.
*
* `WSInput` provides a structure for reading data from a WebSocket connection. It includes
* mechanisms for subscribing to WebSocket messages, managing connection states, and notifying
* when data is received or lost.
*
* This class is meant to be extended by other classes that will define specific read and end
* read behaviors through the `doIniRead` and `doEndRead` methods.
*/
export abstract class WSInput {
private _connection: WSConnection | null = null;
private _target: string | null = null;
private _inputid: string | null = null;
private _read_subscription: Subscription | null = null;
private _conn_subscription: Subscription | null = null;
protected _onInputRead = new Subject<string> ();
/** Observable that emits data read from the WebSocket input. */
public OnInputRead = this._onInputRead.asObservable();
protected _onInputLost = new Subject<void> ();
/** Observable that emits when the WebSocket input is lost or disconnected. */
public OnInputLost = this._onInputLost.asObservable();
/**
* Creates an instance of `WSInput` and initializes the WebSocket connection.
* @param connection The WebSocket connection to be used.
* @param target The target module or service for the WebSocket connection.
*/
constructor(connection: WSConnection, target: string){
Iif (connection.contype != 'WSL'){
console.error("[" + connection.contype + "] cannot read from remote websocket connection")
}
else {
this._connection = connection;
this._conn_subscription = connection.OnDisconnected.subscribe(
() => {
this._onInputLost.next();
});
this._target = target;
}
}
/**
* Starts reading data from the WebSocket input.
* To be implemented by subclasses with specific read initialization logic.
* @param args The arguments required to initialize the read operation.
* @returns A promise that resolves when the read operation is initiated.
*/
abstract doIniRead(args: any): Promise <any>;
/**
* Ends the read operation on the WebSocket input.
* To be implemented by subclasses with specific read termination logic.
* @returns A promise that resolves when the read operation is completed.
*/
abstract doEndRead(): Promise <any>;
/**
* Cleans up the WebSocket input, terminating read operations and unsubscribing from events.
* @returns A promise that resolves when the cleanup is completed.
*/
async OnDestroy(){
await this.doEndRead();
Iif (this._read_subscription){
this._read_subscription.unsubscribe();
this._read_subscription = null;
}
Iif (this._conn_subscription){
this._conn_subscription.unsubscribe();
this._conn_subscription = null;
}
Iif (this._connection){
this._connection.Disconnect();
this._connection = null;
}
}
/***************************/
/* PROTECTED METHODS */
/***************************/
protected async _doIniRead(_action: any): Promise <any> {
Iif (!this._connection || !this._target){
return null; // not connected
}
const _response = await this._connection.Send(this._target, _action);
if (_response && _response.exec == 'SUCCESS'){
this._inputid = _response.data;
console.info("[" + this._connection.contype + "] starting read on target: '" + this._target + "' with session (" + this._inputid + ")");
Iif (this._read_subscription){
this._read_subscription.unsubscribe();
this._read_subscription = null;
}
this._read_subscription = this._connection.OnMessageRecv.subscribe(
data => {
if (data['uuid'] == this._inputid){
this._onInputRead.next(data['data']);
}
});
return _response;
}
else {
console.error("[" + this._connection.contype + "] read request to target '" + this._target + "' failed! (" + (_response ? _response.data : 'no response') + ")")
return null;
}
}
protected async _doEndRead(_action: any): Promise <any> {
if (this._read_subscription){
this._read_subscription.unsubscribe();
this._read_subscription = null;
}
Iif (!this._connection || !this._target){
return null; // not connected
}
const _response = await this._connection.Send(this._target, _action);
if (_response && _response.exec == 'SUCCESS'){
console.info("[" + this._connection.contype + "] stop request to target: '" + this._target + "'");
return _response;
}
else {
console.error("[" + this._connection.contype + "] stop request to target '" + this._target + "' failed! (" + (_response ? _response.data : 'no response') + ")")
return null;
}
}
}
/**
* @internal
*/
class InputScale extends WSInput {
private _args: any = null;
constructor(connection: WSConnection, private device: any){
super(connection, 'SCALE');
}
async doIniRead(args: any): Promise <any> {
this._args = args;
Iif (!this.device){
return null; // no device provided
}
this._args['command'] = 'START';
return this._doIniRead(this._args);
}
async doEndRead(): Promise <any> {
Iif (!this.device){
return null; // no device provided
}
this._args['command'] = 'STOP';
return this._doEndRead(this._args);
}
}
/*****************************/
/* LOCAL WS CONNECTION */
/*****************************/
/*
The connection is performed from the browser to the application (websocket server) running in localhost
The connection is capable od sending and receiving messages to and from this server
*/
/**
* @internal
*/
class WSConnectionL extends WSConnection {
private ws: WebSocket | null = null;
constructor(name: string, _ws: WebSocket){
super('WSL', name); // this is temporal: will be overwritten by the ws server
this.ws = _ws;
_ws.onmessage = (event) => {
const _message_json = JSON.parse(event.data)
if (_message_json){
this._onMessageRecv.next({
name: _message_json['SERVICE_NAME'], // service identifier configured in the ws
vers: _message_json['SERVICE_VERS'], // service version configured in the ws
uuid: _message_json['MESSAGE_UUID'], // message identifier (in case of response)
exec: _message_json['MESSAGE_EXEC'], // execution result
data: _message_json['MESSAGE_DATA'] // message content (result or error message)
});
}
else E{
this._onMessageRecv.next({
exec: 'SUCCESS', // execution result
data: event.data // response is not a JSON object
});
}
};
_ws.onclose = () => {
if (this.ws){ // null if we have closed the session
console.warn("[" + this.contype + "] Websocket connection lost");
this._stopKeepAlive();
this._onDisconnected.next();
}
};
this._startKeepAlive(); // keep-alive ping every 20 seconds
}
override Disconnect(){
if (this.ws){
this.ws.close();
this.ws = null;
this.addinfo("[" + this.contype + "] Websocket connection closed");
}
this._stopKeepAlive();
super.Disconnect();
}
Send(module: string, message: any): Promise <any> {
return new Promise <any> ((resolve) => {
if (this.ws){
const _uuidv4 = GenericUtils.uuidv4();
const _sendmsg = {
UUIDV4: _uuidv4,
TARGET: module,
ACTION: message
};
this.OnMessageRecv.pipe(take(1)).subscribe(
_response => {
if (_response && (_response['uuid'] == _uuidv4)){
Iif (_response.name != this.svcname){
console.warn("[" + this.contype + "] Local server configured for '" + _response.name + "' ('" + this.svcname + "' is expected)")
}
this._version = _response['vers'];
this._svcname = _response['name'];
resolve({
exec: _response['exec'],
data: _response['data']
});
if (_response['exec'] != 'SUCCESS'){
this.addwarn("[" + this.contype + "] " + _response['exec'] + " (" + _response['data'] + ")");
}
}
});
if (this.ws.readyState === WebSocket.OPEN){
this.ws.send(JSON.stringify(_sendmsg));
this._onMessageSent.next(_sendmsg);
}
else E{
resolve(null); // send error
}
}
else E{
resolve(null); // not connected
}
})
}
private _keepalive_interval: any | null = null;
private _startKeepAlive() {
Iif (this._keepalive_interval) {
clearInterval(this._keepalive_interval);
this._keepalive_interval = null;
}
this._keepalive_interval = setInterval(() => {
const _timeout = setTimeout(() => {
console.warn("[" + this.contype + "] ping request not satisfied after 5 seconds");
this.Disconnect();
}, 5000);
this.Send("BASE", "PING").then(
() => {
clearTimeout(_timeout);
})
}, 20000);
}
private _stopKeepAlive() {
if (this._keepalive_interval) {
clearInterval(this._keepalive_interval);
this._keepalive_interval = null;
}
}
};
/**
* @internal
*/
class LocalService extends _Service {
Connect(host: string | null, port: number | null, name: string) : Promise <WSConnection | null> {
this.host = host;
this.port = port;
const _url = "ws://" + this.host + ":" + this.port;
return new Promise <WSConnection | null> ((resolve) => {
Iif (!this.host || !this.port || !name){
console.info("[WSL] Websocket connection error (not provided)");
return resolve(null);
}
let _ws = null;
try {
_ws = new WebSocket(_url);
}
catch(error){
console.error("[WSL] Cannot create websocket connection", error);
return resolve(null); // connection failed
}
_ws.onerror = () => {
console.warn("[WSL] Websocket connection error");
resolve(null); // connection failed (could not open connection)
};
_ws.onopen = () => {
const _connection = new WSConnectionL(name, _ws);
if (_connection){
_connection.service = this;
_connection.OnDisconnected.pipe(take(1)).subscribe(
() => {
resolve(null); // connection closed
});
_connection.Send("BASE", "CHECK").then(
_response => {
if (_response && _response.exec == 'SUCCESS'){
console.info("[WSL] Websocket connection ready");
resolve(_connection); // connected
}
else E{
resolve(null); // connection failed (invalid check)
}
});
}
};
});
}
}
/*****************************/
/* REMOTE WS CONNECTION */
/*****************************/
/*
The connection is performed from the wsr REST server to the wss (websocket server) running hosted
The local websocket server has registered to the remote wss server that will act as gateway
Messages sent to the wsr REST server will be sent to the wss server and then back to the targeted web socket server
Through this method, the connection is capable od sending single messages to the local running server
*/
/**
* @internal
*/
class WSConnectionR extends WSConnection {
constructor(name: string, private http: httpService){
super('WSR', name);
}
override Disconnect(){
super.Disconnect();
}
async Send(module: string, message: any): Promise <any> {
const _url = AppConstants.restURL + "send?service=" + this.svcname;
Iif (!this.http.IsOnline){
return null;
}
try {
const response = await fetch(_url, {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Accept': 'application/json'
},
body: JSON.stringify({ TARGET: module, ACTION: message })
});
if (response.ok) {
const _data = await response.json();
this._version = _data['vers'];
this._svcname = _data['name'];
Iif (_data['exec'] != 'SUCCESS'){
this.addwarn("[" + this.contype + "] " + _data['exec'] + " (" + _data['data'] + ")");
}
return {
exec: _data['exec'],
data: _data['data']
}
}
else {
return null; // error on execution
}
}
catch (error) {
console.error("[WSR] Failed to fetch (" + _url + ")", error);
return null; // exception in fetch
}
};
};
/**
* @internal
*/
class RemoteService extends _Service {
constructor(private http: httpService){
super();
}
Connect(host: string | null, port: number | null, name: string) : Promise <WSConnection | null> {
this.host = host;
this.port = port;
return new Promise <WSConnection | null> ((resolve) => {
const _connection = new WSConnectionR(name, this.http);
if (_connection){
_connection.service = this;
_connection.Send("BASE", "CHECK").then(
_response => {
if (_response && _response.exec == 'SUCCESS'){
this.addinfo("[WSR] Websocket connection ready");
resolve(_connection); // connected
}
else E{
resolve(null); // connection failed (invalid check)
}
});
}
});
}
}
/*****************************/
/* DEVICES SERVICE */
/*****************************/
/**
* Service for managing local and remote WebSocket connections to various devices.
* Provides methods for connecting to WebSocket services, checking availability, and controlling connected devices.
*/
@Injectable({
providedIn: 'root'
})
export class deviceService {
private _localservice: LocalService | null = null;
private get localService(){
if (!this._localservice){
this._localservice = new LocalService()
}
return this._localservice;
}
private _rmoteservice: RemoteService | null = null;
private get rmoteService(){
if (!this._rmoteservice){
this._rmoteservice = new RemoteService(this.http)
}
return this._rmoteservice;
}
/****************************/
/* STARTS HERE */
/****************************/
/**
* Constructs a new instance of the `deviceService` class.
* @param http The HTTP service used for managing HTTP connections.
* @param adhoc The ad hoc service for handling custom HTTP requests.
*/
constructor(private http: httpService, private adhoc: adhocService){
// nothing to do
}
/****************************/
/* NAME AVAILABILITY */
/****************************/
private _freeServiceUrl = 'sockets/available.php';
/**
* Checks if a specific service is available for the current place.
* The place is sent automatically by the adhoc layer from the application state.
* @param service The name of the service to check for availability.
* @returns A promise that resolves to a boolean indicating if the service is available.
*/
Available(service: string): Promise <boolean> {
return new Promise((resolve) => {
this.adhoc.DoRequest(this._freeServiceUrl, { service: service })
.then(data => {
resolve(data as boolean);
}, error => {
if (error.status != 0){
console.error("[REQUEST] Error [" + error.status + "] in http request '" + this._freeServiceUrl + "'");
}
resolve(false);
});
});
}
/****************************/
/* CONNECT CONTROL */
/****************************/
/**
* Establishes a WebSocket connection to either a local or remote service.
* @param host The host address for the connection.
* @param port The port number for the connection.
* @param service The name of the service to connect to.
* @param tolocal Whether to connect to a local or remote service.
* @returns A promise that resolves to the established `WSConnection`, or null if connection fails.
*/
Connect(host: string | null, port: number | null, service: string, tolocal: boolean): Promise <WSConnection | null> {
if (tolocal){
return this.localService.Connect(host, port, service);
}
else {
return this.rmoteService.Connect(host, port, service);
}
}
/**
* Disconnects an existing WebSocket connection.
* @param _connection The WebSocket connection to disconnect.
*/
Disconnect(_connection: WSConnection){
if (_connection){
_connection.Disconnect();
}
this._localservice = null;
this._rmoteservice = null;
}
/****************************/
/* CONNECTION CHECK */
/****************************/
/**
* Tests the connection to a WebSocket service and then disconnects.
* @param host The host address for the connection.
* @param port The port number for the connection.
* @param service The name of the service to connect to.
* @param tolocal Whether to test a local or remote service connection.
* @returns A promise resolving to the established `WSConnection` if successful, or null otherwise.
*/
async Test(host: string | null, port: number | null, service: string, tolocal: boolean) : Promise <WSConnection | null> {
let _success = null;
if (tolocal){
const _connection = await this.localService.Connect(host, port, service);
if (_connection){
this.Disconnect(_connection);
_success = _connection;
}
}
else {
const _connection = await this.rmoteService.Connect(host, port, service);
if (_connection){
this.Disconnect(_connection);
_success = _connection;
}
}
return _success;
}
/****************************/
/* PRINTER ACTIONS */
/****************************/
/**
* Sends an open command to a printing device through the WebSocket connection.
* @param connection The WebSocket connection to use.
* @param device The identifier of the printing device.
* @returns A promise resolving to any response from the server, or logs errors to the console.
*/
async doOpen(connection: WSConnection, device: string){
Iif (!connection || !connection.service){
return;
}
const _message: Record<string, any> = {};
_message[device] = {
print: null,
copies: 0,
open: true
}
const _response = await connection.Send("PRINT", JSON.stringify(_message));
if (_response && _response.exec == 'SUCCESS'){
console.info("[" + connection.contype + "] open request sent to device '" + device + "'")
}
else {
console.error("[" + connection.contype + "] open request to device '" + device + "' failed! (" + (_response ? _response.data : 'no response') + ")")
}
return _response;
}
/**
* Sends a print command with the specified HTML content to a printing device.
* @param connection The WebSocket connection to use.
* @param device The identifier of the printing device.
* @param html The HTML content to print.
* @param copies The number of copies to print. Defaults to 1.
* @returns A promise resolving to any response from the server, or logs errors to the console.
*/
async doPrint(connection: WSConnection, device: string, html: string, copies = 1){
Iif (!connection || !connection.service){
return;
}
const _message: Record<string, any> = {};
_message[device] = {
print: GenericUtils.stringToBase64(html),
copies: copies,
open: false
}
const _response = await connection.Send("PRINT", JSON.stringify(_message));
if (_response && _response.exec == 'SUCCESS'){
console.info("[" + connection.contype + "] print request sent to device '" + device + "'")
}
else {
console.error("[" + connection.contype + "] open request to device '" + device + "' failed! (" + (_response ? _response.data : 'no response') + ")")
}
return _response;
}
/****************************/
/* INPUT DEVICES */
/****************************/
/**
* Establishes a connection and creates a new `InputScale` instance for reading data from an input device.
* @param host The host address for the connection.
* @param port The port number for the connection.
* @param name The name of the service to connect to.
* @param device The DataObject instance with the device parameters
* @returns A promise resolving to an `InputScale` instance, or null if the connection fails.
*/
async WSInputScale(host: string, port: number, name: string, device: any): Promise <WSInput | null> {
const _connection: WSConnection | null = await this.Connect(host, port, name, true);
if (_connection){
return new InputScale(_connection, device);
}
return null; // not connected
}
}
|