Announcement

Collapse
No announcement yet.

Master Device creation in C#

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Just when I think I finally get some momentum...

    The first half of this method works fine:

    Code:
    try
    {
    //hs.WriteLog(HomeSeer.PluginSdk.Logging.ELogType, "trying to get stuff from the server", Name);
    temp = await Server.getInfoFromServerAsync();
    Server.States zmStates = new JavaScriptSerializer().Deserialize<Server.States>(temp);
    
    foreach (var item in zmStates.data)
    {
    Console.WriteLine("Stuff {0} {1}" ,item.Id, item.Name);
    }
    //Console.WriteLine(temp);
    
    
    }
    temp={"states":[{"State":{"Id":"1","Name":"default","Definition":"","IsAc tiv e":"0"}},{"State":{"Id":"2","Name":"Away","Definition":"1 :Mo dect:1,2:Modect:1,5:Modect:1,6:Modect:1,8:Modect:1,9:Modect: 1","IsActive":"0"}},{"State":{"Id":"13","Name":"Work out","Definition":"1:Nodect:1,2:Monitor:1,5:Monitor:1,6:Mod e ct:1,8:Monitor:1,9:Nodect:1,10:None:0,11:Nodect:1,12:Modect: 1,13:Monitor:1,14:Monitor:1,15:Monitor:1,16:Monitor:1,17:Mod ect:1","IsActive":"0"}},{"State":{"Id":"14","Name":"Home","D efinition":"1:Nodect:1,2:Monitor:1,5:Monitor:1,6:Modect:1,8: Monitor:1,9:Nodect:1,10:Monitor:1,11:Nodect:1,12:Modect:1,13 :Monitor:1,14:Monitor:1,15:Monitor:1,16:Modect:1,17:Modect:1 ,20:Monitor:1","IsActive":"1"}},{"State":{"Id":"15","Name":" Bail Quick","Definition":"1:Monitor:1,2:Monitor:1,5:Monitor:1,6:M odect:1,8:Monitor:1,9:Monitor:1,10:Monitor:1,11:Monitor:1,12 :Monitor:1,13:Monitor:1,14:Monitor:1,15:Monitor:1,16:Monitor :1,17:Monitor:1,20:Monitor:1","IsActive":"0"}},{"State":{"Id ":"16","Name":"Night","Definition":"1:Nodect:1,2:Monitor :1,5 :Nodect:1,6:Modect:1,8:Monitor:1,9:Nodect:1,10:Nodect:1,11:N odect:1,12:Modect:1,13:Monitor:1,14:Modect:1,15:Modect:1,16: Modect:1,17:Modect:1,20:Modect:1","IsActive":"0"}}]}
    And:

    Code:
     public class States
    {
    public List<ZoneminderState> data { get; set; }
    }
    
    public class ZoneminderState
    {
    public int Id { get; set; }
    public string Name { get; set; }
    public int IsActive { get; set; }
    }
    What am I doing wrong? When ever the code runs I get a nullpointer error. Eventually I need to parse the Name field to a list.


    Comment


    • #17
      I've been trying to chip away at this and have been spinning my wheels. I came across a post on stackoverflow about looping through json data which led me to a json to c# converter. I copied the data above into it and this is what it returned:

      Code:
       public class State2 {
      public string Id { get; set; }
      public string Name { get; set; }
      public string Definition { get; set; }
      public string IsActive { get; set; }
      
      }
      
      public class State {
      public State2 State { get; set; }
      
      }
      
      public class Root {
      public List<State> states { get; set; }
      
      }
      Obviously I get the error:
      Member names cannot be the same as their enclosing type
      So I tried renaming the class "State" to "SomethingElse".

      every time I run:

      Code:
      var zmStates = new JavaScriptSerializer().Deserialize<Root>(temp);
      
      
      foreach (var obj in zmStates.states)
      
      {
      
      Console.WriteLine("trying");
      
      }
      I get a nullpointer......


      Any thoughts? Is this because the Json starts out with
      {"states":[{"State":?

      Comment


      • #18
        Hi lorenjz,

        I see you are returning a JSON array but usingJavaScriptSerializer.
        Have you thought about using JsonConvert in Newtownsoft Json?
        I find it much better and fast.

        Return your data into a json object using something like:

        Code:
        OverarchingClassName.root zmdata = (OverarchingClassName.root)JsonConvert.DeserializeObject(temp, typeof(OverarchingClassName.root));
        Where temp is youur data string of the json response!
        Then after returning it as the correct type you can iterate over the connection by using code something like:

        Code:
        {
        foreach (OverarchingClassName.Zone stateinfo in zmdata.states)
        {
        //TODO eg:      console.writeline(stateinfo.Name);
        }
        }

        Hope that makes sense!

        Comment


        • #19
          I think the JSON thing ended up being due to a lack of sleep (boneheaded mistake). I think I have that sorted, now for whatever reason I cant get a dropdown display in the feature list. Any thoughts?

          Code:
          public async void CreateMasterDevice()
          {
          DeviceFactory df = DeviceFactory.CreateDevice(Id);
          Console.WriteLine("Id: Step 1" + Id);
          df.WithName("ZoneMinder Main");
          df.AsType(EDeviceType.Generic, 0);
          var ff = FeatureFactory.CreateFeature(Id);
          ff.AddTextDropDown(Storage.MainDeviceOpt);
          ff.WithName("States");
          df.WithFeature(ff);
          df.PrepareForHs();
          NewDeviceData dd;
          dd = df.PrepareForHs();
          
          
          hs.CreateDevice(dd);
          
          
          
          //df = df.WithFeature("State");
          
          
          
          
          }

          Comment


          • #20
            I think you need to set the EMiscFlag to show values

            Code:
            EMiscFlag.ShowValues

            Comment


            • #21
              russr999 Thanks a ton for all of your help. Just to make sure I got it right:

              Code:
              public async void CreateMasterDevice()
              {
              EMiscFlag[] miscFlags = { EMiscFlag.NoLog, EMiscFlag.ShowValues};
              DeviceFactory df = DeviceFactory.CreateDevice(Id);
              Console.WriteLine("Id: Step 1" + Id);
              df.WithName("ZoneMinder Main");
              df.AsType(EDeviceType.Generic, 0);
              var ff = FeatureFactory.CreateFeature(Id);
              ff.AddTextDropDown(Storage.MainDeviceOpt);
              ff.WithName("States");
              ff.WithMiscFlags(miscFlags);
              //ff.PrepareForHsDevice(df);
              df.WithFeature(ff);
              //df.PrepareForHs();
              NewDeviceData dd;
              dd = df.PrepareForHs();
              
              
              hs.CreateDevice(dd);
              
              
              
              //df = df.WithFeature("State");
              
              
              
              
              }
              Still no dice...

              Comment


              • #22
                lorenjz

                When creating the EMiscFlag settings, declare a new instance of them....something like:

                Code:
                EMiscFlag[] hsFlags = new EMiscFlag[] { EMiscFlag.NoLog, EMiscFlag.ShowValues };
                Does that help or work?

                Comment


                • #23
                  lorenjz,

                  What is being stored in Storage.MainDeviceOpt?
                  How are you creating that?

                  Comment


                  • #24
                    russr999 Bummer still no love...

                    Comment


                    • #25
                      Originally posted by russr999 View Post
                      lorenjz,

                      What is being stored in Storage.MainDeviceOpt?
                      How are you creating that?
                      This is declared in Storage.cs (for a bunch of variables):

                      Code:
                       public static SortedDictionary<string,double> MainDeviceOpt => new SortedDictionary<string, double>();
                      This runs in a thread and populates MainDeviceOpt:

                      Code:
                       public static async void ReturnJunkFromServer()
                      {
                      string temp;
                      SortedDictionary<string, Double> myTempDictionary = new SortedDictionary<string, Double>();
                      
                      
                      try
                      {
                      /*if (Storage.MainDeviceOpt.Count >= 0){
                      Storage.MainDeviceOpt.Clear();
                      }*/
                      temp = await Server.getInfoFromServerAsync();
                      //Console.WriteLine(temp);
                      var zmStates = new JavaScriptSerializer().Deserialize<Server.Root>(temp);
                      
                      
                      foreach (var obj in zmStates.states)
                      
                      {
                      double mydoub = Double.Parse(obj.State.Id);
                      
                      //Console.WriteLine("Adding data name: {0} and id: {1}",obj.State.Name ,obj.State.Id);
                      //int myInt = Int32.Parse(obj.State.Id);
                      try
                      {
                      Storage.MainDeviceOpt.Add(obj.State.Name, mydoub);
                      }
                      
                      catch (ArgumentNullException e)
                      {
                      Console.WriteLine("stuff is null");
                      Console.WriteLine(e);
                      }
                      
                      }
                      
                      }
                      
                      catch (NullReferenceException e)
                      {
                      Console.WriteLine("Returned a Null pointer during server retreaval?????");
                      Console.WriteLine(e);
                      }
                      
                      }

                      Comment


                      • #26
                        lorenjz,

                        Just thining out loud, If its running in another thread, is it populated when you call it in the create feature?
                        As a test maybe inn debug mode before you do df.preparefor HS, can you check ff and see if it has the values?
                        I'm wondering is the dropdown not populating or not showing? Hope that makes sense

                        Comment


                        • #27
                          Originally posted by russr999 View Post
                          lorenjz,

                          Just thining out loud, If its running in another thread, is it populated when you call it in the create feature?
                          As a test maybe inn debug mode before you do df.preparefor HS, can you check ff and see if it has the values?
                          I'm wondering is the dropdown not populating or not showing? Hope that makes sense
                          @russr999 That clue was huge in terms of getting me on track. Thank you for that.

                          The thing that I'm running into is that I need to find a way to know when the last item has been passed to onsettingchange so that I can start a login process. Is there a way to do that?

                          Sent from my SM-G988U using Tapatalk


                          Comment


                          • #28
                            Hi lorenjz ,

                            I'd have to check later but have you thought about override on the OnSettingPageSave function?
                            I believe that processes (through iteration) all the settings from a page when you click save on that page.
                            If you override this, keeping the original code and maybe adding a call to your login process from here?

                            Rory

                            Comment

                            Working...
                            X