forked from xoseperez/mqtt2cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt2thingspeak.py
76 lines (63 loc) · 2.49 KB
/
mqtt2thingspeak.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
#! /usr/bin/python
# -*- coding: utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
# MQTT to Cloud
# Copyright (C) 2013 by Xose Pérez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
__app__ = "MQTT to Cloud"
__author__ = "Xose Pérez"
__contact__ = "xose.perez@gmail.com"
__copyright__ = "Copyright (C) 2013 Xose Pérez"
__license__ = 'GPL v3'
import sys
from libs.Config import Config
from libs.Mosquitto import Mosquitto
from libs.Manager import Manager
from libs.services.Thingspeak import Thingspeak
if __name__ == "__main__":
config = Config('config/mqtt2thingspeak.yaml')
manager = Manager(config.get('daemon', 'pidfile', '/tmp/mqtt2thingspeak.pid'))
manager.stdout = config.get('daemon', 'stdout', '/dev/null')
manager.stderr = config.get('daemon', 'stderr', '/dev/null')
manager.debug = config.get('daemon', 'debug', False)
mqtt = Mosquitto(config.get('mqtt', 'client_id'))
mqtt.host = config.get('mqtt', 'host')
mqtt.port = config.get('mqtt', 'port')
mqtt.keepalive = config.get('mqtt', 'keepalive')
mqtt.clean_session = config.get('mqtt', 'clean_session')
mqtt.qos = config.get('mqtt', 'qos', 0)
mqtt.retain = config.get('mqtt', 'retain', True)
mqtt.status_topic = config.get('mqtt', 'status_topic')
mqtt.set_will = config.get('mqtt', 'set_will')
manager.mqtt = mqtt
thingspeak = Thingspeak(
config.get('thingspeak', 'channels'),
)
manager.service = thingspeak
manager.load_topics(config.get('topics', default=[]))
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
manager.start()
elif 'stop' == sys.argv[1]:
manager.stop()
elif 'restart' == sys.argv[1]:
manager.restart()
else:
print "Unknown command"
sys.exit(2)
sys.exit(0)
else:
print "usage: %s start|stop|restart" % sys.argv[0]
sys.exit(2)