跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
Wiki首页
Domoticz 中文站
Domoticz 论坛
Domoticz Github
随机页面
特殊页面
特殊页面
所有页面
分类
最近更改
Domoticz
搜索
搜索
登录
个人工具
登录
查看“Domoticz和脚本”的源代码
页面
讨论
大陆简体
阅读
查看源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
查看源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
←
Domoticz和脚本
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
==介绍== 虽然Domoticz可以在不到半小时的时间内完成配置并运行,但是向家里添加一些智能操作时,乐趣才真正开始。可以使用可用的程序选项(如计时器和通知)来实现大量此类功能。但是,如果需要更复杂的操作,那么就需要通过脚本来解决。本页将介绍创建脚本的基础知识,这些脚本的内容可以在Wiki和论坛的其他地方找到。<br> 请注意,此页内的信息都是基于Linux系统的,Windows相关知识可以通过百度搜索查到。这里做了简单的汇总,使新的Domoticz用户能够快速了解脚本并开始创建脚本。 ==脚本的种类== A script is a text file which contains a number of commands which will be interpreted when the script is executed. To be able to be interpreted the system will need to have the appropriate software installed which understands the specific language used. Examples of scripting languages are Bash, Lua, DOS Batchfiles, Python, Perl, Java, and PHP, just to name a few. They all have in common that the commands are read, interpreted and executed at the moment the script is invoked.<br> In Domoticz two kinds of scripts are commonly used: Lua scripts and Bash shell scripts. The Lua interpreter is integrated by the Domoticz developers (and so is also available in Domoticz on Windows) and the interpreter for the Bash shell scripts is built in to the Linux OS. Information on what to put in the scripts (i.e. the language) can be found [http://luatut.com/ here] for Lua and [http://www.example.com here] for Bash.<br> ==Lua scripts== Domoticz supports two different mechanisms for user-developed Lua scripts. Either or both can be used depending upon what you are trying to achieve. ===Database Based=== Users that are finding Blockly too restrictive and want to extend Domoticz using Lua (with the added convenience of having their scripts integrated into the Backup & Restore functionality) can use the built-in editor on the [[Events]] page. The built in [https://en.wikipedia.org/wiki/Ace_%28editor%29 Ace editor]understands Lua (& Python if Domoticz is built with support turned on) so it provides context-sensitive auto-complete functionality and will flag a number of coding errors within the editor itself. New Lua scripts can be created from standard templates depending on the type of events that the script will handle. Because database scripts are maintained within a Domoticz web-browser session they are operating-system independent. ===File System Based=== For technical users that want to write complicated scripts and include additional Lua modules and scripts, Domoticz also supports file-based scripts. These are not included in the Domoticz standard Backup & Restore functions and are not versioned so this needs to be done manually. File-system based Lua scripts should be placed in the /home/pi/domoticz/scripts/lua directory. Since Lua is integrated in Domoticz it knows about the status of switches and sensors (the commandArray variable) which makes it easy to interact.<br> In the scripts directory you will find a Lua directory. The Lua scripts are named "script_device_demo.lua" and "script_time_demo.lua". By copying these examples and changing demo into a sensible identifier like script_device_light2.lua the script will become active. Device scripts will be run at every device state-change and time-scripts will be run every minute.<br> Domoticz does not provide tools to access or edit these scripts but will execute them if they exist. <br><br> Continue with the info at [[Events]] in this Wiki. ==Windows vs Linux== From here on down this HowTo specifically covers scripting for Domoticz on the Raspberry Pi and uses a Windows PC to get access to the Raspberry Pi. ==Prepare your tools to get access== Get a copy of WinSCP [http://winscp.net/eng/download.php here] and PuTTY from [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html here]. WinSCP can be used to browse through the filesystem and contains an editor to edit the scripts. PuTTY, which can be launched from WinSCP, is a plain terminal where commands can be sent to the Linux Operating System. If you downloaded the Domoticz image the username will be "pi" and the password "raspberry". Both WinSCP and PuTTY will need these.<br> After launching WinSCP for the first time create a new Stored Session. Use File protocol "SCP", enter your Raspberry Pi's IP address in the Host name field, leave the port value at 22, enter username and password, and press Save, give it a sensible name like MyPi and check the Save Password checkbox. With MyPi selected click login.<br> In Options|Preferences|Integration|Applications provide the path to where you stored PuTTY.exe after installation. ==Non-Lua Scripts== Bash scripts (and likewise Python, Perl) know nothing about the state of switches and sensors. This info needs to be retrieved by calling the JSON API. Then again, it is easier to extend the possibilities of the non-Lua scripts by importing functions written by others.<br> The first line of the non-Lua script will identify the interpreter to be used, such as:<br> #!/usr/bin/bash or #!/usr/bin/perl or #!/usr/bin/python ===More on Bash scripts=== Bash scripts are great to perform tasks which consist of a sequence of OS commands, like creating a backup of the Domoticz database and storing it at a safe location every night. Let's have a look at a Bash shell script. After the login WinSCP takes us to the Domoticz home directory /home/pi/domoticz. Move to the scripts directory. Here a number of general Bash shell scripts are included. Double click 'restart_domoticz' and an editor opens which shows this script's content: <br> <syntaxhighlight lang="bash"> #!/bin/sh sudo service domoticz.sh stop echo "please standby... (waiting 8 seconds)" sleep 8 sudo service domoticz.sh start </syntaxhighlight> Here you see (and can change!) the commands executed when the command 'restart_domoticz' would be typed at the command prompt.<br> Newly created bash scripts need to be executable. To make yourscript.sh executable type sudo chmod +x yourscript.sh and exectute it from the script's directory by ./yourscript.sh ===More on Perl=== The use of Perl is well documented on this wiki page - [[Perl for Domoticz]]. ==crontab== In the example of the nightly-backup it would be nice if this could be automated. Linux has a scheduler for that: crontab. To configure this use PuTTY to get to the command prompt by clicking Ctrl-P in WinSCP. Log in with pi and raspberry. You now arrive in the /home/pi directory. Type "crontab -e" at the prompt and the nano editor will open with a file. All lines starting with # are comments and can be ignored. The lines with * and numbers define when to execute a command. This can be as often as once a minute or once a year at 10u42 on your day of birth. As an example this command 0 4 * * * sudo ~/domoticz/scripts/domoticz_backup.sh will execute the domoticz_backup.sh script as a Super User (hence the sudo) daily at 04:00. More info on the cron possibilities can be found [http://www.pantz.org/software/cron/croninfo.html here]. [[Category:脚本]] [[Category:Lua]] [[Category:Bash]]
返回
Domoticz和脚本
。
开关有限宽度模式