Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec7d0e4

Browse files
committed
JK: saved work
1 parent a672a2f commit ec7d0e4

10 files changed

+114
-57
lines changed

Advertiser/AdvertisingDevice.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(self, identifier: str):
2626
self._tracer = None
2727
self._identifier = identifier
2828

29+
2930
def SetAdvertiser(self, advertiser: Advertiser) -> Advertiser:
3031
"""
3132
set advertiser object
@@ -48,6 +49,7 @@ def SetAdvertiser(self, advertiser: Advertiser) -> Advertiser:
4849

4950
return advertiser
5051

52+
5153
def SetTracer(self, tracer: Tracer) -> Tracer:
5254
"""
5355
set tracer object
@@ -56,6 +58,7 @@ def SetTracer(self, tracer: Tracer) -> Tracer:
5658

5759
return tracer
5860

61+
5962
def Connect(self):
6063
"""
6164
connects the device to the advertiser
@@ -68,6 +71,7 @@ def Connect(self):
6871

6972
return
7073

74+
7175
def Disconnect(self):
7276
"""
7377
disconnects the device from the advertiser
@@ -80,18 +84,21 @@ def Disconnect(self):
8084

8185
return
8286

87+
8388
def Stop(self) -> bytes:
8489
"""
8590
stops the device
8691
"""
8792

8893
raise NotImplementedError # override this methode
8994

95+
9096
def AdvertisementSet(self, manufacturerId: bytes, rawdata: bytes):
9197
"""
9298
Set Advertisement data
9399
"""
94100
pass
95101

102+
96103
def GetAdvertisementIdentifier(self) -> str:
97104
return self._identifier

Advertiser/IAdvertisingDevice.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,30 @@ def GetAdvertisementIdentifier(self) -> str:
1919
"""
2020
raise NotImplementedError # override this methode
2121

22+
23+
def Connect(self):
24+
"""
25+
connects the device to the advertiser
26+
"""
27+
raise NotImplementedError # override this methode
28+
29+
30+
def Disconnect(self):
31+
"""
32+
disconnects the device from the advertiser
33+
"""
34+
raise NotImplementedError # override this methode
35+
36+
37+
def Stop(self) -> bytes:
38+
"""
39+
stops the device
40+
"""
41+
raise NotImplementedError # override this methode
42+
43+
44+
def SubDevice_SetChannel(self, channelId: int, value: float) -> bytes:
45+
"""
46+
set internal stored value of channel with channelId to value and return the telegram
47+
"""
48+
raise NotImplementedError # override this methode

MouldKing/MouldKingDevice.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class MouldKingDevice(AdvertisingDevice) :
1919

2020
ManufacturerID = bytes([0xFF, 0xF0])
2121

22+
2223
def __init__(self, identifier: str, numberOfChannels: int, channelStartOffset: int, channelEndOffset: int, telegram_connect: bytes, basetelegram: bytes):
2324
"""
2425
initializes the object and defines the fields
@@ -47,7 +48,8 @@ def __init__(self, identifier: str, numberOfChannels: int, channelStartOffset: i
4748

4849
return
4950

50-
def Register(self, ) -> bytes:
51+
52+
def Connect(self) -> None:
5153
"""
5254
returns the telegram to switch the MouldKing Hubs in bluetooth mode
5355
"""
@@ -57,33 +59,10 @@ def Register(self, ) -> bytes:
5759

5860
self._Advertise(self._Telegram_connect)
5961

60-
return self._Telegram_connect
61-
62-
def Unregister(self):
63-
"""
64-
disconnects the device from the advertiser
65-
"""
66-
67-
self.Stop()
68-
69-
super().Disconnect()
70-
7162
return
72-
7363

74-
def Connect(self) -> bytes:
75-
"""
76-
returns the telegram to switch the MouldKing Hubs in bluetooth mode
77-
"""
78-
79-
# call baseClass to register at Advertiser
80-
super().Connect()
81-
82-
self._Advertise(self._Telegram_connect)
8364

84-
return self._Telegram_connect
85-
86-
def Disconnect(self):
65+
def Disconnect(self) -> None:
8766
"""
8867
disconnects the device from the advertiser
8968
"""
@@ -94,6 +73,7 @@ def Disconnect(self):
9473

9574
return
9675

76+
9777
def Stop(self) -> bytes:
9878
"""
9979
set internal stored value of all channels to zero and return the telegram
@@ -105,7 +85,8 @@ def Stop(self) -> bytes:
10585

10686
return self.CreateTelegram()
10787

108-
def SetChannel(self, channelId: int, value: float) -> bytes:
88+
89+
def SubDevice_SetChannel(self, channelId: int, value: float) -> bytes:
10990
"""
11091
set internal stored value of channel with channelId to value and return the telegram
11192
"""
@@ -117,13 +98,15 @@ def SetChannel(self, channelId: int, value: float) -> bytes:
11798

11899
return self.CreateTelegram()
119100

101+
120102
def CreateTelegram(self) -> bytes:
121103
"""
122104
returns a telegram including the internal stored value from all channels
123105
"""
124106

125107
raise NotImplementedError # override this methode
126108

109+
127110
def _Advertise(self, rawdata: bytes) -> bytes:
128111
"""
129112
sends the data to the advertiser

MouldKing/MouldKingDeviceByte.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
sys.path.append("MouldKing")
77
from MouldKing.MouldKingDevice import MouldKingDevice
8-
from MouldKing.MouldKingCrypt import MouldKingCrypt
98

109
class MouldKingDeviceByte(MouldKingDevice) :
1110
"""
1211
baseclass handling with byte channels
1312
"""
1413

14+
1515
def __init__(self, identifier: str, numberOfChannels, channelStartOffset, channelEndOffset, telegram_connect, basetelegram):
1616
"""
1717
initializes the object and defines the fields
@@ -20,6 +20,7 @@ def __init__(self, identifier: str, numberOfChannels, channelStartOffset, channe
2020
# call baseclass init and set number of channels
2121
super().__init__(identifier, numberOfChannels, channelStartOffset, channelEndOffset, telegram_connect, basetelegram)
2222

23+
2324
def CreateTelegram(self) -> bytes:
2425
"""
2526
returns the telegram including the internal stored value from all channels

MouldKing/MouldKingDeviceNibble.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
sys.path.append("MouldKing")
77
from MouldKing.MouldKingDevice import MouldKingDevice
8-
from MouldKing.MouldKingCrypt import MouldKingCrypt
98

109
class MouldKingDeviceNibble(MouldKingDevice) :
1110
"""
1211
baseclass handling with nibble channels
1312
"""
1413

14+
1515
def __init__(self, identifier: str, numberOfChannels, channelStartOffset, channelEndOffset, telegram_connect, basetelegram):
1616
"""
1717
initializes the object and defines the fields

MouldKing/MouldKing_4_Hub.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class MouldKing_4_Hub(IAdvertisingDevice) :
1818
# static fields/constants
1919
_MouldKing_4_Hubs = MouldKing_4_Hubs()
2020

21+
2122
def __init__(self, deviceId: int):
2223
"""
2324
initializes the object and defines the fields
@@ -27,10 +28,9 @@ def __init__(self, deviceId: int):
2728
raise Exception('only deviceId 0..2 are allowed')
2829

2930
self._deviceId = deviceId
31+
self._NumberOfChannels = 3
3032
self._tracer = None
3133

32-
# call baseclass init and set number of channels
33-
#super().__init__(None, 4, None, None, None, None)
3434

3535
def SetTracer(self, tracer: Tracer) -> Tracer:
3636
"""
@@ -41,36 +41,39 @@ def SetTracer(self, tracer: Tracer) -> Tracer:
4141
return tracer
4242

4343

44-
45-
def Connect(self) -> bytes:
44+
def Connect(self) -> None:
4645
"""
4746
returns the telegram to switch the MouldKing Hubs in bluetooth mode
4847
"""
48+
MouldKing_4_Hub._MouldKing_4_Hubs.SubDevice_Register(self)
49+
50+
return
4951

50-
return MouldKing_4_Hub._MouldKing_4_Hubs.Connect()
5152

52-
def Disconnect(self) -> bytes:
53+
def Disconnect(self) -> None:
5354
"""
5455
disconnects the device from the advertiser
5556
"""
56-
MouldKing_4_Hub._MouldKing_4_Hubs.Disconnect()
57+
MouldKing_4_Hub._MouldKing_4_Hubs.SubDevice_Unregister(self)
5758

5859
return
5960

61+
6062
def Stop(self) -> bytes:
6163
"""
6264
set internal stored value of all channels to zero and return the telegram
6365
"""
6466

65-
return MouldKing_4_Hub._MouldKing_4_Hubs.Stop(self._deviceId, self._NumberOfChannels)
67+
return MouldKing_4_Hub._MouldKing_4_Hubs.SubDevice_Stop(self._deviceId, self._NumberOfChannels)
68+
6669

67-
def SetChannel(self, channelId: int, value: float) -> bytes:
70+
def SubDevice_SetChannel(self, channelId: int, value: float) -> bytes:
6871
"""
6972
set internal stored value of channel with channelId to value and return the telegram
7073
"""
7174

7275
if channelId > self._NumberOfChannels - 1:
7376
raise Exception("only channelId 0.." + int(self._NumberOfChannels - 1) + "are allowed")
7477

75-
return MouldKing_4_Hub._MouldKing_4_Hubs.SetChannel(self._deviceId, self._NumberOfChannels, channelId, value)
78+
return MouldKing_4_Hub._MouldKing_4_Hubs.SubDevice_SetChannel(self._deviceId, self._NumberOfChannels, channelId, value)
7679

MouldKing/MouldKing_4_Hubs.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
import sys
55

6+
sys.path.append("Advertiser")
7+
from Advertiser.IAdvertisingDevice import IAdvertisingDevice
8+
69
sys.path.append("MouldKing")
710
from MouldKing.MouldKingDeviceNibble import MouldKingDeviceNibble
811

@@ -17,16 +20,48 @@ class handling 3 x MouldKing 4.0 Module
1720

1821
__telegram_base = bytes([0x7D, 0x7B, 0xA7, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x82]) # byte array for base Telegram
1922

23+
2024
def __init__(self):
2125
"""
2226
initializes the object and defines the fields
2327
"""
24-
2528
# call baseclass init and set number of channels
2629
super().__init__("MK4", 12, 3, 1, MouldKing_4_Hubs.__telegram_connect, MouldKing_4_Hubs.__telegram_base)
2730

31+
self._connectedSubDevices = list()
32+
33+
34+
def SubDevice_Register(self, subDevice: IAdvertisingDevice) -> None:
35+
"""
36+
returns the telegram to switch the MouldKing Hubs in bluetooth mode
37+
"""
38+
connectedSubDevicesLen = len(self._connectedSubDevices)
39+
40+
if(not subDevice is None and not subDevice in self._connectedSubDevices):
41+
self._connectedSubDevices.append(subDevice)
42+
43+
# first subDevice was added
44+
if(connectedSubDevicesLen == 0):
45+
self.Connect()
46+
47+
return
48+
49+
50+
def SubDevice_Unregister(self, subDevice: IAdvertisingDevice) -> None:
51+
"""
52+
disconnects the device from the advertiser
53+
"""
54+
if(not subDevice is None and subDevice in self._connectedSubDevices):
55+
self._connectedSubDevices.remove(subDevice)
56+
57+
# last subDevice was removed
58+
if(len(self._connectedSubDevices) == 0):
59+
self.Disconnect()
60+
61+
return
62+
2863

29-
def Stop(self, hubDeviceId: int, hubNumberOfChannels: int) -> bytes:
64+
def SubDevice_Stop(self, hubDeviceId: int, hubNumberOfChannels: int) -> bytes:
3065
"""
3166
set internal stored value of all channels to zero and return the telegram
3267
"""
@@ -46,7 +81,7 @@ def Stop(self, hubDeviceId: int, hubNumberOfChannels: int) -> bytes:
4681
return self.CreateTelegram()
4782

4883

49-
def SetChannel(self, hubDeviceId: int, hubNumberOfChannels: int, hubChannelId: int, value: float) -> bytes:
84+
def SubDevice_SetChannel(self, hubDeviceId: int, hubNumberOfChannels: int, hubChannelId: int, value: float) -> bytes:
5085
"""
5186
set internal stored value of channel with channelId to value and return the telegram
5287
"""
@@ -59,9 +94,9 @@ def SetChannel(self, hubDeviceId: int, hubNumberOfChannels: int, hubChannelId: i
5994
# -> channelId 9..12
6095
channelIdHubs = hubDeviceId * hubNumberOfChannels + hubChannelId
6196

62-
if hubChannelId > self._NumberOfChannels - 1:
97+
if channelIdHubs > self._NumberOfChannels - 1:
6398
raise Exception("only channelId 0.." + int(self._NumberOfChannels - 1) + "are allowed")
6499

65-
self._ChannelValueList[hubChannelId] = value
100+
self._ChannelValueList[channelIdHubs] = value
66101

67102
return self.CreateTelegram()

MouldKing/MouldKing_6_Hub.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class handling the MouldKing 6.0 Module
1919
__telegram_base_device_b = bytes([0x62, 0x7B, 0xA7, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x9D]) # byte array for base Telegram
2020
__telegram_base_device_c = bytes([0x63, 0x7B, 0xA7, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x9C]) # byte array for base Telegram
2121

22+
2223
def __init__(self, deviceId: int):
2324
"""
2425
initializes the object and defines the fields

consoletest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def mkstop(deviceId: int=0):
132132

133133
def mkcontrol(deviceId: int=0, channel: int=0, powerAndDirection: float=0):
134134
hub = _getHubId(deviceId)
135-
rawdata = hub.SetChannel(channel, powerAndDirection)
135+
rawdata = hub.SubDevice_SetChannel(channel, powerAndDirection)
136136
return
137137

138138
def test_hub(hubId: int=0):

0 commit comments

Comments
 (0)