/
SNStatusV2.py
executable file
·204 lines (186 loc) · 7.81 KB
/
SNStatusV2.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/python3
# Requires: sudo pip3 install requests
#
import socket
import requests
import json
import urllib3
import ipaddress
import time
from datetime import timedelta
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
haToken = "<hatoken>" # Set your HomeAssistant API Token
whUrl = 'https://<ipaddress>:8123/api/webhook/wh-snapmakera350' # Set to your HomeAssistant WebHook URL
bufferSize = 1024
msg = b'discover'
destPort = 20054
sockTimeout = 1.0
retries = 5
retryCounter = 0
snReply = {}
connectIP = ''
tokenfile = '/home/pi/SmartHome/SnapMaker/SMtoken.txt' # Set to writable path, file will be created if not exists.
snWorking = ''
# Main Program
def main():
global connectIP
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPClientSocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
UDPClientSocket.settimeout(sockTimeout)
# Get Status and IP of Snapmaker
checkState(UDPClientSocket,msg,destPort,retries)
if validate_ip_address(snReply.get("snIP")):
connectIP = snReply.get("snIP")
print("Snapmaker found:", connectIP)
if snWorking == 'IDLE':
SMtoken = getSMToken(connectIP)
postIt(snReply)
else:
SMtoken = getSMToken(connectIP)
print("Connecting with Token:",SMtoken)
postIt(readStatus(SMtoken))
# Not yet used
#readStatusEnclosure(SMtoken)
else:
postIt(snReply)
print("No Snapmaker found.")
def getSMToken(connectIP):
# Create file if not exists
try:
f = open(tokenfile, "r+")
except FileNotFoundError:
f = open(tokenfile, "w+")
SMurl = "http://" + connectIP + ":8080/api/v1/connect"
SMtoken = f.read()
if SMtoken == "":
# Create token
connected = False
while not connected:
r = requests.post(SMurl)
print("Please authorize on Touchscreen.")
time.sleep(10)
if "Failed" in r.text:
print(r.text)
print("Binding failed, please restart script")
exit(1)
SMtoken = (json.loads(r.text).get("token"))
headers = {'Content-Type' : 'application/x-www-form-urlencoded'}
formData = {'token' : SMtoken}
r = requests.post(SMurl, data=formData, headers=headers)
if json.loads(r.text).get("token") == SMtoken:
f.write(SMtoken)
print("Token received and saved.\nRestart Script for autoconnect now.")
connected = True
return(SMtoken)
exit(0)
else:
f.close()
# Connect to SnapMaker with saved token
headers = {'Content-Type' : 'application/x-www-form-urlencoded'}
formData = {'token' : SMtoken}
r = requests.post(SMurl, data=formData, headers=headers)
return(SMtoken)
# Read Status of Snapmaker 2.0 via API
# Example Data IDLE:
# {"status":"IDLE","x":112,"y":142,"z":150,"homed":false,"offsetX":0,"offsetY":0,"offsetZ":0,"toolHead":"TOOLHEAD_3DPRINTING_1",
# "nozzleTemperature":19,"nozzleTargetTemperature":0,"heatedBedTemperature":20,"heatedBedTargetTemperature":0,
# "isFilamentOut":false,"workSpeed":1500,"printStatus":"Idle",
# "moduleList":{"enclosure":true,"rotaryModule":false,"emergencyStopButton":true,"airPurifier":false},
# "isEnclosureDoorOpen":false,"doorSwitchCount":0}
#
# Example Data RUNNING:
# {"status":"RUNNING","x":-19,"y":339,"z":310.763,"homed":false,"offsetX":0,"offsetY":0,"offsetZ":0,"toolHead":"TOOLHEAD_3DPRINTING_1",
# "nozzleTemperature":63,"nozzleTargetTemperature":205,"heatedBedTemperature":20,"heatedBedTargetTemperature":70,
# "isFilamentOut":false,"workSpeed":1500,"printStatus":"Printing",
# "fileName":"Leon_Base.gcode","totalLines":20295,"estimatedTime":3204,"currentLine":91,"progress":0.004483863245695829,"elapsedTime":20,"remainingTime":3195,
# "moduleList":{"enclosure":true,"rotaryModule":false,"emergencyStopButton":true,"airPurifier":false},
# "isEnclosureDoorOpen":false,"doorSwitchCount":0}
#
def readStatus(SMtoken):
#print("Reading SN Status...")
SMstatus = "http://" + connectIP + ":8080/api/v1/status?token="
r = requests.get(SMstatus+SMtoken)
snStatus = json.loads(r.text).get("status")
snNozzleTemp = json.loads(r.text).get("nozzleTemperature")
snNozzleTaTemp = json.loads(r.text).get("nozzleTargetTemperature")
snHeatedBedTemp = json.loads(r.text).get("heatedBedTemperature")
snHeatedBedTaTemp = json.loads(r.text).get("heatedBedTargetTemperature")
if json.loads(r.text).get("fileName") is not None:
snFileName = json.loads(r.text).get("fileName")
else:
snFileName = "N/A"
if json.loads(r.text).get("progress") is not None:
snProgress = ("{:0.1f}".format(json.loads(r.text).get("progress")*100))
else:
snProgress = "0"
if json.loads(r.text).get("elapsedTime") is not None:
snElapsedTime = str(timedelta(seconds=json.loads(r.text).get("elapsedTime")))
else:
snElapsedTime = "00:00:00"
if json.loads(r.text).get("remainingTime") is not None:
snRemainingTime = str(timedelta(seconds=json.loads(r.text).get("remainingTime")))
else:
snRemainingTime = "00:00:00"
snReply = {"snIP":connectIP,"snStatus":snStatus,"snNozzleTemp":snNozzleTemp,"snNozzleTaTemp":snNozzleTaTemp,
"snHeatedBedTemp":snHeatedBedTemp,"snHeatedBedTaTemp":snHeatedBedTaTemp,"snFileName":snFileName,
"snProgress":snProgress,"snElapsedTime":snElapsedTime,"snRemainingTime":snRemainingTime}
return(snReply)
# Read Status of Enclosure
# Example Data:
# {"isReady":true,"isDoorEnabled":false,"led":100,"fan":0}
#
def readStatusEnclosure(SMtoken):
print("Reading Enclosure Status...")
SMenclosure = "http://" + connectIP + ":8080/api/v1/enclosure?token="
r = requests.get(SMenclosure+SMtoken)
print(r.text)
return(r.text)
# Check status of Snapmaker 2.0 via UDP Discovery
# Possible replies:
# 'Snapmaker@X.X.X.X|model:Snapmaker 2 Model A350|status:IDLE'
# 'Snapmaker@X.X.X.X|model:Snapmaker 2 Model A350|status:RUNNING'
def checkState(UDPClientSocket,msg,destPort,retries):
global snReply
global snWorking
global retryCounter
UDPClientSocket.sendto(msg, ("255.255.255.255", destPort))
try:
reply, server_address_info = UDPClientSocket.recvfrom(1024)
elements = str(reply).split('|')
snIP = (elements[0]).replace('\'','')
snModel = (elements[1]).replace('\'','')
snStatus = (elements[2]).replace('\'','')
snIP, snIPVal = snIP.split('@')
snModel, snModelVal = snModel.split(':')
snStatus, snStatusVal = snStatus.split(':')
snWorking = snStatusVal
snReply = {"snIP":snIPVal, "model":snModelVal, "snStatus":snStatusVal}
except socket.timeout:
retryCounter += 1
if (retryCounter==retries):
snReply = {"snIP":"N/A", "model":"N/A", "snStatus":"OFFLINE",
"snNozzleTemp":0,"snNozzleTaTemp":0,
"snHeatedBedTemp":0,"snHeatedBedTaTemp":0,"snFileName":"N/A",
"snProgress":0,"snElapsedTime":"00:00:00","snRemainingTime":"00:00:00"}
return
else:
checkState(UDPClientSocket,msg,destPort,retries);
# Check if IP is valid:
def validate_ip_address(ip_string):
try:
ip_object = ipaddress.ip_address(ip_string)
return True
except ValueError:
return False
# POST to HomeAssistant Webhook
def postIt(state):
session = requests.Session()
session.verify = False
print("Sending State:", state)
try:
requests.post(whUrl, json = state, verify=False)
except requests.exceptions.ConnectionError:
print("Could not connect to HomeAssistant on", whUrl)
return
# Run Main Program
main()