2019-08-13 19:58:35 +03:00
|
|
|
///
|
2023-01-31 10:43:56 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2019-08-13 19:58:35 +03:00
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import { Injectable } from '@angular/core';
|
2020-02-04 15:14:17 +02:00
|
|
|
import { defaultHttpOptionsFromConfig, RequestConfig } from './http-utils';
|
|
|
|
|
import { Observable, ReplaySubject } from 'rxjs';
|
2019-08-13 19:58:35 +03:00
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
|
import { PageLink } from '@shared/models/page/page-link';
|
|
|
|
|
import { PageData } from '@shared/models/page/page-data';
|
2020-02-21 19:04:49 +02:00
|
|
|
import {
|
2020-04-27 09:27:14 +03:00
|
|
|
ClaimRequest,
|
|
|
|
|
ClaimResult,
|
2020-02-21 19:04:49 +02:00
|
|
|
Device,
|
|
|
|
|
DeviceCredentials,
|
2023-07-21 17:53:21 +03:00
|
|
|
DeviceInfo,
|
|
|
|
|
DeviceInfoQuery,
|
2023-09-11 18:45:37 +03:00
|
|
|
DeviceSearchQuery,
|
2023-07-21 17:53:21 +03:00
|
|
|
PublishTelemetryCommand
|
2023-11-28 11:30:46 +02:00
|
|
|
} from '@shared/models/device.models';
|
|
|
|
|
import { EntitySubtype } from '@shared/models/entity-type.models';
|
2020-02-04 15:14:17 +02:00
|
|
|
import { AuthService } from '@core/auth/auth.service';
|
2023-11-28 11:30:46 +02:00
|
|
|
import { BulkImportRequest, BulkImportResult } from '@shared/import-export/import-export.models';
|
2021-11-26 17:00:51 +02:00
|
|
|
import { PersistentRpc, RpcStatus } from '@shared/models/rpc.models';
|
2023-12-18 15:15:09 +02:00
|
|
|
import { ResourcesService } from '@core/services/resources.service';
|
2019-08-13 19:58:35 +03:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class DeviceService {
|
|
|
|
|
|
|
|
|
|
constructor(
|
2023-12-18 15:15:09 +02:00
|
|
|
private http: HttpClient,
|
|
|
|
|
private resourcesService: ResourcesService
|
2019-08-13 19:58:35 +03:00
|
|
|
) { }
|
|
|
|
|
|
2023-04-28 16:30:41 +03:00
|
|
|
public getDeviceInfosByQuery(deviceInfoQuery: DeviceInfoQuery, config?: RequestConfig): Observable<PageData<DeviceInfo>> {
|
|
|
|
|
return this.http.get<PageData<DeviceInfo>>(`/api${deviceInfoQuery.toQuery()}`,
|
|
|
|
|
defaultHttpOptionsFromConfig(config));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public getTenantDeviceInfos(pageLink: PageLink, type: string = '',
|
|
|
|
|
config?: RequestConfig): Observable<PageData<DeviceInfo>> {
|
2019-08-13 19:58:35 +03:00
|
|
|
return this.http.get<PageData<DeviceInfo>>(`/api/tenant/deviceInfos${pageLink.toQuery()}&type=${type}`,
|
2019-11-15 12:22:14 +02:00
|
|
|
defaultHttpOptionsFromConfig(config));
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-07 14:45:41 +03:00
|
|
|
public getTenantDeviceInfosByDeviceProfileId(pageLink: PageLink, deviceProfileId: string = '',
|
|
|
|
|
config?: RequestConfig): Observable<PageData<DeviceInfo>> {
|
|
|
|
|
return this.http.get<PageData<DeviceInfo>>(`/api/tenant/deviceInfos${pageLink.toQuery()}&deviceProfileId=${deviceProfileId}`,
|
|
|
|
|
defaultHttpOptionsFromConfig(config));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public getCustomerDeviceInfos(customerId: string, pageLink: PageLink, type: string = '',
|
|
|
|
|
config?: RequestConfig): Observable<PageData<DeviceInfo>> {
|
2019-08-13 19:58:35 +03:00
|
|
|
return this.http.get<PageData<DeviceInfo>>(`/api/customer/${customerId}/deviceInfos${pageLink.toQuery()}&type=${type}`,
|
2019-11-15 12:22:14 +02:00
|
|
|
defaultHttpOptionsFromConfig(config));
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
2020-09-07 14:45:41 +03:00
|
|
|
public getCustomerDeviceInfosByDeviceProfileId(customerId: string, pageLink: PageLink, deviceProfileId: string = '',
|
|
|
|
|
config?: RequestConfig): Observable<PageData<DeviceInfo>> {
|
|
|
|
|
return this.http.get<PageData<DeviceInfo>>(`/api/customer/${customerId}/deviceInfos${pageLink.toQuery()}&deviceProfileId=${deviceProfileId}`,
|
|
|
|
|
defaultHttpOptionsFromConfig(config));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public getDevice(deviceId: string, config?: RequestConfig): Observable<Device> {
|
|
|
|
|
return this.http.get<Device>(`/api/device/${deviceId}`, defaultHttpOptionsFromConfig(config));
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public getDevices(deviceIds: Array<string>, config?: RequestConfig): Observable<Array<Device>> {
|
|
|
|
|
return this.http.get<Array<Device>>(`/api/devices?deviceIds=${deviceIds.join(',')}`, defaultHttpOptionsFromConfig(config));
|
2019-08-15 20:39:56 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public getDeviceInfo(deviceId: string, config?: RequestConfig): Observable<DeviceInfo> {
|
|
|
|
|
return this.http.get<DeviceInfo>(`/api/device/info/${deviceId}`, defaultHttpOptionsFromConfig(config));
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public saveDevice(device: Device, config?: RequestConfig): Observable<Device> {
|
|
|
|
|
return this.http.post<Device>('/api/device', device, defaultHttpOptionsFromConfig(config));
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
2023-07-04 18:02:11 +03:00
|
|
|
public saveDeviceWithCredentials(device: Device, credentials: DeviceCredentials, config?: RequestConfig): Observable<Device> {
|
|
|
|
|
return this.http.post<Device>('/api/device-with-credentials', {
|
|
|
|
|
device,
|
|
|
|
|
credentials
|
|
|
|
|
}, defaultHttpOptionsFromConfig(config));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public deleteDevice(deviceId: string, config?: RequestConfig) {
|
|
|
|
|
return this.http.delete(`/api/device/${deviceId}`, defaultHttpOptionsFromConfig(config));
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public getDeviceTypes(config?: RequestConfig): Observable<Array<EntitySubtype>> {
|
|
|
|
|
return this.http.get<Array<EntitySubtype>>('/api/device/types', defaultHttpOptionsFromConfig(config));
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public getDeviceCredentials(deviceId: string, sync: boolean = false, config?: RequestConfig): Observable<DeviceCredentials> {
|
2019-08-14 19:55:24 +03:00
|
|
|
const url = `/api/device/${deviceId}/credentials`;
|
|
|
|
|
if (sync) {
|
|
|
|
|
const responseSubject = new ReplaySubject<DeviceCredentials>();
|
|
|
|
|
const request = new XMLHttpRequest();
|
|
|
|
|
request.open('GET', url, false);
|
|
|
|
|
request.setRequestHeader('Accept', 'application/json, text/plain, */*');
|
|
|
|
|
const jwtToken = AuthService.getJwtToken();
|
|
|
|
|
if (jwtToken) {
|
|
|
|
|
request.setRequestHeader('X-Authorization', 'Bearer ' + jwtToken);
|
|
|
|
|
}
|
|
|
|
|
request.send(null);
|
|
|
|
|
if (request.status === 200) {
|
|
|
|
|
const credentials = JSON.parse(request.responseText) as DeviceCredentials;
|
|
|
|
|
responseSubject.next(credentials);
|
|
|
|
|
} else {
|
|
|
|
|
responseSubject.error(null);
|
|
|
|
|
}
|
|
|
|
|
return responseSubject.asObservable();
|
|
|
|
|
} else {
|
2019-11-15 12:22:14 +02:00
|
|
|
return this.http.get<DeviceCredentials>(url, defaultHttpOptionsFromConfig(config));
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public saveDeviceCredentials(deviceCredentials: DeviceCredentials, config?: RequestConfig): Observable<DeviceCredentials> {
|
|
|
|
|
return this.http.post<DeviceCredentials>('/api/device/credentials', deviceCredentials, defaultHttpOptionsFromConfig(config));
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public makeDevicePublic(deviceId: string, config?: RequestConfig): Observable<Device> {
|
|
|
|
|
return this.http.post<Device>(`/api/customer/public/device/${deviceId}`, null, defaultHttpOptionsFromConfig(config));
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 14:48:14 +03:00
|
|
|
public assignDeviceToCustomer(customerId: string, deviceId: string,
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<Device> {
|
|
|
|
|
return this.http.post<Device>(`/api/customer/${customerId}/device/${deviceId}`, null, defaultHttpOptionsFromConfig(config));
|
2019-08-15 14:48:14 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public unassignDeviceFromCustomer(deviceId: string, config?: RequestConfig) {
|
|
|
|
|
return this.http.delete(`/api/customer/device/${deviceId}`, defaultHttpOptionsFromConfig(config));
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
2020-01-22 20:05:30 +02:00
|
|
|
public sendOneWayRpcCommand(deviceId: string, requestBody: any, config?: RequestConfig): Observable<any> {
|
2021-07-27 13:17:24 +03:00
|
|
|
return this.http.post<Device>(`/api/rpc/oneway/${deviceId}`, requestBody, defaultHttpOptionsFromConfig(config));
|
2020-01-22 20:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sendTwoWayRpcCommand(deviceId: string, requestBody: any, config?: RequestConfig): Observable<any> {
|
2021-07-27 13:17:24 +03:00
|
|
|
return this.http.post<Device>(`/api/rpc/twoway/${deviceId}`, requestBody, defaultHttpOptionsFromConfig(config));
|
2020-01-22 20:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-06 18:19:42 +03:00
|
|
|
public getPersistedRpc(rpcId: string, fullResponse = false, config?: RequestConfig): Observable<PersistentRpc> {
|
|
|
|
|
return this.http.get<PersistentRpc>(`/api/rpc/persistent/${rpcId}`, defaultHttpOptionsFromConfig(config));
|
2020-01-22 20:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-26 17:00:51 +02:00
|
|
|
public deletePersistedRpc(rpcId: string, config?: RequestConfig) {
|
|
|
|
|
return this.http.delete<PersistentRpc>(`/api/rpc/persistent/${rpcId}`, defaultHttpOptionsFromConfig(config));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getPersistedRpcRequests(deviceId: string, pageLink: PageLink,
|
2021-12-22 18:12:17 +02:00
|
|
|
rpcStatus?: RpcStatus, config?: RequestConfig): Observable<PageData<PersistentRpc>> {
|
|
|
|
|
let url = `/api/rpc/persistent/device/${deviceId}${pageLink.toQuery()}`;
|
|
|
|
|
if (rpcStatus && rpcStatus.length) {
|
|
|
|
|
url += `&rpcStatus=${rpcStatus}`;
|
|
|
|
|
}
|
|
|
|
|
return this.http.get<PageData<PersistentRpc>>(url, defaultHttpOptionsFromConfig(config));
|
2021-11-26 17:00:51 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-10 13:00:29 +03:00
|
|
|
public findByQuery(query: DeviceSearchQuery,
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<Array<Device>> {
|
|
|
|
|
return this.http.post<Array<Device>>('/api/devices', query, defaultHttpOptionsFromConfig(config));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public findByName(deviceName: string, config?: RequestConfig): Observable<Device> {
|
|
|
|
|
return this.http.get<Device>(`/api/tenant/devices?deviceName=${deviceName}`, defaultHttpOptionsFromConfig(config));
|
2019-10-10 13:00:29 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-21 19:04:49 +02:00
|
|
|
public claimDevice(deviceName: string, claimRequest: ClaimRequest,
|
|
|
|
|
config?: RequestConfig): Observable<ClaimResult> {
|
2020-02-28 14:11:52 +02:00
|
|
|
return this.http.post<ClaimResult>(`/api/customer/device/${deviceName}/claim`, claimRequest, defaultHttpOptionsFromConfig(config));
|
2020-02-21 19:04:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public unclaimDevice(deviceName: string, config?: RequestConfig) {
|
|
|
|
|
return this.http.delete(`/api/customer/device/${deviceName}/claim`, defaultHttpOptionsFromConfig(config));
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-04 07:18:54 +02:00
|
|
|
public assignDeviceToEdge(edgeId: string, deviceId: string,
|
|
|
|
|
config?: RequestConfig): Observable<Device> {
|
|
|
|
|
return this.http.post<Device>(`/api/edge/${edgeId}/device/${deviceId}`,
|
|
|
|
|
defaultHttpOptionsFromConfig(config));
|
2020-06-21 20:38:43 +03:00
|
|
|
}
|
|
|
|
|
|
2020-11-04 07:18:54 +02:00
|
|
|
public unassignDeviceFromEdge(edgeId: string, deviceId: string,
|
|
|
|
|
config?: RequestConfig) {
|
2020-06-22 12:37:36 +03:00
|
|
|
return this.http.delete(`/api/edge/${edgeId}/device/${deviceId}`,
|
|
|
|
|
defaultHttpOptionsFromConfig(config));
|
2020-06-21 20:38:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getEdgeDevices(edgeId: string, pageLink: PageLink, type: string = '',
|
|
|
|
|
config?: RequestConfig): Observable<PageData<DeviceInfo>> {
|
|
|
|
|
return this.http.get<PageData<DeviceInfo>>(`/api/edge/${edgeId}/devices${pageLink.toQuery()}&type=${type}`,
|
2021-08-04 17:46:45 +03:00
|
|
|
defaultHttpOptionsFromConfig(config));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bulkImportDevices(entitiesData: BulkImportRequest, config?: RequestConfig): Observable<BulkImportResult> {
|
|
|
|
|
return this.http.post<BulkImportResult>('/api/device/bulk_import', entitiesData, defaultHttpOptionsFromConfig(config));
|
2020-06-21 20:38:43 +03:00
|
|
|
}
|
|
|
|
|
|
2023-07-21 17:53:21 +03:00
|
|
|
public getDevicePublishTelemetryCommands(deviceId: string, config?: RequestConfig): Observable<PublishTelemetryCommand> {
|
|
|
|
|
return this.http.get<PublishTelemetryCommand>(`/api/device-connectivity/${deviceId}`, defaultHttpOptionsFromConfig(config));
|
2023-07-14 15:48:10 +03:00
|
|
|
}
|
|
|
|
|
|
2023-12-18 15:15:09 +02:00
|
|
|
public downloadGatewayDockerComposeFile(deviceId: string): Observable<any> {
|
|
|
|
|
return this.resourcesService.downloadResource(`/api/device-connectivity/gateway-launch/${deviceId}/docker-compose/download`);
|
2023-08-18 12:39:48 +03:00
|
|
|
}
|
2019-08-13 19:58:35 +03:00
|
|
|
}
|