forked from owagner/modbus2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 28
/
create-openhab-conf.py
169 lines (153 loc) · 6.56 KB
/
create-openhab-conf.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
#!/usr/bin/env python3
import argparse
import csv
class Thing:
def __init__(self,pollerTopic,adr):
self.pollerTopic=pollerTopic
self.channels=[]
self.adr=adr
def addChannel(self,chan):
self.channels.append(chan)
class Channel:
def __init__(self,thing,refname,chantype,rw,interpretation):
self.refname=refname
self.map=None
self.commandTopic=None
self.thing=thing
self.chantype=None
if chantype == "register" and "r" in rw and not "w" in rw:
self.chantype = "number"
self.map=""
elif chantype == "register" and "w" in rw:
self.chantype = "number"
self.map=""
self.commandTopic=globaltopic+"/"+self.thing.pollerTopic+"/set/"+self.refname
elif chantype == "coil" and "w" in rw:
self.chantype = "switch"
self.commandTopic=globaltopic+"/"+self.thing.pollerTopic+"/set/"+self.refname
self.map=", on=\"True\", off=\"False\""
elif chantype == "coil" and not "w" in rw:
self.chantype = "contact"
self.map=", on=\"True\", off=\"False\""
elif chantype == "input_status" and "r" in rw:
self.chantype = "contact"
self.map=", on=\"True\", off=\"False\""
self.rw=rw
self.stateTopic=globaltopic+"/"+self.thing.pollerTopic+"/state/"+self.refname
thinglist=[]
def getconf(x):
if len(x.channels)<1:
return ""
outstring=""
outstring+="Thing mqtt:topic:"+x.pollerTopic+" \""+x.pollerTopic+"\" ("+brokerstring+") {\n"
outstring+=" Channels:\n"
outstring+=" Type contact : "+x.pollerTopic+"_connected [ stateTopic=\"" + globaltopic+"/"+x.pollerTopic+"/connected\", on=\"True\", off=\"False\" ]\n"
for y in x.channels:
outstring+=" Type "+y.chantype+" : "+y.refname+" [ stateTopic=\""+y.stateTopic+"\""
if y.commandTopic:
outstring+=", commandTopic=\""+y.commandTopic+"\""
outstring+=y.map
outstring+=" ]\n"
outstring+="}\n\n"
return outstring
def getitemconf(x):
if len(x.channels)<1:
return ""
outstring=""
outstring+="Contact "+x.pollerTopic+"_"+x.pollerTopic+"_connected"+" \""+x.pollerTopic+"_"+x.pollerTopic+"_connected\" "
outstring+="{ channel=\"mqtt:topic:"+x.pollerTopic+":"+x.pollerTopic+"_connected\" }\n"
for y in x.channels:
outstring+=y.chantype.capitalize()+" "+x.pollerTopic+"_"+y.refname+" \""+x.pollerTopic+"_"+y.refname+"\" "
outstring+="{ channel=\"mqtt:topic:"+x.pollerTopic+":"+y.refname+"\" }"
outstring+="\n"
return outstring
parser = argparse.ArgumentParser(description='Bridge between ModBus and MQTT')
parser.add_argument('globaltopic', help='from argument of modbus2mqtt.py, usually modbus')
parser.add_argument('brokerstring', help='Broker string for ex. mqtt:broker:2c8f40c6')
parser.add_argument('config', help='Configuration file. Required!')
args=parser.parse_args()
globaltopic=args.globaltopic
brokerstring=args.brokerstring
verbosity = 10
with open(args.config,"r") as csvfile:
csvfile.seek(0)
reader=csv.DictReader(csvfile)
currentPoller=None
pollerTopic=None
pollerType=None
for row in reader:
if row["type"]=="poller" or row["type"]=="poll":
rate = float(row["col6"])
slaveid = int(row["col2"])
reference = int(row["col3"])
size = int(row["col4"])
if row["col5"] == "holding_register":
functioncode = 3
dataType="int16"
if size>123: #applies to TCP, RTU should support 125 registers. But let's be safe.
currentPoller=None
if verbosity>=1:
print("Too many registers (max. 123). Ignoring poller "+row["topic"]+".")
continue
elif row["col5"] == "coil":
functioncode = 1
dataType="bool"
if size>2000:
currentPoller=None
if verbosity>=1:
print("Too many coils (max. 2000). Ignoring poller "+row["topic"]+".")
continue
elif row["col5"] == "input_register":
functioncode = 4
dataType="int16"
if size>123:
currentPoller=None
if verbosity>=1:
print("Too many registers (max. 123). Ignoring poller "+row["topic"]+".")
continue
elif row["col5"] == "input_status":
functioncode = 2
dataType="bool"
if size>2000:
currentPoller=None
if verbosity>=1:
print("Too many inputs (max. 2000). Ignoring poller "+row["topic"]+".")
continue
else:
print("Unknown function code ("+row["col5"]+" ignoring poller "+row["topic"]+".")
currentPoller=None
continue
pollerType = row["col5"]
pollerTopic = row["topic"]
currentPoller = None
for x in thinglist:
if x.pollerTopic == pollerTopic:
currentPoller = x
if currentPoller is None:
currentPoller = Thing(pollerTopic,slaveid)
thinglist.append(currentPoller)
continue
elif row["type"]=="reference" or row["type"]=="ref":
if currentPoller is not None:
if pollerType == "coil":
chan=Channel(currentPoller,row["topic"],"coil",row["col3"],"")
currentPoller.channels.append(chan)
elif pollerType == "holding_register":
chan=Channel(currentPoller,row["topic"],"register",row["col3"],"")
currentPoller.channels.append(chan)
elif pollerType == "input_register":
chan=Channel(currentPoller,row["topic"],"register","r","")
currentPoller.channels.append(chan)
elif pollerType == "input_status":
chan=Channel(currentPoller,row["topic"],"input_status","r","")
currentPoller.channels.append(chan)
else:
print("No poller for reference "+row["topic"]+".")
outstring = ""
for x in thinglist:
outfile = open(x.pollerTopic+".things","w+")
outfile.write(getconf(x))
outfile.close()
outfile = open(x.pollerTopic+".items","w+")
outfile.write(getitemconf(x))
outfile.close()