-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHKWeather.py
More file actions
144 lines (110 loc) · 4.59 KB
/
Copy pathHKWeather.py
File metadata and controls
144 lines (110 loc) · 4.59 KB
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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
#
# Module for extending functionality to HK Weather
#
# by Peter Juett
# References: http://rss.weather.gov.hk/rsse.html
#
# Copyright 2018
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import urllib2, urllib, json
import time
import datetime
import feedparser
import DB
import logging
from logging.handlers import RotatingFileHandler
import paho.mqtt.client as mqtt
SLEEP_TIME = 60
class HKWeather(object):
def __init__(self, main_app_log):
if main_app_log is None:
self.log_formatter = logging.Formatter('%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s')
self.logFile = 'logs/HKWeather.log'
self.my_handler = RotatingFileHandler(self.logFile, mode='a', maxBytes=5*1024*1024, backupCount=1, encoding=None, delay=0)
self.my_handler.setFormatter(self.log_formatter)
self.my_handler.setLevel(logging.INFO)
self.app_log = logging.getLogger('root')
self.app_log.setLevel(logging.INFO)
self.app_log.addHandler(self.my_handler)
else:
self.app_log = main_app_log
self._humidity = ""
self._temperature = ""
self._db = DB.DB()
self._db.load_settings()
self.start_mos()
self.process_loop()
def get(self):
self._humidity = ""
self._temperature = ""
targeturl = 'http://rss.weather.gov.hk/rss/CurrentWeather.xml'
d = feedparser.parse(targeturl)
if len(d.entries) > 0:
desc = d.entries[0].description.split('Air temperature : ')
self._temperature = desc[1][:2].strip()
desc = d.entries[0].description.split(' Relative Humidity : ')
self._humidity = desc[1][:3].strip()
logging.info(self._humidity + "%")
logging.info(self._temperature + "C")
else:
logging.info("No entries returned")
def getunits(self, which_units):
if which_units == "temperature":
return (u'\N{DEGREE SIGN}' + 'C')
if which_units == "humidity":
return ("%")
def on_connect(self, mosclient, userdata, flags, rc):
self.app_log.info("Subscribing to topic: " + self._db.get_value("mostopic"))
mosclient.subscribe(self._db.get_value("mostopic"))
def on_message(self, mosclient, userdata, msg):
pass
def start_mos(self):
self.mos_client = mqtt.Client()
self.mos_client.on_connect = self.on_connect
self.mos_client.on_message = self.on_message
if len(self._db.get_value("mospassword"))>0:
self.mos_client.username_pw_set(self._db.get_value("mosusername"),self._db.get_value("mospassword"))
mos_broker_address = self._db.get_value("mosbrokeraddress")
self.app_log.info("Connecting to: " + mos_broker_address)
self.mos_client.connect(mos_broker_address, int(self._db.get_value("mosbrokerport")), 60)
self.app_log.info("Connected")
self.mos_client.loop_start()
def process_loop(self):
x = 10
while(1):
try:
#Data is refreshed every 10 minutes
if x == 10:
x = 0
self.get()
else:
x = x + 1
#This is broadcast every minute, if valid
if len(self._temperature) > 0:
message = self._db.get_value("name") + "/HK Weather/Outside: " + self._temperature + self.getunits("temperature") + " " + self._humidity + self.getunits("humidity")
self.mos_client.publish(self._db.get_value("mostopic"), message)
self.app_log.info("Sent - " + message)
else:
self.app_log.info("Nothing Sent")
except Exception as e:
logging.exception('Exception: %s', e)
finally:
time.sleep(SLEEP_TIME)
def run_program(main_app_log):
HKWeather(main_app_log)
if __name__ == '__main__':
run_program(None)