initial
This commit is contained in:
commit
53e75d98c1
17
.project
Normal file
17
.project
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>mqttClock</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.python.pydev.PyDevBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.python.pydev.pythonNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
8
.pydevproject
Normal file
8
.pydevproject
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<?eclipse-pydev version="1.0"?><pydev_project>
|
||||
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
|
||||
<path>/${PROJECT_DIR_NAME}</path>
|
||||
</pydev_pathproperty>
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
|
||||
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
|
||||
</pydev_project>
|
31
logger.py
Normal file
31
logger.py
Normal file
@ -0,0 +1,31 @@
|
||||
from time import gmtime, strftime
|
||||
|
||||
class Logger(object):
|
||||
debugFlag = False
|
||||
|
||||
@classmethod
|
||||
def log(cls, data):
|
||||
t = strftime("%d %b %Y %H:%M:%S", gmtime())
|
||||
with open(cls.logfile, 'a') as f:
|
||||
f.write("%s %s\n" % (t, data))
|
||||
if cls.debugFlag:
|
||||
print data
|
||||
|
||||
@classmethod
|
||||
def debug(cls, data):
|
||||
if cls.debugFlag:
|
||||
cls.log(data)
|
||||
|
||||
@classmethod
|
||||
def debugEnable(cls):
|
||||
cls.debugFlag = True
|
||||
|
||||
@classmethod
|
||||
def debugDisable(cls):
|
||||
cls.debugFlag = False
|
||||
|
||||
@classmethod
|
||||
def openlog(cls, logfile):
|
||||
cls.logfile = logfile
|
||||
|
||||
|
52
mqttClock.py
Normal file
52
mqttClock.py
Normal file
@ -0,0 +1,52 @@
|
||||
import os
|
||||
import sys
|
||||
from logger import Logger
|
||||
import paho.mqtt.client as mqtt
|
||||
import time
|
||||
import datetime
|
||||
|
||||
|
||||
DEBUG = True
|
||||
BACKGROUND = False
|
||||
PID_FILE = "/tmp/mqttClock.pid"
|
||||
LOG_FILE = "/tmp/mqttClock.log"
|
||||
BROKER = "172.16.2.15"
|
||||
|
||||
|
||||
if BACKGROUND:
|
||||
pid = os.fork()
|
||||
else:
|
||||
pid = 0
|
||||
|
||||
if pid:
|
||||
pidFile = file(PID_FILE , mode='w')
|
||||
pidFile.write("%i\n" % pid)
|
||||
pidFile.close()
|
||||
sys.exit(0)
|
||||
|
||||
Logger.openlog(LOG_FILE)
|
||||
|
||||
if DEBUG:
|
||||
Logger.debugEnable()
|
||||
|
||||
Logger.log("mqttClock started")
|
||||
|
||||
|
||||
|
||||
|
||||
mqttClient = mqtt.Client()
|
||||
mqttClient.connect(BROKER, 1883, 60)
|
||||
mqttClient.loop_start()
|
||||
|
||||
|
||||
try:
|
||||
while True:
|
||||
time.sleep(5)
|
||||
now = datetime.datetime.now()
|
||||
secondsSinceMidnight = now.second + now.minute * 60 + now.hour * 60 * 60
|
||||
mqttClient.publish('IoT/SecondsSinceMidnight', str(secondsSinceMidnight))
|
||||
finally:
|
||||
Logger.log("mqttClock terminating")
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user