Announcement

Collapse
No announcement yet.

VB to C#

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

    VB to C#

    I want to move a very simple piece of code from VB to C#. I found numerous samples in the forum but all failed with different errors.

    This is the code:

    Sub Main(Parm As Object)
    Dim DE, dv
    'Get a list of devices
    DE = hs.GetDeviceEnumerator
    If DE is nothing then
    hs.Writelog("Info", "No thermostats")
    end if

    Do while Not DE.Finished
    dv = DE.GetNext ' DV is van het type DeviceClass en bevast alle informatie over het device
    hs.Writelog("Interface", dv.Interface(hs))
    Loop
    End Sub

    Can someone help me to put this into a working C# script (maybe including 'using' statements).

    Thanks in advance!

    Kees


    Current Date/Time: 03-Oct-17 4:57:13 PM
    HomeSeer Version: HS3 Pro Edition 3.0.0.362
    Operating System: Microsoft Windows 10 Enterprise 2015 LTSB - Work Station
    System Uptime: 13 Days 10 Hours 10 Minutes 44 Seconds
    IP Address: 192.168.253.161
    Number of Devices: 362
    Number of Events: 83
    Available Threads: 400

    Enabled Plug-Ins
    2.0.35.0: BLLAN
    3.0.0.10: P1
    30.0.0.35: RFXCOM
    1.0.0.6: Sample Plugin
    1.2.2.0: SceneMaster
    3.0.0.21: Sonos
    3.0.1.144: Z-Wave

    #2
    Working in C# with HomeSeer is somewhat challenging due to namespaces being wonky (technical term) between the VB classes and C#. Some things take some experimentation to figure out the correct syntax. For example, dv.Interface() works in VB because there is an implied Get function. For whatever reason when you try to write that in C# you'll get an error that there is no Get function defined, and you have to instead use dv.get_Interface().

    I suggest using Visual Studio to create your scripts. You can download for free if you don't have it. Add a reference to Scheduler.dll and use code completion to figure out the correct syntax for C#.

    Here is your script, with appropriate changes to make it work in C#:
    Code:
    public object Main(object[] Parms)
    {
    	Scheduler.Classes.clsDeviceEnumeration DE;
    	Scheduler.Classes.DeviceClass dv;
    	
    	// Get a list of devices
    	DE = (Scheduler.Classes.clsDeviceEnumeration)hs.GetDeviceEnumerator();
    	
    	if (DE == null) {
    		hs.WriteLog("Info", "No thermostats");
    	}
    	
    	while (!DE.Finished) {
    		dv = DE.GetNext();
    		hs.WriteLog("Interface", dv.get_Interface(hs));
    	}
            return null;
    }
    While this code will execute, I'm not sure it's doing what you intended. It will write a log entry containing the Interface value for every device in the system. If you're looking just for thermostats you'll need to filter further.
    HS Pro 3.0 | Linux Ubuntu 16.04 x64 virtualized under Proxmox (KVM)
    Hardware: Z-NET - W800 Serial - Digi PortServer TS/8 and TS/16 serial to Ethernet - Insteon PLM - RFXCOM - X10 Wireless
    Plugins: HSTouch iOS and Android, RFXCOM, BlueIris, BLLock, BLDSC, BLRF, Insteon PLM (MNSandler), Device History, Ecobee, BLRing, Kodi, UltraWeatherWU3
    Second home: Zee S2 with Z-Wave, CT101 Z-Wave Thermostat, Aeotec Z-Wave microswitches, HSM200 occupancy sensor, Ecolink Z-Wave door sensors, STI Driveway Monitor interfaced to Zee S2 GPIO pins.

    Comment


      #3
      Thanks!

      I have experien ced a lot of the wonky-ness indeed. Even using sripts I found in this forum but this one works perfect!

      My problem is I have a seperate development system (ok, I know that is a luxury issue). I might install HS there as well (just never run it) so I can use Visual Studio (which I have).

      My overall goal is a rather advanced script to control my central heating (dual zone). It takes into account multiple rooms/radiators, the fact the boiler uses modulation to safe energy (so you should not turn it off to often) and radiotor build into closets (so the build in Temperature sensor is useless.

      But I felt posting all that was a bit stretching the members goodwill .

      I HAVE it working in VB (now working on documenting it all) BUT I don't like VB (occupational defect).

      But now I have a working starting point I can work from there!

      Thanks again!

      Comment


        #4
        You're welcome. Happy to help if you run into any other issues with it. My development machine is also separate from HS, so when writing scripts I just load up a dummy project in VS and add the Scheduler.dll reference so I can use code completion in the editor. Then I copy the script to HomeSeer. Saves lots of time over writing the scripts in a text editor. Too bad TenScripting doesn't work with C#!
        HS Pro 3.0 | Linux Ubuntu 16.04 x64 virtualized under Proxmox (KVM)
        Hardware: Z-NET - W800 Serial - Digi PortServer TS/8 and TS/16 serial to Ethernet - Insteon PLM - RFXCOM - X10 Wireless
        Plugins: HSTouch iOS and Android, RFXCOM, BlueIris, BLLock, BLDSC, BLRF, Insteon PLM (MNSandler), Device History, Ecobee, BLRing, Kodi, UltraWeatherWU3
        Second home: Zee S2 with Z-Wave, CT101 Z-Wave Thermostat, Aeotec Z-Wave microswitches, HSM200 occupancy sensor, Ecolink Z-Wave door sensors, STI Driveway Monitor interfaced to Zee S2 GPIO pins.

        Comment

        Working...
        X