{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d9c14718",
   "metadata": {},
   "outputs": [],
   "source": [
    "from requests import get,post\n",
    "import time\n",
    "ip = \"http://band.hxzhxy.cn\"\n",
    "# ip  = \"http://192.168.0.209:8100\"\n",
    "param = {\n",
    "    \"username\": \"tw_xiatianle\",\n",
    "    \"password\": \"xia123456\",\n",
    "    \"client_id\": \"client\",\n",
    "    \"grant_type\": \"password\",\n",
    "    \"client_secret\": \"123456\",\n",
    "}\n",
    "data = get(url=f\"{ip}/oauth/token\",params=param).json()\n",
    "token = data[\"access_token\"]\n",
    "header = {\n",
    "    \"content-type\": \"application/json\",\n",
    "    \"authorization\": f\"Bearer {token}\",\n",
    "}\n",
    "teamId=\"119\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1673af54",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = get(f\"{ip}/workforce/classTeamMachine/list\",headers=header,params={\n",
    "    \"page\": \"0\",\n",
    "    \"size\": \"100\",\n",
    "    \"teamId\": teamId\n",
    "}).json()[\"data\"][\"content\"]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4cf83c82",
   "metadata": {},
   "outputs": [],
   "source": [
    "tem = []\n",
    "for item in data:\n",
    "    if item[\"classroom\"]  != \"鸿兴测试分组\":\n",
    "        tem.append(item)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1790800e",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = tem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "34f18ca5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from paho.mqtt import client as mqtt\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7cf83679",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "broker = 'band.hxzhxy.cn'\n",
    "# broker = '192.168.0.111'\n",
    "port = 1883\n",
    "topic = \"/python/mqtt\"\n",
    "client_id = f'python-mqtt-demo'\n",
    "# MQTT客户端的回调函数\n",
    "def on_connect(client, userdata, flags, rc):\n",
    "    print(\"Connected with result code \"+str(rc))\n",
    "    # 连接成功后订阅主题\n",
    "    # client.subscribe(\"#\")\n",
    "\n",
    "def on_message(client, userdata, msg):\n",
    "    print(msg.topic+\" \"+str(msg.payload))\n",
    "\n",
    "# 创建MQTT客户端实例\n",
    "client = mqtt.Client(client_id=client_id)\n",
    "\n",
    "# 绑定回调函数\n",
    "client.on_connect = on_connect\n",
    "client.on_message = on_message\n",
    "\n",
    "# 连接到MQTT代理\n",
    "client.connect(broker, port, 60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e706bbb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<paho.mqtt.client.MQTTMessageInfo at 0x1f295d5cef0>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "client.publish(topic, \"hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72c9ff77",
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in data:\n",
    "    print(item)\n",
    "    mac=item[\"mac\"]\n",
    "    topic=f\"cmd/publicizeBanpai/{mac}/cmd\"\n",
    "    message={\n",
    "        \"type\":\"info\"\n",
    "    }\n",
    "    client.publish(topic, json.dumps(message,ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3da280f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in data:\n",
    "    mac=item[\"mac\"]\n",
    "    topic=f\"cmd/publicizeBanpai/{mac}/cmd\"\n",
    "    message=\"reboot -p\"\n",
    "    client.publish(topic, message)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "82403f63",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<paho.mqtt.client.MQTTMessageInfo at 0x1f295d5c360>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "topic=f\"cmd/publicizeBanpai/08E60E75E09E/control\"\n",
    "message={\"type\":\"info\"}\n",
    "client.publish(topic, json.dumps(message,ensure_ascii=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ff4367a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
