やりたいこと
Ajax を用いて、Pythonからデータを受け取り、それをもとに色分けをする関数を作っています。
pythonでgreen、yellow のそれぞれの IPアドレスを送信したら、「0」か「1」で返ってきます。
green が「1」なら緑で、yellow が「1」なら黄色で、greenもyellow も「0」なら赤色でcanvasで図形を描画したいです。
できていないこと
コンソールで確認したところ、色分けの関数が不十分で、canvasの図形描画がデフォルトの黒色になってしまいます。
Javascript
1 2 3var canvas1 = document.getElementById("myCanvas1"); 4var ctx1 = canvas1.getContext("2d"); 5let client_w = document.getElementById("myCanvas1").clientWidth; 6var e = new Date(); 7var now = new Date(); 8var time = parseInt(e.getHours()*3600) + parseInt(e.getMinutes()*60) + parseInt(e.getSeconds()); //3600 9var startpoint = (time/86400)*client_w*0.5; 10var cw = 0.0058 11 12 13 14console.log(time, startpoint, client_w); 15 16 17setInterval(repeat, 1000); 18 19function repeat() { 20 var now = new Date(); 21 time = parseInt(e.getHours()*3600) + parseInt(e.getMinutes()*60) + parseInt(e.getSeconds()); 22 23 if (time >= 86399){ 24 startpoint = 0; 25 ctx1.clearRect(0, 30, client_w, 60) 26 } 27 28 console.log(time, client_w); 29 ctx1.fillRect(startpoint, 30, 0.0058, 60); 30 startpoint = startpoint + cw; 31 32 console.log(startpoint); 33 34 $(function() { 35 function get_data(){ 36 $.ajax({ 37 url : '/data', 38 type : 'GET', 39 dataType : 'json', 40 }) 41 //通信成功時の処理 42 .done(function(data){ 43 44 if (data(['green']['machine1'])==1) {ctx1.fillStyle = "rgb(0, 255, 0)"; 45 } else if (data(['yellow']['machine1'])==1) { ctx1.fillStyle = "rgb(255, 255, 0)"; 46 } else { ctx1.fillStyle = "rgb(255, 0, 0)"; } 47 console.log(data(['green']['machine1'])); 48 }) 49 //通信失敗時の処理 50 .fail(function () { 51 console.log('データが取れていません'); 52 }); 53 54 } 55 56 57 58 console.log(startpoint); 59 }) 60} 61 62 63 64$(document).ready(function () { 65 66 get_data(); 67 setInterval(get_data, 10000); 68})
問題があるであろう箇所↓
Javascript
1$(function() { 2 function get_data(){ 3 $.ajax({ 4 url : '/data', 5 type : 'GET', 6 dataType : 'json', 7 }) 8 //通信成功時の処理 9 .done(function(data){ 10 11 if (data(['green']['machine1'])==1) {ctx1.fillStyle = "rgb(0, 255, 0)"; 12 } else if (data(['yellow']['machine1'])==1) { ctx1.fillStyle = "rgb(255, 255, 0)"; 13 } else { ctx1.fillStyle = "rgb(255, 0, 0)"; } 14 console.log(data(['green']['machine1'])); 15 }) 16 //通信失敗時の処理 17 .fail(function () { 18 console.log('データが取れていません'); 19 }); 20 21 } 22 23 24 25 console.log(startpoint); 26 }) 27} 28 29 30 31$(document).ready(function () { 32 33 get_data(); 34 setInterval(get_data, 10000); 35})
Python
1import socket 2from flask import Flask, render_template 3from flask_socketio import SocketIO,send, emit 4from decimal import Decimal 5import logging 6import socket 7import webbrowser 8import math 9import time 10import datetime 11import threading 12from openpyxl import * 13import pandas 14import json 15import threading 16 17machines_ip_address = ['ここにIPアドレス'] 18 19production_command = ['ここにIPアドレス'] 20 21cycletime_command = ['ここにIPアドレス'] 22 23green_command = ['ここにIPアドレス'] 24 25yellow_command = ['ここにIPアドレス'] 26 27 28def python_to_html(i,j,k,l,m): 29 30 time.sleep(3) 31 client5 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 32 33 try: 34 client5.connect((i,8501)) 35 except: 36 print("PLC接続NG") 37 return 0, 0 38 39 40 Production5 = j 41 Cycle_Time5= k 42 Green5 = l 43 Yellow5 = m 44 45 client5.send(Production5.encode("ascii")) 46 response5 = client5.recv(64) 47 response5 = response5.decode("UTF-8") 48 response5 = int(response5) 49 50 client5.send(Cycle_Time5.encode("ascii")) 51 response51 = client5.recv(64) 52 response51 = response51.decode("UTF-8") 53 response51 = int(response51) 54 response51 = response51 * Decimal('0.1') 55 56 client5.send(Green5.encode("ascii")) 57 response52 = client5.recv(64) 58 response52 = response52.decode("UTF-8") 59 response52 = int(response52) 60 61 client5.send(Yellow5.encode("ascii")) 62 response53 = client5.recv(64) 63 response53 = response53.decode("UTF-8") 64 response53 = int(response53) 65 66 67 68 print("send : " + Production5) 69 print("send : " + Cycle_Time5) 70 print("send : " + Green5) 71 print("send : " + Yellow5) 72 73 print("Received :" ,response5) 74 print("Received :" ,response51) 75 print("Received :" ,response52) 76 print("Received :" ,response53) 77 78 client5.close() 79 80 return response5, response51, response52, response53 81 82 83 84app = Flask(__name__) 85@app.route('/', methods=['GET']) 86def index(): 87 return render_template('test2.html') 88 89@app.route('/data', methods=['GET']) 90def send_data(): 91 one = python_to_html(" IPアドレス ") 92 93 94 json_data = {'production':{'machine1':one[0]}, 'cycletime':{'machine1':one[1]}} 95 return json_data
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/03 10:13
2022/03/03 13:02
2022/03/03 14:05 編集