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


                              #29
                              Hi lorenjz ,

                              Thought I'd just check back with you and see if you got everything working the way you wanted it in the end? 😀

                              Comment


                                #30
                                russr999 Thanks for checking in!

                                To answer your question yes and no. Yes, from the standpoint until last night, thanks to your help I was making good progress.

                                No, from the standpoint that I've hit a wall with some JSON stuff again. Here is one record of 15 in a JSON request:

                                Code:
                                {"monitors":[{"Monitor":{"Id":"1","Name":"Street","Notes":"","ServerId":"0","StorageId":"0","Type":"Ffmpeg","Function":"Nodect","Enabled":"1","LinkedMonitors":"9","Triggers":"","Device":"","Channel":"0","Format":"0","V4LMultiBuffer":null,"V4LCapturesPerFrame":"1","Protocol":null,"Method":"rtpRtsp","Host":null,"Port":"","SubPath":"","Path":"rtmp:\/\/192.168.90.5\/bcs\/channel0_main.bcs?channel=0&stream=0&user=zoneminder&password=camera","Options":null,"User":null,"Pass":null,"Width":"2560","Height":"1920","Colours":"4","Palette":"0","Orientation":"ROTATE_0","Deinterlacing":"0","DecoderHWAccelName":null,"DecoderHWAccelDevice":null,"SaveJPEGs":"3","VideoWriter":"2","OutputCodec":null,"OutputContainer":null,"EncoderParameters":"# Lines beginning with # are a comment \r\n# For changing quality, use the crf option\r\n# 1 is best, 51 is worst quality\r\n#crf=23","RecordAudio":"0","RTSPDescribe":false,"Brightness":"-1","Contrast":"-1","Hue":"-1","Colour":"-1","EventPrefix":"Event-","LabelFormat":"%N - %d\/%m\/%y %H:%M:%S","LabelX":"0","LabelY":"0","LabelSize":"1","ImageBufferCount":"35","WarmupCount":"0","PreEventCount":"5","PostEventCount":"5","StreamReplayBuffer":"0","AlarmFrameCount":"1","SectionLength":"600","MinSectionLength":"10","FrameSkip":"0","MotionFrameSkip":"0","AnalysisFPSLimit":null,"AnalysisUpdateDelay":"0","MaxFPS":null,"AlarmMaxFPS":null,"FPSReportInterval":"100","RefBlendPerc":"6","AlarmRefBlendPerc":"6","Controllable":"0","ControlId":null,"ControlDevice":null,"ControlAddress":null,"AutoStopTimeout":null,"TrackMotion":"0","TrackDelay":null,"ReturnLocation":"-1","ReturnDelay":null,"DefaultRate":"100","DefaultScale":"100","DefaultCodec":"auto","SignalCheckPoints":"0","SignalCheckColour":"#0000BE","WebColour":"#50593d","Exif":false,"Sequence":"0","TotalEvents":"338","TotalEventDiskSpace":"11125525043","HourEvents":"1","HourEventDiskSpace":"19451058","DayEvents":"38","DayEventDiskSpace":"1162557561","WeekEvents":"338","WeekEventDiskSpace":"11125525043","MonthEvents":"338","MonthEventDiskSpace":"11125525043","ArchivedEvents":null,"ArchivedEventDiskSpace":null,"ZoneCount":"1","Refresh":null},"Monitor_Status":{"MonitorId":"1","Status":"Connected","CaptureFPS":"12.50","AnalysisFPS":"12.50","CaptureBandwidth":"723361"}},{"Monitor":{"Id":"2","Name":"Driveway","Notes":"","ServerId":"0","StorageId":"0","Type":"Ffmpeg","Function":"Nodect","Enabled":"1","LinkedMonitors":"10","Triggers":"","Device":"","Channel":"0","Format":"0","V4LMultiBuffer":null,"V4LCapturesPerFrame":"1","Protocol":null,"Method":"rtpRtsp","Host":null,"Port":"","SubPath":"","Path":"rtmp:\/\/192.168.90.6\/bcs\/channel0_main.bcs?channel=0&stream=0&user=zoneminder&password=camera","Options":null,"User":null,"Pass":null,"Width":"2560","Height":"1920","Colours":"4","Palette":"0","Orientation":"ROTATE_0","Deinterlacing":"0","DecoderHWAccelName":null,"DecoderHWAccelDevice":null,"SaveJPEGs":"3","VideoWriter":"2","OutputCodec":null,"OutputContainer":null,"EncoderParameters":"# Lines beginning with # are a comment \r\n# For changing quality, use the crf option\r\n# 1 is best, 51 is worst quality\r\n#crf=23","RecordAudio":"0","RTSPDescribe":false,"Brightness":"-1","Contrast":"-1","Hue":"-1","Colour":"-1","EventPrefix":"Event-","LabelFormat":"%N - %d\/%m\/%y %H:%M:%S","LabelX":"0","LabelY":"0","LabelSize":"1","ImageBufferCount":"35","WarmupCount":"0","PreEventCount":"5","PostEventCount":"5","StreamReplayBuffer":"0","AlarmFrameCount":"1","SectionLength":"600","MinSectionLength":"10","FrameSkip":"0","MotionFrameSkip":"0","AnalysisFPSLimit":null,"AnalysisUpdateDelay":"0","MaxFPS":null,"AlarmMaxFPS":null,"FPSReportInterval":"100","RefBlendPerc":"6","AlarmRefBlendPerc":"6","Controllable":"0","ControlId":null,"ControlDevice":null,"ControlAddress":null,"AutoStopTimeout":null,"TrackMotion":"0","TrackDelay":null,"ReturnLocation":"-1","ReturnDelay":null,"DefaultRate":"100","DefaultScale":"100","DefaultCodec":"auto","SignalCheckPoints":"0","SignalCheckColour":"#0000BE","WebColour":"#65710e","Exif":false,"Sequence":"2","TotalEvents":"223","TotalEventDiskSpace":"7529530802","HourEvents":"1","HourEventDiskSpace":"27770231","DayEvents":"54","DayEventDiskSpace":"2007443284","WeekEvents":"223","WeekEventDiskSpace":"7529530802","MonthEvents":"223","MonthEventDiskSpace":"7529530802","ArchivedEvents":null,"ArchivedEventDiskSpace":null,"ZoneCount":"1","Refresh":null},"
                                I used an online converter to create the classes to deserialize this and came up with this:

                                Code:
                                 public class Monitor
                                {
                                public string Id { get; set; }
                                public string Name { get; set; }
                                public string Notes { get; set; }
                                public string ServerId { get; set; }
                                public string StorageId { get; set; }
                                public string Type { get; set; }
                                etc....
                                
                                }
                                
                                public class MonitorStatus
                                {
                                public string MonitorId { get; set; }
                                public string Status { get; set; }
                                public string CaptureFPS { get; set; }
                                public string AnalysisFPS { get; set; }
                                public string CaptureBandwidth { get; set; }
                                
                                }
                                
                                public class Monitor_Data
                                {
                                
                                public Monitor Monitor_Info { get; set; }
                                public MonitorStatus Monitor_Status { get; set; }
                                
                                }
                                
                                public class MonRoot
                                {
                                public List<Monitor_Data> monitors { get; set; }
                                
                                }
                                When I try and deserialize the JSON with this:

                                Code:
                                MonRoot m = (MonRoot)JsonConvert.DeserializeObject(MonInfo, typeof(MonRoot));
                                Monitor_Status is set correctly but Monitor_Info remains null. When I set break points within the Monitor_Data class Monitor_Info never triggers the break point. I'm curious why that is and what I'm doing wrong.

                                Comment

                                Working...
                                X