如何在区块链网络上运行连续功能,在运行时更新资产?

时间:2018-04-04 14:41:06

标签: hyperledger hyperledger-composer

我已经编写了逻辑来连续监控Hyperledger Composer上的logic.js文件中的一些资产。它通过运行连续函数来执行此操作,该函数将请求发送到不同的api并根据结果更新资产。

我希望此流程始终运行,不断监控所有资产,并在收到结果时更新网络中的资产。

我现在正在做的是从我的网络setupDemo函数调用函数(repeatMonitorPlane(planes))。然而,一个问题是,如果需要重新启动网络,则会停止setupDemo,当它重新启动时,它不会继续监视资产,我无法再次运行setupDemo。有谁知道怎么解决这个问题?也许有一些系统函数我可以调用repeatMonitorPlane函数,这样如果重新启动网络,它将再次启动进程?

任何想法都会受到极大的反对!

我的代码:

   async function repeatMonitorPlane(planes){
  //passing all four planes in ourSetupDemo into monitor plane function
  console.log('Planes being monitored: ' + planes);

  setInterval(async function () {
     try{
          await monitorPlane(planes);
      } catch (e) {
          console.log('\nPlane is on the ground \n');
      }
  }, 15000);


/**
 * Transaction to allow parties to Monitor planes
 * @param {org.blockaviation.MonitorPlane} monitorPlane
 * @transaction
 */
async function monitorPlane(monitorPlane){
    var NS = 'org.blockaviation';

    var location;


    console.log(monitorPlane.length);


    for(i =0;i<monitorPlane.length;i++){
        var monitor_plane = monitorPlane[i];
        console.log("\n"+i+"\n");


            console.log('\nMonitoring plane: ' + monitorPlane[i].icao);
             await getLocation(monitorPlane[i].icao)
            .then(function(location){

                if (location == null) throw "Plane on ground";
                console.log('location in monitorPlane: latitude: '+location[0] + ' longitude: '+location[1]);

                monitor_plane.lat = location[0]
                monitor_plane.long = location[1]

                //update the location variable on the plane asset
                if(monitor_plane.location){
                    monitor_plane.location.push(location[0],location[1])    //lat,long
                }else{
                    monitor_plane.location = [location[0],location[1]]
                }
                }).then(function(){
                    return getAssetRegistry(NS + '.Plane')
                }).then(function(planeRegistry){
                    console.log('monitor_plane.location');
                    console.log(monitor_plane);
                    return planeRegistry.update(monitor_plane);
                }).catch(function(e){
                    console.log(e);
                    return
                })

              }
      }

async function getLocation(icao){


    var url = 'https://opensky-network.org/api/states/all?icao24=' + icao;

    var lat, long;

    try {

        var result = await request.get({uri: url, json: true });


        if (result.states == null){
            throw "Plane on ground";
        }


        lat = result.states[0][6]
        long = result.states[0][5]

        lat = lat.toString()
        long = long.toString()
        location = [lat,long]
        console.log('Plane: '+ icao + ' location: ' + 'latitude: '+ location[0] +' degrees, ' + 'longitude: ' + location[1] + ' degrees\n' );
        return location
     } catch(e) {
          console.log('\nError trying to get location of plane \n');
          return null
    }

  }
}

dev-peer0.org1.example.com的日志 - &gt;更新资产有问题吗?

4

0


Monitoring plane: 4409c0
Plane: 4409c0 location: latitude: 54.9203 degrees, longitude: -3.0248 degrees

location in monitorPlane: latitude: 54.9203 longitude: -3.0248
2018-04-04T15:31:16.373Z [3e973d0c] [DEBUG   ] @JS : Api                     :getAssetRegistry()       > org.blockaviation.Plane
2018-04-04T15:31:16.373Z [3e973d0c] [DEBUG   ] @JS : RegistryManager         :get()                    > Asset:org.blockaviation.Plane
2018-04-04T15:31:16.374Z [3e973d0c] [DEBUG   ] @JS : NodeDataCollection      :get()                    > Asset:org.blockaviation.Plane
Error: [3e973d0c]No ledger context for GetState. Sending ERROR
    at parseResponse (/usr/local/src/node_modules/fabric-shim/lib/handler.js:673:9)
    at MsgQueueHandler.handleMsgResponse (/usr/local/src/node_modules/fabric-shim/lib/handler.js:137:26)
    at ClientDuplexStream.<anonymous> (/usr/local/src/node_modules/fabric-shim/lib/handler.js:293:28)
    at emitOne (events.js:116:13)
    at ClientDuplexStream.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at ClientDuplexStream.Readable.push (_stream_readable.js:208:10)
    at readCallback (/usr/local/src/node_modules/grpc/src/client.js:312:14)

1


Monitoring plane: 3c674e
Plane: 3c674e location: latitude: 54.8877 degrees, longitude: -3.5034 degrees

location in monitorPlane: latitude: 54.8877 longitude: -3.5034
2018-04-04T15:31:16.551Z [3e973d0c] [DEBUG   ] @JS : Api                     :getAssetRegistry()       > org.blockaviation.Plane
2018-04-04T15:31:16.551Z [3e973d0c] [DEBUG   ] @JS : RegistryManager         :get()                    > Asset:org.blockaviation.Plane
2018-04-04T15:31:16.551Z [3e973d0c] [DEBUG   ] @JS : NodeDataCollection      :get()                    > Asset:org.blockaviation.Plane
Error: [3e973d0c]No ledger context for GetState. Sending ERROR
    at parseResponse (/usr/local/src/node_modules/fabric-shim/lib/handler.js:673:9)
    at MsgQueueHandler.handleMsgResponse (/usr/local/src/node_modules/fabric-shim/lib/handler.js:137:26)
    at ClientDuplexStream.<anonymous> (/usr/local/src/node_modules/fabric-shim/lib/handler.js:293:28)
    at emitOne (events.js:116:13)
    at ClientDuplexStream.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at ClientDuplexStream.Readable.push (_stream_readable.js:208:10)
    at readCallback (/usr/local/src/node_modules/grpc/src/client.js:312:14)

2


Monitoring plane: 4ca264
Plane: 4ca264 location: latitude: 53.4304 degrees, longitude: -4.9469 degrees

location in monitorPlane: latitude: 53.4304 longitude: -4.9469
2018-04-04T15:31:16.794Z [3e973d0c] [DEBUG   ] @JS : Api                     :getAssetRegistry()       > org.blockaviation.Plane
2018-04-04T15:31:16.796Z [3e973d0c] [DEBUG   ] @JS : RegistryManager         :get()                    > Asset:org.blockaviation.Plane
2018-04-04T15:31:16.796Z [3e973d0c] [DEBUG   ] @JS : NodeDataCollection      :get()                    > Asset:org.blockaviation.Plane
Error: [3e973d0c]No ledger context for GetState. Sending ERROR
    at parseResponse (/usr/local/src/node_modules/fabric-shim/lib/handler.js:673:9)
    at MsgQueueHandler.handleMsgResponse (/usr/local/src/node_modules/fabric-shim/lib/handler.js:137:26)
    at ClientDuplexStream.<anonymous> (/usr/local/src/node_modules/fabric-shim/lib/handler.js:293:28)
    at emitOne (events.js:116:13)
    at ClientDuplexStream.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at ClientDuplexStream.Readable.push (_stream_readable.js:208:10)
    at readCallback (/usr/local/src/node_modules/grpc/src/client.js:312:14)

3


Monitoring plane: 40742f

Error trying to get location of plane 

Plane on ground

setupDemo

/**
  * This is jus a setuo demo. It wil create new particpants and assets automatically
  * @param {org.blockaviation.OurSetupDemo} ourSetupDemo
  * @transaction
  */
function ourSetupDemo(ourSetupDemo){

    var factory = getFactory();
    var NS = 'org.blockaviation';


    //Create LESSOR one
    var lessor  = factory.newResource(NS, 'Lessor', 'lessor@gmail.com');
    var lessorAddress = factory.newConcept(NS, 'Address');
    lessorAddress.country = 'USA';
    lessor.address = lessorAddress;

    //Create LESSOR two
    var lessor2  = factory.newResource(NS, 'Lessor', 'lessor2@gmail.com');
    var lessor2Address = factory.newConcept(NS, 'Address');
    lessor2Address.country = 'Ireland';
    lessor2.address = lessor2Address;

    //Create LESSEE one
    var lessee  = factory.newResource(NS, 'Lessee', 'lessee@gmail.com');
    var lesseeAddress = factory.newConcept(NS, 'Address');
    lesseeAddress.country = 'USA';
    lessee.address = lesseeAddress;

    //Create LESSEE two
    var lessee2  = factory.newResource(NS, 'Lessee', 'lessee2@gmail.com');
    var lessee2Address = factory.newConcept(NS, 'Address');
    lessee2Address.country = 'Ireland';
    lessee2.address = lessee2Address;


    //Create MANUFACTURER one

    var manufacturer  = factory.newResource(NS, 'Manufacturer', 'manufacturer@gmail.com');
    var manufacturerAddress = factory.newConcept(NS, 'Address');
    manufacturerAddress.country = 'USA';
    manufacturer.address = manufacturerAddress;

    //Create MANUFACTURER two

    var manufacturer2  = factory.newResource(NS, 'Manufacturer', 'manufacturer2@gmail.com');
    var manufacturer2Address = factory.newConcept(NS, 'Address');
    manufacturer2Address.country = 'USA';
    manufacturer2.address = manufacturer2Address;

    //Create Servicer one

    var servicer  = factory.newResource(NS, 'Servicer', 'servicer@gmail.com');
    var servicerAddress = factory.newConcept(NS, 'Address');
    servicerAddress.country = 'USA';
    servicer.address = servicerAddress;

    //Create Servicer two

    var servicer2  = factory.newResource(NS, 'Servicer', 'servicer2@gmail.com');
    var servicer2Address = factory.newConcept(NS, 'Address');
    servicer2Address.country = 'Germany';
    servicer2.address = servicer2Address;

    //Create the regulator - not extended from member abstract class

    var regulator  = factory.newResource(NS, 'Regulator', 'regulator@gmail.com');

    //Create the first Contract

    var contract = factory.newResource(NS, 'Contract', 'CON_001');
    contract.lessor = factory.newRelationship(NS, 'Lessor', 'lessor@gmail.com');
    contract.lessee = factory.newRelationship(NS, 'Lessee', 'lessee@gmail.com');
    contract.manufacturer = factory.newRelationship(NS, 'Manufacturer', 'manufacturer@gmail.com');
    contract.servicer = factory.newRelationship(NS, 'Servicer', 'servicer@gmail.com');
    contract.contractType = 'DRY_LEASE';
    contract.contract_status = 'INITIALISED';

    //Create the second Contract

    var contract2 = factory.newResource(NS, 'Contract', 'CON_002');
    contract2.lessor = factory.newRelationship(NS, 'Lessor', 'lessor2@gmail.com');
    contract2.lessee = factory.newRelationship(NS, 'Lessee', 'lessee2@gmail.com');
    contract2.manufacturer = factory.newRelationship(NS, 'Manufacturer', 'manufacturer2@gmail.com');
    contract2.servicer = factory.newRelationship(NS, 'Servicer', 'servicer2@gmail.com');
    contract2.contractType = 'WET_LEASE';
    contract2.contract_status = 'INITIALISED';

    //Create the first plane asset

    var plane = factory.newResource(NS,'Plane', 'Plane_001');
    plane.lessor = factory.newRelationship(NS, 'Lessor', 'lessor@gmail.com');
    plane.lessee = factory.newRelationship(NS, 'Lessee', 'lessee@gmail.com');
    plane.manufacturer = factory.newRelationship(NS, 'Manufacturer', 'manufacturer@gmail.com');
    plane.servicer = factory.newRelationship(NS, 'Servicer', 'servicer@gmail.com');
    plane.icao = '4409c0';
    plane.lat = '0';
    plane.long = '0';

    //Create the second plane asset

    var plane2 = factory.newResource(NS,'Plane', 'Plane_002');
    plane2.lessor = factory.newRelationship(NS, 'Lessor', 'lessor@gmail.com');
    plane2.lessee = factory.newRelationship(NS, 'Lessee', 'lessee@gmail.com');
    plane2.manufacturer = factory.newRelationship(NS, 'Manufacturer', 'manufacturer@gmail.com');
    plane2.servicer = factory.newRelationship(NS, 'Servicer', 'servicer@gmail.com');
    plane2.icao = '3c674e';
    plane2.lat = '0';
    plane2.long = '0';

    //Create the third plane asset

    var plane3 = factory.newResource(NS,'Plane', 'Plane_003');
    plane3.lessor = factory.newRelationship(NS, 'Lessor', 'lessor2@gmail.com');
    plane3.lessee = factory.newRelationship(NS, 'Lessee', 'lessee2@gmail.com');
    plane3.manufacturer = factory.newRelationship(NS, 'Manufacturer', 'manufacturer2@gmail.com');
    plane3.servicer = factory.newRelationship(NS, 'Servicer', 'servicer2@gmail.com');
    plane3.icao = '4ca264';
    plane3.lat = '0';
    plane3.long = '0';



    //Create the fourth plane asset

    var plane4 = factory.newResource(NS,'Plane', 'Plane_004');
    plane4.lessor = factory.newRelationship(NS, 'Lessor', 'lessor@gmail.com');
    plane4.lessee = factory.newRelationship(NS, 'Lessee', 'lessee@gmail.com');
    plane4.manufacturer = factory.newRelationship(NS, 'Manufacturer', 'manufacturer@gmail.com');
    plane4.servicer = factory.newRelationship(NS, 'Servicer', 'servicer@gmail.com');
    plane4.icao = '40742f';
    plane4.lat = '0';
    plane4.long = '0';

    //var planes = [plane,plane2,plane3];//,plane2,plane3,plane4
    var planes = [plane,plane2,plane3,plane4];//
    //console.log('plane');
    //console.log(plane);
    repeatMonitorPlane(planes)




    //Create the javascript promises - Populating contract
    return getParticipantRegistry(NS + '.Lessor')
        .then(function(lessorRegistry){
            return lessorRegistry.addAll([lessor,lessor2]);
        })
        .then(function(){
            return getParticipantRegistry(NS + '.Lessee');
        })
        .then(function(lesseeRegistry){
            return lesseeRegistry.addAll([lessee,lessee2]);
        })
        .then(function(){
            return getParticipantRegistry(NS + '.Manufacturer');
        })
        .then(function(manufacturerRegistry){
            return manufacturerRegistry.addAll([manufacturer,manufacturer2]);
        })
        .then(function(){
            return getParticipantRegistry(NS + '.Servicer');
        })
        .then(function(servicerRegistry){
            return servicerRegistry.addAll([servicer,servicer2]);
        })
        .then(function(){
            return getParticipantRegistry(NS + '.Regulator');
        })
        .then(function(regulatorRegistry){
            return regulatorRegistry.addAll([regulator]);
        })
        .then(function(){
            return getAssetRegistry(NS + '.Contract');
        })
        .then(function(contractRegistry){
            return contractRegistry.addAll([contract,contract2]);
        })
        .then(function(){
            return getAssetRegistry(NS + '.Plane');
        })
        .then(function(planeRegistry){
            planeRegistry.addAll([plane,plane2]);
            planeRegistry.addAll([plane3,plane4]);
        })


}

1 个答案:

答案 0 :(得分:1)

不幸的是,这不会飞: - )

在monitorPlanes事务(通过repeatMonitorPlanes)启动之前,您的安装事务未完成(返回),因此还没有数据可供monitorPlanes更新。

作为将repeatMonitorPlanes作为模型逻辑中的函数的替代方法,您可以编写一个单独的API程序,该程序可以使用重复循环来使用客户端API中的TransactionRegistry类来调用monitorPlanes事务