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 { Log } from '@ohos/common'; 17import { Constants, AppCommonEvent, AppStorageKeyName } from '@ohos/common'; 18import AppStorageHelper from '../Common/Adapter/AppStorageHelper'; 19import { PrinterCapability, PrinterInfo } from '@ohos/common'; 20import emitter from '@ohos.events.emitter'; 21import { StringUtil } from '@ohos/common'; 22import Util from '../Common/Utils/Util'; 23 24const TAG = '[DeviceDiscModel]:'; 25 26/** 27 * PrinterDiscModel 28 */ 29 30/** 31 * Printer discovery model 32 */ 33export class PrinterDiscModel { 34 public mPrinters: Array<PrinterInfo> = new Array<PrinterInfo>(); 35 36 /** 37 * reset 38 */ 39 public reset(): void { 40 this.mPrinters = []; 41 AppStorageHelper.setValue<Array<PrinterInfo>>(this.mPrinters, AppStorageKeyName.PRINTER_QUEUE_NAME); 42 } 43 44 /** 45 * remove device 46 * 47 * @param deviceId device id 48 */ 49 public removePrinter(printerId: string): void { 50 this.mPrinters = this.mPrinters.filter((printer) => printer.printerId !== printerId); 51 Log.info(TAG, 'remove printer, printerId = ' + StringUtil.splitMac(printerId) + ', printer num = ' + JSON.stringify(this.mPrinters.length)); 52 AppStorageHelper.setValue<Array<PrinterInfo>>(this.mPrinters, AppStorageKeyName.PRINTER_QUEUE_NAME); 53 } 54 55 public findPrinter(printerId: string): boolean { 56 Log.info(TAG, 'find printer'); 57 let printer = this.mPrinters.find((printer)=> printer.printerId === printerId); 58 Log.debug(TAG, 'find printer, printerId = ' + StringUtil.splitMac(printerId) + ', printer = ' + JSON.stringify(printer)); 59 if (printer !== undefined && printer !== null) { 60 return true; 61 } else { 62 return false; 63 } 64 } 65 66 /** 67 * add device 68 * 69 * @param device Device 70 * @return true for added 71 */ 72 public addPrinter(printer: PrinterInfo): boolean { 73 for (let index = 0; index < this.mPrinters.length; index++) { 74 if (this.mPrinters[index].printerId === printer.printerId) { 75 Log.info(TAG, 'printer is already added, printer = ' + StringUtil.encodeCommonString(printer.printerName)); 76 return false; 77 } 78 } 79 if (Util.isMdnsPrinter(<string> printer.printerId)) { //eprint打印机置顶 80 this.mPrinters.unshift(printer); 81 } else { 82 this.mPrinters.push(printer); 83 } 84 let printerJSON = JSON.stringify(printer); 85 let innerEvent = { 86 eventId: AppCommonEvent.ADD_PRINTER_EVENT, 87 priority: emitter.EventPriority.HIGH 88 }; 89 let eventData = { 90 data: { 91 'printer': printerJSON 92 } 93 }; 94 emitter.emit(innerEvent, eventData); 95 Log.info(TAG, 'add printer, printer :' + StringUtil.encodeCommonString(printer.printerName) + ', printer num = ' + JSON.stringify(this.mPrinters.length)); 96 AppStorageHelper.setValue<Array<PrinterInfo>>(this.mPrinters, AppStorageKeyName.PRINTER_QUEUE_NAME); 97 return true; 98 } 99 100 /** 101 * printer state change 102 * 103 * @param printerId printer id 104 * @param state printer state 105 * @return true for change 106 */ 107 public printerStateChange(printerId: string, state: number): boolean { 108 Log.error(TAG, 'PrinterStateChange printerId: ' + StringUtil.splitMac(printerId) + ' state : ' + JSON.stringify(state)); 109 for (let index = 0; index < this.mPrinters.length; index++) { 110 if (this.mPrinters[index].printerId === printerId) { 111 Log.info(TAG, 'printer: ' + StringUtil.splitMac(printerId) + ' state : ' + JSON.stringify(this.mPrinters[index].printerState) + ' change to :' + JSON.stringify(state)); 112 this.mPrinters[index].printerState = state; 113 AppStorageHelper.setValue<Array<PrinterInfo>>(this.mPrinters, AppStorageKeyName.PRINTER_QUEUE_NAME); 114 115 let innerEvent = { 116 eventId: AppCommonEvent.PRINTER_STATE_CHANGE_EVENT, 117 priority: emitter.EventPriority.HIGH 118 }; 119 emitter.emit(innerEvent); 120 } 121 } 122 return true; 123 } 124 125 /** 126 * printer capability update 127 * 128 * @param printerId printer id 129 * @param capability printer capability 130 * @param options printer capability 131 * @param description printer descriptions 132 * @return true for update 133 */ 134 public printerUpdateCapability(printerId: string, capability: PrinterCapability, options: string, description: string): boolean { 135 Log.info(TAG, 'PrinterUpdateCapability printerId: ' + StringUtil.splitMac(printerId) + ' capability : ' + JSON.stringify(capability)); 136 for (let index = 0; index < this.mPrinters.length; index++) { 137 if (this.mPrinters[index].printerId === printerId) { 138 this.mPrinters[index].capability = capability; 139 this.mPrinters[index].options = options; 140 this.mPrinters[index].description = description; 141 AppStorageHelper.setValue<Array<PrinterInfo>>(this.mPrinters, AppStorageKeyName.PRINTER_QUEUE_NAME); 142 let innerEvent = { 143 eventId: AppCommonEvent.PRINTER_UPDATE_CAPABILITY_EVENT, 144 priority: emitter.EventPriority.HIGH 145 }; 146 emitter.emit(innerEvent); 147 } 148 } 149 return true; 150 } 151} 152