Skip to content

Commit d82bad5

Browse files
committed
Updated readme, Python 3.9, Clutter.Matrix support, only sync files on authentication success
1 parent aa9d1ca commit d82bad5

File tree

11 files changed

+68
-34
lines changed

11 files changed

+68
-34
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ matrix:
22
include:
33
- language: python
44
python:
5-
- "3.6"
5+
- "3.9"
66
before_install:
77
- sudo apt-get update
88
- sudo apt install libdbus-1-dev

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Install actual program
4747
```
4848
git clone https://github.com/intelligent-agent/toggle
4949
cd toggle
50-
pip3 install -e . -r requirements.txt
50+
pip3 install -r requirements.txt
5151
python3 setup.py install_data
5252
```
5353

@@ -77,3 +77,14 @@ To Run toggle once it's installed:
7777
```
7878
CLUTTER_BACKEND=<wayland|x11|eglnative> toggle
7979
```
80+
81+
## Contributing
82+
Make sure all tests pass before creating a pull-request
83+
To test locally, run:
84+
```
85+
pytest
86+
```
87+
and also make sure the formatting is correct
88+
```
89+
yapf3 --diff -e venv -e .git -e build -r .
90+
```

configs/default.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ port = 5000
4343

4444
[OctoPrint]
4545
user = toggle
46+
password = REPLACE_ME
4647
authentication = REPLACE_ME
4748
apikey = REPLACE_ME
4849
timeout = 40

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
requests
22
python-networkmanager
33
tornado
4-
https://github.com/liamw9534/pyconnman/archive/master.tar.gz#egg=pyconnman
4+
https://github.com/liamw9534/pyconnman/archive/master.tar.gz#egg=pyconnman
5+
-e .

tests/conformance/test_graph_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from toggle.ui.Graph import Graph, GraphPlot, GraphScale
1010

1111

12-
def test_graph_plot(mocker):
12+
def test_graph_plot():
1313
ctx = mock.Mock()
1414
plot = GraphPlot("H", (0, 1, 0))
1515
plot.add_point(0, 4)

toggle/Toggle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __init__(self):
124124
config.loader = ModelLoader(config)
125125
config.model = Model(config)
126126
config.plate = Plate(config)
127-
config.socks_client = WebSocksClient(config, self.on_connected_cb)
127+
config.socks_client = WebSocksClient(config, self.on_auth_cb)
128128

129129
# mouse
130130
use_mouse = int(config.get('Input', 'mouse'))
@@ -144,7 +144,7 @@ def __init__(self):
144144
self.config = config
145145
config.stage.show()
146146

147-
def on_connected_cb(self):
147+
def on_auth_cb(self):
148148
self.config.loader.sync_models()
149149

150150
def run(self):

toggle/core/Event.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,25 @@ def timelapse(self):
4848
def plugin(self):
4949
plugin_type = self.payload["data"]["type"]
5050
plugin_name = self.payload["plugin"]
51-
if plugin_type == "filament_sensor":
52-
logging.warning("filament sensor has been disabled")
53-
elif plugin_type == "alarm_filament_jam":
54-
self.config.message.display("Alarm: Filament Jam!")
55-
elif plugin_type == "display_message":
56-
message = plugin_data["message"]
57-
self.config.message.display(message)
58-
elif plugin_type == "bed_probe_point":
59-
point = json.loads(plugin_data["message"])
60-
self.config.plate.add_probe_point(point)
61-
self.config.loader.select_none()
62-
elif plugin_type == "bed_probe_reset":
63-
self.config.plate.remove_probe_points()
64-
self.config.loader.model.show()
51+
if plugin_name == "redeem":
52+
if plugin_type == "filament_sensor":
53+
logging.warning("filament sensor has been disabled")
54+
elif plugin_type == "alarm_filament_jam":
55+
self.config.message.display("Alarm: Filament Jam!")
56+
elif plugin_type == "display_message":
57+
message = plugin_data["message"]
58+
self.config.message.display(message)
59+
elif plugin_type == "bed_probe_point":
60+
point = json.loads(plugin_data["message"])
61+
self.config.plate.add_probe_point(point)
62+
self.config.loader.select_none()
63+
elif plugin_type == "bed_probe_reset":
64+
self.config.plate.remove_probe_points()
65+
self.config.loader.model.show()
66+
elif plugin_name == "klipper":
67+
if plugin_type == "status":
68+
status = self.payload["data"]["title"]
69+
logging.debug(f"Kipper status: {status}")
6570
else:
6671
logging.debug("Unknown plugin type: " + str(plugin_type))
6772

@@ -129,6 +134,9 @@ def ClientOpened(self):
129134
def ClientClosed(self):
130135
logging.debug("Client Closed")
131136

137+
def ClientAuthed(self):
138+
logging.debug("Client Authenticated")
139+
132140
def SlicingStarted(self):
133141
self.config.message.display("Starting slicing!")
134142

toggle/core/RestClient.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, config):
1616
self._headers = {'Content-Type': 'application/json', 'X-Api-Key': self._api_key}
1717

1818
def login(self):
19-
logging.debug("Login")
19+
logging.debug("Rest login")
2020
url = "http://" + self._host + ":" + str(self._port) + "/api/login"
2121
user = self.config.get("OctoPrint", "user")
2222
password = self.config.get("OctoPrint", "password")
@@ -25,6 +25,7 @@ def login(self):
2525
if r.status_code == 200:
2626
return r.json()["session"]
2727
else:
28+
logging.warning("Authentication failed! Check username and password + CORS")
2829
return "INVALID-SESSION"
2930

3031
def start_job(self):
@@ -61,7 +62,8 @@ def send_gcode(self, cmd):
6162
str(self._port) + "/api/printer/command"
6263
data = json.dumps({'command': cmd})
6364
r = requests.post(url, data=data, headers=self._headers)
64-
print(r.json)
65+
if r.status_code != 204:
66+
logging.warning(f"send_gcode returned satus code: {r.status_code}")
6567

6668
def start_preheat(self):
6769
logging.debug("Starting preheat")
@@ -134,12 +136,12 @@ def get_list_of_files(self):
134136
r = requests.get(self._build_url("/api/files"), headers=self._headers)
135137
except requests.ConnectionError as e:
136138
logging.warning("Connection error")
137-
return {}
139+
return {'files': []}
138140
if r.status_code in [200, 204]:
139141
return r.json()
140142
logging.warning("Unable to contact OctoPrint by REST. "
141143
"Check your API key (currently '" + self._api_key + "'")
142-
return {}
144+
return {'files': []}
143145

144146
def download_model(self, url):
145147
try:

toggle/core/WebSocksClient.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ class WebSocksClient():
3434
CLOSED = 0
3535
CONNECTING = 1
3636
CONNECTED = 2
37-
FAILED = 3
37+
AUTHENTICATED = 3
38+
FAILED = 4
3839

39-
def __init__(self, config=None, on_connected_cb=None):
40+
def __init__(self, config=None, on_auth_cb=None):
4041
self.config = config
41-
self.on_connected_cb = on_connected_cb
42+
self.on_auth_cb = on_auth_cb
4243
host = config.get("Server", "host")
4344
port = str(config.get("Server", "port"))
4445
self.connect_timeout = DEFAULT_CONNECT_TIMEOUT
@@ -59,10 +60,12 @@ def connect(self):
5960
def authenticate(self):
6061
user = self.config.get("OctoPrint", "user")
6162
session = self.config.rest_client.login()
63+
if session != "INVALID-SESSION":
64+
self.on_auth_cb()
6265
logging.debug("Authenticating with " + user + ":" + session)
6366
msg = '{ "auth" : "' + str(user) + ':' + str(session) + '" }'
6467
logging.info("Sending message " + msg)
65-
self._ws_connection.write_message(msg)
68+
ret = self._ws_connection.write_message(msg)
6669

6770
def send(self, data):
6871
"""
@@ -103,6 +106,7 @@ def _on_message(self, msg):
103106
:param str msg: server message.
104107
"""
105108
data = json.loads(msg)
109+
#logging.debug(msg)
106110
if 'connected' in data:
107111
logging.debug("SockJS: Socket connected")
108112
self.authenticate()
@@ -114,8 +118,6 @@ def _on_connection_success(self):
114118
"""
115119
self.state = WebSocksClient.CONNECTED
116120
logging.debug('Websocket connected!')
117-
if self.on_connected_cb:
118-
self.on_connected_cb()
119121

120122
def _on_connection_close(self):
121123
"""

toggle/ui/Model.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,12 @@ def __init__(self, config):
4545
vp.add_child(self.light_directional)
4646
vp.add_child(self.light_point)
4747

48-
cm = Cogl.Matrix()
49-
m = Clutter.matrix_init_from_array(cm, [1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])
48+
if hasattr(Clutter, 'Matrix'):
49+
m = Clutter.Matrix.alloc()
50+
m.init_from_array([1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])
51+
else:
52+
cm = Cogl.Matrix()
53+
m = Clutter.matrix_init_from_array(cm, [1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])
5054
config.ui.get_object("model-flipper").set_transform(m)
5155

5256
self.model.connect("show", self.model_loaded)

0 commit comments

Comments
 (0)