pub struct DocumentSync { /* private fields */ }Expand description
Document synchronization manager for HIVE-Lite nodes
Manages the local CRDT state and handles document serialization/merging. Thread-safe for use from multiple BLE callbacks.
§Integration with Full HIVE
This implementation uses a simple GCounter suitable for embedded devices. For integration with the larger HIVE project using AutomergeIroh:
- The
build_document()output can be wrapped in an Automerge-compatible format - The observer events (Emergency, Ack, DocumentSynced) work with any backend
- The BLE transport layer is document-format agnostic
Implementations§
Source§impl DocumentSync
impl DocumentSync
Sourcepub fn with_peripheral_type(
node_id: NodeId,
callsign: &str,
ptype: PeripheralType,
) -> Self
pub fn with_peripheral_type( node_id: NodeId, callsign: &str, ptype: PeripheralType, ) -> Self
Create with a specific peripheral type
Sourcepub fn total_count(&self) -> u64
pub fn total_count(&self) -> u64
Get the total counter value
Sourcepub fn local_count(&self) -> u64
pub fn local_count(&self) -> u64
Get our counter contribution
Sourcepub fn current_event(&self) -> Option<EventType>
pub fn current_event(&self) -> Option<EventType>
Get current event type (if any)
Sourcepub fn is_emergency_active(&self) -> bool
pub fn is_emergency_active(&self) -> bool
Check if we’re in emergency state
Sourcepub fn is_ack_active(&self) -> bool
pub fn is_ack_active(&self) -> bool
Check if we’ve sent an ACK
Sourcepub fn send_emergency(&self, timestamp: u64) -> Vec<u8> ⓘ
pub fn send_emergency(&self, timestamp: u64) -> Vec<u8> ⓘ
Send an emergency - returns the document bytes to broadcast
Sourcepub fn send_ack(&self, timestamp: u64) -> Vec<u8> ⓘ
pub fn send_ack(&self, timestamp: u64) -> Vec<u8> ⓘ
Send an ACK - returns the document bytes to broadcast
Sourcepub fn clear_event(&self)
pub fn clear_event(&self)
Clear the current event
Sourcepub fn increment_counter(&self)
pub fn increment_counter(&self)
Increment the counter (for periodic sync)
Sourcepub fn update_health(&self, battery_percent: u8)
pub fn update_health(&self, battery_percent: u8)
Update health status (battery percentage)
Sourcepub fn update_activity(&self, activity: u8)
pub fn update_activity(&self, activity: u8)
Update activity level (0=still, 1=walking, 2=running, 3=fall)
Sourcepub fn update_health_full(&self, battery_percent: u8, activity: u8)
pub fn update_health_full(&self, battery_percent: u8, activity: u8)
Update full health status (battery and activity)
Sourcepub fn start_emergency(&self, timestamp: u64, known_peers: &[u32]) -> Vec<u8> ⓘ
pub fn start_emergency(&self, timestamp: u64, known_peers: &[u32]) -> Vec<u8> ⓘ
Start a new emergency event
Creates an emergency event that tracks ACKs from all known peers. Returns the document bytes to broadcast.
Sourcepub fn ack_emergency(&self, timestamp: u64) -> Option<Vec<u8>>
pub fn ack_emergency(&self, timestamp: u64) -> Option<Vec<u8>>
Record our ACK for the current emergency
Returns the document bytes to broadcast, or None if no emergency is active.
Sourcepub fn clear_emergency(&self)
pub fn clear_emergency(&self)
Clear the current emergency event
Sourcepub fn has_active_emergency(&self) -> bool
pub fn has_active_emergency(&self) -> bool
Check if there’s an active emergency
Sourcepub fn get_emergency_status(&self) -> Option<(u32, u64, usize, usize)>
pub fn get_emergency_status(&self) -> Option<(u32, u64, usize, usize)>
Get emergency status info
Returns (source_node, timestamp, acked_count, pending_count) if emergency is active.
Sourcepub fn has_peer_acked(&self, peer_id: u32) -> bool
pub fn has_peer_acked(&self, peer_id: u32) -> bool
Check if a specific peer has ACKed the current emergency
Sourcepub fn all_peers_acked(&self) -> bool
pub fn all_peers_acked(&self) -> bool
Check if all peers have ACKed the current emergency
Sourcepub fn add_chat_message(&self, sender: &str, text: &str, timestamp: u64) -> bool
pub fn add_chat_message(&self, sender: &str, text: &str, timestamp: u64) -> bool
Add a chat message to the local CRDT
Returns true if the message was new (not a duplicate).
Sourcepub fn add_chat_reply(
&self,
sender: &str,
text: &str,
reply_to_node: u32,
reply_to_timestamp: u64,
timestamp: u64,
) -> bool
pub fn add_chat_reply( &self, sender: &str, text: &str, reply_to_node: u32, reply_to_timestamp: u64, timestamp: u64, ) -> bool
Add a chat reply to the local CRDT
Returns true if the message was new.
Sourcepub fn chat_count(&self) -> usize
pub fn chat_count(&self) -> usize
Get the number of chat messages
Sourcepub fn chat_messages_since(
&self,
since_timestamp: u64,
) -> Vec<(u32, u64, String, String, u32, u64)>
pub fn chat_messages_since( &self, since_timestamp: u64, ) -> Vec<(u32, u64, String, String, u32, u64)>
Get chat messages newer than a timestamp
Returns a vector of (origin_node, timestamp, sender, text, reply_to_node, reply_to_timestamp) tuples.
Sourcepub fn all_chat_messages(&self) -> Vec<(u32, u64, String, String, u32, u64)>
pub fn all_chat_messages(&self) -> Vec<(u32, u64, String, String, u32, u64)>
Get all chat messages
Returns a vector of (origin_node, timestamp, sender, text, reply_to_node, reply_to_timestamp) tuples.
Sourcepub fn chat_snapshot(&self) -> Option<ChatCRDT>
pub fn chat_snapshot(&self) -> Option<ChatCRDT>
Get a snapshot of the chat CRDT
Sourcepub fn counter_entries(&self) -> Vec<(u32, u64)>
pub fn counter_entries(&self) -> Vec<(u32, u64)>
Get all counter entries for delta document building
Returns a vector of (node_id, count) pairs for all nodes that have contributed to the counter.
Sourcepub fn peripheral_snapshot(&self) -> Peripheral
pub fn peripheral_snapshot(&self) -> Peripheral
Get a clone of the peripheral state
Used for building delta documents with peripheral updates.
Sourcepub fn emergency_snapshot(&self) -> Option<EmergencyEvent>
pub fn emergency_snapshot(&self) -> Option<EmergencyEvent>
Get a clone of the emergency state
Used for building delta documents with emergency data.
Sourcepub fn build_document(&self) -> Vec<u8> ⓘ
pub fn build_document(&self) -> Vec<u8> ⓘ
Build the document for transmission
Returns the encoded bytes ready for BLE GATT write.
Sourcepub fn merge_document(&self, data: &[u8]) -> Option<MergeResult>
pub fn merge_document(&self, data: &[u8]) -> Option<MergeResult>
Merge a received document
Returns Some(MergeResult) if the document was valid, None otherwise.
The result contains information about what changed and any events.
Sourcepub fn decode_document(data: &[u8]) -> Option<HiveDocument>
pub fn decode_document(data: &[u8]) -> Option<HiveDocument>
Create a document from raw bytes (for inspection without merging)