使用IPFS、IOTA构建的智能回收系统

 

大家好,今天我将介绍一个IPFS实现数据库的用例。

介于回收行业面临着严峻的挑战——回收材料的数量不一致,难以估计,我们用区块链来解决这个问题。

 

应用场景

消费者A想回收他的笔记本电脑、智能手机或手表等等。A使用他智能手机上的NFC扫描消费的产品,然后去我们的智能垃圾桶。我们的智能垃圾桶扫描这个NFC标签,给A发送一些MIOTA作为回收的奖励。

 

下面将详细解释图表

 

工作原理

假设一个笔记本电脑工厂有兴趣回收用户消费后的笔记本电脑。

  • 工厂将把所有的笔记本电脑数据和id发送到我们的系统,以保存在我们的系统上。(id将是访问每台笔记本电脑的关键)

  • 所有笔记本电脑都将以如下状态被保存:未找到,由用户A找到,在垃圾桶B中找到

数据库将是这样的↓↓↓

 

  • 所有笔记本电脑将有NFC标签来编写其ID。

  • 现在消费者将扫描他想回收的笔记本电脑。扫描笔记本后,API调用会将使用者设置为回收笔记本的所有者。

使用手机扫描产品

  • 当消费者把他的笔记本电脑扔进我们的垃圾箱,他就会得到代币作为奖励。

智能垃圾扫描产品,并验证其所有者,给予代币。

 

API的实现

我想设计的API看起来会像这样:

 

每个API端点的工作原理:

 

init’get-call来初始化MAM根,并准备使用数据库。

 

路径:

app.route(‘/init’)
.get(recycler.init);

 

控制器:

exports.init = async (req,res) =>{
const response = await manageClients.init()
res.json('DB initialized with some clients with root',response )}

 

实现:(将初始化MAM根,并添加客户端虚拟数字 )

 const init = async()=>

{products['HELLODRAYMANRNFELVTJREBCXJETCFEUGXBZHEGHCJHIYIFPQEQGDXILJZYUQMOYSELXIG9KUYOSYBFAY'] = {
'name':'product01',
'address':'HELLODRAYMANRNFELVTJREBCXJETCFEUGXBZHEGHCJHIYIFPQEQGDXILJZYUQMOYSELXIG9KUYOSYBFAY',
'owner':false,
'producer':'client01'
}
products['AB9CCUACOSC9VHQQFSVUGOVLAGNYJYDPROPBYTZIGNVOF9KMFNXBMSUGFFA9HTVHBPKPGGIBGSRJPUXZK'] = {
'name':'product02',
'address':'AB9CCUACOSC9VHQQFSVUGOVLAGNYJYDPROPBYTZIGNVOF9KMFNXBMSUGFFA9HTVHBPKPGGIBGSRJPUXZK',
'owner':false,
'producer':'client01'
}
products['LHSQELEGSRM9IAQCUBPXOLYXDMFOAPNJFS9JPOJBSRUDJAIRTWHTZCJCFLVYVRJPGPNXEWDDRGTCGKRAB'] = {
'name':'product03',
'address':'LHSQELEGSRM9IAQCUBPXOLYXDMFOAPNJFS9JPOJBSRUDJAIRTWHTZCJCFLVYVRJPGPNXEWDDRGTCGKRAB',
'owner':false,
'producer':'client01'
}
const ipfsHash = await addIPFS.execute(products)
const root = await mamManage.send(ipfsHash)
return root
}

‘addClient’post-call生产商/回收注册到我的服务。  

 

路径:

app.route('/addClient')
.post(recycler.addClient);

 

控制器:

exports.addClient = async (req,res) =>{
const response = await manageClients.addNewClient(req.body.root,req.body.products)
res.json("added new client succefuly "+response)
}

 

实现:

const addNewClient = async (_root,_products)=>{
const currentPropretiesRoot = _root
const currentPropretiesHash = await mamManage.fetch(currentPropretiesRoot)
const currentPropretiesString = await catIPFS.execute(getLastHash(currentPropretiesHash))
const currentPropretiesJSON = JSON.parse(currentPropretiesString)
const productsJSON = JSON.parse(JSON.stringify(_products))
for(let i = 0; i < productsJSON.length;i++){
currentPropretiesJSON[productsJSON[i].address] = productsJSON[i]}
const newPropretiesHash = await addIPFS.execute(currentPropretiesJSON)
const newPropretiesRoot = await mamManage.send(newPropretiesHash)
return newPropretiesRoot}

 

addOwner’post-call将会让顾客拥有某个产品。

 

路径:

app.route('/addOwner')
.post(recycler.addOwner);

 

控制器:

exports.addOwner = async (req,res) =>{
const response = await manageClients.addNewOwner(req.body.root,req.body.productAddress,req.body.ownerAddress)
res.json("added new owner "+ response)
}

 

实现:

const addNewOwner = async(_root,productAddress,ownerAddress)=>{
const proprietiesHash = await mamManage.fetch(_root)
const currentPropretiesString = await catIPFS.execute(getLastHash(proprietiesHash))
const currentPropretiesJSON = JSON.parse(currentPropretiesString)
currentPropretiesJSON[productAddress].owner = ownerAddressconst newPropretiesHash = await addIPFS.execute(currentPropretiesJSON)
const root = await mamManage.send(newPropretiesHash)
return root
}

 

giveReward’post-call将发送奖励给产品的所有者。

 

路径:

app.route('/giveReward').post(recycler.giveReward);

 

控制器:

exports.giveReward = async (req,res) =>{const response = await giveReward.execute(req.body.root,req.body.productAddress)res.json(response)}

 

实现:

const sendToken = require('./sendToken')
const fetchToMap = require('./fetchToMap')
giveRewards = async(root,productAddress) =>{
const OwnerAddrress = await fetchToMap.execute(root)
console.log(OwnerAddrress[productAddress].owner)
await sendToken.execute(OwnerAddrress[productAddress].owner,1)
return "Send 1 i. Congrats"
}

 

演示

现在,我为消费者和智能垃圾制作了一个android应用程序。这是它的演示(短片,时长1:16)。

https://ipfser.org/wp-content/uploads/2020/02/2020021102572033.mp4?_=1

 

你可以在以下库中找到android应用和API:

https://github.com/yehia67/Recycling-System-IOTA

 

【IPFS原力区】

价值观:价值 共建 共享 荣耀

总部位于上海,聚集基于分布式网络&存储的众多技术大咖和爱好者,深耕基于 IPFS 的商业生态建设和社区发展。

 

【原力开放日】

每周二举办,聚集了众多技术大咖和 IPFS 爱好者,通过持续输出全面、精细、优质的IPFS咨询和技术支持,将生态中的爱好者转化为IPFS支持者和参与者,共建IPFS生态的健康发展

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值