-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_state_machine.py
More file actions
executable file
·241 lines (194 loc) · 7.46 KB
/
example_state_machine.py
File metadata and controls
executable file
·241 lines (194 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
"""
Example script showing how to use the Nimbie State Machine for disk processing.
This example demonstrates:
- Basic disk processing with accept/reject logic
- Error handling and recovery
- Progress tracking
- Manual mode for testing
"""
import hashlib
import random
import time
from nimbie import NimbieStateMachine
def verify_disk_content():
"""
Simulate disk content verification.
In a real application, this would read the disk and verify its contents.
"""
print(" Reading disk content...")
time.sleep(3) # Simulate time to read disk
# Simulate verification with 80% success rate
is_valid = random.random() < 0.8
if is_valid:
print(" Disk verification: PASSED")
else:
print(" Disk verification: FAILED")
return is_valid
def calculate_disk_checksum():
"""
Example of a more complex disk processing function.
In reality, this would read actual disk data.
"""
print(" Calculating disk checksum...")
time.sleep(2)
# Simulate checksum calculation
fake_data = f"disk_data_{time.time()}".encode()
checksum = hashlib.md5(fake_data).hexdigest()
print(f" Checksum: {checksum}")
# Simulate checking against expected checksum
# 90% success rate
return random.random() < 0.9
def main():
"""Main example demonstrating state machine usage."""
print("Nimbie State Machine Example")
print("=" * 50)
# Initialize the state machine
print("\nInitializing Nimbie State Machine...")
print("NOTE: State transitions will be logged automatically")
try:
sm = NimbieStateMachine(target_drive="1")
print("State machine initialized successfully!")
except Exception as e:
print(f"Failed to initialize: {e}")
print("Make sure the Nimbie is connected and powered on.")
return
# Check hardware state
print("\nChecking hardware state...")
hw_state = sm.get_hardware_state()
print(f" Disks available: {'Yes' if hw_state['disk_available'] else 'No'}")
print(f" Tray status: {'Open' if hw_state['tray_out'] else 'Closed'}")
print(f" Current state: {sm.state}")
# Example 1: Process a single disk
print("\n" + "=" * 50)
print("Example 1: Processing a single disk")
print("=" * 50)
if hw_state["disk_available"]:
processed, accepted = sm.process_one_disk(process_fn=verify_disk_content)
print(f"\nResult: Processed={processed}, Accepted={accepted}")
else:
print("No disks available. Please add disks to the input queue.")
# Example 2: Process multiple disks with progress tracking
print("\n" + "=" * 50)
print("Example 2: Batch processing with progress tracking")
print("=" * 50)
# Check if we have disks
hw_state = sm.get_hardware_state()
if hw_state["disk_available"]:
print("\nProcessing up to 5 disks...")
# Track progress
processed_count = 0
def process_with_progress():
nonlocal processed_count
processed_count += 1
print(f"\n Processing disk #{processed_count}")
return calculate_disk_checksum()
# Process batch
stats = sm.process_batch(count=5, process_fn=process_with_progress)
print("\nBatch processing complete!")
print(f" Total processed: {stats['total']}")
print(f" Accepted: {stats['accepted']}")
print(f" Rejected: {stats['rejected']}")
print(f" Success rate: {stats['accepted'] / stats['total'] * 100:.1f}%")
else:
print("No more disks available.")
# Example 3: Manual mode for testing/recovery
print("\n" + "=" * 50)
print("Example 3: Manual mode for testing/recovery")
print("=" * 50)
print("\nUsing manual mode to check hardware...")
with sm.manual_operation():
# Open and close tray
print(" Testing tray operation...")
sm.manual_open_tray()
time.sleep(1)
sm.manual_close_tray()
print(" Tray test complete")
# If disk available, test placement
if sm.get_hardware_state()["disk_available"]:
print(" Testing disk placement...")
sm.manual_open_tray()
sm.manual_place_disk()
print(" Disk placed on tray")
# Clean up
sm.manual_lift_disk()
sm.manual_close_tray()
sm.manual_accept_disk()
print(" Test cleanup complete")
# Example 4: Error recovery
print("\n" + "=" * 50)
print("Example 4: Error recovery demonstration")
print("=" * 50)
print("\nChecking if recovery is needed...")
hw_state = sm.get_hardware_state()
if hw_state["tray_out"] or hw_state["disk_lifted"]:
print("Hardware in non-standard state. Recovering...")
with sm.manual_operation():
# Handle lifted disk
if hw_state["disk_lifted"]:
print(" Disk is lifted, dropping it...")
if hw_state["tray_out"]:
sm.manual_close_tray()
sm.manual_accept_disk()
# Ensure tray is closed
if sm.get_hardware_state()["tray_out"]:
print(" Closing tray...")
sm.manual_close_tray()
# Reset state
sm.manual_set_state("idle")
print(" Recovery complete!")
else:
print("Hardware is in safe state. No recovery needed.")
# Final status
print("\n" + "=" * 50)
print("Final Status")
print("=" * 50)
final_state = sm.get_hardware_state()
print(f" State machine: {sm.state}")
print(f" Tray: {'Open' if final_state['tray_out'] else 'Closed'}")
print(f" Disks remaining: {'Yes' if final_state['disk_available'] else 'No'}")
print("\nExample complete!")
def continuous_processing_example():
"""
Example of continuous disk processing.
This would be used in a production environment.
"""
print("Continuous Disk Processing Example")
print("=" * 50)
print("Press Ctrl+C to stop\n")
sm = NimbieStateMachine(target_drive="1")
total_processed = 0
total_accepted = 0
def process_disk():
# Your actual disk processing logic here
time.sleep(5) # Simulate processing
return True # Accept all disks in this example
try:
while True:
# Check for available disks
if sm.get_hardware_state()["disk_available"]:
print(f"\nProcessing disk #{total_processed + 1}...")
processed, accepted = sm.process_one_disk(process_fn=process_disk)
if processed:
total_processed += 1
if accepted:
total_accepted += 1
print(
f"Total processed: {total_processed}, "
f"Accepted: {total_accepted}, "
f"Rejected: {total_processed - total_accepted}"
)
else:
print(".", end="", flush=True) # Waiting indicator
time.sleep(5) # Wait before checking again
except KeyboardInterrupt:
print(f"\n\nStopping... Final count: {total_processed} disks processed")
# Ensure hardware is in safe state
with sm.manual_operation():
if sm.get_hardware_state()["tray_out"]:
sm.manual_close_tray()
if __name__ == "__main__":
# Run the main example
main()
# Uncomment to run continuous processing example
# continuous_processing_example()