-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy patheventHandler.py
162 lines (123 loc) · 6.53 KB
/
eventHandler.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
# coding=utf-8
__author__ = "Jarek Szczepanski <[email protected]>"
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
__copyright__ = "Copyright (C) 2014 Jarek Szczepanski - Released under terms of the AGPLv3 License"
def eventHandler(self, event, payload):
from octoprint.events import Events
import json
import time
from operator import itemgetter
from .parser import UniversalParser
import sqlite3
supported_event = None
# support for print done & cancelled events
if event == Events.PRINT_DONE:
supported_event = event
elif event == Events.PRINT_FAILED:
supported_event = event
elif event == Events.METADATA_STATISTICS_UPDATED:
supported_event = event
# unsupported event
if supported_event is None:
return
if supported_event is not Events.METADATA_STATISTICS_UPDATED:
self._logger.info("Name: "+ payload["name"])
self._logger.info("Origin: "+ payload["origin"])
self._logger.info("Path: "+ payload["path"])
self._logger.info("Path_On_disk: "+ self._file_manager.path_on_disk(payload["origin"], payload["path"]))
try:
fileData = self._file_manager.get_metadata(payload["origin"], payload["name"])
self._logger.info("Got metadata from name")
except:
self._logger.info("Error getting metadata from name, trying with path")
try:
fileData = self._file_manager.get_metadata(payload["origin"], payload["path"])
except:
self._logger.info("Error getting metadata from name and path, terminating")
fileData = None
fileName = payload["name"]
if fileData is None:
self._logger.info("FileData came out empty, trying to get it from path")
fileData = self._file_manager.get_metadata(payload["origin"], payload["path"])
if fileData is not None:
self._logger.info("found fileData")
timestamp = 0
success = None
estimatedPrintTime = 0
gcode_parser = UniversalParser(self._file_manager.path_on_disk(payload["origin"], payload["path"]), logger=self._logger)
parameters = gcode_parser.parse()
currentFile = {
"fileName": fileName,
"note": "",
"parameters": json.dumps(parameters)
}
self._logger.info(json.dumps(parameters))
# analysis - looking for info about filament usage
if "analysis" in fileData:
if "filament" in fileData["analysis"]:
if "tool0" in fileData["analysis"]["filament"]:
filamentVolume = fileData["analysis"]["filament"]["tool0"]["volume"]
filamentLength = fileData["analysis"]["filament"]["tool0"]['length']
currentFile["filamentVolume"] = filamentVolume if filamentVolume is not None else 0
currentFile["filamentLength"] = filamentLength if filamentLength is not None else 0
if "tool1" in fileData["analysis"]["filament"]:
filamentVolume = fileData["analysis"]["filament"]["tool1"]["volume"]
filamentLength = fileData["analysis"]["filament"]["tool1"]['length']
currentFile["filamentVolume2"] = filamentVolume if filamentVolume is not None else 0
currentFile["filamentLength2"] = filamentLength if filamentLength is not None else 0
estimatedPrintTime = fileData["analysis"]["estimatedPrintTime"] if "estimatedPrintTime" in fileData["analysis"] else 0
# Temporarily disabled
# if "tool0" in fileData["analysis"]["filament"] and "tool1" in fileData["analysis"]["filament"]:
# currentFile["note"] = "Dual extrusion"
# make sure we have zeroes for these values if not set above
if not currentFile.get("filamentVolume"):
currentFile["filamentVolume"] = 0
if not currentFile.get("filamentLength"):
currentFile["filamentLength"] = 0
# how long print took
if "time" in payload:
currentFile["printTime"] = payload["time"]
else:
printTime = self._comm.getPrintTime() if self._comm is not None else ""
currentFile["printTime"] = printTime
if "owner" in payload:
currentFile["user"] = payload["user"]
else:
currentFile["user"] = ""
# when print happened and what was the result
if "history" in fileData:
history = fileData["history"]
newlist = sorted(history, key=itemgetter('timestamp'), reverse=True)
if newlist:
last = newlist[0]
success = last["success"]
if not success:
success = False if event == Events.PRINT_FAILED else True
timestamp = int(time.time())
currentFile["success"] = success
currentFile["timestamp"] = timestamp
self._history_dict = None
conn = sqlite3.connect(self._history_db_path)
cur = conn.cursor()
cur.execute("INSERT INTO print_history (fileName, note, filamentVolume, filamentLength, printTime, success, timestamp, user, parameters) VALUES (:fileName, :note, :filamentVolume, :filamentLength, :printTime, :success, :timestamp, :user, :parameters)", currentFile)
conn.commit()
conn.close()
else:
# sometimes Events.PRINT_DONE is fired BEFORE metadata.yaml is updated - we have to wait for Events.METADATA_STATISTICS_UPDATED and update database
self._logger.info("fileData not found")
try:
fileData = self._file_manager.get_metadata(payload["storage"], payload["path"])
except:
fileData = None
if "history" in fileData:
history = fileData["history"]
newlist = sorted(history, key=itemgetter('timestamp'), reverse=True)
if newlist:
last = newlist[0]
success = last["success"]
timestamp = int(last["timestamp"])
conn = sqlite3.connect(self._history_db_path)
cur = conn.cursor()
cur.execute("UPDATE print_history SET success = ? WHERE timestamp = ?", (success, timestamp))
conn.commit()
conn.close()