izuemon commited on
Commit
08493fc
·
verified ·
1 Parent(s): 329bfdb

Update turbowarp-server/qr-converter.py

Browse files
Files changed (1) hide show
  1. turbowarp-server/qr-converter.py +46 -14
turbowarp-server/qr-converter.py CHANGED
@@ -19,28 +19,34 @@ def get_var(name):
19
  try:
20
  value = tw.get_variable(name=name, name_literal=False)
21
 
22
- # 様々な型を文字列変換
 
 
 
23
  if value is None:
24
- return None
25
- elif isinstance(value, (int, float)):
 
 
 
26
  value = str(int(value)) # 浮動小数点の場合は整数に変換してから文字列に
27
- print(f"[GET] Variable '{name}' converted from number to string")
28
  elif not isinstance(value, str):
29
  value = str(value)
30
- print(f"[GET] Variable '{name}' converted from {type(value).__name__} to string")
31
 
32
  # 表示用に切り詰め
33
  display_value = value[:50] + "..." if len(value) > 50 else value
34
- #print(f"[GET] Variable '{name}' = {display_value}")
35
  return value
36
  except Exception as e:
37
  print(f"[ERROR] Failed to get variable '{name}': {e}")
38
- return None
39
 
40
  def set_var(name, value):
41
  try:
42
  tw.set_variable(name=name, value=value, name_literal=False)
43
- print(f"[SET] Variable '{name}' = {value[:50]}..." if len(value) > 50 else f"[SET] Variable '{name}' = {value}")
44
  return True
45
  except Exception as e:
46
  print(f"[ERROR] Failed to set variable '{name}': {e}")
@@ -135,13 +141,25 @@ def split_packets(data, max_length=9998):
135
  # --- メインループ ---
136
  print("[INFO] Starting main loop...")
137
  last_processed_id = "" # 最後に処理したIDを記録
 
 
138
  set_var("n0", "0")
139
  set_var("n1", "0")
 
 
140
  while True:
141
  try:
 
142
  n1 = get_var("n1")
143
  n0 = get_var("n0")
144
 
 
 
 
 
 
 
 
145
  # n1が存在し、長さが3以上で、先頭が"0"(新規リクエスト)
146
  if n1 and len(n1) >= 3 and n1[0] == "0":
147
  user_id = n1[1:3] # ユーザーID(2桁)
@@ -149,6 +167,7 @@ while True:
149
 
150
  print(f"[INFO] Received request from user {user_id}")
151
  print(f"[INFO] Request data length: {len(request_data)}")
 
152
 
153
  # 同じリクエストを繰り返し処理しないようにチェック
154
  if request_data == last_processed_id:
@@ -160,7 +179,7 @@ while True:
160
  continue
161
 
162
  # n0が利用可能かチェック
163
- if n0 == "0" or n0 is None:
164
  print("[INFO] n0 is free, starting processing...")
165
 
166
  # 処理中フラグをセット
@@ -172,7 +191,9 @@ while True:
172
 
173
  # プロンプトをデコード(先頭の"0" + ユーザーID(2桁)を除去)
174
  encoded_prompt = request_data[3:]
 
175
  prompt = decode_prompt(encoded_prompt)
 
176
 
177
  # 画像生成
178
  img = generate_image(prompt)
@@ -181,7 +202,7 @@ while True:
181
  # パケット分割
182
  packets = split_packets(scratch_data)
183
 
184
- # 最初のパケット送信
185
  for i, pkt in enumerate(packets):
186
  print(f"[INFO] Sending packet {i+1}/{len(packets)}")
187
  set_var("n1", "10" + pkt)
@@ -190,15 +211,17 @@ while True:
190
  # 次のパケットのACKを待つ
191
  print("[INFO] Waiting for ACK '11'...")
192
  ack_timeout = 0
 
193
  while ack_timeout < 50: # 5秒タイムアウト
194
  current_n1 = get_var("n1")
195
  if current_n1 == "11":
196
  print("[INFO] Received ACK")
 
197
  break
198
  time.sleep(0.3)
199
  ack_timeout += 1
200
 
201
- if ack_timeout >= 50:
202
  print("[ERROR] ACK timeout")
203
  set_var("n0", "0")
204
  break
@@ -206,28 +229,37 @@ while True:
206
  # 最後のパケットは完了を待つ
207
  print("[INFO] Waiting for completion '99'...")
208
  complete_timeout = 0
 
209
  while complete_timeout < 50:
210
  current_n1 = get_var("n1")
211
  if current_n1 == "99":
212
  print("[INFO] Received completion signal")
 
213
  break
214
  time.sleep(0.3)
215
  complete_timeout += 1
 
 
 
216
 
217
  # 完了後リセット
218
  print("[INFO] Transmission complete. Resetting n0.")
219
  set_var("n0", "0")
220
  last_processed_id = request_data # 処理済みとして記録
 
221
 
222
  else:
223
  print("[INFO] n0 is busy, waiting...")
224
  else:
225
- # 何もしない
226
- pass
 
 
 
227
 
228
  except Exception as e:
229
  print(f"[ERROR] Exception in main loop: {e}")
230
  import traceback
231
  traceback.print_exc()
232
 
233
- time.sleep(0.2)
 
19
  try:
20
  value = tw.get_variable(name=name, name_literal=False)
21
 
22
+ # デバッグ用生の値を出力
23
+ print(f"[DEBUG] Raw value of '{name}': {value} (type: {type(value).__name__})")
24
+
25
+ # Noneの場合は"0"として扱う
26
  if value is None:
27
+ print(f"[WARNING] Variable '{name}' is None, treating as '0'")
28
+ return "0"
29
+
30
+ # 様々な型を文字列に変換
31
+ if isinstance(value, (int, float)):
32
  value = str(int(value)) # 浮動小数点の場合は整数に変換してから文字列に
33
+ print(f"[GET] Variable '{name}' converted from number to string: '{value}'")
34
  elif not isinstance(value, str):
35
  value = str(value)
36
+ print(f"[GET] Variable '{name}' converted from {type(value).__name__} to string: '{value}'")
37
 
38
  # 表示用に切り詰め
39
  display_value = value[:50] + "..." if len(value) > 50 else value
40
+ print(f"[GET] Variable '{name}' = '{display_value}'")
41
  return value
42
  except Exception as e:
43
  print(f"[ERROR] Failed to get variable '{name}': {e}")
44
+ return "0" # エラー時も"0"を返す
45
 
46
  def set_var(name, value):
47
  try:
48
  tw.set_variable(name=name, value=value, name_literal=False)
49
+ print(f"[SET] Variable '{name}' = '{value[:50]}...'" if len(value) > 50 else f"[SET] Variable '{name}' = '{value}'")
50
  return True
51
  except Exception as e:
52
  print(f"[ERROR] Failed to set variable '{name}': {e}")
 
141
  # --- メインループ ---
142
  print("[INFO] Starting main loop...")
143
  last_processed_id = "" # 最後に処理したIDを記録
144
+
145
+ # 初期化時に変数をリセット
146
  set_var("n0", "0")
147
  set_var("n1", "0")
148
+ time.sleep(1) # 設定が反映されるのを待つ
149
+
150
  while True:
151
  try:
152
+ # 変数を取得
153
  n1 = get_var("n1")
154
  n0 = get_var("n0")
155
 
156
+ # デバッグ情報
157
+ print(f"[DEBUG] n0='{n0}', n1='{n1[:30]}...' if n1 and len(n1)>30 else n1")
158
+
159
+ # n0が利用可能かチェック(厳密な比較)
160
+ is_n0_free = (n0 == "0")
161
+ print(f"[DEBUG] is_n0_free: {is_n0_free}")
162
+
163
  # n1が存在し、長さが3以上で、先頭が"0"(新規リクエスト)
164
  if n1 and len(n1) >= 3 and n1[0] == "0":
165
  user_id = n1[1:3] # ユーザーID(2桁)
 
167
 
168
  print(f"[INFO] Received request from user {user_id}")
169
  print(f"[INFO] Request data length: {len(request_data)}")
170
+ print(f"[INFO] Request data preview: {request_data[:50]}...")
171
 
172
  # 同じリクエストを繰り返し処理しないようにチェック
173
  if request_data == last_processed_id:
 
179
  continue
180
 
181
  # n0が利用可能かチェック
182
+ if is_n0_free:
183
  print("[INFO] n0 is free, starting processing...")
184
 
185
  # 処理中フラグをセット
 
191
 
192
  # プロンプトをデコード(先頭の"0" + ユーザーID(2桁)を除去)
193
  encoded_prompt = request_data[3:]
194
+ print(f"[INFO] Encoded prompt length: {len(encoded_prompt)}")
195
  prompt = decode_prompt(encoded_prompt)
196
+ print(f"[INFO] Decoded prompt: '{prompt}'")
197
 
198
  # 画像生成
199
  img = generate_image(prompt)
 
202
  # パケット分割
203
  packets = split_packets(scratch_data)
204
 
205
+ # パケット送信
206
  for i, pkt in enumerate(packets):
207
  print(f"[INFO] Sending packet {i+1}/{len(packets)}")
208
  set_var("n1", "10" + pkt)
 
211
  # 次のパケットのACKを待つ
212
  print("[INFO] Waiting for ACK '11'...")
213
  ack_timeout = 0
214
+ ack_received = False
215
  while ack_timeout < 50: # 5秒タイムアウト
216
  current_n1 = get_var("n1")
217
  if current_n1 == "11":
218
  print("[INFO] Received ACK")
219
+ ack_received = True
220
  break
221
  time.sleep(0.3)
222
  ack_timeout += 1
223
 
224
+ if not ack_received:
225
  print("[ERROR] ACK timeout")
226
  set_var("n0", "0")
227
  break
 
229
  # 最後のパケットは完了を待つ
230
  print("[INFO] Waiting for completion '99'...")
231
  complete_timeout = 0
232
+ complete_received = False
233
  while complete_timeout < 50:
234
  current_n1 = get_var("n1")
235
  if current_n1 == "99":
236
  print("[INFO] Received completion signal")
237
+ complete_received = True
238
  break
239
  time.sleep(0.3)
240
  complete_timeout += 1
241
+
242
+ if not complete_received:
243
+ print("[ERROR] Completion timeout")
244
 
245
  # 完了後リセット
246
  print("[INFO] Transmission complete. Resetting n0.")
247
  set_var("n0", "0")
248
  last_processed_id = request_data # 処理済みとして記録
249
+ print(f"[INFO] Set last_processed_id to: {last_processed_id[:50]}...")
250
 
251
  else:
252
  print("[INFO] n0 is busy, waiting...")
253
  else:
254
+ # n1が条件を満たさない場合
255
+ if n1:
256
+ print(f"[DEBUG] n1 does not meet conditions: first char='{n1[0] if n1 else 'None'}', length={len(n1) if n1 else 0}")
257
+ else:
258
+ print("[DEBUG] n1 is None or empty")
259
 
260
  except Exception as e:
261
  print(f"[ERROR] Exception in main loop: {e}")
262
  import traceback
263
  traceback.print_exc()
264
 
265
+ time.sleep(0.5) # 少し長めの待機時間