echonet 1.3.2

ECHONET Lite framework for Rust
Documentation
// Copyright (C) 2022 The uecho-rs Authors All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::manufacture::*;
use crate::object::*;

#[cfg(feature = "once_cell")]
use once_cell::sync::Lazy;

#[cfg(not(feature = "once_cell"))]
use std::sync::LazyLock;

#[cfg(feature = "once_cell")]
static SHARED_STANDARD_DATABASE: Lazy<StandardDatabase> = Lazy::new(|| StandardDatabase::new());

#[cfg(not(feature = "once_cell"))]
static SHARED_STANDARD_DATABASE: LazyLock<StandardDatabase> =
    LazyLock::new(|| StandardDatabase::new());

/// StandardDatabase provides a standard database for ECHONET-Lite device objects and manufactures. This standard database was generated by importing Machine Readable Appendix and Manufacturer code List provided by the ECHONET Consortium.
/// # Examples
/// ```
/// use echonet::StandardDatabase;
///
/// let db = StandardDatabase::shared();
/// let m = db.find_manufacture(0x00000B);
/// assert!(m.is_some());
///
/// let obj = db.find_object(0x029100);
/// assert!(obj.is_some());
/// let prop = obj.unwrap().find_property(0xB0);
/// assert!(prop.is_some());
/// ```
pub struct StandardDatabase {
    manufactures: Vec<Manufacture>,
    objects: Vec<Object>,
}

impl StandardDatabase {
    pub fn new() -> StandardDatabase {
        let mut db = StandardDatabase {
            manufactures: Vec::new(),
            objects: Vec::new(),
        };
        db.init_manufactures();
        db.init_objects();
        db
    }

    #[cfg(feature = "once_cell")]
    pub fn shared() -> &'static Lazy<StandardDatabase> {
        &SHARED_STANDARD_DATABASE
    }

    #[cfg(not(feature = "once_cell"))]
    pub fn shared() -> &'static LazyLock<StandardDatabase> {
        &SHARED_STANDARD_DATABASE
    }

    pub fn add_manufacture(&mut self, man: Manufacture) -> bool {
        self.manufactures.push(man);
        true
    }

    pub fn find_manufacture(&self, code: ManufactureCode) -> Option<&Manufacture> {
        for n in 0..self.manufactures.len() {
            if self.manufactures[n].code() == code {
                return Some(&self.manufactures[n]);
            }
        }
        None
    }

    pub fn add_object(&mut self, obj: Object) -> bool {
        self.objects.push(obj);
        true
    }

    pub fn find_object(&self, code: ObjectCode) -> Option<&Object> {
        let class_group_code = code & 0xFFFF00;
        for n in 0..self.objects.len() {
            if self.objects[n].code() == class_group_code {
                return Some(&self.objects[n]);
            }
        }
        None
    }
}