Advertisement
Abrara7

insert.js

Jul 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * SPDX-License-Identifier: Apache-2.0
  3.  */
  4.  
  5. 'use strict';
  6.  
  7. const { FileSystemWallet, Gateway } = require('fabric-network');
  8. const path = require('path');
  9.  
  10. async function main() {
  11.    
  12.     const numberArgs = process.argv.length
  13.     if(numberArgs != 4) {
  14.         console.log(`Error bad number of arguments. Syntax: node invoke.js companyName jsonFilepath`);
  15.         return;
  16.     }
  17.     const companyName = process.argv[2];
  18.     const jsonData = require(process.argv[3]);
  19.  
  20.     const ccpPath = path.resolve(__dirname, '..', '..', 'network', `connection-${companyName}.json`);
  21.  
  22.     try {
  23.         // Transaction submission time calculation.
  24.         var start, finish;
  25.         start = new Date();
  26.  
  27.        
  28.         // Create a new file system based wallet for managing identities.
  29.         const walletPath = path.join(process.cwd(), 'wallet');
  30.         const wallet = new FileSystemWallet(walletPath);
  31.         console.log(`Wallet path: ${walletPath}`);
  32.  
  33.         // Check to see if we've already enrolled the user.
  34.         const userExists = await wallet.exists(`user${companyName}`);
  35.         if (!userExists) {
  36.             console.log(`An identity for the user "user${companyName}" does not exist in the wallet`);
  37.             console.log('Run the registerUser.js application before retrying');
  38.             return;
  39.         }
  40.  
  41.         // Create a new gateway for connecting to our peer node.
  42.         const gateway = new Gateway();
  43.         await gateway.connect(ccpPath, { wallet, identity: `user${companyName}`, discovery: { enabled: true, asLocalhost: true } });
  44.         // await gateway.connect(ccpPath, { wallet, identity: 'admin', discovery: { enabled: true, asLocalhost: true } });
  45.         // Get the network (channel) our contract is deployed to.
  46.         const network = await gateway.getNetwork('mychannel');
  47.  
  48.         // Get the contract from the network.
  49.         const contract = network.getContract('fabcar');
  50.  
  51.         // We format each reading into a string and store them into a tab. Each entry is a reading.
  52.         var jsonDataAsAStringTab = [];
  53.         for (const reading of jsonData) {
  54.             jsonDataAsAStringTab.push([reading.SensorType + " " + reading.Timestamp + " " + reading.Unit + " " + reading.DeviceID + " " + reading.Value]);
  55.         }
  56.  
  57.         // 'pushData' is the 'createCar' from fabcar.
  58.         // The slightly altered chaincode function still works, as tested on single machine network
  59.         await contract.submitTransaction('pushData', jsonDataAsAStringTab.toString());
  60.        
  61.         finish = new Date();
  62.         console.log("Data inserted. Elapsed time: " + (finish.getTime() - start.getTime()) + " ms");
  63.  
  64.         // Disconnect from the gateway.
  65.         await gateway.disconnect();
  66.  
  67.     } catch (error) {
  68.         console.error(`Failed to submit transaction: ${error}`);
  69.         process.exit(1);
  70.     }
  71. }
  72.  
  73. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement