· 5 years ago · Oct 04, 2020, 06:20 PM
1import aiohttp
2import asyncio
3import pysmartthings
4import time
5from octorest import OctoRest
6import time
7
8token = 'API_TOKEN_HERE' # Smartthings api key
9
10URL = "http://192.168.1.112/" # octoprint URL
11APIKEY = "API_TOKEN_HERE" # octoprint api key
12
13lightDelay = 13 # time delay for lights to be off
14
15def delayAndCheck(lightDelay):
16 for x in range(lightDelay):
17 time.sleep(1)
18 getPrintTime()
19
20
21
22
23async def lights_off():
24 async with aiohttp.ClientSession() as session:
25 api = pysmartthings.SmartThings(session, token)
26 devices = await api.devices()
27
28 device = devices[15]
29 result = await device.switch_off()
30 assert result == True
31 await device.status.refresh()
32
33 device = devices[16]
34 result = await device.switch_off()
35 assert result == True
36 await device.status.refresh()
37
38 device = devices[17]
39 result = await device.switch_off()
40 assert result == True
41 await device.status.refresh()
42
43 lights = open('lights.txt', 'w+')
44 lights.write('')
45 lights.close()
46
47 delayAndCheck(lightDelay)
48
49
50
51 device = devices[15]
52 result = await device.switch_on()
53 assert result == True
54 await device.status.refresh()
55
56 device = devices[16]
57 result = await device.switch_on()
58 assert result == True
59 await device.status.refresh()
60
61 device = devices[17]
62 result = await device.switch_on()
63 assert result == True
64 await device.status.refresh()
65
66
67
68def make_client(url, apikey):
69 client = OctoRest(url=url, apikey=apikey)
70 return client
71
72def get_job_name(turtles):
73 job_name = turtles.job_info()['job']['file']['name'] # gets file name from octoprint
74 return job_name[:-6].replace('_', ' ') #removes ".gcode" from file name, replaces '_' with spaces, RETurns : STR
75def convert(seconds):
76 return time.strftime("%H:%M:%S", time.gmtime(seconds))
77
78def getPrintTime():
79 turtles = make_client(URL, APIKEY)
80 elapsed_time = turtles.job_info()['progress']['printTime'] #Gets print time from octoprint in seconds
81 status = turtles.job_info()['progress']['printTimeLeft'] #gets how much time is left on print
82 converted_time = convert(elapsed_time) # converts seconds into 00:00:00 format
83
84 if status == None or elapsed_time == None:
85 file1 = open("printInfo.txt", "w+")
86 file1.write("The Printer is Offline!")
87 file1.close()
88 return
89
90 if status == 0:
91 file1 = open("printInfo.txt", "w+")
92 file1.write("No Current Print!")
93 file1.close()
94 elif elapsed_time <= 120:
95 file1 = open("printInfo.txt", "w+")
96 file1.write("'" +get_job_name(turtles) + "' print is starting!")
97 file1.close()
98 else:
99 file1 = open("printInfo.txt", "w+")
100 file1.write("'" + get_job_name(turtles) + "' has been going for: " + converted_time)
101 file1.close()
102
103
104
105def main():
106 lights = open('lights.txt', 'r')
107 lightsText = lights.read()
108 lights.close()
109
110 printer = open('print.txt', 'r')
111 printerText = printer.read()
112 printer.close()
113
114
115 if lightsText != '':
116 loop = asyncio.get_event_loop()
117 loop.run_until_complete(lights_off())
118
119 if printerText != '':
120 getPrintTime()
121 printer = open('print.txt', 'w+')
122 printer.write('')
123 printer.close()
124
125
126
127while True:
128 main();
129 time.sleep(2)
130
131
132Streamlabs chatbot settings
133
134!lights command : $overwritefile("C:\Twitch\programs\lights.txt","on","$username turned off the lights! 50 shells deducted.","Sorry $username, something went wrong")
135
136!print command : $overwritefile("C:\Twitch\programs\print.txt","on","","")
137
138 $readline(C:\Twitch\programs\printInfo.txt)