private void Update(Device sd, ZiBase.SensorInfo se)
 {
     /*  if (se.sType == "lev") // common value for all sensor
         sd.RXLevel = se.sValue;
     else if (se.sType == "bat")// common value for all sensor
         sd.BatLevel = se.sValue;
     else
     {*/
         UpdateValue(sd, se, Device.EnumConverter(se.sType));
     //}
 }
        public void UpdateSensorData(ZiBase.SensorInfo se)
        {
            var q = from c in SensorList where c.ID.Contains(se.sID) select c;
            if (q.Any())
            {
                foreach (Device sd in q)
                {
                    //SensorData sd = q.First();
                    Update(sd, se);
                }
            }
            else
            {
                Device sd = new Device(se.sID) {ID = se.sID, Name = se.sName};

                Update(sd, se);
                SensorList.Add(sd);
            }
        }
 void m_Zibase_UpdateSensorInfo(ZiBase.SensorInfo seInfo)
 {
     m_SensorDataManager.UpdateSensorData(seInfo);
 }
 void m_Zibase_NewZibaseDetected(ZiBase.ZibaseInfo zbInfo)
 {
     HsObjet.getInstance().WriteLog("Debug", "Zibase detected");
     m_ZibaseInfo = zbInfo;
     //m_ZibaseDetectedEvent.Set();
     m_Zibase.SetZibaseToken(m_ZibaseInfo.sLabelBase, m_Config.ZibaseToken);
     RefreshScenarioList();
 }
 void m_Zibase_NewSensorDetected(ZiBase.SensorInfo seInfo)
 {
     m_SensorDataManager.UpdateSensorData(seInfo);
 }
        //         public void SetIO(object dv, string housecode, string devicecode, int command, int brightness, int data1, int data2)
        //         {
        //             int a;
        //         }
        public String InitIO(long port)
        {
            if (Util.HighestNETFrameworkVersion() < 3.5)
                return "Oops, you need at least the .NET framework 3.5.";

            // Init Zibase and subscribe to its events
            m_Zibase = new ZiBase();

            //m_Zibase.RestartZibaseSearch();
            m_Zibase.WriteMessage += m_Zibase_WriteMessage;
            m_Zibase.NewSensorDetected += m_Zibase_NewSensorDetected;
            m_Zibase.UpdateSensorInfo += m_Zibase_UpdateSensorInfo;
            m_Zibase.NewZibaseDetected += m_Zibase_NewZibaseDetected;

            AppDomain MyDomain = AppDomain.CurrentDomain;
            Assembly[] AssembliesLoaded = MyDomain.GetAssemblies();
            var q = from a in AssembliesLoaded where a.FullName.ToLower().Contains("zibasedll") select a;
            Assembly asm = q.First();
            ZIBASE_SDK_VERSION = asm.GetName().Version;

            m_SensorDataManager = new SensorDataManager();

            // Init our web configuration class ...
            m_WebConfig = new WebConfig {SensorList = m_SensorDataManager.SensorList};

            m_WebConfig.OnUserAddDevice += cfg_OnUserAddDevice;
            //m_WebConfig.OnUserUpdateControledDevices += new WebConfig.UserUpdateControledDevicesHandler(cfg_OnUserUpdateControledDevices);
            m_WebConfig.OnConfigurationChanged += m_WebConfig_OnConfigurationChanged;
            Object obj = m_WebConfig;
            HsObjet.getInstance().RegisterLinkEx(ref obj, Name());

            // ...and the event we will manage
            object objMaster = (object)this;
            int nEvent = HomeSeer.EV_TYPE_X10 |
                HomeSeer.EV_TYPE_STATUS_CHANGE |
                HomeSeer.EV_TYPE_X10_TRANSMIT |
                HomeSeer.EV_TYPE_CONFIG_CHANGE |
                HomeSeer.EV_TYPE_STRING_CHANGE;

            HsObjet.getInstance().RegisterEventCB(ref nEvent, ref objMaster);

            String s = HsObjet.getInstance().GetINISetting("Settings", "app_path", "def", "Settings.ini");
            ConfigFilePath = Path.Combine(s, "config\\Zibase.xml"); // config file for V 0.x

            try
            {
                if (File.Exists(ConfigFilePath)) // file exists, this looks like an upgrade from 0.x version.
                {
                    /*m_ControledDevices*/
                    //List<DeviceAssociation> ControledDevices = Util.DeserializeFromXml<List<DeviceAssociation>>(ConfigFilePath);
                    try
                    {
                        HsObjet.getInstance().WriteLog("Debug", "Get configuration from file");
                        m_Config = Util.DeserializeFromXml<PluginConfig>(ConfigFilePath);
                        HsObjet.getInstance().WriteLog("Debug", "Config file successfully read");
                    }
                    catch (Exception)
                    {
                        HsObjet.getInstance().WriteLog("Error", "Cannot get configuration from file, maybe it is from an older version");
                        HsObjet.getInstance().WriteLog("Error", "It can happens for the first time you are running the plugin with a version above 0.x");
                        HsObjet.getInstance().WriteLog("Debug", "Try to read configuration from 0.x version");
                        List<DeviceAssociation> ControledDevices = Util.DeserializeFromXml<List<DeviceAssociation>>(ConfigFilePath);
                        HsObjet.getInstance().WriteLog("Debug", "Config from version 0.x successfully read");
                        m_Config = new PluginConfig {ControledDevices = ControledDevices};
                        Util.SerializeToXml(m_Config, ConfigFilePath);
                    }

                }
                else
                {
                    m_Config = new PluginConfig(); // default configuration
                }

            }
            catch
            {
                HsObjet.getInstance().WriteLog("Debug", "No device association file found");
                m_Config = new PluginConfig();
            }
            Debug.Assert(m_Config != null);
            m_WebConfig.ControledDevices = m_Config.ControledDevices;
            m_WebConfig.Config = m_Config;

            m_WatchdogTimer = new System.Timers.Timer();

            if (m_Config.WatchdogTimerMinutes > 0)
            {
                m_WatchdogTimer.Interval = m_Config.WatchdogTimerMinutes * 60 * 1000;
                m_WatchdogTimer.Start();
            }
            m_WatchdogTimer.Elapsed += m_WatchdogTimer_Elapsed;
            m_WatchdogTimer.AutoReset = true;

            //HsObjet.getInstance().WriteLog("Debug", "Waiting for Zibase");
            //m_ZibaseDetectedEvent.WaitOne();
            //HsObjet.getInstance().WriteLog("Debug", "ZibaseDetected, set Token");
            m_Zibase.StartZB(true, 17202);
            HsObjet.getInstance().WriteLog("Debug", "IOInit End");
            return "";
        }
        private string GenerateZibaseProtocolListBox(String ListBoxName, ZiBase.Protocol SelectedProtocol)
        {
            StringBuilder p = new StringBuilder();
            p.Append("<select name=" + ListBoxName + ">\n");

            foreach (ZiBase.Protocol Protocol in Enum.GetValues(typeof(ZiBase.Protocol)))
                p.Append("<option value=" + (int)Protocol + " " + (Protocol == SelectedProtocol ? "Selected" : "") + ">" + Protocol + "</option>\n");

            p.Append("</select>\n");
            return p.ToString();
        }