1/*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import CommonUtils from '../utils/CommonUtils';
17import uri from '@ohos.uri';
18import { Log } from '@ohos/common';
19import CheckEmptyUtils from '@ohos/common';
20import { IPP_PORT, IPP_PATH, SCHEME_IPP, SCHEME_IPPS, EPSON_PRINTER, BISHENG_PRINTER } from '@ohos/common';
21
22const TAG = 'DiscoveredPrinter';
23
24export default class DiscoveredPrinter {
25
26  /** Device name */
27  private deviceName: string;
28
29  /** Device mac address */
30  private deviceAddress: string;
31
32  /** Device status */
33  private deviceStatus: number;
34
35  /** Device path */
36  private path: string;
37
38  /** Device location */
39  private location: string;
40
41  /** Device connect uri */
42  private uri: uri.URI;
43
44  constructor(deviceName: string, path: string, deviceStatus: number, deviceAddress: string) {
45    this.deviceName = deviceName;
46    this.deviceStatus = deviceStatus;
47    this.path = <string>path;
48    this.deviceAddress = deviceAddress;
49  }
50
51  getId(): string {
52    return this.path;
53  }
54
55  public getDeviceName(): string {
56    return this.deviceName;
57  }
58
59  public setDeviceName(deviceName: string): void {
60    this.deviceName = deviceName;
61  }
62
63  public getPath(): string {
64    return this.path;
65  }
66
67  public setPath(path: string): void {
68    this.path = path;
69  }
70
71  public getDeviceAddress(): string {
72    return this.deviceAddress;
73  }
74
75  public setDeviceAddress(deviceAddress: string): void {
76    this.deviceAddress = deviceAddress;
77  }
78
79  public getDeviceStatus(): number {
80    return this.deviceStatus;
81  }
82
83  public setDeviceStatus(deviceStatus: number): void {
84    this.deviceStatus = deviceStatus;
85  }
86
87  public getUri(): uri.URI {
88    return this.uri;
89  }
90
91  public setUri(ip: string, port ?: number, path?: string): void {
92    let portIt = port === undefined ? IPP_PORT : port;
93    let pathIt = path === undefined ? IPP_PATH : path;
94    Log.debug(TAG, 'portIt 2: ' + portIt);
95    Log.debug(TAG, 'pathIt 2: ' + pathIt);
96    this.uri = new uri.URI(`${SCHEME_IPP}://${ip}:${portIt}/${pathIt}`);
97  }
98
99  public isBiSheng(): boolean {
100    if (CheckEmptyUtils.checkStrIsEmpty(this.deviceName)) {
101      return false;
102    }
103    return this.deviceName.includes(BISHENG_PRINTER);
104  }
105
106  public toString(): string {
107    let printerName = CommonUtils.getSecurityPrinterName(this.deviceName);
108    if (CheckEmptyUtils.isEmpty<uri.URI>(this.uri)) {
109      return `deviceName: ${printerName}, uri: undefined`;
110    }
111    let uri = CommonUtils.getSecurityIp(this.uri.toString());
112    return `deviceName: ${printerName}, uri: ${uri}`;
113  }
114}