chore: update version to 1.4.3, enhance printing service, and add MyTcpSocket module
This commit is contained in:
		@@ -93,7 +93,7 @@ android {
 | 
			
		||||
        minSdkVersion rootProject.ext.minSdkVersion
 | 
			
		||||
        targetSdkVersion rootProject.ext.targetSdkVersion
 | 
			
		||||
        versionCode 1
 | 
			
		||||
        versionName "1.4.2"
 | 
			
		||||
        versionName "1.4.3"
 | 
			
		||||
    }
 | 
			
		||||
    signingConfigs {
 | 
			
		||||
        debug {
 | 
			
		||||
 
 | 
			
		||||
@@ -25,6 +25,7 @@ class MainApplication : Application(), ReactApplication {
 | 
			
		||||
                val packages = PackageList(this).packages
 | 
			
		||||
                packages.add(PdfToBitmapPackage())
 | 
			
		||||
                packages.add(TcpSocketPackage())
 | 
			
		||||
                packages.add(MyTcpSocketPackage())
 | 
			
		||||
                return packages
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,42 @@
 | 
			
		||||
package com.anonymous.Assemblr;
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import com.facebook.react.bridge.ReactApplicationContext;
 | 
			
		||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
 | 
			
		||||
import com.facebook.react.bridge.ReactMethod;
 | 
			
		||||
import com.facebook.react.bridge.Promise;
 | 
			
		||||
import com.facebook.react.bridge.ReadableArray;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.OutputStream;
 | 
			
		||||
import java.net.Socket;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public class MyTcpSocketModule extends ReactContextBaseJavaModule {
 | 
			
		||||
    public MyTcpSocketModule(ReactApplicationContext reactContext) {
 | 
			
		||||
        super(reactContext);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getName() {
 | 
			
		||||
        return "MyTcpSocket";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @ReactMethod
 | 
			
		||||
    public void writeToSocket(String ip, int port, ReadableArray data, Promise promise) {
 | 
			
		||||
        try (Socket socket = new Socket(ip, port)) {
 | 
			
		||||
            OutputStream output = socket.getOutputStream();
 | 
			
		||||
            for (int i = 0; i < data.size(); i++) {
 | 
			
		||||
                String item = data.getString(i);
 | 
			
		||||
                assert item != null;
 | 
			
		||||
                output.write(item.getBytes());
 | 
			
		||||
            }
 | 
			
		||||
            output.flush();
 | 
			
		||||
            promise.resolve("ok");
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            promise.reject("ERROR", e.getMessage());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,27 @@
 | 
			
		||||
package com.anonymous.Assemblr;
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import com.facebook.react.ReactPackage;
 | 
			
		||||
import com.facebook.react.bridge.NativeModule;
 | 
			
		||||
import com.facebook.react.bridge.ReactApplicationContext;
 | 
			
		||||
import com.facebook.react.uimanager.ViewManager;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public class MyTcpSocketPackage implements ReactPackage {
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
 | 
			
		||||
        List<NativeModule> modules = new ArrayList<>();
 | 
			
		||||
        modules.add(new MyTcpSocketModule(reactContext));
 | 
			
		||||
        return modules;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
 | 
			
		||||
        return Collections.emptyList();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2
									
								
								app.json
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								app.json
									
									
									
									
									
								
							@@ -12,7 +12,7 @@
 | 
			
		||||
    ],
 | 
			
		||||
    "name": "Assemblr",
 | 
			
		||||
    "slug": "Assemblr",
 | 
			
		||||
    "version": "1.4.2",
 | 
			
		||||
    "version": "1.4.3",
 | 
			
		||||
    "orientation": "portrait",
 | 
			
		||||
    "icon": "./src/assets/icon.png",
 | 
			
		||||
    "userInterfaceStyle": "light",
 | 
			
		||||
 
 | 
			
		||||
@@ -101,6 +101,7 @@ const styles = StyleSheet.create({
 | 
			
		||||
        borderBottomLeftRadius: responsiveWidth(1),
 | 
			
		||||
        paddingLeft: responsiveHeight(2),
 | 
			
		||||
        fontSize: RFPercentage(2),
 | 
			
		||||
        color:"black",
 | 
			
		||||
    }
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										13
									
								
								src/connectors/myTcpSocket.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/connectors/myTcpSocket.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
import { NativeModules } from 'react-native';
 | 
			
		||||
 | 
			
		||||
const { MyTcpSocket } = NativeModules;
 | 
			
		||||
 | 
			
		||||
interface MyTcpSocketInterface {
 | 
			
		||||
    writeToSocket(ip: string, port: number, data: string[]): Promise<string>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const myTcpSocket: MyTcpSocketInterface = {
 | 
			
		||||
    writeToSocket: async (ip: string, port: number, data: string[]): Promise<string> => {
 | 
			
		||||
        return await MyTcpSocket.writeToSocket(ip, port, data);
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
@@ -2,9 +2,8 @@ import {Text, View} from "react-native";
 | 
			
		||||
import BasicButton from "../../components/BasicButton/BasicButton";
 | 
			
		||||
import {useScanningContext} from "../../providers/ScanProvider";
 | 
			
		||||
import printingApi from "../../api/printingApi";
 | 
			
		||||
import {convertPdfToBitmaps} from "../../connectors/PdfToBitmap";
 | 
			
		||||
import TcpSocket from 'react-native-tcp-socket';
 | 
			
		||||
import {Zpl} from "react-native-zpl-code";
 | 
			
		||||
import PrintingService from "../../utils/PrintingService";
 | 
			
		||||
 | 
			
		||||
async function generateZplCodes(images: string[]) {
 | 
			
		||||
    const zplCodes = [];
 | 
			
		||||
@@ -49,39 +48,13 @@ function MoneyScreen() {
 | 
			
		||||
    const {scan} = useScanningContext();
 | 
			
		||||
    const a = "123";
 | 
			
		||||
    const onPress = () => {
 | 
			
		||||
        printingApi.getLabel(386618).then(async result => {
 | 
			
		||||
            const images = await convertPdfToBitmaps(result);
 | 
			
		||||
            const client = TcpSocket.createConnection(
 | 
			
		||||
                {
 | 
			
		||||
                    host: "192.168.1.69",
 | 
			
		||||
                    port: 9100,
 | 
			
		||||
                },
 | 
			
		||||
                () => {
 | 
			
		||||
                    console.log('Connected')
 | 
			
		||||
                    generateZplCodes(images).then(codes => {
 | 
			
		||||
                        console.log('Generated codes')
 | 
			
		||||
                        codes.forEach((code, idx) => {
 | 
			
		||||
                            console.log(`Printing ${idx}`)
 | 
			
		||||
                            client.write(code)
 | 
			
		||||
                        })
 | 
			
		||||
                        console.log('Ending')
 | 
			
		||||
                        client.end();
 | 
			
		||||
                        // client.destroy();
 | 
			
		||||
                        console.log('Disconnected')
 | 
			
		||||
 | 
			
		||||
                    })
 | 
			
		||||
 | 
			
		||||
                },
 | 
			
		||||
            );
 | 
			
		||||
            client.on("error", error => {
 | 
			
		||||
                console.log("Error:", error);
 | 
			
		||||
        printingApi.getLabel(391845).then(async result => {
 | 
			
		||||
            PrintingService.getInstance().printPdf("", result).then(() => {
 | 
			
		||||
                    console.log("Printed successfully");
 | 
			
		||||
                }
 | 
			
		||||
            ).catch(error => {
 | 
			
		||||
                console.error("Error printing:", error);
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            // Handle connection close
 | 
			
		||||
            client.on("close", () => {
 | 
			
		||||
                console.log("Connection closed");
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
    return (
 | 
			
		||||
 
 | 
			
		||||
@@ -75,7 +75,7 @@ function ProfileScreen() {
 | 
			
		||||
 | 
			
		||||
            </View>
 | 
			
		||||
            <View style={styles.printerInputView}>
 | 
			
		||||
                <TextInput onChangeText={onPrinterIpChange} style={styles.printerInput}
 | 
			
		||||
                <TextInput onChangeText={onPrinterIpChange} defaultValue={printerIp} style={styles.printerInput}
 | 
			
		||||
                           placeholderTextColor={"black"} placeholder={"IP принтера"}/>
 | 
			
		||||
            </View>
 | 
			
		||||
            <View style={styles.content}>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,5 @@
 | 
			
		||||
import TcpSocket from "react-native-tcp-socket";
 | 
			
		||||
import * as SecureStore from 'expo-secure-store';
 | 
			
		||||
import {myTcpSocket} from "../connectors/myTcpSocket";
 | 
			
		||||
 | 
			
		||||
class PrintingService {
 | 
			
		||||
    private static instance: PrintingService | null = null;
 | 
			
		||||
@@ -27,36 +27,14 @@ class PrintingService {
 | 
			
		||||
        // const images = await convertPdfToBitmaps(pdf);
 | 
			
		||||
        // const codes = await this.generateZplCodes(images);
 | 
			
		||||
        const codes = pdf.labels;
 | 
			
		||||
 | 
			
		||||
        return new Promise<void>((resolve, reject) => {
 | 
			
		||||
            // Create and connect the socket
 | 
			
		||||
            const client = TcpSocket.createConnection({host: printerIp, port: 9100}, () => {
 | 
			
		||||
 | 
			
		||||
                // Write each ZPL chunk
 | 
			
		||||
                codes.forEach((code, idx) => {
 | 
			
		||||
                    console.log(`Printing ${idx}`);
 | 
			
		||||
                    client.write(code);
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
                client.end();
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            // Reject on socket error
 | 
			
		||||
            client.on("error", (err) => {
 | 
			
		||||
                console.error("Socket error:", err);
 | 
			
		||||
                reject(err);
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
            // Resolve when the socket fully closes
 | 
			
		||||
            client.on("close", (hadError) => {
 | 
			
		||||
                if (hadError) {
 | 
			
		||||
                    // close after an error will have already rejected
 | 
			
		||||
                    return;
 | 
			
		||||
        try {
 | 
			
		||||
            const result = await myTcpSocket.writeToSocket(printerIp, 9100, codes);
 | 
			
		||||
        } catch (error) {
 | 
			
		||||
            console.error("Error printing label:", error);
 | 
			
		||||
            await Promise.reject(error)
 | 
			
		||||
        }
 | 
			
		||||
                console.log("Connection closed");
 | 
			
		||||
                resolve();
 | 
			
		||||
            });
 | 
			
		||||
        });
 | 
			
		||||
        await Promise.resolve()
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -78,7 +56,6 @@ class PrintingService {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public getPrinter(baseMarketplace: number): string {
 | 
			
		||||
        if (baseMarketplace == 2) return 'ozon';
 | 
			
		||||
        return "wildberries";
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user