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
pub use ffi::notification::{ ControllerState, ControllerError, NotificationType, NotificationCode, Notification as ExternNotification };
use ffi::notification as extern_notification;
use ffi::value_classes::value_id as extern_value_id;
use value_classes::value_id::ValueID;
use libc::c_char;
use ffi::utils::{ rust_string_creator, recover_string };
use node::Node;
use controller::Controller;

pub struct Notification {
    ptr: *const ExternNotification
}

impl Notification {
    pub fn new(ptr: *const ExternNotification) -> Notification {
        // Because the Notification object is not mutable, we might as well just fetch all
        // information right away and store it in a normal rust struct ?
        Notification {
            ptr: ptr
        }
    }

    pub fn get_type(&self) -> NotificationType {
        unsafe { extern_notification::notification_get_type(self.ptr) }
    }

    pub fn get_home_id(&self) -> u32 {
        unsafe { extern_notification::notification_get_home_id(self.ptr) }
    }

    pub fn get_controller(&self) -> Controller {
        Controller::new(self.get_home_id())
    }

    pub fn get_node_id(&self) -> u8 {
        unsafe { extern_notification::notification_get_node_id(self.ptr) }
    }

    pub fn get_node(&self) -> Node {
        Node::from_id(self.get_home_id(), self.get_node_id())
    }

    pub fn get_value_id(&self) -> ValueID {
        unsafe {
            let ozw_vid = extern_notification::notification_get_value_id(self.ptr);

            ValueID::from_packed_id(extern_value_id::value_id_get_home_id(&ozw_vid),
                                    extern_value_id::value_id_get_id(&ozw_vid))
        }
    }

    pub fn get_group_idx(&self) -> Option<u8> {
        match self.get_type() {
            NotificationType::Type_Group =>
                Some(unsafe { extern_notification::notification_get_group_idx(self.ptr) }),
            _ => None
        }
    }

    pub fn get_event(&self) -> Option<u8> {
        match self.get_type() {
            NotificationType::Type_NodeEvent | NotificationType::Type_ControllerCommand =>
                Some(unsafe { extern_notification::notification_get_event(self.ptr) }),
            _ => None
        }
    }

    pub fn get_button_id(&self) -> Option<u8> {
        match self.get_type() {
            NotificationType::Type_CreateButton | NotificationType::Type_DeleteButton |
            NotificationType::Type_ButtonOn | NotificationType::Type_ButtonOff =>
                Some(unsafe { extern_notification::notification_get_button_id(self.ptr) }),
            _ => None
        }
    }

    pub fn get_scene_id(&self) -> Option<u8> {
        match self.get_type() {
            NotificationType::Type_SceneEvent =>
                Some(unsafe { extern_notification::notification_get_scene_id(self.ptr) }),
            _ => None
        }
    }

    pub fn get_controller_state(&self) -> Option<ControllerState> {
        if self.get_type() != NotificationType::Type_ControllerCommand {
            return None;
        }
        ControllerState::from_u8(unsafe { extern_notification::notification_get_notification(self.ptr) })
    }

    pub fn get_notification_code(&self) -> Option<NotificationCode> {
        if self.get_type() != NotificationType::Type_Notification {
            return None;
        }
        NotificationCode::from_u8(unsafe { extern_notification::notification_get_notification(self.ptr) })
    }

    pub fn get_byte(&self) -> u8 {
        unsafe { extern_notification::notification_get_byte(self.ptr) }
    }

    pub fn get_as_string(&self) -> String {
        recover_string(
            unsafe {
                extern_notification::notification_get_as_string(self.ptr, rust_string_creator)
            } as *mut c_char
        )
    }
}

use std::fmt;
impl fmt::Display for Notification {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.get_as_string())
    }
}

impl fmt::Debug for Notification {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Notification {{ type: {:?}, home_id: {:?}, group_idx: {:?}, event: {:?}, button_id: {:?}, scene_id: {:?}, notification: {:?}, byte: {:?}, as_string: {:?}, \
                   value_id: {:?} }}",
               self.get_type(),
               self.get_home_id(),
               self.get_group_idx(),
               self.get_event(),
               self.get_button_id(),
               self.get_scene_id(),
               self.get_notification_code(),
               self.get_byte(),
               self.get_as_string(),
               self.get_value_id()
        )
    }
}