wu_xinjun
2022-03-09 c724653beb2bb5f2ef31cf19e42b28adfce1b199
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import datetime
import os
import pika
 
class DeviceManager:
    def __init__(self, host, user, password):
        self.host = host
        self.user = user
        self.password = password
        self._devices = {}
 
    def _create_controll_channel(self, device_id: str):
        credentials = pika.PlainCredentials(self.user, self.password)
        parameters = pika.ConnectionParameters(
            credentials=credentials, host=self.host)
        # 连接队列服务器
        connection = pika.BlockingConnection(parameters)
        channel = connection.channel()
        channel.queue_declare(queue=device_id + "_callback")
        return channel
 
    def get_device(self, device_id: str, save_folder: str):
        device = self._devices.get(device_id, None)
        if device is None:
            start = datetime.datetime.now()
            if not os.path.exists(save_folder):
                os.mkdir(save_folder)
            path = os.path.join(save_folder,f"{device_id}-{start.strftime('%Y-%m-%d-%H-%M-%S')}")
            if not os.path.exists(path):
                os.mkdir(path)
            device = dict(
                power_on=True, 
                srart=start, 
                path=path, 
                images=0, 
                micro_wave=0,
                controll_channel=self._create_controll_channel(device_id)
                )
            self._devices[device_id] = device
        return device