-
Notifications
You must be signed in to change notification settings - Fork 35
/
iot-platform-device-data-on-the-cloud.yml
567 lines (551 loc) · 17.6 KB
/
iot-platform-device-data-on-the-cloud.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
ROSTemplateFormatVersion: '2015-09-01'
Description:
zh-cn: 创建VPC环境,部署ECS实例,配置IoT设备数据自动上报至云端,并通过函数计算推送温湿度数据至钉钉群。
en: Create a VPC environment, deploy ECS instances, configure IoT devices for automatic
data reporting to the cloud, and utilize Function Compute to push temperature
and humidity data to DingTalk groups.
Parameters:
ZoneId:
Type: String
Label:
en: VSwitch Availability Zone
zh-cn: 可用区ID
Description:
en: Availability Zone ID,<br><b>note: <font color='blue'>Before selecting, please confirm that the Availability Zone supports the specification of creating ECS resources.</font></b>
zh-cn: 可用区ID。<br><b>注: <font color='blue'>选择可用区前请确认该可用区是否支持创建ECS资源的规格。</font></b>
AssociationProperty: ALIYUN::ECS::Instance:ZoneId
InstanceType:
Type: String
Label:
en: Instance Type
zh-cn: 实例类型
Description:
en: 'Fill in the specifications that can be used under the VSwitch availability zone;</b></font><br>general specifications:<font color=''red''><b>ecs.c5.large</b></font><br>note: a few zones do not support general specifications<br>see detail: <a href=''https://www.alibabacloud.com/help/en/doc-detail/25378.html'' target=''_blank''><b><font color=''blue''>Instance Specification Family</font></a></b>'
zh-cn: 填写VSwitch可用区下可使用的规格;<br>通用规格:<font color='red'><b>ecs.c5.large</b></font><br>注:可用区可能不支持通用规格<br>规格详见:<a href='https://help.aliyun.com/document_detail/25378.html' target='_blank'><b><font color='blue'>实例规格族</font></a></b>
AssociationProperty: ALIYUN::ECS::Instance::InstanceType
AssociationPropertyMetadata:
ZoneId: ZoneId
SystemDiskCategory:
Type: String
Label:
en: System Disk Type
zh-cn: 系统盘类型
Description:
en: '<font color=''blue''><b>Optional values:</b></font><br>[cloud_efficiency: <font color=''green''>Efficient Cloud Disk</font>]<br>[cloud_ssd: <font color=''green''>SSD Cloud Disk</font>]<br>[cloud_essd: <font color=''green''>ESSD Cloud Disk</font>]<br>[cloud: <font color=''green''>Cloud Disk</font>]<br>[ephemeral_ssd: <font color=''green''>Local SSD Cloud Disk</font>]'
zh-cn: '<font color=''blue''><b>可选值:</b></font><br>[cloud_efficiency: <font color=''green''>高效云盘</font>]<br>[cloud_ssd: <font color=''green''>SSD云盘</font>]<br>[cloud_essd: <font color=''green''>ESSD云盘</font>]<br>[cloud: <font color=''green''>普通云盘</font>]<br>[ephemeral_ssd: <font color=''green''>本地SSD盘</font>]'
AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
AssociationPropertyMetadata:
LocaleKey: DiskCategory
InstanceType: ${InstanceType}
ZoneId: ZoneId
InstancePassword:
Type: String
Label:
en: Instance Password
zh-cn: 实例密码
Description:
en: Server login password, Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
zh-cn: 服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)。
ConstraintDescription:
en: Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in).
zh-cn: 长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号)。
AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$'
MinLength: 8
MaxLength: 30
NoEcho: true
CommonName:
Type: String
Default: iot_cloud
DeviceName:
Type: String
Label:
en: Device Name
zh-cn: 设备名称
ProductKey:
Type: String
Label:
en: Product Key
zh-cn: 产品密钥
DeviceSecret:
Type: String
Label:
en: Device Secret
zh-cn: 设备密钥
Host:
Type: String
Label:
en: The MQTT device Host
zh-cn: MQTT设备接入域名(VPC内地址)
Description:
en: How to get access to a domain name, Please see <a href="https://help.aliyun.com/zh/iot/user-guide/manage-the-endpoint-of-an-instance" target="_blank"> manage instances terminal node </a>.
zh-cn: 获取接入域名的方法,请参见<a href="https://help.aliyun.com/zh/iot/user-guide/manage-the-endpoint-of-an-instance" target="_blank">管理实例终端节点</a>。
AccessToken:
Type: String
Label:
en: Access Token
zh-cn: Access Token
Description:
en: The value of AccessToken from the Webhook address of the internal enterprise application robot.
zh-cn: 企业内部应用机器人Webhook地址中AccessToken的值。
Resources:
ResourceManagerResourceGroup:
Type: ALIYUN::ResourceManager::ResourceGroup
Properties:
DisplayName: IoT-Resource-Group
Name:
Fn::Join:
- '-'
- - IoT
- Fn::Select:
- 0
- Fn::Split:
- '-'
- Ref: ALIYUN::StackId
Vpc:
Type: ALIYUN::ECS::VPC
DependsOn:
- ResourceManagerResourceGroup
Properties:
VpcName:
Fn::Sub: ${CommonName}_VPC_IOT
CidrBlock: 192.168.0.0/16
ResourceGroupId:
Ref: ResourceManagerResourceGroup
VSwitch:
Type: ALIYUN::ECS::VSwitch
Properties:
ZoneId:
Ref: ZoneId
VpcId:
Ref: Vpc
VSwitchName:
Fn::Sub: ${CommonName}_vsw_001
CidrBlock: 192.168.1.0/24
SecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
DependsOn:
- ResourceManagerResourceGroup
Properties:
VpcId:
Ref: Vpc
SecurityGroupName:
Fn::Sub: ${CommonName}_SecurityGroup_1
ResourceGroupId:
Ref: ResourceManagerResourceGroup
InstanceGroup:
Type: ALIYUN::ECS::InstanceGroup
DependsOn:
- ResourceManagerResourceGroup
Properties:
VpcId:
Ref: Vpc
InstanceName:
Fn::Sub: ${CommonName}_APP001
VSwitchId:
Ref: VSwitch
SecurityGroupId:
Ref: SecurityGroup
ImageId: aliyun_3_x64_20G_alibase_20230727.vhd
SystemDiskSize: 20
InstanceType:
Ref: InstanceType
SystemDiskCategory:
Ref: SystemDiskCategory
Password:
Ref: InstancePassword
IoOptimized: optimized
MaxAmount: 1
ResourceGroupId:
Ref: ResourceManagerResourceGroup
InstanceRunCommand:
Type: 'ALIYUN::ECS::RunCommand'
Properties:
InstanceIds:
- Ref: InstanceGroup
Sync: true
Type: RunShellScript
Timeout: '300'
CommandContent:
'Fn::Sub': >-
#!/bin/bash
sudo yum install -y nodejs
cat >/root/iot_device.js << 'EOF'
const mqtt = require("aliyun-iot-mqtt");
// 1. 设备身份信息
var options = {
productKey: "${ProductKey}",
deviceName: "${DeviceName}",
deviceSecret: "${DeviceSecret}",
host: "${Host}",
};
// 2. 建立MQTT连接
const client = mqtt.getAliyunIotMqttClient(options);
//订阅云端指令Topic
client.subscribe("/${ProductKey}/${DeviceName}/c");
client.on("message", function (topic, message) {
console.log("topic " + topic);
console.log("message " + message);
});
setInterval(function () {
// 3.定时上报温湿度数据
client.publish(
"/sys/${ProductKey}/${DeviceName}/thing/event/property/post",
getPostData(),
{ qos: 0 }
);
}, 5 * 1000);
function getPostData() {
const payloadJson = {
id: Date.now(),
version: "1.0",
params: {
temperature: Math.floor(Math.random() * 20 + 10),
humidity: Math.floor(Math.random() * 20 + 10),
},
method: "thing.event.property.post",
};
console.log("payloadJson " + JSON.stringify(payloadJson));
return JSON.stringify(payloadJson);
}
EOF
cd /root
npm config set registry https://registry.npm.taobao.org
npm install aliyun-iot-mqtt -S
nohup node iot_device.js &
RamUser:
Type: ALIYUN::RAM::User
Properties:
UserName:
Fn::Join:
- '-'
- - IoT-Test
- Ref: ALIYUN::StackId
CustomPolicy:
Type: ALIYUN::RAM::ManagedPolicy
Properties:
PolicyName:
Fn::Join:
- '-'
- - IoT-FC
- Ref: ALIYUN::StackId
PolicyDocument:
Version: '1'
Statement:
- Effect: Allow
Action:
- fc:ListServices
- fc:ListFunctions
- fc:GetService
- fc:GetFunction
- fc:CreateFunction
- fc:CreateService
- fc:DeleteFunction
- fc:DeleteService
- fc:GetFunctionCode
- fc:InvokeFunction
Resource:
- '*'
AliyunIOTAccessingFCRolePolicy:
Type: ALIYUN::RAM::ManagedPolicy
Properties:
PolicyName:
Fn::Join:
- '-'
- - AliyunIOTAccessingFCRole
- Ref: ALIYUN::StackId
PolicyDocument:
Version: '1'
Statement:
- Action:
- ram:AttachPolicyToRole
- ram:CreateRole
Resource:
- acs:ram:*:system:policy/AliyunIOTAccessingFCRolePolicy
- acs:ram:*:*:role/AliyunIOTAccessingFCRole
Effect: Allow
AttachCustomPolicyToUser:
DependsOn:
- CustomPolicy
- RamUser
Type: ALIYUN::RAM::AttachPolicyToUser
Properties:
PolicyType: Custom
UserName:
Fn::GetAtt:
- RamUser
- UserName
PolicyName:
Fn::GetAtt:
- CustomPolicy
- PolicyName
AttachAliyunIOTAccessingFCRolePolicyToUser:
DependsOn:
- AliyunIOTAccessingFCRolePolicy
- RamUser
Type: ALIYUN::RAM::AttachPolicyToUser
Properties:
PolicyType: Custom
UserName:
Fn::GetAtt:
- RamUser
- UserName
PolicyName:
Fn::GetAtt:
- AliyunIOTAccessingFCRolePolicy
- PolicyName
AliyunBSSOrderAccessPolicyAttachment:
Type: ALIYUN::ResourceManager::PolicyAttachment
DependsOn:
- ResourceManagerResourceGroup
Properties:
PolicyType: System
ResourceGroupId:
Ref: ResourceManagerResourceGroup
PolicyName: AliyunBSSOrderAccess
PrincipalName:
Fn::Join:
- ''
- - Ref: RamUser
- '@'
- Fn::GetAtt:
- ResourceManagerResourceGroup
- AccountId
- .onaliyun.com
PrincipalType: IMSUser
AliyunVPCReadOnlyAccessPolicyAttachment:
Type: ALIYUN::ResourceManager::PolicyAttachment
DependsOn:
- ResourceManagerResourceGroup
Properties:
PolicyType: System
ResourceGroupId:
Ref: ResourceManagerResourceGroup
PolicyName: AliyunVPCReadOnlyAccess
PrincipalName:
Fn::Join:
- ''
- - Ref: RamUser
- '@'
- Fn::GetAtt:
- ResourceManagerResourceGroup
- AccountId
- .onaliyun.com
PrincipalType: IMSUser
AliyunECSFullAccessPolicyAttachment:
Type: ALIYUN::ResourceManager::PolicyAttachment
DependsOn:
- ResourceManagerResourceGroup
Properties:
PolicyType: System
ResourceGroupId:
Ref: ResourceManagerResourceGroup
PolicyName: AliyunECSFullAccess
PrincipalName:
Fn::Join:
- ''
- - Ref: RamUser
- '@'
- Fn::GetAtt:
- ResourceManagerResourceGroup
- AccountId
- .onaliyun.com
PrincipalType: IMSUser
AliyunVPCFullAccessPolicyAttachment:
Type: ALIYUN::ResourceManager::PolicyAttachment
DependsOn:
- ResourceManagerResourceGroup
Properties:
PolicyType: System
ResourceGroupId:
Ref: ResourceManagerResourceGroup
PolicyName: AliyunVPCFullAccess
PrincipalName:
Fn::Join:
- ''
- - Ref: RamUser
- '@'
- Fn::GetAtt:
- ResourceManagerResourceGroup
- AccountId
- .onaliyun.com
PrincipalType: IMSUser
AliyunIOTFullAccessPolicyAttachment:
Type: ALIYUN::ResourceManager::PolicyAttachment
DependsOn:
- ResourceManagerResourceGroup
Properties:
PolicyType: System
ResourceGroupId:
Ref: ResourceManagerResourceGroup
PolicyName: AliyunIOTFullAccess
PrincipalName:
Fn::Join:
- ''
- - Ref: RamUser
- '@'
- Fn::GetAtt:
- ResourceManagerResourceGroup
- AccountId
- .onaliyun.com
PrincipalType: IMSUser
CustomPolicyAttachment:
Type: ALIYUN::ResourceManager::PolicyAttachment
DependsOn:
- CustomPolicy
- ResourceManagerResourceGroup
- RamUser
Properties:
PolicyType: Custom
ResourceGroupId:
Ref: ResourceManagerResourceGroup
PolicyName:
Ref: CustomPolicy
PrincipalName:
Fn::Join:
- ''
- - Ref: RamUser
- '@'
- Fn::GetAtt:
- ResourceManagerResourceGroup
- AccountId
- .onaliyun.com
PrincipalType: IMSUser
FcService:
Type: ALIYUN::FC::Service
Properties:
InternetAccess: true
ServiceName:
Fn::Join:
- '-'
- - IoT_Service
- Ref: ALIYUN::StackId
Function:
Type: ALIYUN::FC::Function
Properties:
ServiceName:
Fn::GetAtt:
- FcService
- ServiceName
FunctionName: pushData2DingTalk
Handler: index.handler
Runtime: nodejs14
Code:
SourceCode:
'Fn::Sub': >-
const https = require("https");
const accessToken = "${AccessToken}";
module.exports.handler = function (event, context, callback) {
var eventJson = JSON.parse(event.toString());
//钉钉消息格式
const postData = JSON.stringify({
msgtype: "markdown",
markdown: {
title: "温湿度传感器",
text:
"#### 温湿度传感器上报\n" +
"> 设备位置:" +
eventJson.tag +
"\n\n" +
"> 设备编号:" +
eventJson.isn +
"\n\n" +
"> 实时温度:" +
eventJson.temperature +
"℃\n\n" +
"> 相对湿度:" +
eventJson.humidity +
"%\n\n" +
"> ###### " +
eventJson.time +
" 发布 by [物联网平台](https://www.aliyun.com/product/iot) \n",
},
at: {
isAtAll: false,
},
});
const options = {
hostname: "oapi.dingtalk.com",
port: 443,
path: "/robot/send?access_token=" + accessToken,
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(postData),
},
};
const req = https.request(options, (res) => {
res.setEncoding("utf8");
res.on("data", (chunk) => {});
res.on("end", () => {
callback(null, "success");
});
});
// 异常返回
req.on("error", (e) => {
callback(e);
});
// 写入数据
req.write(postData);
req.end();
};
Outputs:
FcService:
Description:
en: Fc Service.
zh-cn: FC 服务。
Value:
Fn::GetAtt:
- FcService
- ServiceName
RamUser:
Description:
en: Ram User
zh-cn: RAM用户名称。
Value:
Fn::GetAtt:
- RamUser
- UserName
ResourceManagerResourceGroup:
Description:
en: Resource Manager Resource Group Display Name
zh-cn: 资源组显示名称。
Value:
Fn::GetAtt:
- ResourceManagerResourceGroup
- DisplayName
InstanceGroup:
Description:
en: ECS Instance ID.
zh-cn: 实例ID。
Value:
Fn::GetAtt:
- InstanceGroup
- InstanceIds
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- ZoneId
- InstanceType
- SystemDiskCategory
- InstancePassword
Label:
en: ECS Configuration
zh-cn: ECS配置
- Parameters:
- DeviceName
- ProductKey
- DeviceSecret
- Host
Label:
en: IOT Configuration
zh-cn: IOT配置
- Parameters:
- AccessToken
Label:
en: Webhook
zh-cn: Webhook
TemplateTags:
- acs:solution:iot_cloud:物联网平台设备数据上云
Hidden:
- CommonName