modification of the sim to be more close to the real device

This commit is contained in:
2026-05-06 14:38:45 +02:00
parent 62a7406dac
commit 48ac088900
3 changed files with 51 additions and 22 deletions
+47 -19
View File
@@ -1,25 +1,35 @@
import socket
import datetime
import random
port=55555
outlet_state = ['Off', 'Off', 'Off', 'Off', 'Off', 'Off', 'Off', 'Off']
outlet_restart_time = [0,0,0,0,0,0,0,0]
socket_listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def updateSocketRestartTimer():
for i in range(len(outlet_restart_time)):
if outlet_restart_time[i] > 0:
outlet_restart_time[i] -= 1
if outlet_restart_time[i] == 0:
outlet_state[i] = 'On'
def readAscii(connexion_client : socket):
message = ""
chunk = ""
while chunk != b'\r\n':
chunk = connexion_client.recv(1024)
print(chunk)
message += chunk.decode()
print(message)
if message[0] == 'P' :
outlet_index = ord(message[21]) - ord('0')
match message[24]:
if message[15] == '\n' :
outlet_index = ord(message[23]) - ord('0')
print(outlet_state[outlet_index])
match message[26]:
case 'n':
outlet_state[outlet_index] = 'On'
print("powering on " + str(outlet_index))
@@ -27,22 +37,39 @@ def readAscii(connexion_client : socket):
outlet_state[outlet_index] = 'Off'
print("powering off " + str(outlet_index))
case 'e':
outlet_state[outlet_index] = 'Restart'
outlet_state[outlet_index] = 'Rst'
print("restarting " + str(outlet_index))
if message[0] == 'G':
print("sending current state report")
outlet_restart_time[outlet_index] = random.randint(2,5)
if message[15] == '?':
time = datetime.datetime.now().strftime('%d %B %Y %H:%M:%S')
connexion_client.send(bytes("Hidden Page\n" + time + "\nVersion: 1.5.0.1\n" +
"M0:O1=" + outlet_state[0] + "\n"+
"M0:O2=" + outlet_state[1] + "\n"+
"M0:O3=" + outlet_state[2] + "\n"+
"M0:O4=" + outlet_state[3] + "\n"+
"M0:O5=" + outlet_state[4] + "\n"+
"M0:O6=" + outlet_state[5] + "\n"+
"M0:O7=" + outlet_state[6] + "\n"+
"M0:O8=" + outlet_state[7] + "\n", "UTF-8"))
connexion_client.send(bytes("HTTP/1.0 200 OK\r\n" +
"Server: lwIP/1.3.2 (http://www.sics.se/~adam/lwip/)\r\n"+
"Content-type: text/html;charset=iso-8859-1\r\n"+
"Expires: Fri, 01 Jan 2010 00:00:00 GMT\r\n"+
"Pragma: no-cache\r\n"+
"\r\n" +
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n" +
"<html>\r\n" +
"<head>\r\n" +
"<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\r\n" +
"</head>\r\n" +
"<body>Hidden Page<br><pre>\r\n" +
time + "\r\n" +
"ePowerSwitch 8M+ R3\r\n" +
"Version: v 1.5.0.1\r\n" +
"M0:O1=" + outlet_state[0] + "\r\n" +
"M0:O2=" + outlet_state[1] + "\r\n" +
"M0:O3=" + outlet_state[2] + "\r\n" +
"M0:O4=" + outlet_state[3] + "\r\n" +
"M0:O5=" + outlet_state[4] + "\r\n" +
"M0:O6=" + outlet_state[5] + "\r\n" +
"M0:O7=" + outlet_state[6] + "\r\n" +
"M0:O8=" + outlet_state[7] + "\r\n" +
"\r\n" +
"</pre>\r\n" +
"</body></html> "
, "UTF-8"))
socket_listener.bind(('', port))
@@ -51,4 +78,5 @@ socket_listener.listen()
while(True):
connexion_client, address_client = socket_listener.accept()
readAscii(connexion_client)
updateSocketRestartTimer()
connexion_client.close()
+1 -1
View File
@@ -6,7 +6,7 @@ epicsEnvSet("ASYN_PORT", "myDriverPort")
epicsEnvSet("DB", "$(ePowerSwitch_DB)")
# I use HTTP to use TCP and disconnect after, but no disconnection is done, so I disconnect manually.
drvAsynIPPortConfigure("$(ASYN_PORT)","$(HOST_NAME):80 http", 0, 0, 0)
drvAsynIPPortConfigure("$(ASYN_PORT)","$(HOST_NAME):$(HOST_PORT) http", 0, 0, 0)
dbLoadTemplate("$(DB)template$(EPOWERSWITCH_SOCKET_NUMBER).substitutions", "PORT=$(ASYN_PORT), DEVICE_NAME=$(DEVICE_NAME), DB=$(DB), HOST_NAME=$(HOST_NAME)")
+3 -2
View File
@@ -9,8 +9,9 @@ export DEVICE_NAME="ePowerSwitch"
# This drivers only support 4 or 8 outlet
export EPOWERSWITCH_SOCKET_NUMBER=8
# The host name of the outlet
export HOST_NAME="eps-vms-1-weha.psi.ch"
# The host name and port of the ePowerSwitch device
export HOST_NAME="localhost"
export HOST_PORT="55555"
iocsh st.cmd