Re: [Domoticz插件]空气指数AQI插件
发表于 : 周五 7月 14, 2017 00:42
代码: 全选
#sic Python Plugin Example
#
# Author: GizMoCuz
#
"""
<plugin key="DT27-AQI" name="DT27-AQI" author="Zack" version="1.0.0" externallink="http://www.pm25.com/">
<params>
<param field="Address" label="城市" width="80px" required="true" default="上海"/>
<param field="Mode1" label="监测点" width="120px" required="true" default="浦东张江"/>
<param field="Mode2" label="API标准" width="100px">
<options>
<option label="中国标准" value="中国标准"/>
<option label="美国标准" value="美国标准" default="中国标准" />
</options>
</param>
<param field="Mode3" label="更新频率(分钟)" width="30px" required="true" default="60"/>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="False" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import urllib.parse
import urllib.request
import re
import time
import sys
sys.path.append('/usr/local/lib/python3.4/dist-packages')
class plugin:
def __init__(self):
self.AQI_URI = "http://www.pm25.com/city/mon/aqi/{}/{}.html"
self.PM25_URI = "http://www.pm25.com/city/mon/pm2_5/{}/{}.html"
self.PM10_URI = "http://www.pm25.com/city/mon/pm10/{}/{}.html"
self.SO2_URI = "http://www.pm25.com/city/mon/so2/{}/{}.html"
self.NO2_URI = "http://www.pm25.com/city/mon/no2/{}/{}.html"
self.CO_URI = "http://www.pm25.com/city/mon/co/{}/{}.html"
self.O3_URI = "http://www.pm25.com/city/mon/o3/{}/{}.html"
return
def onStart(self):
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
Domoticz.Debug("Debugger started, use 'telnet 0.0.0.0 4444' to c onnect")
import rpdb
rpdb.set_trace()
Domoticz.Debug("Debugger start failed, Please Check the [rpdb] m odel")
self.repeatTime = int(Parameters["Mode3"])
if ( 1 not in Devices):
Domoticz.Device(Name=Parameters["Address"] + "空气指数", Unit=1, TypeName="Custom", Options={"Custom":"1;AQI"}, Used=1).Create()
if ( 2 not in Devices):
Domoticz.Device(Name=Parameters["Address"] + "PM2.5浓度", Unit=2, TypeName="Custom", Options={"Custom":"1;μg/m³"}, Used=1).Create()
if ( 3 not in Devices):
Domoticz.Device(Name=Parameters["Address"] + "PM10浓度", Unit=3, TypeName="Custom", Options={"Custom":"1;μg/m³"}, Used=1).Create()
if ( 4 not in Devices):
Domoticz.Device(Name=Parameters["Address"] + "SO2浓度", Unit=4, TypeName="Custom", Options={"Custom":"1;μg/m³"}, Used=1).Create()
if ( 5 not in Devices):
Domoticz.Device(Name=Parameters["Address"] + "NO2浓度", Unit=5, TypeName="Custom", Options={"Custom":"1;μg/m³"}, Used=1).Create()
if ( 6 not in Devices):
Domoticz.Device(Name=Parameters["Address"] + "CO浓度", Unit=6, TypeName="Custom", Options={"Custom":"1;μg/m³"}, Used=1).Create()
if ( 7 not in Devices):
Domoticz.Device(Name=Parameters["Address"] + "O3浓度", Unit=7, TypeName="Custom", Options={"Custom":"1;μg/m³"}, Used=1).Create()
Domoticz.Heartbeat(60)
def onStop(self):
Domoticz.Log("onStop called")
def onConnect(self, Connection, Status, Description):
Domoticz.Log("onConnect called")
def onMessage(self, Connection, Data, Status, Extra):
Domoticz.Log("onMessage called")
def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)
def onDisconnect(self, Connection):
Domoticz.Log("onDisconnect called")
def onHeartbeat(self):
if int(time.time()/60)%self.repeatTime==0:
Domoticz.Log("onHeartbeat called")
aqi = self.getAirQuality(self.AQI_URI)
if aqi != '':
self.UpdateDevice(1,0,aqi)
pm25 = self.getAirQuality(self.PM25_URI)
if pm25 != '':
self.UpdateDevice(2,0,pm25)
pm10 = self.getAirQuality(self.PM10_URI)
if pm10 != '':
self.UpdateDevice(3,0,pm10)
so2 = self.getAirQuality(self.SO2_URI)
if so2 != '':
self.UpdateDevice(4,0,so2)
no2 = self.getAirQuality(self.NO2_URI)
if no2 != '':
self.UpdateDevice(5,0,no2)
co = self.getAirQuality(self.CO_URI)
if co != '':
self.UpdateDevice(6,0,co)
o3 = self.getAirQuality(self.O3_URI)
if o2 != '':
self.UpdateDevice(7,0,o3)
# Update Device into DB
def UpdateDevice(self, Unit, nValue, sValue):
# Make sure that the Domoticz device still exists (they can be deleted) before updating it
if (Unit in Devices):
if (Devices[Unit].nValue != nValue) or (Devices[Unit].sValue != sValue):
Devices[Unit].Update(nValue=nValue, sValue=str(sValue))
Domoticz.Log("Update "+str(nValue)+":'"+str(sValue)+"' ("+Devices[Unit].Name+")")
def getAirQuality(self,url):
try:
num=0
if 'pm25.com/city/mon/aqi/' in url:
if Parameters["Mode2"] == "中国标准":
num=1
else:
num=0
url = urllib.parse.quote_plus(url.format(Parameters["Address"], Parameters["Mode1"]),safe='http://')
results=urllib.request.urlopen(url,timeout=5).read().decode('utf-8')
data = re.findall(r"data:\[([\d+,]+)\]", results)
data = data[num]
l24 = data.split(',')
# airquality=self.onlyNum(l24[23],oth='')
if l24:
airquality=l24[-1]
# else:
# airquality=0
return airquality
except BaseException:
return ''
global _plugin
_plugin = plugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onConnect(Connection, Status, Description):
global _plugin
_plugin.onConnect(Connection, Status, Description)
def onMessage(Connection, Data, Status, Extra):
global _plugin
_plugin.onMessage(Connection, Data, Status, Extra)
def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)
def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
global _plugin
_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)
def onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return