-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOmnikMQTT.py
73 lines (55 loc) · 2.43 KB
/
OmnikMQTT.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
#!/usr/bin/python
import InverterMsg # Import the Msg handler
import socket # Needed for talking to inverter
import datetime # Used for timestamp
import time # Used for sleep function
import sys
import signal
import logging
import ConfigParser, os
import paho.mqtt.publish as publish
import sniffer
def MQTTcallback(pkt):
logger.debug("Callback")
msg.setMsg(pkt)
now = datetime.datetime.now()
logger.debug("ID: {0}".format(msg.getID()))
if mqtt_enabled:
d=msg.getDict()
msgs=list()
for key in d:
msgs.append((mqtt_topic+"/"+key,d[key],0,False))
logger.debug("Publishing packet data to MQTT broker")
publish.multiple(msgs,hostname=mqtt_host,port=mqtt_port)
msg.clear()
if __name__ == "__main__":
mydir = os.path.dirname(os.path.abspath(__file__))
config = ConfigParser.RawConfigParser()
# Load the setting file
config.read([mydir + '/config-org.cfg', mydir + '/config.cfg'])
ip = config.get("inverter","ip")
sniff_iface = config.get("inverter",'sniff_iface')
log_filename = mydir + '/' + config.get('log','log_filename')
mqtt_enabled = config.getboolean('mqtt','mqtt_enabled')
mqtt_host = config.get('mqtt','mqtt_host')
mqtt_port = config.get('mqtt','mqtt_port')
mqtt_topic = config.get('mqtt','mqtt_topic')
logger = logging.getLogger(__name__)
hdlr = logging.FileHandler(log_filename)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)
msg = InverterMsg.InverterMsg()
logging.getLogger("scapy.runtime").setLevel(logging.ERROR) # Suppress warning messages from Scapy
if mqtt_enabled:
logger.debug('publishing to MQTT: %s : %s' % (mqtt_topic,"OmnikExport running"))
publish.single(mqtt_topic,"OmnikExport running",hostname=mqtt_host,port=mqtt_port)
logger.info('sniffing data with filter %s on %s. Callback function = %s' % ("tcp and host "+ip,sniff_iface,MQTTcallback))
sn=sniffer.sniffer(sniff_iface,"tcp and host "+ip,minInterval=10,minPacketLength=120,callback=MQTTcallback)
sn.daemon=True
sn.start()
while True:
time.sleep(5)
if mqtt_enabled:
publish.single(mqtt_topic,"OmnikExport stopping",hostname=mqtt_host,port=mqtt_port)