智能家居通用管理平台 - 家居设备驱动程序的编写

Posted ionfox

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了智能家居通用管理平台 - 家居设备驱动程序的编写相关的知识,希望对你有一定的参考价值。

    前面提到过,使用家居设备驱动程序(以后简称SHDD)是SHP平台监控不同厂商设备的核心机制,并且在ISmartHome.cs文档中,规范了驱动程序的内容。当然,遗憾的是,它仅仅只是一个契约,理解实现起来有一定的困难和疑惑,本节通过编写一个具体的设备驱动程序,来帮助您编写SHDD。
    首先,我们需要生产一个设备:智能冰箱。当然,我们不是电商,目前的智能冰箱业没有我们期望的功能。怎么办?OK,那就设计一个模拟的智能冰箱吧。
    用C#编写一个模拟冰箱(暂不考虑智能化),很简单,2天就写完了。运行界面如下:

这样模拟操作“智能冰箱”,鼠标点击冰箱图片:模拟打开/关闭冰箱门,右键点击是放入一个食品,左键点击是取走一个食品(未来的智能冰箱,很可能支持RFID,而食品都贴有电子标签);冰箱有三个储存室,温度可以单独设定;冰箱门30秒未关会报警,冰箱有故障也会报警;冰箱有待机和工作两种状态,工作状态有两种
模式:省电模式和正常制冷模式。
接下来,如何为这个智能冰箱编写驱动程序。
驱动程序暴露设备的数据、操作功能,所以首先要决定哪些功能期望暴露出去。我们这样设计它的子设备(你也可以增加或隐藏一些子设备):
DO设备2个
    DO0:冰箱待机/制冷开关
    DO1:正常/省电模式转换开关
DI设备2个
    DI0:冰箱门未关信号
    DI 1:冰箱故障报警信号
AO设备3个
    3个储存室的设定温度
AI设备3个
    3个储存室的实际温度
SO设备0个
    目前不需要,用过将来支持语音功能,可添加
SI设备4个
    SI0:放入一个食品,报告放入食品的名称和过期日期
    SI1:取走一个食品,报告取走食品的名称和过期日期
    SI2:报告冰箱所有过期食品的列表
    SI3:报告冰箱所有食品的列表(不主动发送)
   现在,打开VS2013,在解决方案中添加一个类库项目,取名叫RefrigeratorDriver。在项目属性中,修改命名空间为eaglesmartrefrigerator。使用.NET framework 4版本。生成的程序集也改为eaglesmartrefrigerator。保存。

   重新打开项目文件,添加对HomeLibrary类库的引用,并重命名类:public class SmartHome : ISmartHome, IWriteReadInterface;关键:必须实现ISmartHome接口和IWriteReadInterface接口。接口是可以多继承的。
    cs文件名也重命名为RefrigeratorDriver.cs。然后在类定义的 ISmartHome接口处弹右键,选择“实现接口”,这样,在源代码中,VS给您生成了整个代码的框架。你现在需要做的,就是实现这些代码(适当添加内部使用的一些属性和方法,帮助你实现接口)。见下图

    先不去编码实现这个接口。我们接下来,先实现设备类和6个子设备类。添加7个类的定义:

public class HomeDevice : IHomeDevice  
public class DeviceDO : IDeviceDO  
public class DeviceDI : IDeviceDI  
public class DeviceAO : IDeviceAO  
public class DeviceSO : IDeviceSO  
public class DeviceSI : IDeviceSI  

    同样的方法生成7各类的代码框架。
    好了,有了8个类。它们代表了整个设备系统具有的功能。先实现那个接口较好?经过反复实践,我们发现,6个子设备几乎可以设计得一样(通过把操作子设备的方法移到上层的设备类去实现后达到这样的效果);一般来说,具有整体 - 部分关系的结构类,“整体类”具有管理操控“部分”类的责任。而我们的软件架构正是这样设计的,如果忘记了,请参阅本系列博文的第二部分。
    就这样,先把6个子类实现了。他们以后都不需要改动。在您编写其他设备系统的驱动程序时,直接复制过去接OK了。简单吧。下面是实现的完整免费代码(转载务必注明来源):
#region 后面6个子设备,都一样,复制到新的设备驱动程序源代码中即可
    public class DeviceDO : IDeviceDO  // author吴志辉 2014.4
   
        private DeviceType devicetype;
        public DeviceType DeviceType
       
            get
           
                return devicetype;
           
            set
           
                devicetype = value;
           
       
        private DOType dotype;          //7、DO类型
        public DOType DoType
       
            get
           
                return dotype;
           
            set
           
                dotype = value;
           
       

        private HomeLibrary.Devices.PowerState powerstate; //6、上电状态
        public HomeLibrary.Devices.PowerState PowerState
       
            get
           
                return powerstate;
           
            set
           
                if (powerstate != value)
               
                    powerstate = value;
                    if (OnDigitalDataChanged != null)
                        OnDigitalDataChanged(new DataChangedEventArgs<bool>(this, ON));
               
           
       

        public bool ON  //通断状态:计算字段
       
            get
           
                if (PowerState == HomeLibrary.Devices.PowerState.PowerON)  //电源接通
               
                    return (DoType == DOType.Open) ? true : false;
               
                else
               
                    return (DoType == DOType.Open) ? false : true;
               
           
       

        private int parentid;  //1、设备编号
        public int ParentID
       
            get
           
                return parentid;
           
            set
           
                parentid = value;
           
       

        private ushort id;          //2、子设备编号
        public ushort Id
       
            get
           
                return id;
           
            set
           
                id = value;
           
       

        private int tag;          //3、子设备编号
        public int Tag
       
            get
           
                return tag;
           
            set
           
                tag = value;
           
       

        private string unitname;  //4、计量单位文本描述
        public string UnitName
       
            get
           
                return unitname;
           
            set
           
                unitname = value;
           
       

        private string functiondescription;  //5、DO子设备功能描述
        public string FunctionDescription
       
            get
           
                return functiondescription;
           
            set
           
                functiondescription = value;
           
       

        private string pictureoff;  //8、图像文件
        public string PictureOFF
       
            get
           
                return pictureoff;
           
            set
           
                pictureoff = value;
           
       

        private string pictureon;  //9、图像文件
        public string PictureON
       
            get
           
                return pictureon;
           
            set
           
                pictureon = value;
           
       
        public DeviceDO()  //构造函数
       
            Id = 0;
            devicetype = DeviceType.DO;
            DoType = DOType.Open;           //常开开关
            PowerState = HomeLibrary.Devices.PowerState.PowerON;  //电源断开           
            PictureOFF = "";
            PictureON = "";
            UnitName = "";
            functiondescription = "DO子设备";
       

        public void ReadFromStream(BinaryReader br)
       
            devicetype = (DeviceType)br.ReadByte();
            parentid = br.ReadInt32();     //1、设备编号
            id = br.ReadUInt16();          //2、子设备编号
            tag = br.ReadInt32();          //3、tag
            unitname = br.ReadString();    //4、计量单位文本描述
            functiondescription = br.ReadString();      //5、DO子设备功能描述
            dotype = (DOType)br.ReadByte();             //6、DO类型
            powerstate = (HomeLibrary.Devices.PowerState)br.ReadByte();     //7、上电状态
            pictureoff = br.ReadString(); //8、图像文件
            pictureon = br.ReadString();  //9、图像文件
       
        public void WriteToStream(BinaryWriter bw)
       
            bw.Write((byte)devicetype);
            bw.Write(parentid);    //1、设备编号
            bw.Write(id);          //2、子设备编号
            bw.Write(tag);         //3、tag对象
            bw.Write(unitname);    //4、计量单位文本描述
            bw.Write(functiondescription);  //5、DO子设备功能描述
            bw.Write((byte)dotype);         //6、DO类型
            bw.Write((byte)powerstate);     //7、上电状态
            bw.Write(pictureoff);           //8、图像文件
            bw.Write(pictureon);            //9、图像文件
       

        public event DigitalDataChanged OnDigitalDataChanged;
   
    public class DeviceAO : IDeviceAO
   
        private DeviceType devicetype;
        public DeviceType DeviceType
       
            get
           
                return devicetype;
           
            set
           
                devicetype = value;
           
       
        private int parentid;  //1、设备编号
        public int ParentID
       
            get
           
                return parentid;
           
            set
           
                parentid = value;
           
       

        private ushort id;          //2、子设备编号
        public ushort Id
       
            get
           
                return id;
           
            set
           
                id = value;
           
       

        private int tag;          //3、子设备编号
        public int Tag
       
            get
           
                return tag;
           
            set
           
                tag = value;
           
       


        public string unitname;   //4、计量单位文本描述
        public string UnitName
       
            get
           
                return unitname;
           
            set
           
                unitname = value;
           
       

        private string functiondescription;  //5、AO子设备功能描述
        public string FunctionDescription
       
            get
           
                return functiondescription;
           
            set
           
                functiondescription = value;
           
       

        private float aovalue;  //6、AO数据
        public float AoValue
       
            get
           
                return aovalue;
           
            set
           
                if (aovalue != value)
               
                    aovalue = value;
                    if (OnAnalogDataChanged != null)
                        OnAnalogDataChanged(new DataChangedEventArgs<float>(this, aovalue));
               
           
       

        private string picture; //7、图像文件
        public string Picture
       
            get
           
                return picture;
           
            set
           
                picture = value;
           
       
        byte dotplace;
        public byte DotPlace
       
            get
           
                return dotplace;
           
            set
           
                if (value != dotplace)
                    dotplace = value;
           
       

        public DeviceAO()
       
            Id = 0;
            devicetype = DeviceType.AO;
            Picture = "";
            UnitName = "";
            DotPlace = 1;
            functiondescription = "AO子设备";
       
        public void ReadFromStream(BinaryReader br)
       
            devicetype = (DeviceType)br.ReadByte();
            parentid = br.ReadInt32();     //1、设备编号
            id = br.ReadUInt16();          //2、子设备编号
            tag = br.ReadInt32();          //3、tag
            unitname = br.ReadString();    //4、计量单位文本描述
            functiondescription = br.ReadString();      //5、DO子设备功能描述
            aovalue = br.ReadSingle();                  //6、模拟量数据
            picture = br.ReadString();                  //7、图像文件
            dotplace = br.ReadByte();                   //8、小数点位数
       
        public void WriteToStream(BinaryWriter bw)
       
            bw.Write((byte)devicetype);
            bw.Write(parentid);    //1、设备编号
            bw.Write(id);          //2、子设备编号
            bw.Write(tag);         //3、tag
            bw.Write(unitname);    //4、计量单位文本描述
            bw.Write(functiondescription);  //5、AO子设备功能描述
            bw.Write(aovalue);              //6、模拟量数据
            bw.Write(picture);              //7、图像文件
            bw.Write(dotplace);             //8、小数点位数
       

        public event AnalogDataChanged OnAnalogDataChanged;

   
    public class DeviceDI : IDeviceDI
   
        private DeviceType devicetype;
        public DeviceType DeviceType
       
            get
           
                return devicetype;
           
            set
           
                devicetype = value;
           
       
        private int parentid;  //1、设备编号
        public int ParentID
       
            get
           
                return parentid;
           
            set
           
                parentid = value;
           
       
        private ushort id;          //2、子设备编号
        public ushort Id
       
            get
           
                return id;
           
            set
           
                id = value;
           
       

        private int tag;          //3、子设备编号
        public int Tag
       
            get
           
                return tag;
           
            set
           
                tag = value;
           
       

        public string unitname;   //4、计量单位文本描述
        public string UnitName
       
            get
           
                return unitname;
           
            set
           
                unitname = value;
           
       

        private string functiondescription;  //5、AO子设备功能描述
        public string FunctionDescription
       
            get
           
                return functiondescription;
           
            set
           
                functiondescription = value;
           
       

        private bool hassignal;  //6、是否有信号
        public bool HasSignal
       
            get
           
                return hassignal;
           
            set
           
                if (hassignal != value)
               
                    hassignal = value;
                    if (OnDigitalDataChanged != null)
                        OnDigitalDataChanged(new DataChangedEventArgs<bool>(this, hassignal));
               
           
       

        private string pictureoff;  //7、OFF图像
        public string PictureOFF
       
            get
           
                return pictureoff;
           
            set
           
                pictureoff = value;
           
       

        private string pictureon;   //8、ON图像
        public string PictureON
       
            get
           
                return pictureon;
           
            set
           
                pictureon = value;
           
       

        public DeviceDI()
       
            devicetype = DeviceType.DI;
            functiondescription = "DI子设备";
            PictureOFF = "";
            PictureON = "";
            UnitName = "";
       
        public void ReadFromStream(BinaryReader br)
       
            devicetype = (DeviceType)br.ReadByte();
            parentid = br.ReadInt32();     //1、设备编号
            id = br.ReadUInt16();          //2、子设备编号
            tag = br.ReadInt32();          //3、tag
            unitname = br.ReadString();    //4、计量单位文本描述
            functiondescription = br.ReadString();      //5、DI子设备功能描述
            hassignal = br.ReadBoolean();               //6、是否有信号
            pictureoff = br.ReadString(); //7、图像文件
            pictureon = br.ReadString();  //8、图像文件
       
        public void WriteToStream(BinaryWriter bw)
       
            bw.Write((byte)devicetype);
            bw.Write(parentid);    //1、设备编号
            bw.Write(id);          //2、子设备编号
            bw.Write(tag);         //3、tag
            bw.Write(unitname);    //4、计量单位文本描述
            bw.Write(functiondescription);  //5、DI子设备功能描述
            bw.Write(hassignal);            //6、是否有信号
            bw.Write(pictureoff);           //7、图像文件
            bw.Write(pictureon);            //8、图像文件
       

        public event DigitalDataChanged OnDigitalDataChanged;

   
    public class DeviceAI : IDeviceAI
   
        private DeviceType devicetype;
        public DeviceType DeviceType
       
            get
           
                return devicetype;
           
            set
           
                devicetype = value;
           
       
        private int parentid;  //1、设备编号
        public int ParentID
       
            get
           
                return parentid;
           
            set
           
                parentid = value;
           
       

        private ushort id;          //2、子设备编号
        public ushort Id
       
            get
           
                return id;
           
            set
           
                id = value;
           
       

        private int tag;           //3、通用数据
        public int Tag
       
            get
           
                return tag;
           
            set
           
                tag = value;
           
       

        public string unitname;   //4、计量单位文本描述
        public string UnitName
       
            get
           
                return unitname;
           
            set
           
                unitname = value;
           
       

        private string functiondescription;  //5、AO子设备功能描述
        public string FunctionDescription
       
            get
           
                return functiondescription;
           
            set
           
                functiondescription = value;
           
       

        private float aivalue;
        public float AiValue     //6、采集的模拟量数据
       
            get
           
                return aivalue;
           
            set
           
                if (aivalue != value)
               
                    aivalue = value;
                    if (OnAnalogDataChanged != null)
                        OnAnalogDataChanged(new DataChangedEventArgs<float>(this, aivalue));
               
           
       

        private string picture;  //7、图像文件
        public string Picture
       
            get
           
                return picture;
           
            set
           
                picture = value;
           
       
        byte dotplace;
        public byte DotPlace
       
            get
           
                return dotplace;
           
            set
           
                if (value != dotplace)
                    dotplace = value;
           
       

        public DeviceAI()
       
            devicetype = DeviceType.AI;
            functiondescription = "AI子设备";
            Picture = "";
            UnitName = "";
            dotplace = 0;
       
        public void ReadFromStream(BinaryReader br)
       
            devicetype = (DeviceType)br.ReadByte();
            parentid = br.ReadInt32();     //1、设备编号
            id = br.ReadUInt16();          //2、子设备编号
            tag = br.ReadInt32();            //3、tag
            unitname = br.ReadString();    //4、计量单位文本描述
            functiondescription = br.ReadString();      //5、DI子设备功能描述
            aivalue = br.ReadSingle();                  //6、采集的模拟量数据
            picture = br.ReadString();                  //7、图像文件
            dotplace = br.ReadByte();                   //8、小数点位数
       

        public void WriteToStream(BinaryWriter bw)
       
            bw.Write((byte)devicetype);
            bw.Write(parentid);    //1、设备编号
            bw.Write(id);          //2、子设备编号
            bw.Write(tag);         //3、tag
            bw.Write(unitname);    //4、计量单位文本描述
            bw.Write(functiondescription);  //5、子设备功能描述
            bw.Write(aivalue);              //6、采集的模拟量数据
            bw.Write(picture);              //7、图像文件
            bw.Write(dotplace);             //8、小数点位数
       

        public event AnalogDataChanged OnAnalogDataChanged;

   
    public class DeviceSI : IDeviceSI
   
        public event StreamDataChanged OnStreamDataChanged;
        private DeviceType devicetype;
        public DeviceType DeviceType
       
            get
           
                return devicetype;
           
            set
           
                devicetype = value;
           
       
        private int parentid;    //1、设备编号
        public int ParentID
       
            get
           
                return parentid;
           
            set
           
                parentid = value;
           
       

        private ushort id;          //2、子设备编号
        public ushort Id
       
            get
           
                return id;
           
            set
           
                id = value;
           
       

        private int tag;           //3、通用数据
        public int Tag
       
            get
           
                return tag;
           
            set
           
                tag = value;
           
       

        private StreamType streamtype;  //4、流类型
        public StreamType StreamType
       
            get
           
                return streamtype;
           
            set
           
                streamtype = value;
           
       

        public string unitname;         //5、计量单位文本描述
        public string UnitName
       
            get
           
                return unitname;
           
            set
           
                unitname = value;
           
       

        private string functiondescription;  //6、子设备功能描述
        public string FunctionDescription
       
            get
           
                return functiondescription;
           
            set
           
                functiondescription = value;
           
       

        private string picture;  //7、图像文件
        public string Picture
       
            get
           
                return picture;
           
            set
           
                picture = value;
           
       

        private byte[] sivalue; //8、字节流数据,一般不保存
        public byte[] SiValue
       
            get
           
                return sivalue;
           
            set
           
                if (MemoryCompare(sivalue, value) != 0)
               
                    sivalue = new byte[value.Length];
                    Buffer.BlockCopy(value, 0, sivalue, 0, value.Length);
                    if (OnStreamDataChanged != null)
                        OnStreamDataChanged(new DataChangedEventArgs<byte[]>(this, sivalue));
               
           
       

        public static int MemoryCompare(byte[] b1, byte[] b2)
       
            int result = 0;
            //if (b1 == null && b2 == null) return 0;
            if (b1 == null) return -1;
            if (b2 == null) return 1;
            if (b1.Length != b2.Length)
                result = b1.Length - b2.Length;
            else
           
                for (int i = 0; i < b1.Length; i++)
               
                    if (b1[i] != b2[i])
                   
                        result = (int)(b1[i] - b2[i]);
                        break;
                   
               
           
            return result;
       

        public DeviceSI()
       
            devicetype = DeviceType.SI;
            functiondescription = "SI子设备";
            Picture = "";
            UnitName = "";
            sivalue = new byte[0];
            streamtype = StreamType.TEXT;
       
        public void ReadFromStream(BinaryReader br)
       
            devicetype = (DeviceType)br.ReadByte();
            parentid = br.ReadInt32();        //1、设备编号
            id = br.ReadUInt16();             //2、子设备编号
            tag = br.ReadInt32();             //3、tag
            streamtype = (StreamType)br.ReadByte(); //4流类型
            unitname = br.ReadString();             //5、计量单位文本描述
            functiondescription = br.ReadString();  //6、子设备功能描述
            picture = br.ReadString();              //7、图像文件
       

        public void WriteToStream(BinaryWriter bw)
       
            bw.Write((byte)devicetype);
            bw.Write(parentid);    //1、设备编号
            bw.Write(id);          //2、子设备编号
            bw.Write(tag);         //3、tag
            bw.Write((byte)streamtype); //4流类型
            bw.Write(unitname);    //5、计量单位文本描述
            bw.Write(functiondescription);  //6、子设备功能描述
            bw.Write(picture);              //7、图像文件
       
   
    public class DeviceSO : IDeviceSO
   

        public event StreamDataChanged OnStreamDataChanged;
        private DeviceType devicetype;
        public DeviceType DeviceType
       
            get
           
                return devicetype;
           
            set
           
                devicetype = value;
           
       

        private int parentid;    //1、设备编号
        public int ParentID
       
            get
           
                return parentid;
           
            set
           
                parentid = value;
           
       

        private ushort id;          //2、子设备编号
        public ushort Id
       
            get
           
                return id;
           
            set
           
                id = value;
           
       

        private int tag;           //3、通用数据
        public int Tag
       
            get
           
                return tag;
           
            set
           
                tag = value;
           
       

        private StreamType streamtype;  //4、流类型
        public StreamType StreamType
       
            get
           
                return streamtype;
           
            set
           
                streamtype = value;
           
       

        public string unitname;         //5、计量单位文本描述
        public string UnitName
       
            get
           
                return unitname;
           
            set
           
                unitname = value;
           
       

        private string functiondescription;  //6、子设备功能描述
        public string FunctionDescription
       
            get
           
                return functiondescription;
           
            set
           
                functiondescription = value;
           
       

        private string picture;  //7、图像文件
        public string Picture
       
            get
           
                return picture;
           
            set
           
                picture = value;
           
       

        private byte[] sovalue; //8、字节流数据,一般不保存
        public byte[] SoValue
       
            get
           
                return sovalue;
           
            set
           
                if (DeviceSI.MemoryCompare(sovalue, value) != 0)
               
                    sovalue = new byte[value.Length];
                    Buffer.BlockCopy(value, 0, sovalue, 0, value.Length);
                    if (OnStreamDataChanged != null)
                        OnStreamDataChanged(new DataChangedEventArgs<byte[]>(this, sovalue));
               
           
       
        public DeviceSO()
       
            devicetype = DeviceType.SO;
            functiondescription = "SO子设备";
            Picture = "";
            streamtype = StreamType.TEXT;
       
        public void ReadFromStream(BinaryReader br)
       
            devicetype = (DeviceType)br.ReadByte();
            parentid = br.ReadInt32();        //1、设备编号
            id = br.ReadUInt16();             //2、子设备编号
            tag = br.ReadInt32();             //3、tag
            streamtype = (StreamType)br.ReadByte(); //4流类型
            unitname = br.ReadString();             //5、计量单位文本描述
            functiondescription = br.ReadString();  //6、子设备功能描述
            picture = br.ReadString();              //7、图像文件
       

        public void WriteToStream(BinaryWriter bw)
       
            bw.Write((byte)devicetype);
            bw.Write(parentid);    //1、设备编号
            bw.Write(id);          //2、子设备编号
            bw.Write(tag);         //3、tag
            bw.Write((byte)streamtype); //4流类型
            bw.Write(unitname);    //5、计量单位文本描述
            bw.Write(functiondescription);  //6、子设备功能描述
            bw.Write(picture);              //7、图像文件
       
   
#endregion
     代码虽多,其实核心部分是事件响应机制的实现,请注意了。
     接下来,最关键的两个对象如何实现。SmartHome和HomeDevice也具有整体- 部分关系。所有先编写“部分”类,一般“部分”类相对简单,先易后难是大多数人做事的取向。下面是实现冰箱这个设备类的完整代码:
    public class HomeDevice : IHomeDevice   //金鹰智能家居设备定义:一类设备
   
        #region 接口属性
        private int deviceid;  //1
        public int DeviceID
       
            get
           
                return deviceid;
           
            set
           
                deviceid = value;
           
       

        private string devicename;  //2
        public string DeviceName
       
            get
           
                return devicename;
           
            set
           
                devicename = value;
           
       

        private string positiondescription;  //3
        public string PositionDescription
       
            get
           
                return positiondescription;
           
            set
           
                positiondescription = value;
           
       

        private bool used;  //4
        public bool Used
       
            get
           
                return used;
           
            set
           
                used = value;
           
       

        #endregion
        public string GetState(DeviceType DeviceType)
       
            string result = "";
            if (DeviceType == DeviceType.DO)
           
                for (int i = 0; i < DODevices.Count; i++)
               
                    IDeviceDO dv = DODevices[i];
                    result += String.Format("(0)12 ", dv.Id + 1, dv.UnitName, (dv.ON ? "√" : "ㄨ"));
               
           
            else if (DeviceType == DeviceType.DI)
           
                for (int i = 0; i < DIDevices.Count; i++)
               
                    IDeviceDI dv = DIDevices[i];
                    result += String.Format("(0)12 ", dv.Id + 1, dv.UnitName, (dv.HasSignal ? "√" : "ㄨ"));
               
           
            else if (DeviceType == DeviceType.AI)
           
                for (int i = 0; i < AIDevices.Count; i++)
               
                    IDeviceAI dv = AIDevices[i];
                    string fstr = "(0)1:f" + dv.DotPlace.ToString() + "2";
                    result += String.Format(fstr, dv.Id + 1, dv.AiValue, dv.UnitName);
                    if (i < AIDevices.Count - 1) result += ", ";
               
           
            else if (DeviceType == DeviceType.AO)
           
                for (int i = 0; i < AODevices.Count; i++)
               
                    IDeviceAO dv = AODevices[i];
                    string fstr = "(0)1:f" + dv.DotPlace.ToString() + "2";
                    result += String.Format(fstr, dv.Id + 1, dv.AoValue, dv.UnitName);
                    if (i < AODevices.Count - 1) result += ", ";
               
           
            else if (DeviceType == DeviceType.SI)
           
                for (int i = 0; i < SIDevices.Count; i++)
               
                    IDeviceSI dv = SIDevices[i];
                    if (dv.StreamType == StreamType.TEXT && dv.SiValue != null)
                        result += String.Format("0:1 ", dv.Id + 1, Encoding.UTF8.GetString(dv.SiValue));
                    else result += String.Format("0:1 ", dv.Id + 1, dv.StreamType);
               
           
            else if (DeviceType == DeviceType.SO)
           
                for (int i = 0; i < SODevices.Count; i++)
               
                    IDeviceSO dv = SODevices[i];
                    if (dv.StreamType == StreamType.TEXT && dv.SoValue != null)
                        result += String.Format("0:1 ", dv.Id + 1, Encoding.UTF8.GetString(dv.SoValue));
                    else result += String.Format("0:1 ", dv.Id + 1, dv.StreamType);
               
           
            return result;
       
        public HomeDevice()  //构造函数
       
            DeviceName = "";
            PositionDescription = "";
            DODevices = new List<IDeviceDO>();
            DIDevices = new List<IDeviceDI>();
            AODevices = new List<IDeviceAO>();
            AIDevices = new List<IDeviceAI>();
            SODevices = new List<IDeviceSO>();
            SIDevices = new List<IDeviceSI>();
       
        ~HomeDevice()
       
            DODevices.Clear();
            DIDevices.Clear();
            AODevices.Clear();
            AIDevices.Clear();
            SODevices.Clear();
            SIDevices.Clear();
       
        public void ReadFromStream(BinaryReader br)
       
            DeviceID = br.ReadInt32();      //1 设备唯一识别码:
            Used = br.ReadBoolean();         //2 设备能否被使用或禁止,被禁用的设备不受监控
            DeviceName = br.ReadString();              //3 设备名称文本描述
            PositionDescription = br.ReadString();     //4 设备位置信息描述

            int count = br.ReadUInt16();               //5 DO子设备的数量
            DODevices.Clear();
            for (int i = 0; i < count; i++)
           
                DeviceDO device = new DeviceDO();
                device.ReadFromStream(br);
                DODevices.Add(device);
           
            count = br.ReadUInt16();               //6 DI子设备的数量
            DIDevices.Clear();
            for (int i = 0; i < count; i++)
           
                DeviceDI device = new DeviceDI();
                device.ReadFromStream(br);
                DIDevices.Add(device);
           

            count = br.ReadUInt16();               //7 AO子设备的数量
            AODevices.Clear();
            for (int i = 0; i < count; i++)
           
                DeviceAO device = new DeviceAO();
                device.ReadFromStream(br);
                AODevices.Add(device);
           
            count = br.ReadUInt16();               //8 AI子设备的数量
            AIDevices.Clear();
            for (int i = 0; i < count; i++)
           
                DeviceAI device = new DeviceAI();
                device.ReadFromStream(br);
                AIDevices.Add(device);
           
            count = br.ReadUInt16();               //9 SO子设备的数量
            SODevices.Clear();
            for (int i = 0; i < count; i++)
           
                DeviceSO device = new DeviceSO();
                device.ReadFromStream(br);
                SODevices.Add(device);
           
            count = br.ReadUInt16();               //10 SI子设备的数量
            SIDevices.Clear();
            for (int i = 0; i < count; i++)
           
                DeviceSI device = new DeviceSI();
                device.ReadFromStream(br);
                SIDevices.Add(device);
           
       
        public void WriteToStream(BinaryWriter bw)
       
            bw.Write(DeviceID);     //1 设备唯一识别码:
            bw.Write(Used);         //2 设备能否被使用或禁止,被禁用的设备不受监控
            bw.Write(DeviceName);            //3 设备名称文本描述
            bw.Write(PositionDescription);   //4 设备位置信息描述

            bw.Write((ushort)DODevices.Count);     //5 DO子设备的数量
            for (int i = 0; i < DODevices.Count; i++)
           
                IDeviceDO device = DODevices[i];
                device.WriteToStream(bw);
           
            bw.Write((ushort)DIDevices.Count);     //6 DI子设备的数量
            for (int i = 0; i < DIDevices.Count; i++)
           
                IDeviceDI device = DIDevices[i];
                device.WriteToStream(bw);
           
            bw.Write((ushort)AODevices.Count);     //7 AO子设备的数量
            for (int i = 0; i < AODevices.Count; i++)
           
                IDeviceAO device = AODevices[i];
                device.WriteToStream(bw);
           
            bw.Write((ushort)AIDevices.Count);     //8 AI子设备的数量
            for (int i = 0; i < AIDevices.Count; i++)
           
                IDeviceAI device = AIDevices[i];
                device.WriteToStream(bw);
           
            bw.Write((ushort)SODevices.Count);     //9 SO子设备的数量

以上是关于智能家居通用管理平台 - 家居设备驱动程序的编写的主要内容,如果未能解决你的问题,请参考以下文章

智能家居通用管理平台-软件架构设计

智能家居通用管理平台 – 监控程序的设计

智能家居通用管理平台 – WP客户端程序设计

智能家居通用管理平台 - 通信协议

智能家居通用管理平台-架构设计理念

智能家居通用管理平台-架构设计理念