add end-to-end example sensor_e53_ia1_e2e_demo

This commit is contained in:
royye
2019-12-20 11:09:49 +08:00
parent 539dc6a152
commit c9cb7ba16d
20 changed files with 1140 additions and 53 deletions

View File

@@ -0,0 +1,22 @@
//app.js
App({
globalData: {
// 腾讯云物联网通信平台中获取 产品ID和设备名称
productId: "U1BZWHF7F9", // 产品ID
deviceName: "dev_01", // 设备名称
},
onLaunch: function () {
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
} else {
wx.cloud.init({
// env 参数说明:
// env 参数决定接下来小程序发起的云开发调用wx.cloud.xxx会默认请求到哪个云环境的资源
// 此处请填入环境 ID, 环境 ID 可打开云控制台查看
// 如不填则使用默认环境(第一个创建的环境)
env: "tos-demo",
traceUser: true,
})
}
}
})

View File

@@ -0,0 +1,13 @@
{
"pages": [
"pages/index/index"
],
"window": {
"backgroundColor": "#F6F6F6",
"backgroundTextStyle": "light",
"navigationBarBackgroundColor": "#F6F6F6",
"navigationBarTitleText": "智慧农业Demo",
"navigationBarTextStyle": "black"
},
"sitemapLocation": "sitemap.json"
}

View File

@@ -0,0 +1,156 @@
/**app.wxss**/
.container {
display: flex;
flex-direction: column;
align-items: center;
box-sizing: border-box;
}
button {
background: initial;
}
button:focus{
outline: 0;
}
button::after{
border: none;
}
page {
background: #f6f6f6;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.userinfo, .uploader, .tunnel {
margin-top: 40rpx;
height: 140rpx;
width: 100%;
background: #fff;
border: 1px solid rgba(0, 0, 0, 0.1);
border-left: none;
border-right: none;
display: flex;
flex-direction: row;
align-items: center;
transition: all 300ms ease;
}
.userinfo-avatar {
width: 100rpx;
height: 100rpx;
margin: 20rpx;
border-radius: 50%;
background-size: cover;
background-color: white;
}
.userinfo-avatar:after {
border: none;
}
.userinfo-nickname {
font-size: 32rpx;
color: #007aff;
background-color: white;
background-size: cover;
}
.userinfo-nickname::after {
border: none;
}
.uploader, .tunnel {
height: auto;
padding: 0 0 0 40rpx;
flex-direction: column;
align-items: flex-start;
box-sizing: border-box;
}
.uploader-text, .tunnel-text {
width: 100%;
line-height: 52px;
font-size: 34rpx;
color: #007aff;
}
.uploader-container {
width: 100%;
height: 400rpx;
padding: 20rpx 20rpx 20rpx 0;
display: flex;
align-content: center;
justify-content: center;
box-sizing: border-box;
border-top: 1px solid rgba(0, 0, 0, 0.1);
}
.uploader-image {
width: 100%;
height: 360rpx;
}
.tunnel {
padding: 0 0 0 40rpx;
}
.tunnel-text {
position: relative;
color: #222;
display: flex;
flex-direction: row;
align-content: center;
justify-content: space-between;
box-sizing: border-box;
border-top: 1px solid rgba(0, 0, 0, 0.1);
}
.tunnel-text:first-child {
border-top: none;
}
.tunnel-switch {
position: absolute;
right: 20rpx;
top: -2rpx;
}
.disable {
color: #888;
}
.service {
position: fixed;
right: 40rpx;
bottom: 40rpx;
width: 140rpx;
height: 140rpx;
border-radius: 50%;
background: linear-gradient(#007aff, #0063ce);
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3);
display: flex;
align-content: center;
justify-content: center;
transition: all 300ms ease;
}
.service-button {
position: absolute;
top: 40rpx;
}
.service:active {
box-shadow: none;
}
.request-text {
padding: 20rpx 0;
font-size: 24rpx;
line-height: 36rpx;
word-break: break-all;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

View File

@@ -0,0 +1,90 @@
const app = getApp()
Page({
data: {
productId: app.globalData.productId,
deviceName: app.globalData.deviceName,
stateReported: {},
},
onLoad: function (options) {
console.log("index onLoad")
if (!app.globalData.productId) {
wx.showToast({
title: "产品ID不能为空",
icon: 'none',
duration: 3000
})
return
} else if (!app.globalData.deviceName) {
wx.showToast({
title: "设备名称不能为空",
icon: 'none',
duration: 3000
})
return
}
this.update()
},
update() {
wx.showLoading()
wx.cloud.callFunction({
name: 'iothub-shadow-query',
data: {
productId: app.globalData.productId,
deviceName: app.globalData.deviceName,
},
success: res => {
wx.showToast({
title: '调用成功',
})
let deviceData = JSON.parse(res.result.Data)
this.setData({
stateReported: deviceData.payload.state.reported
})
console.log("result:", deviceData)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '调用失败',
})
console.error('[云函数] [iotexplorer] 调用失败:', err)
}
})
},
switchChange(e) {
let value = 0
if (e.detail.value == true) {
value = 1
}
let item = e.currentTarget.dataset.item
let obj = {
[`${item}`]: value
}
let payload = JSON.stringify(obj)
console.log(payload)
wx.showLoading()
wx.cloud.callFunction({
name: 'iothub-publish',
data: {
ProductId: app.globalData.productId,
DeviceName: app.globalData.deviceName,
Topic: "",
Payload: payload,
},
success: res => {
wx.showToast({
title: '调用成功',
})
console.log("res:", res)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '调用失败',
})
console.error('[云函数] [iotexplorer] 调用失败:', err)
}
})
},
})

View File

@@ -0,0 +1,65 @@
<view style="display:flex; flex-direction:column; align-items:center;">
<image style="width:200px; height:60px;" src="../../images/TencentOS_tiny_log.png" mode="cover"></image>
</view>
<view class="body">
<view style="font-weight: bold;">
设备信息
</view>
<view class="box">
<view class="cell">
<view class="status-left">产品ID</view>
<view class="status-right">{{productId}}</view>
</view>
<view class="cell">
<view class="status-left">设备名称</view>
<view class="status-right">{{deviceName}}</view>
</view>
</view>
<text>\n</text>
<form bindsubmit="">
<view style="display:flex; flex-direction:row; justify-content: space-between;">
<view style="font-weight: bold;">
农业Demo
</view>
<view>
<button type="primary" size="mini" bindtap="update">刷新</button>
</view>
</view>
<view class="box">
<view class="cell">
<view>照明</view>
<view>
<switch name="light" data-item="light" checked="{{stateReported.light}}" bindchange="switchChange"/>
</view>
</view>
<view class="cell">
<view>风扇</view>
<view>
<switch name="motor" data-item="motor" checked="{{stateReported.motor}}" bindchange="switchChange"/>
</view>
</view>
<view class="cell">
<view class="left">温度</view>
<view class="right">
{{stateReported.temperature}}
</view>
<view class="unit">℃</view>
</view>
<view class="cell">
<view class="left">湿度</view>
<view class="right">
{{stateReported.humidity}}
</view>
<view class="unit">%</view>
</view>
<view class="cell">
<view class="left">光照强度</view>
<view class="right">{{stateReported.light_intensity}}</view>
<view class="unit">lux</view>
</view>
</view>
</form>
</view>

View File

@@ -0,0 +1,31 @@
.body {
margin: 10rpx 20rpx;
}
.box {
padding: 0rpx 20rpx;
border-top: 2px solid #000;
}
.cell {
margin-top: 10rpx;
margin-bottom: 40rpx;
display: flex;
justify-content: space-between;
align-items: center;
}
.left {
width: 50%;
text-align: left;
}
.right {
width: 40%;
text-align: right;
}
.unit {
width: 10%;
text-align: right;
}

View File

@@ -0,0 +1,7 @@
{
"desc": "关于本文件的更多信息,请参考文档 https://developers.weixin.qq.com/miniprogram/dev/framework/sitemap.html",
"rules": [{
"action": "allow",
"page": "*"
}]
}