@iotize/device-client.js
Version:
IoTize Device client for Javascript
254 lines (253 loc) • 9.21 kB
TypeScript
import '../client/impl/frame/stream-generated';
import '../core/extensions';
import { BehaviorSubject, Observable } from 'rxjs';
import { IoTizeClient, ResponseInterface } from '../client/api';
import { Response } from '../client/api/response';
import { TapConfigurator } from '../client/api/tap-configurator';
import { TapInterface } from '../client/api/tap-device';
import { ComProtocol } from '../protocol/api';
import { SinglePacket as SinglePacketModel } from './all-models';
import { ApiConfig, Routes } from './config/api-config';
import { EncryptionKeys, ScramInterceptor, ScramInterceptorOptions } from './interceptors/scram-interceptor';
import { IoTizeDeviceService } from './iotize-device-services';
import { InterfaceLock, MultiRequest, SinglePacketPart } from './model';
import { ScramAuth } from './scram';
import { ServiceCallType } from './service';
import { VariableManager } from './target-variable';
import { BundleManager } from './target-variable/bundle-manager';
import { SinglePacketProgress } from './types';
import { ConverterProvider } from './util/converter-provider';
export { EncryptionKeys };
export declare type ServiceClass = {
new (client: IoTizeClient, converterProvider: ConverterProvider): any;
};
export interface Lwm2mCall {
commandType: 'GET' | 'PUT' | 'POST';
path: string;
responseType?: string;
params?: {
[key: string]: string;
};
}
export interface SessionState {
groupId: number;
startTime?: Date;
lifeTime: number;
name: string;
}
export interface EncryptionSessionState {
enabled?: boolean;
keys?: EncryptionKeys;
frameCounter?: number;
}
export interface SecurityOptions {
disableFactoryResetByResource: boolean;
disableLoginWithUID: boolean;
disableFactoryResetByHardware: boolean;
scramActivated: boolean;
hashPassword: boolean;
}
export interface DisconnectOptions {
clearSession?: boolean;
clearCache?: boolean;
}
export declare const NFC_PAIRING_COMMAND: Uint8Array;
export declare type Tap = IoTizeDevice;
export declare namespace Tap {
function create(): Tap;
function fromProtocol(protocol: ComProtocol): Tap;
}
/**
*
* @deprecated Use Tap instead
*/
export declare class IoTizeDevice implements TapInterface {
static TAG: string;
static INITIAL_SESSION_STATE: SessionState;
/**
* Cache log info
*/
protected _interfaceLockOptions?: InterfaceLock;
protected _variableManager?: VariableManager;
protected _bundleManager?: BundleManager;
protected _services: IoTizeDeviceService;
protected _client: IoTizeClient;
protected _sessionState$: BehaviorSubject<SessionState>;
protected _apiConfig: ApiConfig;
protected _lwm2m: Lwm2mInterface;
protected _scramAuth?: ScramAuth;
protected _scramInterceptor: ScramInterceptor;
/**
* Get cached lock options
* May no be synchronized with real device state
*/
lockOptions: InterfaceLock;
readonly currentSessionState: SessionState;
/**
* Setter for the session key
* @param key if null, it will stop encryption and remove session key. If true it will update session key used for encryption
*/
/**
* Get current session key for encrypted communicatioin
*/
sessionKey: Uint8Array | undefined;
client: IoTizeClient;
/**
* Listen to session state changed
*/
readonly sessionState: Observable<SessionState>;
constructor(client: IoTizeClient);
/**
* @deprecated use {@link Tap.create()} instead
*
*/
static create(): IoTizeDevice;
/**
* @deprecated
* @param protocol
*/
static fromProtocol(protocol: ComProtocol): IoTizeDevice;
/**
* Enable/Disable encrytion when communicating with a device
* @param enable true if requests must be encrypted
* @param resetSessionKey true if you want to reset the the session key.
* @param refreshInitializationVector true if you want to change initialization vectors
*
*/
encryption(enable: boolean, resetSessionKey?: boolean, refreshInitializationVector?: boolean): Promise<void>;
setEncryptionKeys(keys: EncryptionKeys): void;
setEncryptedFrameCounter(frameCounter: number): void;
getEncryptionOptions(): ScramInterceptorOptions;
refreshEncryptionInitializationVector(): Promise<void>;
setInitializationVectorRefreshPeriod(period: number): void;
/**
* Enable/Disable encrytion when communicating with a device
* @param enable true if requests must be encrypted
* @param resetSessionKey true if you want to reset the the session key.
* @param refreshInitializationVector true if you want to change initialization vectors
*
* @deprecated use {@link encryption()} instead
*/
enableEncryption(enable: boolean, resetSessionKey?: boolean, refreshInitializationVector?: boolean): Promise<void>;
readonly protocol: ComProtocol;
/**
* Connect with the given communication protocol
* TODO what if there is already a connected communication protocol
* @param protocol
*/
connect(protocol?: ComProtocol): Promise<void>;
/**
* Disconnect device
*/
disconnect(options?: DisconnectOptions): Promise<void>;
isConnected(): boolean;
readonly variables: VariableManager;
readonly bundles: BundleManager;
/**
* Setter for hashPassword option
* @deprecated this should not be used anymore as this configuration must be read from the tap
*/
/**
* Return true if password will be hashed
* @deprecated
*/
/**
* Tap logout
* Reject if logout failed
*/
logout(): Promise<void>;
/**
* Change password for the groupId (If groupId is not specified, it will be changed for the current group id)
* @param newPassword the new password
* @param userIdOrName default to the current group id
* @param userPassword the current user password. if provided it will perform a login before changing password
*/
changePassword(newPassword: string, userId?: number, userPassword?: string): Promise<void>;
/**
* Upload single packets to single packet store (one by one)
* Unsubsribe from observable to cancel this operation
*
* @warning this is an experimental function. It may be modified in future releases
* @experimental
*
* TODO use dependency injection instead
*/
loadSinglePacketInStore(packets: SinglePacketPart[]): Observable<SinglePacketProgress>;
/**
* Send a single packet to the tap and execute
*
* @warning this is an experimental function. It may be modified in future releases
* @experimental
*
* TODO maybe move to extension ?
*/
sendSinglePacket(packetOrModel: SinglePacketModel | Uint8Array): Observable<SinglePacketProgress>;
/**
* Configure current tap instance thanks to a TapConfigurator
* @param configurator
*/
configure<DataType>(configurator: TapConfigurator<DataType>): Promise<DataType>;
/**
*
* @param protocol
*/
useComProtocol(protocol: ComProtocol | string): this;
/**
* Tap login
*
*/
login(username: string, password: string, refreshSessionState?: boolean): Promise<boolean>;
private _isLoggedIn;
/**
*
* @param commands run mutiple requests in one
* @param expectedCodeRet
*/
multiRequests(commands: ServiceCallType[], expectedCodeRet?: MultiRequest.ExpectedResultCode | number): Promise<ResponseInterface<any>[]>;
/**
* Send an NFC pairing request
*/
nfcPairing(): Promise<Response<Uint8Array>>;
readonly scramAuth: ScramAuth;
readonly rooter: Routes;
readonly apiConfig: ApiConfig;
readonly service: IoTizeDeviceService;
/**
* @experimental
*/
readonly lwm2m: Lwm2mInterface;
/**
* Initialize device cache
*/
protected _initCache(): void;
/**
* Clear cache
*/
clearCache(): void;
/**
* Clear session encryption keys
*/
clearSession(): void;
/**
* Read session state from the device
* This will perform a few tap requests
*
* TODO we should invalidate encryption key if session state changed
* TODO we should use multi commands to improve performance
*/
refreshSessionState(): Promise<SessionState>;
/**
* Get lock info
*/
protected getLockInfo(): Promise<InterfaceLock>;
getSecurityOptions(): Promise<SecurityOptions>;
}
export declare class Lwm2mInterface {
client: IoTizeClient;
apiConfig: ApiConfig;
constructor(client: IoTizeClient, apiConfig: ApiConfig);
get(path: string, body?: any): Promise<ResponseInterface<any>>;
put(path: string, body: any): Promise<ResponseInterface<any>>;
post(path: string, body?: any): Promise<ResponseInterface<any>>;
call(call: ServiceCallType): Promise<Response<any>>;
}