izuemon commited on
Commit
09bbf20
·
verified ·
1 Parent(s): ade46c3

Update turbowarp-server/qr-converter.py

Browse files
Files changed (1) hide show
  1. turbowarp-server/qr-converter.py +85 -21
turbowarp-server/qr-converter.py CHANGED
@@ -18,7 +18,7 @@ print("[INFO] Connected successfully.")
18
  def get_var(name):
19
  try:
20
  value = tw.get_variable(name=name, name_literal=False)
21
- print(f"[GET] Variable '{name}' = {value}")
22
  return value
23
  except Exception as e:
24
  print(f"[ERROR] Failed to get variable '{name}': {e}")
@@ -27,11 +27,11 @@ def get_var(name):
27
  def set_var(name, value):
28
  try:
29
  tw.set_variable(name=name, value=value, name_literal=False)
30
- print(f"[SET] Variable '{name}' = {value}")
31
  return True
32
  except Exception as e:
33
  print(f"[ERROR] Failed to set variable '{name}': {e}")
34
- return False
35
 
36
  # --- n-chars.txt の読み込み ---
37
  print("[INFO] Loading n-chars.txt...")
@@ -47,7 +47,7 @@ def decode_prompt(encoded_str):
47
  if idx < len(n_chars):
48
  chars.append(n_chars[idx])
49
  decoded = "".join(chars)
50
- print(f"[DECODE] Encoded prompt '{encoded_str[:20]}...' -> '{decoded[:30]}...'")
51
  return decoded
52
 
53
  def rgb_to_scratch_number(rgb):
@@ -99,35 +99,99 @@ def split_packets(data, max_length=9998):
99
 
100
  # --- メインループ ---
101
  print("[INFO] Starting main loop...")
 
 
102
  while True:
103
  try:
104
  n1 = get_var("n1")
105
- if n1 and len(n1) >= 3 and n1[0] == "0": # 未処理フラグ
106
- print("[INFO] Received new request from n1.")
107
- if get_var("n0") == "0": # 他ユーザー未使用
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  set_var("n0", "1")
109
- user_id = n1[1:3] # 実際のIDはここを調整
110
- encoded_prompt = n1[3:]
 
 
 
 
 
111
  prompt = decode_prompt(encoded_prompt)
112
-
113
  # 画像生成
114
  img = generate_image(prompt)
115
  scratch_data = resize_and_encode(img)
116
-
117
- # パケット分割・送信
118
  packets = split_packets(scratch_data)
 
 
119
  for i, pkt in enumerate(packets):
 
120
  set_var("n1", pkt)
121
- time.sleep(0.2)
122
- while get_var("n1") != "11":
123
- time.sleep(0.1)
124
-
125
- # --- ここで既読フラグに変更 ---
126
- set_var("n1", "01" + n1[2:])
127
- print("[INFO] Marked n1 as processed.")
128
-
129
- # 処理完了後リセット
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  set_var("n0", "0")
 
 
 
 
 
 
 
 
131
  except Exception as e:
132
  print(f"[ERROR] Exception in main loop: {e}")
 
 
 
133
  time.sleep(0.2)
 
18
  def get_var(name):
19
  try:
20
  value = tw.get_variable(name=name, name_literal=False)
21
+ print(f"[GET] Variable '{name}' = {value[:50]}..." if value and len(value) > 50 else f"[GET] Variable '{name}' = {value}")
22
  return value
23
  except Exception as e:
24
  print(f"[ERROR] Failed to get variable '{name}': {e}")
 
27
  def set_var(name, value):
28
  try:
29
  tw.set_variable(name=name, value=value, name_literal=False)
30
+ print(f"[SET] Variable '{name}' = {value[:50]}..." if len(value) > 50 else f"[SET] Variable '{name}' = {value}")
31
  return True
32
  except Exception as e:
33
  print(f"[ERROR] Failed to set variable '{name}': {e}")
34
+ return None
35
 
36
  # --- n-chars.txt の読み込み ---
37
  print("[INFO] Loading n-chars.txt...")
 
47
  if idx < len(n_chars):
48
  chars.append(n_chars[idx])
49
  decoded = "".join(chars)
50
+ print(f"[DECODE] Encoded prompt -> '{decoded[:50]}...'")
51
  return decoded
52
 
53
  def rgb_to_scratch_number(rgb):
 
99
 
100
  # --- メインループ ---
101
  print("[INFO] Starting main loop...")
102
+ last_processed_id = "" # 最後に処理したIDを記録
103
+
104
  while True:
105
  try:
106
  n1 = get_var("n1")
107
+ n0 = get_var("n0")
108
+
109
+ # n1が存在し、長さが3以上で、先頭が"0"(新規リクエスト)
110
+ if n1 and len(n1) >= 3 and n1[0] == "0":
111
+ user_id = n1[1:3] # ユーザーID(2桁)
112
+ request_data = n1 # 完全なリクエストデータ
113
+
114
+ print(f"[INFO] Received request from user {user_id}")
115
+ print(f"[INFO] Request data length: {len(request_data)}")
116
+
117
+ # 同じリクエストを繰り返し処理しないようにチェック
118
+ if request_data == last_processed_id:
119
+ print("[INFO] Already processed this request, marking as read...")
120
+ # 既読マークをつける(先頭を"01"に変更)
121
+ marked_as_read = "01" + request_data[2:]
122
+ set_var("n1", marked_as_read)
123
+ time.sleep(0.1)
124
+ continue
125
+
126
+ # n0が利用可能かチェック
127
+ if n0 == "0" or n0 is None:
128
+ print("[INFO] n0 is free, starting processing...")
129
+
130
+ # 処理中フラグをセット
131
  set_var("n0", "1")
132
+
133
+ # リクエストを既読にする(先頭を"01"に変更)
134
+ marked_as_read = "01" + request_data[2:]
135
+ set_var("n1", marked_as_read)
136
+
137
+ # プロンプトをデコード(先頭の"0" + ユーザーID(2桁)を除去)
138
+ encoded_prompt = request_data[3:]
139
  prompt = decode_prompt(encoded_prompt)
140
+
141
  # 画像生成
142
  img = generate_image(prompt)
143
  scratch_data = resize_and_encode(img)
144
+
145
+ # パケット分割
146
  packets = split_packets(scratch_data)
147
+
148
+ # 最初のパケットを送信
149
  for i, pkt in enumerate(packets):
150
+ print(f"[INFO] Sending packet {i+1}/{len(packets)}")
151
  set_var("n1", pkt)
152
+
153
+ if i < len(packets) - 1:
154
+ # 次のパケットのACKを待つ
155
+ print("[INFO] Waiting for ACK '11'...")
156
+ ack_timeout = 0
157
+ while ack_timeout < 50: # 5秒タイムアウト
158
+ current_n1 = get_var("n1")
159
+ if current_n1 == "11":
160
+ print("[INFO] Received ACK")
161
+ break
162
+ time.sleep(0.1)
163
+ ack_timeout += 1
164
+
165
+ if ack_timeout >= 50:
166
+ print("[ERROR] ACK timeout")
167
+ set_var("n0", "0")
168
+ break
169
+ else:
170
+ # 最後のパケットは完了を待つ
171
+ print("[INFO] Waiting for completion '99'...")
172
+ complete_timeout = 0
173
+ while complete_timeout < 50:
174
+ current_n1 = get_var("n1")
175
+ if current_n1 == "99":
176
+ print("[INFO] Received completion signal")
177
+ break
178
+ time.sleep(0.1)
179
+ complete_timeout += 1
180
+
181
+ # 完了後リセット
182
+ print("[INFO] Transmission complete. Resetting n0.")
183
  set_var("n0", "0")
184
+ last_processed_id = request_data # 処理済みとして記録
185
+
186
+ else:
187
+ print("[INFO] n0 is busy, waiting...")
188
+ else:
189
+ # 何もしない
190
+ pass
191
+
192
  except Exception as e:
193
  print(f"[ERROR] Exception in main loop: {e}")
194
+ import traceback
195
+ traceback.print_exc()
196
+
197
  time.sleep(0.2)