pan rel="diff-6b553a966426569e57c3c863768ed7eacbc8d39cL22">22 17
 import com.gprinter.service.GpPrintService;
23 18
 
24
-import org.apache.commons.lang.ArrayUtils;
25
-
26 19
 import java.util.ArrayList;
27 20
 import java.util.List;
28 21
 import java.util.Set;
29
-import java.util.Vector;
30 22
 
31 23
 import ai.pai.lensman.App;
32 24
 import ai.pai.lensman.R;
@@ -36,44 +28,42 @@ import ai.pai.lensman.db.Preferences;
36 28
 public class PrinterSettingPresenter implements PrinterSettingContract.Presenter {
37 29
 
38 30
     private Context context;
31
+    private PrinterService printerService;
39 32
     private PrinterSettingContract.View view;
33
+    private PrinterServiceConnection connection;
40 34
     private  BluetoothAdapter bluetoothAdapter;
41 35
 
42
-    private GpService mGpService;
43
-    private PrinterServiceConnection conn = null;
44
-
45 36
     private static final String TAG = "PrinterSettingPresenter";
46 37
 
47
-    class PrinterServiceConnection implements ServiceConnection {
38
+    private class PrinterServiceConnection implements ServiceConnection{
39
+
48 40
         @Override
49
-        public void onServiceDisconnected(ComponentName name) {
50
-            Log.i(TAG, "onServiceDisconnected() called");
51
-            mGpService = null;
41
+        public void onServiceConnected(ComponentName name, IBinder service) {
42
+            printerService = ((PrinterService.PrintServiceBinder)service).getService();
52 43
         }
53 44
 
54 45
         @Override
55
-        public void onServiceConnected(ComponentName name, IBinder service) {
56
-            Log.i(TAG, "onServiceConnected() called");
57
-            mGpService = GpService.Stub.asInterface(service);
46
+        public void onServiceDisconnected(ComponentName name) {
47
+            printerService = null;
58 48
         }
59 49
     }
60 50
 
61
-
62 51
     public PrinterSettingPresenter(Context context, PrinterSettingContract.View view){
63 52
         this.view = view;
64 53
         this.context = context;
54
+        connection = new PrinterServiceConnection();
65 55
         bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
66 56
     }
67 57
 
68 58
     @Override
69 59
     public void start() {
70 60
         registerPrinterBroadcast();
71
-        startAndBindPrintService();
61
+        context.startService(new Intent(context,PrinterService.class));
62
+        context.bindService(new Intent(context,PrinterService.class),connection,Context.BIND_AUTO_CREATE);
72 63
         if(queryBluetoothStatus()){
73 64
             view.onBluetoothEnabled();
74 65
             view.onPairedDeviceDiscovered(queryPairedDevices());
75 66
             discoverNewDevices();
76
-
77 67
         }else{
78 68
             view.onBluetoothDisabled();
79 69
             view.showToast(context.getString(R.string.bt_is_disabled));
@@ -83,85 +73,39 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter
83 73
     @Override
84 74
     public void stop() {
85 75
         cancelDiscovery();
86
-        if (conn != null) {
87
-            App.getAppContext().unbindService(conn);
88
-        }
89 76
         context.unregisterReceiver(PrinterStatusBroadcastReceiver);
77
+        if(connection!=null){
78
+            App.getAppContext().unbindService(connection);
79
+        }
90 80
     }
91 81
 
92 82
     @Override
93 83
     public void queryPrinterStatus() {
94
-        try {
95
-            int status = mGpService.queryPrinterStatus(0, 10000);
96
-            String str = new String();
97
-            if (status == GpCom.STATE_NO_ERR) {
98
-                str = "打印机正常";
99
-            } else {
100
-                str = "打印机 ";
101
-                if ((byte) (status & GpCom.STATE_OFFLINE) > 0) {
102
-                    str += "脱机";
103
-                }
104
-                if ((byte) (status & GpCom.STATE_PAPER_ERR) > 0) {
105
-                    str += "缺纸";
106
-                }
107
-                if ((byte) (status & GpCom.STATE_COVER_OPEN) > 0) {
108
-                    str += "打印机开盖";
109
-                }
110
-                if ((byte) (status & GpCom.STATE_ERR_OCCURS) > 0) {
111
-                    str += "打印机出错";
112
-                }
113
-                if ((byte) (status & GpCom.STATE_TIMES_OUT) > 0) {
114
-                    str += "查询超时";
115
-                }
116
-            }
117
-            view.onPrinterStatusFetched(str);
118
-
119
-        } catch (Exception e1) {
84
+        if(printerService==null){
120 85
             view.showToast(context.getString(R.string.printer_status_query_fail));
86
+            return;
121 87
         }
88
+        view.onPrinterStatusFetched(printerService.queryPrinterStatus());
122 89
     }
123 90
 
124 91
 
125 92
     @Override
126 93
     public void printQR(String qrCodeStr) {
94
+        if(printerService==null){
95
+            view.showToast(context.getString(R.string.printer_status_query_fail));
96
+            return;
97
+        }
127 98
         if(TextUtils.isEmpty(Preferences.getInstance().getPrinterMac())){
128 99
             view.showToast(App.getAppContext().getString(R.string.not_set_printer_yet));
129 100
             return;
130 101
         }
131
-        EscCommand esc = new EscCommand();
132
-		/*打印文字*/
133
-        esc.addSelectPrintModes(EscCommand.FONT.FONTA, EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF,
134
-                EscCommand.ENABLE.OFF, EscCommand.ENABLE.OFF);//取消倍高倍宽
135
-        esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);//设置打印左对齐
136
-        esc.addText("拍爱\n");   //  打印文字
137
-        esc.addPrintAndLineFeed();
138
-		/*QRCode命令打印
139
-			此命令只在支持QRCode命令打印的机型才能使用。
140
-			在不支持二维码指令打印的机型上,则需要发送二维条码图片
141
-		*/
142
-        esc.addSelectErrorCorrectionLevelForQRCode((byte) 0x31); //设置纠错等级
143
-        esc.addSelectSizeOfModuleForQRCode((byte)8);//设置qrcode模块大小
144
-        esc.addStoreQRCodeData(qrCodeStr);//设置qrcode内容
145
-        esc.addSelectJustification(EscCommand.JUSTIFICATION.CENTER);//设置打印中心对齐
146
-        esc.addPrintQRCode();//打印QRCode
147
-        esc.addPrintAndFeedLines((byte) 1);
148
-//        esc.addSelectJustification(EscCommand.JUSTIFICATION.LEFT);//设置打印左对齐
149
-//        esc.addText("拍摄日期 "+new SimpleDateFormat("yyyy/MM/dd HH:mm").format(new Date()));
150
-//        esc.addPrintAndFeedLines((byte) 2);
151
-
152
-        Vector<Byte> datas = esc.getCommand(); //发送数据
153
-        Byte[] Bytes = datas.toArray(new Byte[datas.size()]);
154
-        byte[] bytes = ArrayUtils.toPrimitive(Bytes);
155
-        String str = Base64.encodeToString(bytes, Base64.DEFAULT);
156
-        int rel;
157
-        try {
158
-            rel = mGpService.sendEscCommand(0, str);
159
-            GpCom.ERROR_CODE r = GpCom.ERROR_CODE.values()[rel];
160
-            if (r != GpCom.ERROR_CODE.SUCCESS) {
161
-                view.showToast( GpCom.getErrorText(r));
162
-            }
163
-        } catch (Exception e) {
164
-            view.showToast(App.getAppContext().getString(R.string.go_check_printer));
102
+        int code = printerService.printQR(qrCodeStr);
103
+        if(code == PrinterService.ERROR_CODE_PRINTER_SERVICE_EXCEPTION){
104
+            view.showToast(context.getString(R.string.go_check_printer));
105
+        }else if(code == PrinterService.ERROR_CODE_PRINTER_SERVICE_OFF){
106
+            view.showToast(context.getString(R.string.go_check_printer));
107
+        }else if( GpCom.ERROR_CODE.values()[code]!= GpCom.ERROR_CODE.SUCCESS){
108
+            view.showToast( GpCom.getErrorText(GpCom.ERROR_CODE.values()[code]));
165 109
         }
166 110
     }
167 111
 
@@ -197,17 +141,14 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter
197 141
 
198 142
     @Override
199 143
     public void connectPrinter(BluetoothDevice device) {
200
-        if(mGpService==null){
144
+        if(printerService==null){
201 145
             view.showToast(context.getString(R.string.printer_service_boot_fail));
202 146
             return;
203 147
         }
204
-        try {
205
-            int code = mGpService.openPort(0, PortParameters.BLUETOOTH, device.getAddress(), 0);
206
-            if(code==0){
207
-                Preferences.getInstance().setPrinterMac(device.getAddress());
208
-            }
209
-            LogHelper.d(TAG,"open port return code ="+code);
210
-        } catch (Exception e) {
148
+        int code = printerService.connectPrinter(device);
149
+        if(code==0){
150
+            Preferences.getInstance().setPrinterMac(device.getAddress());
151
+        }else{
211 152
             view.showToast(context.getString(R.string.printer_port_open_fail));
212 153
         }
213 154
 
@@ -223,12 +164,6 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter
223 164
         }
224 165
     }
225 166
 
226
-    private void startAndBindPrintService() {
227
-        Intent intent = new Intent(App.getAppContext(), GpPrintService.class);
228
-        App.getAppContext().startService(intent);
229
-        conn = new PrinterServiceConnection();
230
-        App.getAppContext().bindService(intent, conn, Context.BIND_AUTO_CREATE);
231
-    }
232 167
 
233 168
     // changes the title when discovery is finished
234 169
     private final BroadcastReceiver mFindBlueToothReceiver = new BroadcastReceiver() {
@@ -264,12 +199,11 @@ public class PrinterSettingPresenter implements PrinterSettingContract.Presenter
264 199
         public void onReceive(Context context, Intent intent) {
265 200
             if (GpCom.ACTION_CONNECT_STATUS.equals(intent.getAction())) {
266 201
                 int type = intent.getIntExtra(GpPrintService.CONNECT_STATUS, 0);
267
-                int id = intent.getIntExtra(GpPrintService.PRINTER_ID, 0);
268 202
                 Log.d(TAG, "connect status " + type);
269 203
                 if (type == GpDevice.STATE_CONNECTING) {
270 204
                     view.onPrinterStatusFetched(context.getString(R.string.connecting));
271 205
                 } else if (type == GpDevice.STATE_NONE) {
272
-
206
+                    view.onPrinterStatusFetched(context.getString(R.string.connecting));
273 207
                 } else if (type == GpDevice.STATE_VALID_PRINTER) {
274 208
                     view.onPrinterStatusFetched(context.getString(R.string.printer_is_connected));
275 209
                 } else if (type == GpDevice.STATE_INVALID_PRINTER) {

kodo - Gogs: Go Git Service

Нет описания

Brightcells: 4547c709bf Add api get_server_time_api лет %!s(int64=9): %!d(string=назад)
..
__init__.py 4547c709bf Add api get_server_time_api лет %!s(int64=9): %!d(string=назад)