多用途传感器处理程序,读取传感器和做一些事情(发送,保存,触发,…)的价值。
pondslider的Python项目详细描述
pondslider
多用途传感器处理程序,读取传感器并用该值执行某些操作(发送、保存、触发等)。
什么是Pondslider
pondslider是一个python模块,用于通过传感器处理程序读取传感器值,并使用逐个值处理程序执行某些操作。
谢天谢地,github、pypi和其他存储库中有大量由有能力的良性工程师编写的开源代码库。根据我的经验,很少有人会遇到需要自己编写代码来读取传感器值的情况,因为我可以很容易地找到大多数传感器的可重用代码并加以感激地使用。但是这些代码的接口和结果的形式也有很多种,所以我只需要编写包装器代码来适应接口。 传感器处理程序只是包装这些已有的有价值代码以提供统一接口的代码。通常,传感器处理程序被创建为退出python模块的包装器模块,并使用import来读取传感器值,如下所示:
# import existing module import SomethingExistingSensorModule ##################################### # # unified read() interface # def read(): # prepare existing module a_sensor = SomethingExistingSensorModule.new() # call there function to read sensor value values = a_sensor.there_func_to_read_sensor() # re-format to name-value pair. return adjust_the_format(values) def adjust_the_format(value) ''' adjst the format of value as a dictionaly of name & value pair. '''
在其他情况下,对于外部可执行文件,
# import python standard external executable handle module import subprocess def read(): # call external executable and get return strings p = subprocess.Popen("./SomethingExistingExecutable2GetSensorValue", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) std_out, std_err = p.communicate(None, timeout=20) value = std_out.strip() # re-format to name-value pair. return adjust_the_format(value) def adjust_the_format(value) ''' adjst the format of value as a dictionaly of name & value pair. '''
对于cource,可以将传感器处理程序设置为读取传感器值目录。
import serial def read(): # mh-z19 CO2 sensor https://github.com/UedaTakeyuki/mh-z19 try: ser = serial.Serial(serial_dev, baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1.0) while 1: result=ser.write("\xff\x01\x86\x00\x00\x00\x00\x00\x79") s=ser.read(9) if len(s) >= 4 and s[0] == "\xff" and s[1] == "\x86": return {'co2': ord(s[2])*256 + ord(s[3])} break except: traceback.print_exc()
什么是值处理程序?
值处理程序是一个python模块,它接收传感器值并对其执行一些操作,例如发送到服务器、写入strage等。 值处理程序的目的是提供一个统一的接口来处理获取的传感器值,接口如下:
def handle(sensor_hander, data_name, value):
示例处理程序
处理程序示例可在https://github.com/uedatakeyuki/handlers" rel="nofollow">https://github.com/uedatakeyuki/handlers
安装
pip install pondslider
安装
如何设置传感器和值处理程序
有两种方法可以设置手。一种是使用命令行选项,另一种是格式化配置文件。 如果同时设置了config和command-line选项,则pondslider句柄也将同时位于order config文件中,然后是command-line。
有关在不同包目录中指定不同处理程序的方法,请参阅"如何在VA中指定处理程序包"Rious包和目录层次结构"
通过命令行选项设置传感器和值处理程序
传感器处理程序可以通过命令行选项设置。 这是list-type命令行选项,您可以按如下方式指定mh-z19和dht22等nsessary sensor handlers模块:
--sensor_handlers mh_z19 dht22
对于值处理程序,也有类似的命令行选项。 例如,sender.monitor.send和saver.strage.save的spesify模块如下:
--value_handlers sender.monitor.send saver.strage.save
每个值处理程序handle()函数都是用all传感器处理程序中红色的值调用的。如果您只需要用对应的值调用对应的值处理程序,您应该检查作为handle()函数参数传递的值名称和值处理程序模块,如下所示。
def handle(data_source_name, data_name, value): if data_name is "co2": # do something else: # do nothing pass
或者,使用稍后提到的配置文件,它可以将值和值处理程序一一关联。
通过格式化配置文件设置传感器和值处理程序
您可以通过配置文件指定传感器处理程序的值和相应的值处理程序与之之间的一对一关系。 预期内容如下:
[[sensors]]
handler = "dht22"
[[sensors.values]]
name = "temp"
handlers = [
"send",
"save"
]
[[sensors.values]]
name = "humidity"
handlers = [
"send",
"save"
]
[[sensors.values]]
name = "humiditydeficit"
handlers = [
"send",
"save"
]
[[sensors]]
handler = "mh-z19"
[[sensors.values]]
name = "co2"
handlers = [
"send",
"save"
]
pondslider的配置文件由一个表数组***[[sensors]***组成。 pondslider读取这个数组,然后读取每个传感器处理程序并调用相应的值处理程序。
传感器的元素有两个键,一个是指示相应传感器处理程序的处理程序,另一个是指示相应值处理程序的处理程序。
[[sensor s.values]]也有两个kye,一个是名称,表示相应的红色传感器值名称,这是传感器处理程序返回函数的关键字。其他是表示值处理程序模块的处理程序数组。这些处理程序的handle()功能仅用名称相同的红色传感器值调用,而不像用值处理器指定的值处理器,用所有传感器处理器的值red来调用值处理器。
配置文件由命令行选项指定。在没有--config选项的情况下,pondslider会在当前工作目录中搜索名为config.toml的文件并在找到时使用它。
添加Python模块搜索路径
如果处理程序模块没有通过标准的python模块搜索路径链接,您可以告诉pondslider通过命令行选项添加搜索路径,如下所示:
# import existing module import SomethingExistingSensorModule ##################################### # # unified read() interface # def read(): # prepare existing module a_sensor = SomethingExistingSensorModule.new() # call there function to read sensor value values = a_sensor.there_func_to_read_sensor() # re-format to name-value pair. return adjust_the_format(values) def adjust_the_format(value) ''' adjst the format of value as a dictionaly of name & value pair. '''0
例如,如果mh-z19传感器处理程序位于/home/pi/mh-z19中,而发送处理程序位于/home/pi/handlers/send.py中,则可以读取mh-z19并按如下方式发送值:
# import existing module import SomethingExistingSensorModule ##################################### # # unified read() interface # def read(): # prepare existing module a_sensor = SomethingExistingSensorModule.new() # call there function to read sensor value values = a_sensor.there_func_to_read_sensor() # re-format to name-value pair. return adjust_the_format(values) def adjust_the_format(value) ''' adjst the format of value as a dictionaly of name & value pair. '''1
注意,mh-z19需要读取串行接口,可能需要sudo。
如何使用
作为python程序。
# import existing module import SomethingExistingSensorModule ##################################### # # unified read() interface # def read(): # prepare existing module a_sensor = SomethingExistingSensorModule.new() # call there function to read sensor value values = a_sensor.there_func_to_read_sensor() # re-format to name-value pair. return adjust_the_format(values) def adjust_the_format(value) ''' adjst the format of value as a dictionaly of name & value pair. '''2
由--imppaths指定的路径用于附加的python导入库路径。 使用--interval选项,pondslider在指定的间隔分钟内重复该操作。如果没有--间隔,只需运行一次并退出即可。
作为python库。
# import existing module import SomethingExistingSensorModule ##################################### # # unified read() interface # def read(): # prepare existing module a_sensor = SomethingExistingSensorModule.new() # call there function to read sensor value values = a_sensor.there_func_to_read_sensor() # re-format to name-value pair. return adjust_the_format(values) def adjust_the_format(value) ''' adjst the format of value as a dictionaly of name & value pair. '''3
问答
任何问题,建议,报告都欢迎!请毫不犹豫地发布!
历史记录
- 0.1.1 2018.11.03第一版自分叉,来自传感器处理程序
- 0.2.1 2018.11.06添加--间隔选项。
- 0.2.2 2018.11.06次要修复:删除多余打印
- 0.3.1 2018.11.07添加--传感器处理器和--值处理器
- 0.3.7 2018.11.19缺少修复需要
- 0.4.1 2018.12.02添加终止符
- 0.4.2 2018.12.06修复--间隔选项