0% found this document useful (0 votes)
28 views4 pages

PicoReflow WebSocket Control Script

The document is a Python script for a web server application called 'picoreflowd' that manages a reflow oven. It utilizes the Bottle framework for routing and WebSockets for real-time communication, allowing users to control the oven, manage profiles, and access configuration settings. The script includes functions for handling commands like RUN, SIMULATE, STOP, GET, PUT, and DELETE for oven profiles.

Uploaded by

Micheal Williams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views4 pages

PicoReflow WebSocket Control Script

The document is a Python script for a web server application called 'picoreflowd' that manages a reflow oven. It utilizes the Bottle framework for routing and WebSockets for real-time communication, allowing users to control the oven, manage profiles, and access configuration settings. The script includes functions for handling commands like RUN, SIMULATE, STOP, GET, PUT, and DELETE for oven profiles.

Uploaded by

Micheal Williams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

File: D:\NextCloud\Work\Electronic\PicoReflow\2025 Update rework\picoReflow-master\p

picoReflow-master\[Link] 11/10/2020

#!/usr/bin/env python2

import os
import sys
import logging
import json

import bottle
import gevent
import geventwebsocket
from [Link] import WSGIServer
from [Link] import WebSocketHandler

try:
sys.dont_write_bytecode = True
import config
sys.dont_write_bytecode = False
except:
print "Could not import config file."
print "Copy [Link] to [Link] and adapt it for your setup."
exit(1)

[Link](level=config.log_level, format=config.log_format)
log = [Link]("picoreflowd")
[Link]("Starting picoreflowd")

script_dir = [Link]([Link](__file__))
[Link](0, script_dir + '/lib/')
profile_path = [Link](script_dir, "storage", "profiles")

from oven import Oven, Profile


from ovenWatcher import OvenWatcher

app = [Link]()
oven = Oven()
ovenWatcher = OvenWatcher(oven)

@[Link]('/')
def index():
return [Link]('/picoreflow/[Link]')

@[Link]('/picoreflow/:filename#.*#')
def send_static(filename):
[Link]("serving %s" % filename)
return bottle.static_file(filename,
root=[Link]([Link]([Link]([Link][0])), "public"))

def get_websocket_from_request():
env = [Link]
wsock = [Link]('[Link]')
if not wsock:
abort(400, 'Expected WebSocket request.')

Page: 1
File: D:\NextCloud\Work\Electronic\PicoReflow\2025 Update rework\picoReflow-master\p
picoReflow-master\[Link] 11/10/2020

return wsock

@[Link]('/control')
def handle_control():
wsock = get_websocket_from_request()
[Link]("websocket (control) opened")
while True:
try:
message = [Link]()
[Link]("Received (control): %s" % message)
msgdict = [Link](message)
if [Link]("cmd") == "RUN":
[Link]("RUN command received")
profile_obj = [Link]('profile')
if profile_obj:
profile_json = [Link](profile_obj)
profile = Profile(profile_json)
oven.run_profile(profile)
[Link](profile)
elif [Link]("cmd") == "SIMULATE":
[Link]("SIMULATE command received")
profile_obj = [Link]('profile')
if profile_obj:
profile_json = [Link](profile_obj)
profile = Profile(profile_json)
simulated_oven = Oven(simulate=True, time_step=0.05)
simulation_watcher = OvenWatcher(simulated_oven)
simulation_watcher.add_observer(wsock)
#simulated_oven.run_profile(profile)
#simulation_watcher.record(profile)
elif [Link]("cmd") == "STOP":
[Link]("Stop command received")
oven.abort_run()
except WebSocketError:
break
[Link]("websocket (control) closed")

@[Link]('/storage')
def handle_storage():
wsock = get_websocket_from_request()
[Link]("websocket (storage) opened")
while True:
try:
message = [Link]()
if not message:
break
[Link]("websocket (storage) received: %s" % message)

try:
msgdict = [Link](message)
except:
msgdict = {}

Page: 2
File: D:\NextCloud\Work\Electronic\PicoReflow\2025 Update rework\picoReflow-master\p
picoReflow-master\[Link] 11/10/2020

if message == "GET":
[Link]("GET command recived")
[Link](get_profiles())
elif [Link]("cmd") == "DELETE":
[Link]("DELETE command received")
profile_obj = [Link]('profile')
if delete_profile(profile_obj):
msgdict["resp"] = "OK"
[Link]([Link](msgdict))
#[Link](get_profiles())
elif [Link]("cmd") == "PUT":
[Link]("PUT command received")
profile_obj = [Link]('profile')
force = [Link]('force', False)
if profile_obj:
#del msgdict["cmd"]
if save_profile(profile_obj, force):
msgdict["resp"] = "OK"
else:
msgdict["resp"] = "FAIL"
[Link]("websocket (storage) sent: %s" % message)

[Link]([Link](msgdict))
[Link](get_profiles())
except WebSocketError:
break
[Link]("websocket (storage) closed")

@[Link]('/config')
def handle_config():
wsock = get_websocket_from_request()
[Link]("websocket (config) opened")
while True:
try:
message = [Link]()
[Link](get_config())
except WebSocketError:
break
[Link]("websocket (config) closed")

@[Link]('/status')
def handle_status():
wsock = get_websocket_from_request()
ovenWatcher.add_observer(wsock)
[Link]("websocket (status) opened")
while True:
try:
message = [Link]()
[Link]("Your message was: %r" % message)
except WebSocketError:
break
[Link]("websocket (status) closed")

Page: 3
File: D:\NextCloud\Work\Electronic\PicoReflow\2025 Update rework\picoReflow-master\p
picoReflow-master\[Link] 11/10/2020

def get_profiles():
try:
profile_files = [Link](profile_path)
except:
profile_files = []
profiles = []
for filename in profile_files:
with open([Link](profile_path, filename), 'r') as f:
[Link]([Link](f))
return [Link](profiles)

def save_profile(profile, force=False):


profile_json = [Link](profile)
filename = profile['name']+".json"
filepath = [Link](profile_path, filename)
if not force and [Link](filepath):
[Link]("Could not write, %s already exists" % filepath)
return False
with open(filepath, 'w+') as f:
[Link](profile_json)
[Link]()
[Link]("Wrote %s" % filepath)
return True

def delete_profile(profile):
profile_json = [Link](profile)
filename = profile['name']+".json"
filepath = [Link](profile_path, filename)
[Link](filepath)
[Link]("Deleted %s" % filepath)
return True

def get_config():
return [Link]({"temp_scale": config.temp_scale,
"time_scale_slope": config.time_scale_slope,
"time_scale_profile": config.time_scale_profile,
"kwh_rate": config.kwh_rate,
"currency_type": config.currency_type})

def main():
ip = config.listening_ip
port = config.listening_port
[Link]("listening on %s:%d" % (ip, port))

server = WSGIServer((ip, port), app,


handler_class=WebSocketHandler)
server.serve_forever()

if __name__ == "__main__":
main()

Page: 4

You might also like