『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 7718|回复: 6

[流程系列] node-red TTS 播报昨日世界杯赛果

[复制链接]

40

主题

3056

帖子

1万

积分

超级版主

Nero

Rank: 8Rank: 8

积分
11149
金钱
8042
HASS币
182
发表于 2018-6-18 23:43:34 | 显示全部楼层 |阅读模式
本帖最后由 neroxps 于 2018-6-18 23:46 编辑

玩 node-red 有几天了,粗略学了下 JavaScript,今天交个功课~感谢坛子的大牛写的 node-red 教程。

woc.png


世界杯 JSON 接口地址:http://worldcup.sfg.io/matches,大佬可以用这个来写一些播报啥的。很好玩喔。

[{"id":"9e3d9b7e.813c28","type":"http request","z":"93b24c1b.4650a","name":"获取世界杯json","method":"GET","ret":"obj","url":"http://worldcup.sfg.io/matches","tls":"","x":324,"y":311,"wires":[["794ae990.160938"]]},{"id":"80a9d921.fcc948","type":"inject","z":"93b24c1b.4650a","name":"","topic":"woc","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":311,"wires":[["9e3d9b7e.813c28"]]},{"id":"beae9bfa.c15cf8","type":"function","z":"93b24c1b.4650a","name":"获取指定日的赛事","func":"// msg.date 应当为字符串,格式 yy-mm-dd\nmsg.payload = msg.payload.filter(function (e) { return e.datetime.substring(0,10) == msg.date;}); \nreturn msg;","outputs":1,"noerr":0,"x":721,"y":310,"wires":[["c07b24e5.3de8e8"]]},{"id":"794ae990.160938","type":"function","z":"93b24c1b.4650a","name":"指定时间为昨日","func":"function GetDateStr(AddDayCount) {\n    var dd = new Date();\n    dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期\n    var y = dd.getFullYear();\n    var m = String(dd.getMonth()+1);//获取当前月份的日期\n    if (m.length == 1){\n        m = '0' + m;\n    }\n    var d = dd.getDate();\n    return y+"-"+m+"-"+d;\n}\nmsg.date = GetDateStr(-1);\nreturn msg;","outputs":1,"noerr":0,"x":523,"y":310,"wires":[["beae9bfa.c15cf8"]]},{"id":"c07b24e5.3de8e8","type":"function","z":"93b24c1b.4650a","name":"TTS内容","func":"var translate = {"Russia":"俄罗斯","Belgium":"比利时","Germany":"德国","England":"英格兰","Spain":"西班牙","Poland":"波兰","Iceland":"冰岛","Serbia":"塞尔维亚","Portugal":"葡萄牙","France":"法国","Switzerland":"瑞士","Croatia":"克罗地亚","Sweden":"瑞典","Denmark":"丹麦","Brazil":"巴西","Uruguay":"乌拉圭","Argentina":"阿根廷","Colombia":"哥伦比亚","Peru":"秘鲁","Iran":"伊朗","Japan":"日本","Korea":"韩国","Saudi Arabia":"沙特阿拉伯","Australia":"澳大利亚","Nigeria":"尼日利亚","Egypt":"埃及","Senegal":"塞内加尔","Morocco":"摩洛哥","Tunisia":"突尼斯","Mexico":"墨西哥","Costa Rica":"哥斯达黎加","Panama":"巴拿马"};\nvar str = "昨天世界杯共有"+msg.payload.length+"场比赛,";\nvar home_team_name;\nvar home_team_goals;\nvar away_team_name;\nvar away_team_goals;\nfunction Get_winner(home_team_goals,away_team_goals){\n    if (Number(home_team_goals) - Number(away_team_goals) > 0){\n        return "赢";\n    }\n    else if (Number(home_team_goals) - Number(away_team_goals) < 0){\n        return "输给";\n    }\n    else{\n        return "平";\n    }\n}\nfor (var i in msg.payload){\n    home_team_name = msg.payload[i].home_team.country;\n    home_team_goals = msg.payload[i].home_team.goals;\n    away_team_name = msg.payload[i].away_team.country;\n    away_team_goals = msg.payload[i].away_team.goals;\n    str = str + translate[home_team_name] + "对" + translate[away_team_name] + "," + \n        home_team_goals + "比" + away_team_goals +\n        "," + translate[home_team_name] + Get_winner(home_team_goals,away_team_goals) +\n        translate[away_team_name] + ",";\n}\nmsg.payload = {};\nvar tts = str.substring(0,str.length-1)+"。";\nmsg.payload.data = {"message":tts};\nreturn msg;","outputs":1,"noerr":0,"x":900,"y":310,"wires":[["dea0cc91.221d8"]]},{"id":"dea0cc91.221d8","type":"api-call-service","z":"93b24c1b.4650a","name":"小爱同学TTS","server":"73841890.65ca18","service_domain":"hello_miai","service":"send","data":"","mergecontext":"","x":1052,"y":313,"wires":[[]]},{"id":"73841890.65ca18","type":"server","z":"","name":"Home Assistant","url":"HA地址","pass":"HA密码"}]
Nero
回复

使用道具 举报

26

主题

553

帖子

2728

积分

金牌会员

Rank: 6Rank: 6

积分
2728
金钱
2150
HASS币
100

教程狂人

发表于 2018-6-19 00:03:32 | 显示全部楼层
终于有人做世界杯插件了咩
回复

使用道具 举报

0

主题

221

帖子

1095

积分

论坛积极会员

积分
1095
金钱
874
HASS币
0
发表于 2018-6-19 09:00:03 | 显示全部楼层
不服墙 就服你 开发的进度 一个工具你几天都玩的这么⑥
回复

使用道具 举报

23

主题

658

帖子

4643

积分

元老级技术达人

积分
4643
金钱
3970
HASS币
30

教程狂人

QQ
发表于 2018-6-19 12:57:23 | 显示全部楼层
javascript玩的好6
回复

使用道具 举报

40

主题

3056

帖子

1万

积分

超级版主

Nero

Rank: 8Rank: 8

积分
11149
金钱
8042
HASS币
182
 楼主| 发表于 2018-6-19 14:54:07 | 显示全部楼层
都建民 发表于 2018-6-19 09:00
不服墙 就服你 开发的进度 一个工具你几天都玩的这么⑥

我只是东抄一点,西抄一点合在一起而已,数据类型就搞了好长时间。
node-red 好处是调试方便,所有信息一目了然,所以易学,但难精啊。
Nero
回复

使用道具 举报

40

主题

3056

帖子

1万

积分

超级版主

Nero

Rank: 8Rank: 8

积分
11149
金钱
8042
HASS币
182
 楼主| 发表于 2018-6-19 15:10:33 | 显示全部楼层

google来的代码
Nero
回复

使用道具 举报

65

主题

853

帖子

3044

积分

论坛元老

Rank: 8Rank: 8

积分
3044
金钱
2184
HASS币
40
发表于 2018-6-20 13:00:05 | 显示全部楼层
这个必须要顶啊!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-12-22 11:21 , Processed in 0.055380 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表