Changes to System.DeviceAttached/System.DeviceRemoved

Got a good idea? You can suggest new features here.

Add device names and other device data to System.DeviceAttached/System.DeviceRemoved

Yes
14
100%
No
0
No votes
Have no clue what I am talking about
0
No votes
Don't care
0
No votes
 
Total votes: 14

User avatar
kgschlosser
Site Admin
Posts: 4204
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by kgschlosser » Mon Mar 06, 2017 5:40 pm

Yeah I know...

This is a big learning thing for me... and Microsoft has some back asswards ways of doing things. and it really makes no sense as to why some of the things are the way they are.. but I would imagine it is because of rears af adding and adding and adding.. but sometimes you need to redesign the way something works. and make a means to use an old API with the newly designed addition. instead of just adding onto it. the process of getting something simple like a drive letter for a specific disk drive is bonkers. I have to go through 4 different WMI queries passing a piece of information from the previous query to it

I have attached a complete copy of the system plugin.. it has the new device change notifier as well as the power notifications.
YOU HAVE TO PUT THE PREVIOUSLY ATTACHED WindowsVersion.py file into your eg/Classes folder for this to work.

I am am still running into an issue with the network mapped drives. but This does have a working GetDevices action.. tho the help system in it seems to have some kind of a glitch. the glitch doesn't stop it from working it just appends a bunch of garbage at the end of the help and then seems show a help file that is unrelated. but when you see the garbage.. that is the end of the help file for that device type.

this is all just for a drive letter for mounting and unmounting drives

it's CRAZY!!! ALL OF THIS JUST FOR A FRIGGIN LETTER!!!

Code: Select all

def GetDrives(self):
        logicalDisks = self.wmi.ExecQuery(
            "Select * from Win32_LogicalDisk"
        )

        for disk in logicalDisks:
            if disk.DriveType == 2: # Removable
                suffix = []
                storedDrives = {}
                drives = {}

            elif disk.DriveType == 3: # Local
                suffix = ['Drive']
                storedDrives = self.localDrives
                diskDrives = self.wmi.ExecQuery(
                    "Select * from Win32_DiskDrive"
                )
                drives = {drive.DeviceID: drive for drive in diskDrives}

            elif disk.DriveType == 4: # Network
                suffix = ['NetworkDrive']
                storedDrives = self.networkDrives
                mappedDrives = self.wmi.ExecQuery(
                    "Select * from  Win32_MappedLogicalDisk"
                )
                drives = {drive.DeviceID: drive for drive in mappedDrives}

            elif disk.DriveType == 5: # Compact
                suffix = ['CD-Rom']
                storedDrives = self.cdromDrives
                cdromDrives = self.wmi.ExecQuery(
                    "Select * from Win32_CDROMDrive"
                )
                drives = {drive.DeviceID: drive for drive in cdromDrives}

            elif disk.DriveType == 6: # Ram
                suffix = []
                storedDrives = {}
                drives = {}

            else:
                suffix = []
                storedDrives = {}
                drives = {}

            yield suffix, storedDrives, drives

    def UnmountDrive(self, letter):
        for suffix, storedDrives, drives in self.GetDrives():
            if 'Drive' in suffix:
                suffix.append('Unmounted')
            elif 'NetworkDrive' in suffix:
                suffix.append('Detached')
            else:
                suffix.append('Ejected')

            for deviceId in storedDrives.keys():
                drive, payload = storedDrives[deviceId]
                if deviceId in drives:
                    continue

                suffix.extend([
                    payload['name'],
                    payload['drive_letter'][:-1]
                ])

                if suffix[-1] == letter:
                    self.plugin.TriggerEvent(
                        '.'.join(suffix),
                        payload
                    )
                    del (storedDrives[deviceId])

    def MountDrive(self, letter=None):

        for suffix, storedDrives, drives in self.GetDrives():
            for deviceId in drives.keys():
                if deviceId in storedDrives:
                    continue

                if 'CD-Rom' in suffix:
                    cdrom = drives[deviceId]
                    if cdrom.MediaLoaded:
                        suffix.append('Inserted')
                        payload = dict(
                            drive_letter=cdrom.Drive,
                            max_size=cdrom.MaxMediaSize,
                            media_type=cdrom.MediaType,
                            manufacturer=cdrom.Manufacturer,
                            size=cdrom.Size,
                            status=cdrom.Status,
                            name=cdrom.Caption,
                        )
                        suffix.extend([
                            cdrom.Caption,
                            letter
                        ])
                        if cdrom.Drive[:-1] == letter or letter is None:
                            storedDrives[deviceId] = (
                                cdrom,
                                payload
                            )
                            if letter is not None:
                                self.plugin.TriggerEvent(
                                    '.'.join(suffix),
                                    payload
                                )

                elif 'NetworkDrive' in suffix:
                    nDrive = drives[deviceId]
                    suffix.append('Attached')

                    payload = dict(
                        network_path=nDrive.ProviderName,
                        name=nDrive.ProviderName.replace('\\', '\\\\'),
                        volume_name=nDrive.VolumeName,
                        size=nDrive.Size,
                        drive_letter=nDrive.Name,
                        free_space=nDrive.FreeSpace
                    )
                    suffix.extend([
                        nDrive.ProviderName.replace('\\', '\\\\'),
                        letter
                    ])

                    if nDrive.Name[:-1] == letter or letter is None:
                        storedDrives[deviceId] = (
                            nDrive,
                            payload
                        )
                        if letter is not None:
                            self.plugin.TriggerEvent(
                                '.'.join(suffix),
                                payload
                            )

                else:
                    drive = drives[deviceId]

                    partitionQuery = ASSOCIATORS % (
                        'DiskDrive',
                        deviceId.replace('\\', '\\\\'),
                        'DiskDriveToDiskPartition'
                    )
                    for partition in self.wmi.ExecQuery(partitionQuery):
                        diskQuery = ASSOCIATORS % (
                            'DiskPartition',
                            partition.DeviceID,
                            'LogicalDiskToPartition'
                        )
                        for disk in self.wmi.ExecQuery(diskQuery):
                            suffix.append('Mounted')
                            payload = dict(
                                drive_letter=disk.DeviceID,
                                free_space=disk.FreeSpace,
                                size=disk.Size,
                                volume_name=disk.VolumeName,
                                name=drive.Caption
                            )
                            suffix.extend([
                                drive.Caption,
                                letter
                            ])
                            if disk.DeviceID[:-1] == letter or letter is None:
                                storedDrives[deviceId] = (
                                    disk,
                                    payload
                                )
                                if letter is not None:
                                    self.plugin.TriggerEvent(
                                        '.'.join(suffix),
                                        payload
                                    )
If you like the work I have been doing then feel free to Image

Diz
Experienced User
Posts: 121
Joined: Tue Jan 10, 2017 4:49 pm

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by Diz » Mon Mar 06, 2017 8:36 pm

wow! yeah that does seem totally crazy! maybe there is an easier way you didnt find yet :)

anyway new version running good here so far

User avatar
kgschlosser
Site Admin
Posts: 4204
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by kgschlosser » Tue Mar 07, 2017 1:33 am

that is the easy way.. LOL.. I am getting the letter easy enough.. but then i have to work through the various drive types and then through partitions and volumes until i can get the drive letter for a specific drive.. then i can match the 2 up and make sure they are the same and then i know 100% that i have the right drive.. this is all just to be able to load up the payload with metrics. like device name and volume name...free space.. drive size.. media type which would be a really handy thing as i think i already mentioned. for knowing what program to run if you insert a drive.. no matter what the drive letter is that gets assigned to it.. just have it look at the volume label. and you set the volume label to photos or movies.. maybe videos. a saved game. who knows.

maybe it's just to let you know that someone put a drive in there and you can have it kick the drive out if it's not one of yours. Windows allows you to lock out plugging drives in but it locks out all drives.. this will allow you to specify the serial number of a drive that you will allow to be plugged in and all the rest it would kick the drive back out. now that's in a word.. kickass
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 4204
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by kgschlosser » Tue Mar 07, 2017 1:36 am

and also with the get device.. you now have control over the device. you can tell it to do things like go into power save mode.. or unplug it you will have to read the help files to determine what can be done with what devices.
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 4204
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by kgschlosser » Tue Mar 07, 2017 4:41 pm

ok... I have walked through all of the code and organized it. I have added most of the docstrings so those of you that use help() in the python shell will be able to see what does what.

Fixed: Help
Fixed: Double and single click of tree items
Fixed: Startup help in accessible after changing to a new tree item - click on the root item to get the startup help

Todo: Finish docstrings
Todo: Fix Mapped drive unmount event (or lack of event)
If you like the work I have been doing then feel free to Image

Diz
Experienced User
Posts: 121
Joined: Tue Jan 10, 2017 4:49 pm

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by Diz » Wed Mar 08, 2017 6:02 am

oops! seems another of those xp firmwarerevision attribute error thingies crept back into this version lol

05:17:39 Exception in thread WMI Thread:
05:17:39 Traceback (most recent call last):
05:17:39 File "threading.pyc", line 801, in __bootstrap_inner
05:17:39 File "D:\EventGhost\plugins\System\DeviceChangeNotifier.py", line 1041, in run
05:17:39 for attrName in attrs
05:17:39 File "D:\EventGhost\plugins\System\DeviceChangeNotifier.py", line 1041, in <dictcomp>
05:17:39 for attrName in attrs
05:17:39 File "win32com\client\dynamic.pyc", line 527, in __getattr__
05:17:39 AttributeError: <unknown>.FirmwareRevision

so... you changed the version checking method again? this line doesnt work for my windows xp version:

if platform.version() in ('5.2', '5.1'):

i dont know how many version numbers xp can have but mine is xp service pack 3 and its exact version number is 5.1.2600 so i changed the line to:

if platform.version() in ('5.2', '5.1.2600'):

that fixed it :)

User avatar
kgschlosser
Site Admin
Posts: 4204
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by kgschlosser » Wed Mar 08, 2017 6:17 am

yeahhh i did that for the people that are not running EG.WindowsVersion.. Thats my bad i forgot that platform has the build on the end...

I will modify the code to accommodate
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 4204
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by kgschlosser » Wed Mar 08, 2017 6:23 am

ok I have changed how it works.. this will all be different in the version that gets released with EG.. but it will allow for people to test it and report any errors.
If you like the work I have been doing then feel free to Image

Diz
Experienced User
Posts: 121
Joined: Tue Jan 10, 2017 4:49 pm

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by Diz » Wed Mar 08, 2017 6:43 am

yup, that works for me ;)

User avatar
kgschlosser
Site Admin
Posts: 4204
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by kgschlosser » Wed Mar 08, 2017 7:00 am

OK i just finished up the last of the docstrings.. I also fixed the network drive not generating an event when it's unplugged.


So unless someone can find something wrong with this.. it's pretty much a done deal..
If you like the work I have been doing then feel free to Image

Diz
Experienced User
Posts: 121
Joined: Tue Jan 10, 2017 4:49 pm

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by Diz » Wed Mar 08, 2017 7:13 am

seems good so far

User avatar
kgschlosser
Site Admin
Posts: 4204
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by kgschlosser » Wed Mar 08, 2017 7:36 am

this plugin covers every device class in windows.. so it should generate device attached events if you plug in an esata device.. it also supports network devices as well. like network TV tuners.
If you like the work I have been doing then feel free to Image

Snowbird
Experienced User
Posts: 348
Joined: Fri Jul 03, 2009 10:04 am

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by Snowbird » Thu Mar 09, 2017 4:57 pm

Hi K,

I have just tried this new plugin (1.1.22) and got some errors, I didn't check much yet, but just wanted to let you know.

Here's a screenshot :

Image

let me know if you need me to test something.

User avatar
kgschlosser
Site Admin
Posts: 4204
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by kgschlosser » Thu Mar 09, 2017 8:59 pm

That's because you are running EG 0.4 I didn't make the plugin to work with 0.4 this is a requested feature addition to a new version of EG which would be 0.5 so if you download the beta and install this then it will work
If you like the work I have been doing then feel free to Image

User avatar
kgschlosser
Site Admin
Posts: 4204
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Changes to System.DeviceAttached/System.DeviceRemoved

Post by kgschlosser » Fri Mar 10, 2017 7:23 am

I found a glitch in the code.
and I have fixed it. had to do with how it was finding and storing the devices.. I also added another means of identifying the notification. at the current moment it only changes the event string. I am trying to figure out a way to use the PNP Entity and then marry it to an actual device type without having to iterate through all of the devices looking for the device setup class guid and the vendor id.. but this may be the only way to get the actual device instead of a PNP entity. but it's another stepping stone. so the sttachment fixes that glitch i found.. and i will work on getting the rest of the devices fully added instead of adding a PNP entity.
Attachments
System-1.1.22.egplugin
(130.53 KiB) Downloaded 94 times
If you like the work I have been doing then feel free to Image

Post Reply