summaryrefslogtreecommitdiff
path: root/ebustest.py
blob: d6f84fdcb9c8225141c9f4f642d761eff5b4f4ff (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding:utf8 -*-
import asynchat
import asyncore
import socket
import sys
#import json
# Master-Master
# 1 quell
# 1 ziel
# 1 1primaerbefehl
# 1 sekundaerbefehl
# 1 n=laenge (bytes)
# n daten....
# 1 CRC
# 1 ack (=0x00 || 0xff)
# 1 syn (=0xaa)

# Master-Slave
# 1 quell
# 1 ziel
# 1 1primaerbefehl
# 1 sekundaerbefehl
# 1 n=laenge (bytes)
# n daten vom master/Anfrage....
# 1 CRC
# 1 ack (=0x00 || 0xff)
# 1 m=länge (bytes)
# m daten vom slave/Antwort...
# 1 syn (=0xaa)
deviceDescription = [
    {'address':0x03, 'type':'master', 'description':'Feuerungsautomat'},
    {'address':0x10, 'type':'master', 'description':'Heizungsregler #2'},
    {'address':0x30, 'type':'master', 'description':'Heizkreisregler 1'},
    {'address':0x70, 'type':'master', 'description':'Heizkreisregler 2'},
    {'address':0x71, 'type':'master', 'description':'Heizungsregler #9'},
    {'address':0xf1, 'type':'master', 'description':'Heizungsregler #10'},
    {'address':0x50, 'type':'slave', 'description':'Mischer 1'},
    {'address':0x51, 'type':'slave', 'description':'Mischer 2'},
    {'address':0x90, 'type':'slave', 'description':'Raumgeräte/Fernsteller 1'},
    {'address':0x90, 'type':'slave', 'description':'Raumgeräte/Fernsteller 2'},
    {'address':0xfe, 'type':'broadcast', 'description':'Broadcast'},
]

packetDescription = [
     # Service 0x05 (Brennersteuerbefehle)
     {'primary':0x5,  'secondary':0x3, 'name':'Betriebsdaten des Feuerungsautomaten an den Regler Block1'},
     {'primary':0x5,  'secondary':0x7, 'name':'Betriebsdaten des Reglers an den Feuerungsautomaten'},
     # Service 0x07 (Systemdatenbefehle)
     {'primary':0x7,  'secondary':0x0, 'name':'Datum/Zeit - Meldung eines eBUS Masters'},
     {'primary':0x7,  'secondary':0x4, 'name':'Identifikation'},
     # Service 0x08 (Reglerbefehle)
     {'primary':0x8,  'secondary':0x0, 'name':'Sollwertübertragung des Reglers an andere Regler'},

     #  Response
     #p[0] = Einheit (1=>Liter, 2=>Kubik)
     #p[1] = 10^0
     #p[2] = 10^2
     #p[3] = 10^4
     #p[4] = 10^6
     {'primary':0x3,  'secondary':0x8, 'name':'Gesamtbrennstoffmengenzähle lesen'},
     {'primary':''},
]

def formatHex(data):
    return " ".join(map(lambda byte: "%.2x"%ord(byte), data))

def getDsc(address):
    dev=filter(lambda dev: dev['address'] == address, deviceDescription)
    if len(dev)>0:
        return dev[0]['description']
    else:
        return None

class EbusPacket(object):
    def __init__(self, source, destination, primary_command, secondary_command):
        self.source = source
        self.destination = destination
        self.primary_command = primary_command
        self.secondary_command = secondary_command

    def name(self):
        desc = filter(lambda p: p['primary'] == self.primary_command \
            and p['secondary'] == self.secondary_command, packetDescription)
        if len(desc) > 0:
	    return desc[0]['name']
        else:
            return "UNKNOWN"

    def __str__(self):
        return "<%-18s name=\"%-15s\" source=\"%s\" destination=\"%s\" primary=0x%x secondary=0x%x length=0x%x>" % \
            (self.__class__.__name__, self.name(), getDsc(self.source), getDsc(self.destination), \
             self.primary_command, self.secondary_command, self.length)

class EbusMasterMaster(EbusPacket):
    def __init__(self, source, destination, primary_command, secondary_command, data):
        EbusPacket.__init__(self, source, destination, primary_command, secondary_command)
	self.length = len(data)
	self.data = data



class EbusMasterSlave(EbusPacket):
    def __init__(self, source, destination, primary_command, secondary_command, request, response):
        EbusPacket.__init__(self, source, destination, primary_command, secondary_command)
        self.length = len(request)
	self.request = request
        self.response = response

class EbusBroadcast(EbusPacket):
     def __init__(self, source, destination, primary_command, secondary_command, data):
        EbusPacket.__init__(self, source, destination, primary_command, secondary_command)
        self.length = len(data)
	self.data = data

   

class Fetcher(asynchat.async_chat):
    def __init__(self):
        self.buffer = ""

        asynchat.async_chat.__init__(self)
        self.set_terminator("")
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect(("10.2.2.200", 7970))

        self.packetIndex = 0
        self.buf = ""

    def collect_incoming_data(self,data):
        for it in range(len(data)):
            if data[it] == "\xaa":
                if it+1 < len(data) and data[it+1] != "\xaa":
                    self._parse(self.buf)
                    self.buf = ""
                    self.packetIndex = 0
            else:
                self.buf += data[it]
                self.packetIndex = self.packetIndex + 1
                #print "%.2x [%d]" % (ord(data[it]),self.packetIndex)

    def _parse(self,data):
        if len(data) < 2:
            print "GAGA"
            return
        source = ord(data[0])
        destination = ord(data[1])

        sourceDevice = filter(lambda dev: dev['address'] == source, deviceDescription)
        destinationDevice = filter(lambda dev: dev['address'] == destination, deviceDescription)
        if len(sourceDevice) == 0 or len(destinationDevice) == 0:
            print "Unbekanntes Paket: source=%x destination=%x" % (source, destination)
            return

        if len(data) < 9:
            print "Unvollständige Daten"
            return

        primaryCommand = ord(data[2])
        secondaryCommand = ord(data[3])
        payloadLength = ord(data[4])
        payload = data[5:6+payloadLength]
	#delete 0x50 packets for devel
	if (primaryCommand == ord("\x50")):
		return
	#print "PR SC NN D0 D1 D2 D3 D4 D5 D6 D7 ..."
	#print "%.2x" % (primaryCommand,)
	#print "%.2x %.2x %.2x %s" % (primaryCommand,secondaryCommand,payloadLength,"bla")
	#print "%.2x %.2x %.2x %s" % (primaryCommand,secondaryCommand,payloadLength,formatHex(payload))

        p = None
        if sourceDevice[0]['type'] == 'master' and destinationDevice[0]['type'] == 'master':
            p = EbusMasterMaster(source, destination, primaryCommand, secondaryCommand, payload)
            print p
            print "payload=%s" % formatHex(payload)
        elif sourceDevice[0]['type'] == 'master' and destinationDevice[0]['type'] == 'slave':
            p = EbusMasterSlave(source, destination, primaryCommand, secondaryCommand, payload, None)
            print p
	    print "payload=%s" % formatHex(payload)
        elif sourceDevice[0]['type'] == 'master' and destinationDevice[0]['type'] == 'broadcast':
            p = EbusBroadcast(source, destination, primaryCommand, secondaryCommand, payload)
            print p
            print "payload=%s" % formatHex(payload)
        else:
            print "KOMISCHES ZEUG"
            return
	print ""

Fetcher()
asyncore.loop()