januslo / react-native-sunmi-inner-printer

http://docs.sunmi.com/htmls/index.html?lang=zh##V1文档资源 根据商米V1文档开发的热敏打印
MIT License
74 stars 120 forks source link

Loop printing with bitmap #7

Closed RobertStuff closed 7 years ago

RobertStuff commented 7 years ago

Hi good day.. Have you tried printing loop with bitmap and text.

here is my code.. RNFetchBlob .config({ fileCache : true }) .fetch('GET', 'http://www.example.net:8000' + item[0])

       .then((resp) => {

           imagePath = resp.path()
           return resp.readFile('base64')
       })
       .then((base64Data) => {
            for(var i=0; i<data.netcode.length;i++){

            var note = data.netcode[i]
            Reactotron.log(note)

            SunmiInnerPrinter.printBitmap(base64Data, 200, 200)
            fs.unlink(imagePath)

            .then(()=>SunmiInnerPrinter.setAlignment(1))
            .then(()=>SunmiInnerPrinter.printString('\n'))
            .then(()=>SunmiInnerPrinter.setFontSize(26))
            .then(()=>SunmiInnerPrinter.printString(
                    note.service_category.company.company_name+ ' ' +  -  + ' ' +note.category.service.service+ ' ' + -  + ' ' +note.category.value+ ' ' + 'ID' + '\n'
            ))
            .then(()=>SunmiInnerPrinter.setAlignment(1))
            .then(()=>SunmiInnerPrinter.setFontSize(36))
            .then(()=>SunmiInnerPrinter.printString('Pin Code' + '\n' ))
            .then(()=>SunmiInnerPrinter.printString(note.code + '\n'))
            .then(()=>SunmiInnerPrinter.setAlignment(1))
            .then(()=>SunmiInnerPrinter.setFontSize(32))
            .then(()=>SunmiInnerPrinter.printString(note.service_category. + '\n'))
            .then(()=>SunmiInnerPrinter.setFontSize(28))
            .then(()=>SunmiInnerPrinter.printString(
                    note.service_category.message_service_en + '\n' +
                    note.service_category.message_service_kd + '\n' 
            ))
            .then(()=>SunmiInnerPrinter.setFontSize(26))
            .then(()=>SunmiInnerPrinter.printString(
                    'Serial Number' + '\n' 
            ))
            .then(()=>SunmiInnerPrinter.printString(note.serial_number + '\n'))
            .then(()=>SunmiInnerPrinter.setFontSize(24))
            .then(()=>SunmiInnerPrinter.printString(note.date_of_purchased + '\n'))
            .then(()=>SunmiInnerPrinter.setFontSize(28))
            .then(()=>SunmiInnerPrinter.printString('Code generated by eshortway'))
            .then(()=>SunmiInnerPrinter.printString('\n\n\n\n'))

but the result is like looping 1 item on the length of datasource.. The machice is printing the line base on the length

its like image image image same text same text same text and so on .......

Thanks.. cheers..

januslo commented 7 years ago

Sorry cannot get your point

发自我的 iPhone

在 2017年5月30日,下午7:22,Roberto P. Gludo II notifications@github.com 写道:

Hi good day.. Have you tried printing loop with bitmap and text.

here is my code.. RNFetchBlob .config({ fileCache : true }) .fetch('GET', 'http://www.example.net:8000' + item[0])

   .then((resp) => {

       imagePath = resp.path()
       return resp.readFile('base64')
   })
   .then((base64Data) => {
          for(var i=0; i<data.netcode.length;i++){

      var note = data.netcode[i]
      Reactotron.log(note)

      SunmiInnerPrinter.printBitmap(base64Data, 200, 200)
      fs.unlink(imagePath)

      .then(()=>SunmiInnerPrinter.setAlignment(1))
      .then(()=>SunmiInnerPrinter.printString('\n'))
      .then(()=>SunmiInnerPrinter.setFontSize(26))
      .then(()=>SunmiInnerPrinter.printString(
              note.service_category.company.company_name+ ' ' +  -  + ' ' +note.category.service.service+ ' ' + -  + ' ' +note.category.value+ ' ' + 'ID' + '\n'
      ))
      .then(()=>SunmiInnerPrinter.setAlignment(1))
      .then(()=>SunmiInnerPrinter.setFontSize(36))
      .then(()=>SunmiInnerPrinter.printString('Pin Code' + '\n' ))
      .then(()=>SunmiInnerPrinter.printString(note.code + '\n'))
      .then(()=>SunmiInnerPrinter.setAlignment(1))
      .then(()=>SunmiInnerPrinter.setFontSize(32))
      .then(()=>SunmiInnerPrinter.printString(note.service_category. + '\n'))
      .then(()=>SunmiInnerPrinter.setFontSize(28))
      .then(()=>SunmiInnerPrinter.printString(
              note.service_category.message_service_en + '\n' +
              note.service_category.message_service_kd + '\n' 
      ))
      .then(()=>SunmiInnerPrinter.setFontSize(26))
      .then(()=>SunmiInnerPrinter.printString(
              'Serial Number' + '\n' 
      ))
      .then(()=>SunmiInnerPrinter.printString(note.serial_number + '\n'))
      .then(()=>SunmiInnerPrinter.setFontSize(24))
      .then(()=>SunmiInnerPrinter.printString(note.date_of_purchased + '\n'))
      .then(()=>SunmiInnerPrinter.setFontSize(28))
      .then(()=>SunmiInnerPrinter.printString('Code generated by eshortway'))
      .then(()=>SunmiInnerPrinter.printString('\n\n\n\n'))

but the result is like looping 1 item on the length of datasource.. The machice is printing the line base on the length

its like image image image same text same text same text and so on .......

Thanks.. cheers..

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

RobertStuff commented 7 years ago

HI good day.. here's the sample images

I was trying to print this http://i.imgur.com/z77ijP6.jpg

but I achieve this http://i.imgur.com/ZoV1WOS.jpg

Thanks.. cheers..

januslo commented 7 years ago

Because you not wait for the print operation finish in your loop. Try with asycn- await operators instead

发自我的 iPhone

在 2017年5月31日,上午7:37,Roberto P. Gludo II notifications@github.com 写道:

HI good day.. here's the sample images

I was trying to print this http://i.imgur.com/z77ijP6.jpg

but I achieve this http://i.imgur.com/ZoV1WOS.jpg

Thanks.. cheers..

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

RobertStuff commented 7 years ago

ok thanks I will try it... cheers

RobertStuff commented 7 years ago

Please show samples thanks...

januslo commented 7 years ago

plz check this https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop

RobertStuff commented 7 years ago

Thanks will try that...

RobertStuff commented 7 years ago

Hi I've try async/await but still looping on every then function...

januslo commented 7 years ago

Show me the code again plz

发自我的 iPhone

在 2017年5月31日,下午2:35,Roberto P. Gludo II notifications@github.com 写道:

Hi I've try async/await but still looping on every then function...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

RobertStuff commented 7 years ago

Here.

return RNFetchBlob .config({ fileCache : true }) .fetch('GET', 'http://example.website.info:8000' + item[0])

           .then((resp) => {

               imagePath = resp.path()
               return resp.readFile('base64')
           })
           .then((base64Data) => {
                data.netcode.map(async (note) => {
                    Reactotron.log('start loop')
                const testing = await SunmiInnerPrinter.printBitmap(base64Data, 200, 200)
                    .then(()=>SunmiInnerPrinter.setAlignment(1))
                    .then(()=>SunmiInnerPrinter.printString('\n'))
                    .then(()=>SunmiInnerPrinter.setFontSize(26))
                    .then(()=>SunmiInnerPrinter.printString(
                            note.service_category.company.company_name+ ' ' +  -  + ' ' +note.service_category.service.service+ ' ' + -  + ' ' +note.service_category.value+ ' ' + 'ID' + '\n'
                    ))
                    .then(()=>SunmiInnerPrinter.setAlignment(1))
                    .then(()=>SunmiInnerPrinter.setFontSize(36))
                    .then(()=>SunmiInnerPrinter.printString('Pin Code' + '\n' ))
                    .then(()=>SunmiInnerPrinter.printString(note.code + '\n'))
                    .then(()=>SunmiInnerPrinter.setAlignment(1))
                    .then(()=>SunmiInnerPrinter.setFontSize(32))
                    .then(()=>SunmiInnerPrinter.printString(note.service_category.message_service_ar + '\n'))
                    .then(()=>SunmiInnerPrinter.setFontSize(28))
                    .then(()=>SunmiInnerPrinter.printString(
                            note.service_category.message_service_en + '\n' +
                            note.service_category.message_service_kd + '\n' 
                    ))
                    .then(()=>SunmiInnerPrinter.setFontSize(26))
                    .then(()=>SunmiInnerPrinter.printString(
                            'Serial Number' + '\n' 
                    ))
                    .then(()=>SunmiInnerPrinter.printString(note.serial_number + '\n'))
                    .then(()=>SunmiInnerPrinter.setFontSize(24))
                    .then(()=>SunmiInnerPrinter.printString(note.date_of_purchased + '\n'))
                    .then(()=>SunmiInnerPrinter.setFontSize(28))
                    .then(()=>SunmiInnerPrinter.printString('Code generated by eshortway'))
                    .then(()=>SunmiInnerPrinter.printString('\n\n\n\n'))
                    Reactotron.log('End of loop')
                    return fs.unlink(imagePath)
                })
                dispatch(setPrintPinCode({printCodeState: 'Code has been successfully printed.'})); 
           })

But when I remove the async/await the logs looks good but It also doesn't print correctly.

januslo commented 7 years ago

May show thing like this:


const Reactotron = {};
const RNFetchBlob={};
const SunmiInnerPrinter = {
    printBitmap(){
        return new Promise();
    }
};

export default class Test {
    data = {
        netcode: [{
            service_category: {
                company: {company_name: "company_name"},
                service: {service: "service"},
                value: "value",
                message_service_ar: 'message_service_ar',
                message_service_en: "message_service_en",
                message_service_kd: "message_service_kd"
            },
            serial_number: "serial_number",
            date_of_purchased: "date_of_purchased",
            code: "code"
        }]
    }
    imagePath = "imagePath";
    startsHere = ()=> {
        return RNFetchBlob
            .config({
                fileCache: true
            })
            .fetch('GET', 'http://example.website.info:8000' + item[0])
            .then((resp) => {
                imagePath = resp.path()
                return resp.readFile('base64')
            })
            .then((base64Data) => {
                print(base64Data)
            }).then(()=> {
                dispatch(setPrintPinCode({printCodeState: 'Code has been successfully printed.'}))
            })
    }

    async print = (base64Data)=> {
        return new Promise(function (resolve, reject) {
            for (let note of data.netcode) {
                Reactotron.log('start loop');
                let result = await
                SunmiInnerPrinter.printBitmap(base64Data, 200, 200);
                result = await
                SunmiInnerPrinter.setAlignment(1);
                result = await
                SunmiInnerPrinter.printString('\n');
                result = await
                SunmiInnerPrinter.setFontSize(26);
                result = await
                SunmiInnerPrinter.printString(
                    note.service_category.company.company_name + ' ' + -+' ' + note.service_category.service.service + ' ' + -+' ' + note.service_category.value + ' ' + 'ID' + '\n'
                );
                result = await
                SunmiInnerPrinter.setAlignment(1);
                result = await
                SunmiInnerPrinter.setFontSize(36);
                result = await
                SunmiInnerPrinter.printString('Pin Code' + '\n');
                result = await
                SunmiInnerPrinter.printString(note.code + '\n');
                result = await
                SunmiInnerPrinter.setAlignment(1);
                result = await
                SunmiInnerPrinter.setFontSize(32);
                result = await
                SunmiInnerPrinter.printString(note.service_category.message_service_ar + '\n');
                result = await
                SunmiInnerPrinter.setFontSize(28);
                result = await
                SunmiInnerPrinter.printString(
                    note.service_category.message_service_en + '\n' +
                    note.service_category.message_service_kd + '\n'
                );
                result = await
                SunmiInnerPrinter.setFontSize(26);
                result = await
                SunmiInnerPrinter.printString(
                    'Serial Number' + '\n'
                );
                result = await
                SunmiInnerPrinter.printString(note.serial_number + '\n');
                result = await
                SunmiInnerPrinter.setFontSize(24);
                result = await
                SunmiInnerPrinter.printString(note.date_of_purchased + '\n');
                result = await
                SunmiInnerPrinter.setFontSize(28);
                result = await
                SunmiInnerPrinter.printString('Code generated by eshortway');
                result = await
                SunmiInnerPrinter.printString('\n\n\n\n');

            }
            Reactotron.log('End of loop')
            resolve(fs.unlink(imagePath))
        })
    }

}
RobertStuff commented 7 years ago

Thanks with your prompt reply. I'll check it out thanks...

RobertStuff commented 7 years ago

Hi Im having an error in this part 'async print = (base64Data)=> {'

RobertStuff commented 7 years ago

Hi I have added another async function inside and it work perfectly... Thank you very much...

januslo commented 7 years ago

@RobertStuff cool man. so let's close this case.