-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock2mqtt.py
executable file
·174 lines (160 loc) · 6.47 KB
/
clock2mqtt.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim tabstop=4 expandtab shiftwidth=4 softtabstop=4
#
# clock2mqtt
#
# Provides real time clock data
#
# Data items provided: TODO not up to date!!!!!
# year
# month
# day
# hour
# minute
#
# utc-year
# utc-month
# utc-day
# utc-hour
# utc-minute
#
# tz
# dst
#
# weekday
# dow
# doy
#
# moonrise
# moonphase
# sunrise
# sunrise-civil
# sunrise-astronautical
#
__author__ = "Dennis Sell"
__copyright__ = "Copyright (C) Dennis Sell"
APPNAME = "clock2mqtt"
VERSION = "0.11"
import sys
import os
import mosquitto
import socket
import time
import subprocess
import logging
import signal
import threading
import commands
import datetime
import ephem
from daemon import Daemon
from mqttcore import MQTTClientCore
from mqttcore import main
class MyMQTTClientCore(MQTTClientCore):
def __init__(self, appname, clienttype):
MQTTClientCore.__init__(self, appname, clienttype)
self.clientversion = VERSION
self.interval = self.cfg.INTERVAL
self.clientversion = VERSION
self.observer = ephem.Observer()
self.observer.lat = '38'
self.observer.lon = '-93'
self.sun = ephem.Sun()
self.moon = ephem.Moon()
self.year=""
self.month=""
self.day=""
self.weekday=""
self.dow=""
self.doy=""
self.hour=""
self.mil_hour=""
self.ampm=""
self.minute=""
self.sunrise=""
self.utc_sunrise=""
self.sunset=""
self.utc_sunset=""
self.moonrise=""
self.utc_moonrise=""
self.moonset=""
self.utc_moonset=""
self.sunstate=""
self.moonstate=""
self.t = threading.Thread(target=self.do_thread_loop)
self.t.start()
def do_thread_loop(self):
while(self.running):
if ( self.mqtt_connected ):
nowtime=datetime.datetime.now()
if(nowtime.year != self.year):
self.year = nowtime.year
self.mqttc.publish("/raw/clock/year", self.year, retain=True)
if(nowtime.month != self.month):
self.month = nowtime.month
self.mqttc.publish("/raw/clock/month", self.month, retain=True)
if(nowtime.day != self.day):
self.day = nowtime.day
self.mqttc.publish("/raw/clock/day", self.day, retain=True)
self.weekday=nowtime.weekday()
self.mqttc.publish("/raw/clock/weekday", self.weekday, retain=True)
self.utc_sunrise=self.observer.next_rising(self.sun)
# self.mqttc.publish("/raw/clock/utc_sunrise", str(self.utc_sunrise), retain=True)
self.sunrise=ephem.localtime(self.observer.next_rising(self.sun))
self.mqttc.publish("/raw/clock/sunrise", str(self.sunrise), retain=True)
self.utc_sunset=self.observer.next_setting(self.sun)
# self.mqttc.publish("/raw/clock/utc_sunset", str(self.utc_sunset), retain=True)
self.sunset=ephem.localtime(self.observer.next_setting(self.sun))
self.mqttc.publish("/raw/clock/sunset", str(self.sunset), retain=True)
self.utc_moonrise=self.observer.next_rising(self.moon)
# self.mqttc.publish("/raw/clock/utc_moonrise", str(self.utc_moonrise), retain=True)
self.moonrise=ephem.localtime(self.observer.next_rising(self.moon))
self.mqttc.publish("/raw/clock/moonrise", str(self.moonrise), retain=True)
self.utc_moonset=self.observer.next_setting(self.moon)
# self.mqttc.publish("/raw/clock/utc_moonset", str(self.utc_moonset), retain=True)
self.moonset=ephem.localtime(self.observer.next_setting(self.moon))
self.mqttc.publish("/raw/clock/moonset", str(self.moonset), retain=True)
self.nextnewmoon=ephem.next_new_moon(nowtime)
self.mqttc.publish("/raw/clock/nextnewmoon", str(self.nextnewmoon), retain=True)
self.nextfirstquartermoon=ephem.next_first_quarter_moon(nowtime)
self.mqttc.publish("/raw/clock/nextfirstquartermoon", str(self.nextfirstquartermoon), retain=True)
self.nextlastquartermoon=ephem.next_last_quarter_moon(nowtime)
self.mqttc.publish("/raw/clock/nextlastquartermoon", str(self.nextlastquartermoon), retain=True)
self.nextfullmoon=ephem.next_full_moon(nowtime)
self.mqttc.publish("/raw/clock/nextfullmoon", str(self.nextfullmoon), retain=True)
if(nowtime.hour != self.mil_hour):
self.mil_hour = nowtime.hour
self.hour = self.mil_hour % 12
if (self.hour == 0):
self.hour = 12
self.mqttc.publish("/raw/clock/hour", self.hour, retain=True)
self.mqttc.publish("/raw/clock/militaryhour", self.mil_hour, retain=True)
if(self.mil_hour > 11):
self.ampm = "PM"
else:
self.ampm = "AM"
if((self.mil_hour == 12) or (self.mil_hour == 0)):
self.mqttc.publish("/raw/clock/ampm", self.ampm, retain=True)
if(nowtime.minute != self.minute):
self.minute = nowtime.minute
self.mqttc.publish("/raw/clock/minute", self.minute, retain=True)
if(nowtime < self.sunrise): # is this right???
temp = "set"
elif(nowtime > self.sunset):
temp="set"
else:
temp="rise"
if(temp != self.sunstate):
self.sunstate=temp
self.mqttc.publish("/raw/clock/sunstate", self.sunstate, retain=True)
if ( self.interval ):
print "Waiting ", self.interval, " seconds for next update."
time.sleep(self.interval)
class MyDaemon(Daemon):
def run(self):
mqttcore = MyMQTTClientCore(APPNAME, clienttype="single")
mqttcore.main_loop()
if __name__ == "__main__":
daemon = MyDaemon('/tmp/' + APPNAME + '.pid')
main(daemon)