Esempio n. 1
0
def handle(sock, addr):
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

    deviceId, command, params = unpack.read(sock)
    try:
        Device.objects(deviceId=deviceId).get()
    except:
        sock.send("register first!")
        return None
    """
    短连接命令执行
    """
    if command in ComandList:
        ComandList[command](sock, deviceId, params)
        return None
    # 建立长连接
    elif command == "conn":
        dc = DeviceConnect(sock, addr)
        if not deviceId in DeviceList:
            dt = DeviceTerminal(deviceId)
            a = Greenlet.spawn(timeLiving, dt)
            a.start()
            DeviceList[deviceId] = dt
        else:
            dt = DeviceList[deviceId]
            if dt.conn:
                dt.conn.stop()
        dt.connect(dc)
        # 循环发送队列消息
        a = Greenlet.spawn(DeviceSocketSend, dt, dc)
        a.start()
Esempio n. 2
0
 def __init__(self,deviceId):
     self.deviceId = deviceId
     try:
         dev = Device.objects(deviceId=deviceId).get()
     except:
         dev = Device(deviceId=deviceId)
         dev.save()
     self.dev = dev
Esempio n. 3
0
def create_device(db: Session, device: dto.Device, user: User):
    db_device: Device = Device(**device.dict())
    db.add(db_device)
    db.flush()
    user.devices.append(db_device)
    db.refresh(db_device)
    db.commit()
    return db_device
Esempio n. 4
0
def timeLiving(dt):
    while True:
        gevent.sleep(10)
        device = Device.objects(deviceId=dt.deviceId).get()
        if time.time() - device.lastTime > setting.server["living"]:  # 2分钟未更新数据视为断线
            dt.conn.stop()
            del DeviceList[dt.deviceId]
            return None
Esempio n. 5
0
def postData(sock, deviceId, params):
    sock.send("I got it\n")
    if not len(params) == setting.device["postNum"]:
        sock.send("param num error!")
        return None
    try:
        device = Device.objects(deviceId=deviceId).get()
    except:
        sock.send("register first")
        return None
    device.temperatures.append(Temperature(temperature=eval(params[0]), makeTime=time.time()))
    device.humiditys.append(Humidity(humidity=eval(params[1]), makeTime=time.time()))
    device.lastTime = time.time()
    device.save()
    sock.send("ok")
Esempio n. 6
0
def sos(sock, deviceId, params):
    sock.send("I got it\n")
    if not len(params) == setting.device["sosNum"]:
        sock.send("params num error")
        return None
    try:
        device = Device.objects(deviceId=deviceId).get()
    except:
        sock.send("register first")
        return None
    device.coAlert = eval(params[0])
    device.chAlert = eval(params[1])
    device.guardAlert = eval(params[2])
    device.lastTime = time.time()
    device.save()
    sock.send("ok")
    """
Esempio n. 7
0
 def _create_dummy_device(device_id):
     return Device(id=device_id, name=f"Unnamed {device_id}")