Get the number of host monitor connections?

The company's DIY conference room audio and video solution is not as comprehensive as the commercial solution in terms of monitoring dimensions. There is no corresponding monitoring of the number of monitor connections and monitor status, so we borrowed some information to do a simple monitoring of the number of monitor connections in the conference room.

demo1

# 主机显示器连接功能实现-Demo1
import json
import requests
import win32api
from win32api import GetSystemMetrics
from win32con import SM_CMONITORS


# 定义一个类
 class Display_Number:
    # 实例化两个变量
    def __int__(self):
        self.strDisplay_Detection = ''
        self.strDisplay_Monitors = ''

    # 这个函数是获取当前连接显示器的数量-拓展模式下
    @staticmethod
    def getDisplay_Detection():
        MonitorNumber = GetSystemMetrics(SM_CMONITORS)
        return MonitorNumber

    # 这个函数是获取已连接显示器的信息:分辨率、主屏还是辅屏、Device name等-拓展模式下
    @staticmethod
    def getDisplay_Monitors():
        monitors = win32api.EnumDisplayMonitors()
        dis = win32api.GetMonitorInfo(monitors[0][0])
        '''
        monitor = dis.get('Monitor')  # 屏幕分辨率
        device = dis.get('Device')
        name = dis.get('Name')
        toal = monitor + device + name
        '''
        return dis

    # 定义钉钉webhook,将上面两个函数获取到的信息推送到钉钉群
    def Webhook(self):
        headers = {
    
    'Content-Type': 'application/json'}  # 定义数据类型
        webhook = 'https://oapi.dingtalk.com/robot/send?access_token=' # 你个人的webhook url地址
        # 定义要发送的数据
        # "at": {"atMobiles": "['"+ mobile + "']"

        data = {
    
    
            "msgtype": "text",
            "text": {
    
    "content": '显示器连接数量:' + str(self.getDisplay_Detection()) + '\n' + '显示器信息:' + str(
                self.getDisplay_Monitors())},
            "isAtAll": True}
        res = requests.post(webhook, data=json.dumps(data), headers=headers)  # 发送post请求

        print(res.text)


'''
 def Display_Monitors():
    a = win32.EnumDisplayDevices()
    print(a)
 '''

 if __name__ == '__main__':
    mon = Display_Number()
    mon.Webhook()

insert image description here

Demo2

# 主机显示器连接功能实现-Demo2
import json

import requests
import wmi

 w = wmi.WMI()
 dis_number = []
 dis_cap = []
 dis_ava = []
 dis_dev = []
 dis_name = []
 dis_sta = []
 monitors = w.Win32_DesktopMonitor()
for m in monitors:
    dis_number.append(m.DeviceID)
    dis_cap.append(m.Caption)
    dis_ava.append(str(m.Availability))
    dis_dev.append(str(m.DeviceID))
    dis_name.append(str(m.PNPDeviceID))
    dis_sta.append(m.Status)

# print(dis_numbers)
 dis_caps = '显示器类别:' + str(dis_cap)
# print(dis_caps)
 dis_avas = '显示器可用状态代码:' + str(dis_ava)
# print(dis_avas)
 dis_devs = '显示器设备ID:' + str(dis_dev)
# print(dis_devs)
 dis_names = '外接显示器名称:' + str(dis_name)
# print(dis_names)
 dis_stas = '外接显示器状态:' + str(dis_sta)
# print(dis_stas)

 dis_total = str(dis_caps) + '\n' + str(dis_avas) + '\n' + str(dis_devs) + '\n' + str(
        dis_names) + '\n' + str(dis_stas)
print(len(dis_number))
print(dis_total)

 headers = {
    
    'Content-Type': 'application/json'}  # 定义数据类型
 webhook = 'https://oapi.dingtalk.com/robot/send?access_token'  # 你的webhook url地址 
 # 定义要发送的数据
 # "at": {"atMobiles": "['"+ mobile + "']"

 data = {
    
    
    "msgtype": "text",
    "text": {
    
    "content": '显示器连接数量:' + str(len(dis_number)) + '\n' + str(dis_total)},
    "isAtAll": True}
 res = requests.post(webhook, data=json.dumps(data), headers=headers)  # 发送post请求

 print(res.text)
 

insert image description here

Guess you like

Origin blog.csdn.net/qq_45194089/article/details/128369688