Skip to content
Commits on Source (2)
......@@ -7,25 +7,23 @@ import json
app = Flask(__name__)
socketio = SocketIO(app,
async_mode='threading',
# async_mode='threading',
binary=True,
)
# Function to generate a random NumPy matrix
def generate_matrix():
return np.random.rand(3, 3)
# Function to send the matrix to the client
def send_matrix():
matrix = generate_matrix()
matrix_json = json.dumps(matrix.tolist())
socketio.emit('matrix', matrix_json)
matrix = np.random.rand(3, 3).astype("uint8")
binary_data = matrix.tobytes()
socketio.emit(u'matrix', {"data": binary_data}) # object with binary stuff
socketio.emit(u'matrix', bytes({"data": [1,2,3]})) # normal "json" object
# Background thread to send the matrix every 5 seconds
def background_thread():
while True:
send_matrix()
socketio.sleep(5)
socketio.sleep(2)
# Route for the HTML page
......
......@@ -11,7 +11,7 @@
// Connect to Socket.IO server
// var socket = io.connect('http://' + document.domain + ':' + location.port);
const socket = io.connect({
// transports: ['websocket'],
transports: ['polling'],
});
// Event handlers
......@@ -28,10 +28,16 @@
});
socket.on('matrix', function(matrix_json) {
var matrix = JSON.parse(matrix_json);
socket.on('matrix', function(matrix) {
console.log('Received matrix:');
console.log(matrix);
try {
parsedJson = JSON.parse(new TextDecoder().decode(matrix));
console.log(parsedJson);
} catch (e) {
console.log(matrix);
}
});
// // Example: Sending a message
......
......@@ -26,47 +26,104 @@ Output
<div id="webgl" class="form mt-2 row">
<div class="col">
<figure class="card">
<canvas id="canvas-webgl" class="card-img-top"
data-status="canvas-webgl"
alt="canvas webgl">
<canvas id="cred-canvas" class="card-img-top"
data-status="canvas-webgl">
</canvas>
<figcaption class="card-body">
<h5 class="card-title">Webgl FITS</h5>
<h5 class="card-title">CRED</h5>
<fieldset class="row">
<fieldset class="row mt-2">
<div class="row mt-2">
<div class="input-group col">
<button data-control="get-fits-api" class="btn btn-primary w-100" type="button"
>Display http</button>
<div class="col-3 col-form-label">
<span>Color</span>
<label for="min-cred">min</label>
<span>,</span>
<label for="max-cred">max</label>
</div>
<div class="col">
<div class="input-group">
<input id="min-cred" type="number" class="form-control" value="2000">
<input id="max-cred" type="number" class="form-control" value="4000">
</div>
</div>
<div class="row mt-2">
<div class="input-group col">
<button data-control="get-fits" class="btn btn-primary w-100" type="button"
>Display websocket</button>
</fieldset>
<fieldset class="row mt-2">
<div class="col-3 col-form-label">
<label for="scale-cred">scale</label>
<span>,</span>
<label for="min-cred">x0</label>
<span>,</span>
<label for="max-cred">y0</label>
</div>
<div class="col">
<div class="input-group">
<input id="scale-cred" type="number" class="form-control" value="1">
<input id="x0-cred" type="number" class="form-control" value="0">
<input id="y0-cred" type="number" class="form-control" value="0">
</div>
</div>
</fieldset>
<label for="min_value">min</label>
<input id="min_value" type="number" min="0" max="65535" value="0" step="1">
<label for="max_value">max</label>
<input id="max_value" type="number" min="0" max="65535" value="56635" step="1">
<label for="scale_factor">scale</label>
<input id="scale_factor" type="number" min="0" max="10" value="1" step="0.1">
<label for="x_0">x0</label>
<input id="x_0" type="number" min="0" value="0" step="1">
<label for="y_0">y0</label>
<input id="y_0" type="number" min="0" value="0" step="1">
<aside class="row mt-2">
</aside>
</figcaption>
</figure>
</div>
<!-- **************************************************** -->
<div class="col">
<figure class="card">
<canvas id="dm-canvas" class="card-img-top"
data-status="canvas-webgl">
</canvas>
<figcaption class="card-body">
<h5 class="card-title">DM</h5>
<fieldset class="row mt-2">
<div class="col-3 col-form-label">
<span>Color</span>
<label for="min-dm">min</label>
<span>,</span>
<label for="max-dm">max</label>
</div>
<div class="col">
<div class="input-group">
<input id="min-dm" type="number" class="form-control" value="2000">
<input id="max-dm" type="number" class="form-control" value="4000">
</div>
</div>
</fieldset>
<fieldset class="row mt-2">
<div class="col-3 col-form-label">
<label for="scale-dm">scale</label>
<span>,</span>
<label for="min-dm">x0</label>
<span>,</span>
<label for="max-dm">y0</label>
</div>
<div class="col">
<div class="input-group">
<input id="scale-dm" type="number" class="form-control" value="1">
<input id="x0-dm" type="number" class="form-control" value="0">
<input id="y0-dm" type="number" class="form-control" value="0">
</div>
</div>
</fieldset>
......@@ -78,76 +135,37 @@ Output
</div>
</div>
<script>
////////////////////
// CRED
///////////////////
var matrix;
// $(document).on("click",'[data-control="get-fits-api"]', function(e){
// const ref = new Date().getTime()/1000
// e.preventDefault();
// $(this).crud("GET", "/web/other/fits/", null, false).then(function(e){
// console.log("here")
// })
// });
socket.on('data-cred', function(data) {
// $(document).on("click",'[data-control="get-fits"]', function(e){
// const ref = new Date().getTime()/1000
// e.preventDefault();
// button = $(this)
// button.prop('disabled', true);
// socket.emit('send_fits');
// });
//// Event listeners for min and max value inputs
var min = +$("#min-cred").val();
var max = +$("#max-cred").val();
var scale = +$("#scale-cred").val();
var x0 = +$("#x0-cred").val();
var y0 = +$("#y0-cred").val();
socket.on('data_fits',function(data){
matrix = data
drawMatrixWithWebGL(matrix, "canvas-webgl", 1, 0, 0, false)
$('[data-control="get-fits"]').prop('disabled', false);
console.log(difftime(data.timestamp))
})
var diff1 = difftime(data.timestamp)
drawMatrixWithWebGL("#cred-canvas", data, scale, x0, y0, min, max)
var diff2 = difftime(data.timestamp)
socket.on('data_binary', function(data) {
console.log("arrived")
// // Event listeners for min and max value inputs
// var minValue = parseFloat(document.getElementById("min_value").value);
// var maxValue = parseFloat(document.getElementById("max_value").value);
// var scaleFactor = parseFloat(document.getElementById("scale_factor").value);
// var x0 = parseFloat(document.getElementById("x_0").value);
// var y0 = parseFloat(document.getElementById("y_0").value);
// var binaryData = data.data
// var diff1 = difftime(data.timestamp)
// var matrixData = reform(binaryData, data.shape[0], data.shape[1], data.dtype);
// var diff2 = difftime(data.timestamp)
// drawMatrixWithWebGL(matrixData, "matrixBinary", scaleFactor, x0, y0)
// var diff3 = difftime(data.timestamp)
// // matrix = data
// // drawMatrixWithWebGL(matrix, "canvas-webgl", scaleFactor, x0, y0, false)
// var diff4 = difftime(data.timestamp)
// console.log("arrived, reformed, js canvas, webgl",
// console.log("arrived, drawed",
// diff1,
// (diff2-diff1).toFixed(3),
// (diff3-diff2).toFixed(3),
// (diff4-diff3).toFixed(3) )
});
// )
$("#scale_factor").change(function(e){
console.log("changed")
const val = +$(this).val()
drawMatrixWithWebGL(matrix, "canvas-webgl", val , 0, 0, false)
});
</script>
<script id="vertex-shader-2d" type="x-shader/x-vertex">
attribute vec2 a_position;
attribute vec2 a_texCoord;
......@@ -182,8 +200,38 @@ Output
</script>
{% endblock content %}
<script type="module">
////////////////////
// DM
///////////////////
import * as deformable from "/static/smooth3.js";
socket.on('data-dm', function(data) {
var scale = +$("#scale-dm").val();
var diff1 = difftime(data.timestamp)
var values = typeArray(data.data, data.dtype)
console.log(values)
// console.log(values)
deformable.render_dm(values.map(x => x * scale))
// //render_dm(data.data)
// var diff2 = difftime(data.timestamp)
// console.log((diff2-diff1).toFixed(3))
});
</script>
{% endblock content %}
{% block footer %}
{% endblock footer %}
......@@ -22,9 +22,11 @@ def enable(app):
socketio = SocketIO(app,
path="/web/socket",
async_mode='threading',
# async_mode='threading',
# transports=["websocket"],
threaded=True)
threaded=True,
binary=True,
)
connected_clients = {}
......@@ -69,16 +71,22 @@ def enable(app):
print('ERROR: {}'.format(e))
socketio.start_background_task(stream.send_timestamp, socketio=socketio)
socketio.start_background_task(stream.send_timestamp, socketio=socketio,
name=u'timestamp')
socketio.start_background_task(stream.send_status, socketio=socketio,
name="json-status", url="/api/devices/")
name=u"json-status", url="/api/devices/")
socketio.start_background_task(stream.send_status, socketio=socketio,
name="rtc-status", url="/api/rtc/plots/")
name=u"rtc-status", url="/api/rtc/plots/")
socketio.start_background_task(stream.tail_f, socketio=socketio,
name="new-lines")
name=u"new-lines")
socketio.start_background_task(stream.send_cred, socketio=socketio,
name=u"data-cred")
# socketio.start_background_task(stream.send_binary, socketio=socketio)
socketio.start_background_task(stream.send_dm, socketio=socketio,
name=u"data-dm")
return socketio
......@@ -7,20 +7,25 @@
import os
import time
from datetime import datetime
import json
from astropy.time import Time
# Third-party modules
from astropy.io import fits
import numpy as np
# Custom modules
from app import app
from util.master_listener_davide import thread
class Status:
"""store last status"""
def __init__(self):
self.last = {}
def initial(self, socketio, names):
socketio.sleep(1)
for name in names:
socketio.emit(name, self.last[name])
......@@ -30,13 +35,13 @@ status = Status()
def to_seconds(date):
return time.mktime(date.timetuple())
def send_timestamp(socketio):
def send_timestamp(socketio, name):
"""send a unix timestamp"""
while True:
unix = to_seconds(datetime.utcnow())
#print(unix)
socketio.emit('timestamp', unix)
socketio.emit(name, unix)
socketio.sleep(1)
......@@ -52,7 +57,7 @@ def send_status(socketio, name, url, sleep=1, once=False):
while not once:
socketio.sleep(sleep)
with app.test_client() as client:
res = client.get(url).get_json()
res = client.get(url).get_json()
status.last[name] = res
socketio.emit(name, res)
# if res != old:
......@@ -68,9 +73,10 @@ def tail_f(socketio, name, num_lines=30, sleep=0.25, once=False):
# First time
with open(filename, 'r') as f:
lines = f.readlines()[-num_lines:]
orig_lines = f.readlines()[-num_lines:]
lines = [unicode(l) for l in orig_lines]
# Initial lines on first page load
socketio.emit(name, lines)
socketio.emit(name, lines)
status.last[name] = lines
while not once:
......@@ -81,56 +87,105 @@ def tail_f(socketio, name, num_lines=30, sleep=0.25, once=False):
# File has been appended
with open(filename, 'r') as f:
f.seek(file_size)
new_lines = f.readlines()
orig_lines = f.readlines()
new_lines = [unicode(l) for l in orig_lines]
socketio.emit(name, new_lines)
file_size = current_size
if new_lines:
socketio.emit(name, new_lines)
status.last[name] = new_lines
socketio.sleep(sleep) # Sleep before checking again
def send_binary(socketio):
def send_binary(socketio, name):
"""send data in binary format"""
# Third-party modules
import numpy as np
while True:
# for i in range(1):
dtype = "uint16"
shape = (248,248)
matrix = np.random.uniform(0, 65535, shape).astype(dtype)
dtype = u"uint16"
# shape = (64,64)
# matrix = np.random.uniform(3000, 4000, shape).astype(dtype)
try:
matrix = thread.img.astype(dtype)
except:
print("dummy")
matrix = np.zeros(shape=(64,64)).astype(dtype)
shape = matrix.shape
binary_data = matrix.tobytes()
unix = to_seconds(datetime.utcnow())
unix = Time.now()
data_bundle = {
"shape": matrix.shape,
"dtype": dtype,
"data": binary_data,
"timestamp": unix,
"timestamp": unix.unix,
}
socketio.emit(name, data_bundle)
#print(matrix)
#print("bin {}".format(len(binary_data)))
socketio.emit('data_binary', data_bundle)
print((to_seconds(datetime.utcnow())-unix), "emitted")
print("bin {}".format(len(binary_data)))
socketio.sleep(0.2)
socketio.sleep(1)
def send_cred(socketio, name):
"""send data in binary format"""
while True:
socketio.sleep(0.2)
dtype = u"uint16"
try:
matrix = thread.img.astype(dtype)
except:
print("dummy")
matrix = np.zeros(shape=(64,64)).astype(dtype)
binary_data = matrix.tobytes()
now = Time.now()
data_bundle = {
"shape": matrix.shape,
"dtype": dtype,
"data": binary_data,
"timestamp": now.unix,
}
# print(matrix)
socketio.emit(name, data_bundle)
def send_fits(socketio):
"""download a fits"""
def send_dm(socketio, name):
"""send data in binary format"""
with fits.open("./temp.fits") as hdul:
matrix = hdul[0].data
header = hdul[0].header
unix = to_seconds(datetime.utcnow())
while True:
socketio.sleep(0.2)
dtype = u"float32"
try:
matrix = thread.shape.astype(dtype)
except:
print("dummy")
matrix = np.zeros(shape=(97)).astype(dtype)
binary_data = matrix.tobytes()
now = Time.now()
data_bundle = {
"shape": matrix.shape,
"dtype": matrix.dtype.name,
"data": matrix.tobytes(),
"header": [list(c) for c in header.cards],
"timestamp": unix,
}
socketio.emit('data_fits', data_bundle)
print((to_seconds(datetime.utcnow())-unix), "emitted")
print("bin {}".format(len(matrix)))
"dtype": dtype,
"data": binary_data,
"timestamp": now.unix,
}
#print(matrix)
socketio.emit(name, data_bundle)
# def send_fits(socketio):
# """download a fits"""
# with fits.open("./temp.fits") as hdul:
# matrix = hdul[0].data
# header = hdul[0].header
# unix = to_seconds(datetime.utcnow())
# data_bundle = {
# "shape": matrix.shape,
# "dtype": matrix.dtype.name,
# "data": matrix.tobytes(),
# "header": [list(c) for c in header.cards],
# "timestamp": unix,
# }
# socketio.emit('data_fits', data_bundle)
# print((to_seconds(datetime.utcnow())-unix), "emitted")
# print("bin {}".format(len(matrix)))
This diff is collapsed.
......@@ -9,6 +9,7 @@ var texcoordBuffer;
var texture;
var resolutionLocation;
var textureLocation; // Define texture location
var precomputedTextureData = []; // Precompute texture data offline
function difftime(unix_timestamp){
let client_unix_time = new Date().getTime()/1000.0
......@@ -26,9 +27,9 @@ function setRectangle(gl, x, y, width, height) {
}
// Function to setup WebGL resources
function setupWebGL(canvasId) {
function setupWebGL(selector) {
// Get the WebGL context
var canvas = document.getElementById(canvasId);
var canvas = document.querySelector(selector);
gl = canvas.getContext("webgl");
if (!gl) {
return false; // WebGL not supported
......@@ -54,50 +55,49 @@ function setupWebGL(canvasId) {
return true;
}
// Precompute texture data offline
var precomputedTextureData = [];
function typeArray(data, dtype) {
var typedArray = [];
var maxDataValue = 65535;
if (dtype === 'float32') {
typedArray = new Float32Array(data);
} else if (dtype === 'int32') {
typedArray = new Int32Array(data);
} else if (dtype === 'int16') {
typedArray = new Int16Array(data);
maxDataValue = 32767;
} else if (dtype === 'int8' || dtype === 'uint8') {
typedArray = new Uint8Array(data);
maxDataValue = 255;
} else if (dtype === 'uint16') {
typedArray = new Uint16Array(data);
} else {
console.error('Unsupported data type:', dtype);
return;
}
//console.log(typedArray)
return typedArray
}
function precomputeTextureData(data, min=3000, max=4000) {
var typedArray = data
function precomputeTextureData(data, width, height, dtype, is_mask=false) {
var typedArray = typeArray(data, dtype);
var maxDataValue = 65535
if (is_mask) {
for (var i = 0; i < typedArray.length; i++) {
var value = typedArray[i];
precomputedTextureData.push(0,255,255, value * 0.8);
}
} else {
for (var i = 0; i < typedArray.length; i++) {
var value = typedArray[i];
var index = Math.round((value / maxDataValue) * (viridis.length - 1));
var rgb = viridis[index];
var index = Math.round((value - min)
/ (max - min)
* (viridis.length - 1));
//console.log(value, index)
if (index > 255)
var rgb = viridis[255]
else if (index < 0)
var rgb = viridis[0]
else
var rgb = viridis[index];
precomputedTextureData.push(rgb[0], rgb[1], rgb[2], 255);
}
}
}
......@@ -123,18 +123,11 @@ function uploadTextureDataToGPU(width, height) {
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border, format, type, textureData);
}
// Function to upload matrix data to texture
function uploadMatrixData(data, width, height, dtype, is_mask) {
precomputedTextureData = [];
precomputeTextureData(data, width, height, dtype, is_mask);
uploadTextureDataToGPU(width, height);
}
// Function to draw matrix with WebGL, scaling each value by a custom factor and starting coordinates
function drawMatrixWithWebGL(data, canvasId, scaleFactor=1, x0=0, y0=0, is_mask=false) {
function drawMatrixWithWebGL(selector, data, scaleFactor=1, x0=0, y0=0, minDataValue, maxDataValue) {
// Check if WebGL resources need to be setup
if (!gl) {
if (!setupWebGL(canvasId)) {
if (!setupWebGL(selector)) {
console.error("WebGL not supported");
return;
}
......@@ -149,7 +142,9 @@ function drawMatrixWithWebGL(data, canvasId, scaleFactor=1, x0=0, y0=0, is_mask=
// Setup vertex position buffer with adjusted starting coordinates
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
setRectangle(gl, x0, y0, data.shape[0] * scaleFactor + x0, data.shape[1] * scaleFactor + y0);
setRectangle(gl, x0, y0,
data.shape[0] * scaleFactor + x0,
data.shape[1] * scaleFactor + y0);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
......@@ -170,8 +165,12 @@ function drawMatrixWithWebGL(data, canvasId, scaleFactor=1, x0=0, y0=0, is_mask=
gl.uniform2f(resolutionLocation, gl.canvas.width, gl.canvas.height);
// Upload matrix data to texture
uploadMatrixData(data.data, data.shape[0], data.shape[1], data.dtype, is_mask);
precomputedTextureData = [];
var typedarr = typeArray(data.data, data.dtype);
precomputeTextureData(typedarr, minDataValue, maxDataValue);
uploadTextureDataToGPU(data.shape[0], data.shape[1]);
// Draw the rectangle with scaled dimensions
gl.drawArrays(gl.TRIANGLES, 0, 6);
}
import * as THREE from "./three.module.js";
import {OrbitControls} from "./OrbitControls.js";
const canvas = document.getElementById("dm-canvas")
const renderer = new THREE.WebGLRenderer({canvas});
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(100, 1, 1, 1000);
camera.position.set(7,0,5);
const controls = new OrbitControls(camera, canvas);
// Lights
const ambient =new THREE.AmbientLight(0xffffff, 0.2);
scene.add(ambient);
const light1 = new THREE.DirectionalLight(0xffffff, 2);
light1.position.set(-8,0,2);
scene.add(light1);
const lightHelper1 = new THREE.DirectionalLightHelper(light1, 3);
scene.add(lightHelper1);
const points_material = new THREE.PointsMaterial({
//color: 0x99ccff,
vertexColors: true,
size: .1
})
const material = new THREE.MeshLambertMaterial({
color: "white",
vertexColors: true,
side: THREE.DoubleSide,
wireframe: false
});
var geom = new THREE.BufferGeometry()
// var gui = new dat.GUI();
// gui.add(mesh.material, "wireframe");
const cloud = new THREE.Points(geom, points_material);
const mesh = new THREE.Mesh(geom, material)
function render_dm(values) {
//var values = Array.from({length: 97}, () => (Math.random() -0.5) / 1.0 );
//console.log(values)
// Remove old meshes from the scene
scene.remove(scene.getObjectByName("piezo"));
scene.remove(scene.getObjectByName("coating"));
// Add z values
const points3d = [];
for (let i = 0; i < dm.actuators.length; i++) {
let x = dm.actuators[i][0] *10 -5
let y = dm.actuators[i][1] *10 -5
let z = values[i]
points3d.push(new THREE.Vector3(x, y, z));
}
// Set points to the geometry
geom.setFromPoints(points3d);
// Set colors to the geometry
const colors = color_scale(values)
geom.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3));
///////////////
/// Add piezos
cloud.name = "piezo"
scene.add(cloud);
///////////////
/// Add coating
// triangulate x, y
const indexDelaunay = Delaunator.from(
points3d.map(v => {
return [v.x, v.y];
})
);
const meshIndex = []; // delaunay index => three.js index
for (let i = 0; i < indexDelaunay.triangles.length; i++){
meshIndex.push(indexDelaunay.triangles[i]);
}
geom.setIndex(meshIndex);
geom.computeVertexNormals();
mesh.name = "coating"
scene.add(mesh);
}
function resize(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
function render(canvas) {
if (resize(renderer)) {
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
renderer.render(scene, camera);
requestAnimationFrame(render);
}
function color_scale(values) {
// Set colors for each vertex based on values
const colors = [];
const minValues = Math.min(...values)
const maxValues = Math.max(...values)
//console.log(minValues, maxValues)
for (let i = 0; i < values.length; i++) {
if (maxValues!=minValues) {
// Convert values to a range between 0 and 1
var index = Math.round( (values[i] - minValues) / (maxValues-minValues) * (brbg.length - 1));
var rgb = brbg[index];
} else {
var rgb = [255,255,255]
}
const color = new THREE.Color().setRGB(rgb[0]/255, rgb[1]/255, rgb[2]/255);
colors.push(color.r, color.g, color.b);
}
return colors
}
render(canvas)
export {render_dm}
......@@ -4,6 +4,7 @@
var socket = io.connect({
//transports: ['websocket'],
transports: ['polling'],
path: "/web/socket",
});
......@@ -24,9 +25,19 @@ socket.on('timestamp', function(server_unix_time) {
//console.log(server_unix_time)
var diff = difftime(server_unix_time)
$('[data-status=timestamp-diff]').text(diff)
// console.log(diff)
//console.log(diff)
});
// try {
// parsedJson = JSON.parse(new TextDecoder().decode(matrix));
// console.log(parsedJson);
// } catch (e) {
// console.log(matrix);
// }
///////////////////////
/// Logfile stream
///////////////////////
......
This diff is collapsed.