gravity-controller-operator 2.2.4__py3-none-any.whl → 2.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,28 +1,29 @@
1
- from gravity_controller_operator.controllers.emulator_contr import EmulatorController
2
- from gravity_controller_operator.controllers.netping_relay import NetPing2Controller
3
- from gravity_controller_operator.controllers.arm_k210 import ARMK210Controller
4
- from gravity_controller_operator.controllers.wb_mr6lv import WBMR6LV
5
- from gravity_controller_operator.exceptions import UnknownController
6
- from gravity_controller_operator.controllers.moxa import MoxaE1214
7
- from gravity_controller_operator.controllers.sigur import Sigur
8
-
9
-
10
- AVAILABLE_CONTROLLERS = [
11
- ARMK210Controller,
12
- WBMR6LV,
13
- NetPing2Controller,
14
- EmulatorController,
15
- Sigur,
16
- MoxaE1214
17
- ]
18
-
19
-
20
- class ControllerCreator:
21
- @staticmethod
22
- def get_controller(model, emulator=False, *args, **kwargs):
23
- for contr in AVAILABLE_CONTROLLERS:
24
- if emulator:
25
- return EmulatorController(*args, **kwargs)
26
- if contr.model.lower() == model.lower():
27
- return contr(*args, **kwargs)
28
- raise UnknownController(model, [contr.model for contr in AVAILABLE_CONTROLLERS])
1
+ from gravity_controller_operator.controllers.emulator_contr import EmulatorController
2
+ from gravity_controller_operator.controllers.netping_relay import NetPing2Controller
3
+ from gravity_controller_operator.controllers.arm_k210 import ARMK210Controller
4
+ from gravity_controller_operator.controllers.wb_mr6lv import WBMR6LV, WBMR6LVTCP
5
+ from gravity_controller_operator.exceptions import UnknownController
6
+ from gravity_controller_operator.controllers.moxa import MoxaE1214
7
+ from gravity_controller_operator.controllers.sigur import Sigur
8
+
9
+
10
+ AVAILABLE_CONTROLLERS = [
11
+ ARMK210Controller,
12
+ WBMR6LV,
13
+ WBMR6LVTCP,
14
+ NetPing2Controller,
15
+ EmulatorController,
16
+ Sigur,
17
+ MoxaE1214
18
+ ]
19
+
20
+
21
+ class ControllerCreator:
22
+ @staticmethod
23
+ def get_controller(model, emulator=False, *args, **kwargs):
24
+ for contr in AVAILABLE_CONTROLLERS:
25
+ if emulator:
26
+ return EmulatorController(*args, **kwargs)
27
+ if contr.model.lower() == model.lower():
28
+ return contr(*args, **kwargs)
29
+ raise UnknownController(model, [contr.model for contr in AVAILABLE_CONTROLLERS])
@@ -1,54 +1,54 @@
1
- from gravity_controller_operator.controllers_super import DIInterface, RelayInterface, ControllerInterface
2
- from pyModbusTCP.client import ModbusClient
3
- import time
4
-
5
- class ARMK210ControllerDI(DIInterface):
6
- map_keys_amount = 8
7
- starts_with = 0
8
-
9
- def __init__(self, client):
10
- self.client = client
11
- super().__init__()
12
-
13
- def get_phys_dict(self):
14
- for _ in range(5):
15
- response = self.client.read_input_registers(self.starts_with, self.map_keys_amount)
16
- if response:
17
- return {i: val for i, val in enumerate(response)}
18
- time.sleep(0.1) # Не грузим CPU и даём контроллеру время
19
- return {"error": "No response from controller"}
20
-
21
-
22
- class ARMK210ControllerRelay(RelayInterface):
23
- map_keys_amount = 8
24
- starts_with = 0
25
-
26
- def __init__(self, client):
27
- self.client = client
28
- super().__init__()
29
-
30
- def get_phys_dict(self):
31
- for _ in range(5):
32
- response = self.client.read_holding_registers(self.starts_with, self.map_keys_amount)
33
- if response:
34
- return {i: val for i, val in enumerate(response)}
35
- time.sleep(0.1) # Не грузим CPU и даём контроллеру время
36
- return {"error": "No response from controller"}
37
-
38
- def change_phys_relay_state(self, addr, state: bool):
39
- for _ in range(5):
40
- result = self.client.write_single_coil(addr, state)
41
- if result:
42
- return
43
- time.sleep(0.1)
44
- raise Exception("Failed to change relay state after 5 tries")
45
-
46
-
47
- class ARMK210Controller:
48
- model = "arm_k210"
49
-
50
- def __init__(self, ip: str, port: int = 8234, name="ARM_K210_Controller", *args, **kwargs):
51
- client = ModbusClient(host=ip, port=port)
52
- di = ARMK210ControllerDI(client)
53
- relay = ARMK210ControllerRelay(client)
54
- self.interface = ControllerInterface(di_interface=di, relay_interface=relay)
1
+ from gravity_controller_operator.controllers_super import DIInterface, RelayInterface, ControllerInterface
2
+ from pyModbusTCP.client import ModbusClient
3
+ import time
4
+
5
+ class ARMK210ControllerDI(DIInterface):
6
+ map_keys_amount = 8
7
+ starts_with = 0
8
+
9
+ def __init__(self, client):
10
+ self.client = client
11
+ super().__init__()
12
+
13
+ def get_phys_dict(self):
14
+ for _ in range(5):
15
+ response = self.client.read_input_registers(self.starts_with, self.map_keys_amount)
16
+ if response:
17
+ return {i: val for i, val in enumerate(response)}
18
+ time.sleep(0.1) # Не грузим CPU и даём контроллеру время
19
+ return {"error": "No response from controller"}
20
+
21
+
22
+ class ARMK210ControllerRelay(RelayInterface):
23
+ map_keys_amount = 8
24
+ starts_with = 0
25
+
26
+ def __init__(self, client):
27
+ self.client = client
28
+ super().__init__()
29
+
30
+ def get_phys_dict(self):
31
+ for _ in range(5):
32
+ response = self.client.read_holding_registers(self.starts_with, self.map_keys_amount)
33
+ if response:
34
+ return {i: val for i, val in enumerate(response)}
35
+ time.sleep(0.1) # Не грузим CPU и даём контроллеру время
36
+ return {"error": "No response from controller"}
37
+
38
+ def change_phys_relay_state(self, addr, state: bool):
39
+ for _ in range(5):
40
+ result = self.client.write_single_coil(addr, state)
41
+ if result:
42
+ return
43
+ time.sleep(0.1)
44
+ raise Exception("Failed to change relay state after 5 tries")
45
+
46
+
47
+ class ARMK210Controller:
48
+ model = "arm_k210"
49
+
50
+ def __init__(self, ip: str, port: int = 8234, name="ARM_K210_Controller", *args, **kwargs):
51
+ client = ModbusClient(host=ip, port=port)
52
+ di = ARMK210ControllerDI(client)
53
+ relay = ARMK210ControllerRelay(client)
54
+ self.interface = ControllerInterface(di_interface=di, relay_interface=relay)
@@ -1,37 +1,37 @@
1
- from gravity_controller_operator.controllers_super import DIInterface, \
2
- RelayInterface, ControllerInterface
3
-
4
-
5
- class EmulatorDI(DIInterface):
6
- map_keys_amount = 4
7
- starts_with = 1
8
-
9
- def __init__(self):
10
- super().__init__()
11
-
12
- def get_phys_dict(self):
13
- return {1: 0, 2: 0, 3: 0, 4: 0}
14
-
15
-
16
- class EmulatorRelay(RelayInterface):
17
- map_keys_amount = 4
18
- starts_with = 1
19
-
20
- def __init__(self):
21
- super().__init__()
22
-
23
- def get_phys_dict(self):
24
- return {1: 0, 2: 0, 3: 0, 4: 0}
25
-
26
- def change_phys_relay_state(self, addr, state: bool):
27
- # Ничего не делает, просто имитация
28
- pass
29
-
30
-
31
- class EmulatorController:
32
- model = "emulator_controller"
33
-
34
- def __init__(self, *args, **kwargs):
35
- di = EmulatorDI()
36
- relay = EmulatorRelay()
1
+ from gravity_controller_operator.controllers_super import DIInterface, \
2
+ RelayInterface, ControllerInterface
3
+
4
+
5
+ class EmulatorDI(DIInterface):
6
+ map_keys_amount = 4
7
+ starts_with = 1
8
+
9
+ def __init__(self):
10
+ super().__init__()
11
+
12
+ def get_phys_dict(self):
13
+ return {1: 0, 2: 0, 3: 0, 4: 0}
14
+
15
+
16
+ class EmulatorRelay(RelayInterface):
17
+ map_keys_amount = 4
18
+ starts_with = 1
19
+
20
+ def __init__(self):
21
+ super().__init__()
22
+
23
+ def get_phys_dict(self):
24
+ return {1: 0, 2: 0, 3: 0, 4: 0}
25
+
26
+ def change_phys_relay_state(self, addr, state: bool):
27
+ # Ничего не делает, просто имитация
28
+ pass
29
+
30
+
31
+ class EmulatorController:
32
+ model = "emulator_controller"
33
+
34
+ def __init__(self, *args, **kwargs):
35
+ di = EmulatorDI()
36
+ relay = EmulatorRelay()
37
37
  self.interface = ControllerInterface(di_interface=di, relay_interface=relay)
@@ -1,80 +1,80 @@
1
- import requests
2
- from gravity_controller_operator.controllers_super import DIInterface, RelayInterface, ControllerInterface
3
-
4
-
5
- class MoxaClient:
6
- def __init__(self, ip: str):
7
- self.base_url = f"http://{ip}/api/slot/0/io"
8
- self.headers = {
9
- "Content-Type": "application/json",
10
- "Accept": "vdn.dac.v1"
11
- }
12
-
13
- def get_di(self):
14
- url = f"{self.base_url}/di"
15
- r = requests.get(url, headers=self.headers, timeout=2)
16
- r.raise_for_status()
17
- return r.json()["io"]["di"]
18
-
19
- def get_relays(self):
20
- url = f"{self.base_url}/relay"
21
- r = requests.get(url, headers=self.headers, timeout=2)
22
- r.raise_for_status()
23
- return r.json()["io"]["relay"]
24
-
25
- def set_relay(self, channel: int, state: int):
26
- url = f"{self.base_url}/relay/{channel}/relayStatus"
27
- payload = {
28
- "slot": "0",
29
- "io": {
30
- "relay": {
31
- str(channel): {
32
- "relayStatus": str(state)
33
- }
34
- }
35
- }
36
- }
37
- headers = self.headers.copy()
38
- headers["Content-Length"] = str(len(str(payload)))
39
- r = requests.put(url, headers=headers, json=payload, timeout=2)
40
- r.raise_for_status()
41
- return r.status_code == 200
42
-
43
-
44
- class MoxaDI(DIInterface):
45
- map_keys_amount = 16
46
- starts_with = 0
47
-
48
- def __init__(self, client):
49
- self.client = client
50
- super().__init__()
51
-
52
- def get_phys_dict(self):
53
- data = self.client.get_di()
54
- return {int(item["diIndex"]): item["diStatus"] for item in data}
55
-
56
-
57
- class MoxaRelay(RelayInterface):
58
- map_keys_amount = 4
59
- starts_with = 0
60
-
61
- def __init__(self, client):
62
- self.client = client
63
- super().__init__()
64
-
65
- def get_phys_dict(self):
66
- data = self.client.get_relays()
67
- return {int(item["relayIndex"]): int(item["relayStatus"]) for item in data}
68
-
69
- def change_phys_relay_state(self, addr, state: bool):
70
- return self.client.set_relay(addr, int(state))
71
-
72
-
73
- class MoxaE1214:
74
- model = "moxa_e1214"
75
-
76
- def __init__(self, ip, *args, **kwargs):
77
- client = MoxaClient(ip)
78
- di = MoxaDI(client)
79
- relay = MoxaRelay(client)
80
- self.interface = ControllerInterface(di_interface=di, relay_interface=relay)
1
+ import requests
2
+ from gravity_controller_operator.controllers_super import DIInterface, RelayInterface, ControllerInterface
3
+
4
+
5
+ class MoxaClient:
6
+ def __init__(self, ip: str):
7
+ self.base_url = f"http://{ip}/api/slot/0/io"
8
+ self.headers = {
9
+ "Content-Type": "application/json",
10
+ "Accept": "vdn.dac.v1"
11
+ }
12
+
13
+ def get_di(self):
14
+ url = f"{self.base_url}/di"
15
+ r = requests.get(url, headers=self.headers, timeout=2)
16
+ r.raise_for_status()
17
+ return r.json()["io"]["di"]
18
+
19
+ def get_relays(self):
20
+ url = f"{self.base_url}/relay"
21
+ r = requests.get(url, headers=self.headers, timeout=2)
22
+ r.raise_for_status()
23
+ return r.json()["io"]["relay"]
24
+
25
+ def set_relay(self, channel: int, state: int):
26
+ url = f"{self.base_url}/relay/{channel}/relayStatus"
27
+ payload = {
28
+ "slot": "0",
29
+ "io": {
30
+ "relay": {
31
+ str(channel): {
32
+ "relayStatus": str(state)
33
+ }
34
+ }
35
+ }
36
+ }
37
+ headers = self.headers.copy()
38
+ headers["Content-Length"] = str(len(str(payload)))
39
+ r = requests.put(url, headers=headers, json=payload, timeout=2)
40
+ r.raise_for_status()
41
+ return r.status_code == 200
42
+
43
+
44
+ class MoxaDI(DIInterface):
45
+ map_keys_amount = 16
46
+ starts_with = 0
47
+
48
+ def __init__(self, client):
49
+ self.client = client
50
+ super().__init__()
51
+
52
+ def get_phys_dict(self):
53
+ data = self.client.get_di()
54
+ return {int(item["diIndex"]): item["diStatus"] for item in data}
55
+
56
+
57
+ class MoxaRelay(RelayInterface):
58
+ map_keys_amount = 4
59
+ starts_with = 0
60
+
61
+ def __init__(self, client):
62
+ self.client = client
63
+ super().__init__()
64
+
65
+ def get_phys_dict(self):
66
+ data = self.client.get_relays()
67
+ return {int(item["relayIndex"]): int(item["relayStatus"]) for item in data}
68
+
69
+ def change_phys_relay_state(self, addr, state: bool):
70
+ return self.client.set_relay(addr, int(state))
71
+
72
+
73
+ class MoxaE1214:
74
+ model = "moxa_e1214"
75
+
76
+ def __init__(self, ip, *args, **kwargs):
77
+ client = MoxaClient(ip)
78
+ di = MoxaDI(client)
79
+ relay = MoxaRelay(client)
80
+ self.interface = ControllerInterface(di_interface=di, relay_interface=relay)
@@ -1,76 +1,76 @@
1
- from gravity_controller_operator.controllers_super import DIInterface, RelayInterface, ControllerInterface
2
- from netping_contr import mixins
3
- import requests
4
- from requests.auth import HTTPBasicAuth
5
-
6
-
7
- class NetPingDevice(mixins.NetPingResponseParser):
8
- def __init__(self, ip, port=80, username="visor", password="ping"):
9
- self.ip = ip
10
- self.port = port
11
- self.username = username
12
- self.password = password
13
- self.schema = "http://"
14
-
15
- def get_full_url(self):
16
- return f"{self.schema}{self.ip}:{self.port}"
17
-
18
- def get_all_di_status(self):
19
- return requests.get(
20
- url=f"{self.get_full_url()}/io.cgi?io",
21
- auth=HTTPBasicAuth(self.username, self.password))
22
-
23
- def get_all_relay_states(self):
24
- states = {}
25
- for i in range(1, 5):
26
- r = requests.get(
27
- url=f"{self.get_full_url()}/relay.cgi?r{i}",
28
- auth=HTTPBasicAuth(self.username, self.password))
29
- states[i] = self.parse_relay_state(r)
30
- return states
31
-
32
- def change_relay_status(self, relay_num, state):
33
- return requests.get(
34
- url=f"{self.get_full_url()}/relay.cgi?r{relay_num}={state}",
35
- auth=HTTPBasicAuth(self.username, self.password))
36
-
37
-
38
- class NetPingDI(DIInterface):
39
- map_keys_amount = 4
40
- starts_with = 1
41
-
42
- def __init__(self, controller):
43
- self.controller = controller
44
- super().__init__()
45
-
46
- def get_phys_dict(self):
47
- raw = self.controller.get_all_di_status()
48
- return self.controller.parse_all_lines_request(raw)
49
-
50
-
51
- class NetPingRelay(RelayInterface):
52
- map_keys_amount = 4
53
- starts_with = 1
54
-
55
- def __init__(self, controller):
56
- self.controller = controller
57
- super().__init__()
58
-
59
- def get_phys_dict(self):
60
- return self.controller.get_all_relay_states()
61
-
62
- def change_phys_relay_state(self, addr, state: bool):
63
- result = self.controller.change_relay_status(addr, state)
64
- while "error" in result:
65
- result = self.controller.change_relay_status(addr, state)
66
-
67
-
68
- class NetPing2Controller:
69
- model = "netping_relay"
70
-
71
- def __init__(self, ip, port=80, username="visor", password="ping",
72
- name="netping_relay2", *args, **kwargs):
73
- device = NetPingDevice(ip=ip, port=port, username=username, password=password)
74
- di = NetPingDI(device)
75
- relay = NetPingRelay(device)
76
- self.interface = ControllerInterface(di_interface=di, relay_interface=relay)
1
+ from gravity_controller_operator.controllers_super import DIInterface, RelayInterface, ControllerInterface
2
+ from netping_contr import mixins
3
+ import requests
4
+ from requests.auth import HTTPBasicAuth
5
+
6
+
7
+ class NetPingDevice(mixins.NetPingResponseParser):
8
+ def __init__(self, ip, port=80, username="visor", password="ping"):
9
+ self.ip = ip
10
+ self.port = port
11
+ self.username = username
12
+ self.password = password
13
+ self.schema = "http://"
14
+
15
+ def get_full_url(self):
16
+ return f"{self.schema}{self.ip}:{self.port}"
17
+
18
+ def get_all_di_status(self):
19
+ return requests.get(
20
+ url=f"{self.get_full_url()}/io.cgi?io",
21
+ auth=HTTPBasicAuth(self.username, self.password))
22
+
23
+ def get_all_relay_states(self):
24
+ states = {}
25
+ for i in range(1, 5):
26
+ r = requests.get(
27
+ url=f"{self.get_full_url()}/relay.cgi?r{i}",
28
+ auth=HTTPBasicAuth(self.username, self.password))
29
+ states[i] = self.parse_relay_state(r)
30
+ return states
31
+
32
+ def change_relay_status(self, relay_num, state):
33
+ return requests.get(
34
+ url=f"{self.get_full_url()}/relay.cgi?r{relay_num}={state}",
35
+ auth=HTTPBasicAuth(self.username, self.password))
36
+
37
+
38
+ class NetPingDI(DIInterface):
39
+ map_keys_amount = 4
40
+ starts_with = 1
41
+
42
+ def __init__(self, controller):
43
+ self.controller = controller
44
+ super().__init__()
45
+
46
+ def get_phys_dict(self):
47
+ raw = self.controller.get_all_di_status()
48
+ return self.controller.parse_all_lines_request(raw)
49
+
50
+
51
+ class NetPingRelay(RelayInterface):
52
+ map_keys_amount = 4
53
+ starts_with = 1
54
+
55
+ def __init__(self, controller):
56
+ self.controller = controller
57
+ super().__init__()
58
+
59
+ def get_phys_dict(self):
60
+ return self.controller.get_all_relay_states()
61
+
62
+ def change_phys_relay_state(self, addr, state: bool):
63
+ result = self.controller.change_relay_status(addr, state)
64
+ while "error" in result:
65
+ result = self.controller.change_relay_status(addr, state)
66
+
67
+
68
+ class NetPing2Controller:
69
+ model = "netping_relay"
70
+
71
+ def __init__(self, ip, port=80, username="visor", password="ping",
72
+ name="netping_relay2", *args, **kwargs):
73
+ device = NetPingDevice(ip=ip, port=port, username=username, password=password)
74
+ di = NetPingDI(device)
75
+ relay = NetPingRelay(device)
76
+ self.interface = ControllerInterface(di_interface=di, relay_interface=relay)
@@ -1,52 +1,52 @@
1
- import time
2
- from http.client import responses
3
-
4
- from gravity_controller_operator.controllers_super import DIInterface, RelayInterface, ControllerInterface
5
-
6
-
7
- class SigurBase:
8
- def __init__(self, sock):
9
- self.sock = None
10
-
11
- class SigurDI(DIInterface, SigurBase):
12
- map_keys_amount = 5
13
- starts_with = 3
14
-
15
- def __init__(self, sock):
16
- SigurBase.__init__(self, sock)
17
- DIInterface.__init__(self)
18
-
19
- def get_phys_dict(self):
20
- result = {}
21
- for point in range(self.starts_with, self.starts_with + self.map_keys_amount):
22
- result[point] = 0
23
- return result
24
-
25
-
26
- class SigurRelay(RelayInterface, SigurBase):
27
- map_keys_amount = 3
28
- starts_with = 1
29
-
30
- def __init__(self, sock):
31
- SigurBase.__init__(self, sock)
32
- RelayInterface.__init__(self)
33
-
34
- def get_phys_dict(self):
35
- result = {}
36
- for point in range(self.starts_with, self.starts_with + self.map_keys_amount):
37
- result[point] = 0
38
- return result
39
-
40
- def change_phys_relay_state(self, addr, state: bool):
41
- # Имитация действия, при необходимости можно добавить настоящую команду
42
- pass
43
-
44
-
45
- class Sigur:
46
- model = "sigur"
47
-
48
- def __init__(self, sock, login="Administrator", password="",
49
- name="Sigur", *args, **kwargs):
50
- di = SigurDI(sock)
51
- relay = SigurRelay(sock)
52
- self.interface = ControllerInterface(di_interface=di, relay_interface=relay)
1
+ import time
2
+ from http.client import responses
3
+
4
+ from gravity_controller_operator.controllers_super import DIInterface, RelayInterface, ControllerInterface
5
+
6
+
7
+ class SigurBase:
8
+ def __init__(self):
9
+ pass
10
+
11
+ class SigurDI(DIInterface, SigurBase):
12
+ map_keys_amount = 5
13
+ starts_with = 3
14
+
15
+ def __init__(self):
16
+ SigurBase.__init__(self)
17
+ DIInterface.__init__(self)
18
+
19
+ def get_phys_dict(self):
20
+ result = {}
21
+ for point in range(self.starts_with, self.starts_with + self.map_keys_amount):
22
+ result[point] = 0
23
+ return result
24
+
25
+
26
+ class SigurRelay(RelayInterface, SigurBase):
27
+ map_keys_amount = 3
28
+ starts_with = 1
29
+
30
+ def __init__(self):
31
+ SigurBase.__init__(self)
32
+ RelayInterface.__init__(self)
33
+
34
+ def get_phys_dict(self):
35
+ result = {}
36
+ for point in range(self.starts_with, self.starts_with + self.map_keys_amount):
37
+ result[point] = 0
38
+ return result
39
+
40
+ def change_phys_relay_state(self, addr, state: bool):
41
+ # Имитация действия, при необходимости можно добавить настоящую команду
42
+ pass
43
+
44
+
45
+ class Sigur:
46
+ model = "sigur"
47
+
48
+ def __init__(self, sock, login="Administrator", password="",
49
+ name="Sigur", *args, **kwargs):
50
+ di = SigurDI()
51
+ relay = SigurRelay()
52
+ self.interface = ControllerInterface(di_interface=di, relay_interface=relay)