Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SPDX-License-Identifier: Apache-2.0
- */
- 'use strict';
- const { FileSystemWallet, Gateway } = require('fabric-network');
- const path = require('path');
- async function main() {
- const numberArgs = process.argv.length
- if(numberArgs != 4) {
- console.log(`Error bad number of arguments. Syntax: node invoke.js companyName jsonFilepath`);
- return;
- }
- const companyName = process.argv[2];
- const jsonData = require(process.argv[3]);
- const ccpPath = path.resolve(__dirname, '..', '..', 'network', `connection-${companyName}.json`);
- try {
- // Transaction submission time calculation.
- var start, finish;
- start = new Date();
- // Create a new file system based wallet for managing identities.
- const walletPath = path.join(process.cwd(), 'wallet');
- const wallet = new FileSystemWallet(walletPath);
- console.log(`Wallet path: ${walletPath}`);
- // Check to see if we've already enrolled the user.
- const userExists = await wallet.exists(`user${companyName}`);
- if (!userExists) {
- console.log(`An identity for the user "user${companyName}" does not exist in the wallet`);
- console.log('Run the registerUser.js application before retrying');
- return;
- }
- // Create a new gateway for connecting to our peer node.
- const gateway = new Gateway();
- await gateway.connect(ccpPath, { wallet, identity: `user${companyName}`, discovery: { enabled: true, asLocalhost: true } });
- // await gateway.connect(ccpPath, { wallet, identity: 'admin', discovery: { enabled: true, asLocalhost: true } });
- // Get the network (channel) our contract is deployed to.
- const network = await gateway.getNetwork('mychannel');
- // Get the contract from the network.
- const contract = network.getContract('fabcar');
- // We format each reading into a string and store them into a tab. Each entry is a reading.
- var jsonDataAsAStringTab = [];
- for (const reading of jsonData) {
- jsonDataAsAStringTab.push([reading.SensorType + " " + reading.Timestamp + " " + reading.Unit + " " + reading.DeviceID + " " + reading.Value]);
- }
- // 'pushData' is the 'createCar' from fabcar.
- // The slightly altered chaincode function still works, as tested on single machine network
- await contract.submitTransaction('pushData', jsonDataAsAStringTab.toString());
- finish = new Date();
- console.log("Data inserted. Elapsed time: " + (finish.getTime() - start.getTime()) + " ms");
- // Disconnect from the gateway.
- await gateway.disconnect();
- } catch (error) {
- console.error(`Failed to submit transaction: ${error}`);
- process.exit(1);
- }
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement