public void SetCurrentServer(ServerData server)
 {
     if (server != null)
      {
     log.Append("SetCurrentServer", "Showing server " + server.name + " " + server.id);
     if (currentServer == null)
     {
        client.MessageReceived += receiveHandler;
     }
     clockTimer = new System.Threading.Timer(clockCallback, null, 2000, 1000);
     this.currentServer = server;
     ThreadPool.QueueUserWorkItem(new WaitCallback(SaveServerDetails));
      }
      else
      {
     log.Append("SetCurrentServer", "Clearing server details");
     if (currentServer != null)
     {
        client.MessageReceived -= receiveHandler;
        if(clockTimer!=null)
        {
           clockTimer.Dispose();
           clockTimer=null;
        }
     }
     currentServer = null;
      }
      InvokeUpdateCurrentServerDetails();
 }
 public DescriptionMessage(ServerData serverData)
 {
     this.Name = serverData.name;
      this.Location = serverData.location;
      this.Description = serverData.description;
      this.PictureBytes = new byte[serverData.pictureBytes.Length];
      Array.Copy(serverData.pictureBytes, this.PictureBytes, serverData.pictureBytes.Length);
      this.Sensors = serverData.sensors;
 }
示例#3
0
 public void Add(ServerData server)
 {
     serverToAdd = server;
     if (this.InvokeRequired)
     {
         this.Invoke(addInvokeHandler, server);
     }
     else
     {
         InvokedAdd(server);
     }
 }
示例#4
0
        private static ServerData ResultToServerData(SQLiteDataReader result)
        {
            ServerData serverToReturn = null;

             Guid id = result.GetGuid(0);
             string name = result.GetString(1);
             string description = result.GetString(2);
             string location = result.GetString(3);
             byte[] imageBytes = (byte[])result.GetValue(4);

             SensorDescriptionsData sensorData = new SensorDescriptionsData();

             SensorDefinition[] sensor = new SensorDefinition[4] { new SensorDefinition(), new SensorDefinition(), new SensorDefinition(), new SensorDefinition()};

             for (int s = 0; s < 4; s++)
             {
            // Do 4 sensor descriptions and 4 fields for each
            sensor[s].Name = (string)SQLHelper.ValueIfNull(result, s + 5, "");
            sensor[s].Unit = (string)SQLHelper.ValueIfNull(result, s + 9, "");
            sensor[s].ID = Convert.ToInt16(SQLHelper.ValueIfNull(result, s + 13, 255));
            sensor[s].Range = Convert.ToInt16(SQLHelper.ValueIfNull(result, s + 17, 255));

            sensorData.Add(sensor[s]);
             }
             serverToReturn = new ServerData(id, name, location, description, sensorData, imageBytes);
             return serverToReturn;
        }
示例#5
0
        public static void SaveServerConfigData(SQLiteConnection connection, ServerData data)
        {
            Hashtable serverRow = new Hashtable();
             serverRow["server_id"] = SQLHelper.NullOrFormatAndAddQuotes(data.id.ToString());
             serverRow["name"] = SQLHelper.NullOrFormatAndAddQuotes(data.name);
             serverRow["location"] = SQLHelper.NullOrFormatAndAddQuotes(data.location);
             serverRow["description"] = SQLHelper.NullOrFormatAndAddQuotes(data.description);
             serverRow["image"] = "X'" + BitConverter.ToString(data.pictureBytes).Replace("-", "") + "'";
             serverRow["sensor_1_name"] = SQLHelper.NullOrFormatAndAddQuotes(data.sensors[0].Name);
             serverRow["sensor_2_name"] = SQLHelper.NullOrFormatAndAddQuotes(data.sensors[1].Name);
             serverRow["sensor_3_name"] = SQLHelper.NullOrFormatAndAddQuotes(data.sensors[2].Name);
             serverRow["sensor_4_name"] = SQLHelper.NullOrFormatAndAddQuotes(data.sensors[3].Name);
             serverRow["sensor_1_unit"] = SQLHelper.NullOrFormatAndAddQuotes(data.sensors[0].Unit);
             serverRow["sensor_2_unit"] = SQLHelper.NullOrFormatAndAddQuotes(data.sensors[1].Unit);
             serverRow["sensor_3_unit"] = SQLHelper.NullOrFormatAndAddQuotes(data.sensors[2].Unit);
             serverRow["sensor_4_unit"] = SQLHelper.NullOrFormatAndAddQuotes(data.sensors[3].Unit);
             serverRow["sensor_1_id"] = data.sensors[0].ID;
             serverRow["sensor_2_id"] = data.sensors[1].ID;
             serverRow["sensor_3_id"] = data.sensors[2].ID;
             serverRow["sensor_4_id"] = data.sensors[3].ID;
             serverRow["sensor_1_range"] = data.sensors[0].Range;
             serverRow["sensor_2_range"] = data.sensors[1].Range;
             serverRow["sensor_3_range"] = data.sensors[2].Range;
             serverRow["sensor_4_range"] = data.sensors[3].Range;

             string query = String.Format("INSERT INTO [servers] {0}", SQLHelper.GenerateColumnsAndValues(serverRow));
             SQLHelper.ExecuteQuery(connection, query);
        }
示例#6
0
        public static ServerData GetServerNameByID(SQLiteConnection connection, Guid serverID)
        {
            ServerData toReturn = null;
             lock (connection)
             {
            if (connection.State != ConnectionState.Open)
            {
               connection.Open();
            }
            SQLiteCommand command = connection.CreateCommand();
            string commandText = String.Format("SELECT [server_id], [name], [description], [location] from [servers] WHERE [server_id]='{0}';", serverID.ToString());
            command.CommandText = commandText;
            SQLiteDataReader reader = command.ExecuteReader();

            if (reader.Depth > 0)
            {
               Guid id = reader.GetGuid(0);
               string name = reader.GetString(1);
               string description = reader.GetString(2);
               string location = reader.GetString(3);
               byte[] imageBytes = new byte[] { };
               toReturn = new ServerData(id, name, location, description, new SensorDescriptionsData(), imageBytes);
            }
            connection.Close();
             }
             return toReturn;
        }
示例#7
0
 public ServerSelectedEventArgs(ServerData serverSelected)
     : base()
 {
     this.server = serverSelected;
 }
示例#8
0
 private void InvokedAdd(ServerData server)
 {
     if (serverDataList.ContainsKey(server.id.ToString()))
     {
         serverDataList[server.id.ToString()] = server;
         RefreshTree();
     }
     else
     {
         serverDataList[serverToAdd.id.ToString()] = server;
         addServerNode(server);
     }
 }
示例#9
0
 private void FireOnServerSelected(ServerData server)
 {
     if (ServerSelected != null)
     {
         ServerSelected(this, new ServerSelectedEventArgs(server));
     }
 }
示例#10
0
        private void addServerNode(ServerData server)
        {
            TreeNode newNode = new TreeNode(server.name);

            TreeNode connectNode = new TreeNode("Connect");
            newNode.Nodes.Add(connectNode);
            nodesAndServerData[connectNode] = server;

            newNode.Nodes.Add(server.description);

            TreeNode sensorsNode = new TreeNode("Sensors");

            newNode.Nodes.Add(sensorsNode);
            this.Nodes.Add(newNode);
        }
示例#11
0
        public void SetUpWorkings()
        {
            descriptionUpdater = new VoidDelegate(UpdateServerDescription);
             connectionHandler = new DeviceNotification(DeviceManagement_NetworkConnected);

             InitialiseWiFi();

             log.Name = "log";
             log.LogMessage += new LogMessageEventHandler(log_LogMessage);
             log.Start();

             #region Regestry data setup

             RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(SensorShareConfig.ConfigKeyName);

             bool createNewConfig = true;

             object regKey = registryKey.GetValue("ServerID");

             if (regKey != null)
             {
            Guid serverID = new Guid((string)regKey);
            CurrentServerData = DatabaseHelper.GetServerByID(database, serverID);
            if (CurrentServerData != null)
            {
               createNewConfig = false;
            }
            else
            {
               log.Append("Server ID saved in Registry not found");
            }
             }

            #endregion

             #region Server Configuration Setup

             if (createNewConfig)
             {
            log.Append("Need to create a new server configuration");
            // Show server config before starting server (which is automatically done when config box is closed)
            Bitmap serverPic = new Bitmap(Application2.StartupPath + "/defaultImage.jpg");
            CurrentServerData = new ServerData(Guid.Empty, "New Server", "Location", "Description", new SensorDescriptionsData(), JpegImage.GetBytes(serverPic));
            configMenuItem_Click(this, new EventArgs());
             }
             else
             {
            //If config isn't needed, start now
            registryKey.Close();
            log.Append("Starting with server: " + CurrentServerData.id.ToString() + " " + CurrentServerData.name);

            // Save sensor data laoded from database (past session)
            SensorDescriptionsData loadedSensors = new SensorDescriptionsData();
            foreach (SensorDefinition sensor in CurrentServerData.sensors)
            {
               loadedSensors.Add(sensor);
            }
            //Clear current data as logbook isn't connected yet so sensors are unknown
            CurrentServerData.sensors.Clear();
            InitialiseLogbook(); // Set up logbook data

            // Connect Logbook and send identify commands (reply comes on other threads
            ConnectLogbookAndIdentifySensors();
            // Should have done Thread.Sleep in above method to allow reply to have come by now
            // Check sensors match the database version
            ConfirmSensors();

            bool sensorSelectionNotCompleted = true;
            while (sensorSelectionNotCompleted)
            {
               // check that the sensors connected are the same
               bool sameSensorsConnected = true;
               lock (CurrentServerData.sensors)
               {
                  if (loadedSensors.Count != CurrentServerData.sensors.Count)
                  {
                     sameSensorsConnected = false;
                  }
                  else
                  {

                     for (int s = 0; s < loadedSensors.Count; s++)
                     {
                        if ((loadedSensors[s].ID != CurrentServerData.sensors[s].ID) || (loadedSensors[s].Range != CurrentServerData.sensors[s].Range))
                        {
                           sameSensorsConnected = false;
                        }
                     }

                  }
               }
               if (!sameSensorsConnected)
               {
                  DialogResult result = MessageBox.Show("The sensors currently attached to the Logbook do not match the previously saved sensors.  Click Retry to change the sensors, or Cancel to create a new server configuration.", "Different Sensors Connected",
                  MessageBoxButtons.RetryCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                  if (result == DialogResult.Retry)
                  {
                     disconnectLogbook();
                     StringBuilder sb = new StringBuilder();
                     sb.Append("You need to connect the following sensors:\r\n");
                     foreach (SensorDefinition sensor in loadedSensors)
                     {
                        sb.AppendFormat("{0}\r\n", sensor.Description);
                     }
                     MessageBox.Show(sb.ToString(), "Sensors Required", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                     CurrentServerData.sensors.Clear();
                     ConnectLogbookAndIdentifySensors();
                     ConfirmSensors();
                  }
                  else
                  {
                     if (result == DialogResult.Cancel)
                     {
                        CurrentServerData.id = Guid.NewGuid();
                        DatabaseHelper.SaveServerConfigData(database, CurrentServerData);
                        sensorSelectionNotCompleted = false;
                     }
                  }
               }
               else
               {
                  sensorSelectionNotCompleted = false;
               }
            }
            StartServer();
             }
             #endregion

             DeviceManagement.NetworkConnected += connectionHandler;
        }