Exemplo n.º 1
0
    def getAllUnitsFromDB():
        sql = """
		SELECT 	units_run.lastValue,
				units_run.id,
				units_run.unid,
				units_run.setid,
				units_run.lastTime,
				units_run.needSetValue,
				units_run.name,
				units_def.description,
				units_def.units,
				units_def.direction,
				units_def.valueType,
				units_def.timeout
		FROM   `units_run`
			   LEFT OUTER JOIN units_def ON units_def.unid = units_run.unid
			"""
        #print(sql)
        data = DB.sqlSelect(sql)
        if len(data) == 0:
            print("NOTE: No any devices where registed yet!")

        else:
            for row in data:
                unid = row['unid']
                setid = row['setid']
                device = Device(unid, setid, None, False)
                device.id = row['id']
                device.lastTime = row['lastTime']
                device.lastValue = row['lastValue']
                device.needSetValue = int(row['needSetValue']) == 1
                device.name = row['name']
                device.desc = row['description']
                device.units = row['units']
                device.valueType = row['valueType']
                device.direction = row['direction']
                device.timeout = row['timeout']
                allId = unid + '' + setid
                device.isOnline = False
                SocketWrap.allUnits[allId] = device
Exemplo n.º 2
0
    def registerDevice(self, unid, setid=''):
        Log.i('SocketWrap', "Registering new device " + unid)
        resInfo = DB.sqlSelect(
            "SELECT units_def.description  FROM `units_def` WHERE unid = '%s'"
            % unid)
        if (setid == ''):
            res = DB.sqlSelect(
                "SELECT units_run.setid  FROM `units_run` WHERE units_run.unid = '%s' ORDER BY `id` DESC LIMIT 1 "
                % unid)
        #print("SELECT `id` FROM `units_run` WHERE `unid` = '%s' ORDER BY `id` DESC LIMIT 1 " % unid)
        nextSetdId = ''

        if len(resInfo) == 0:
            Log.e('SocketWrap', 'Unknown produced device connected')
            DB.addAlert(unid, nextSetdId, 1004)
            return Device(0, 0, self)
        else:
            if (setid != ''):
                nextSetdId = setid
            elif len(res) > 0:
                Log.i('SocketWrap', "Last id: %s" % res[0]['setid'])
                nextSetdId = '{:04x}'.format(int(res[0]['setid'], 16) + 1)
            else:
                nextSetdId = '0001'
            nextSetdId = nextSetdId.upper()
            Log.i('SocketWrap',
                  "Registering new device with id: " + unid + "|" + nextSetdId)

            sql = """
				INSERT INTO `units_run`
				(
				 `unid`,
				 `setid`,
				 `lastvalue`,
				 `lasttime`,
				 `timeAdded`,
				 `interface`,
				 `name`,
				 `alive`
				)
				VALUES
				(
				 '%s',
				 '%s',
				 '',
				 '',
				 '%s',
				 '%s',
				 '%s',
				 '1'
				); 
			""" % (unid, nextSetdId, strftime("%Y.%m.%d %H:%M:%S", localtime()),
            self.interface, resInfo[0]['description'])
            id = DB.sqlInsert(sql)

            msg = Msg(unid=unid,
                      setid=setid,
                      cmd="device.reg.res",
                      value=nextSetdId[:2] + ',' + nextSetdId[2:],
                      socket=self)
            self.outgoingMSG.append(msg)

            device = Device(unid, nextSetdId, self)
            device.id = id
            device.isOnline = True
            allId = unid + '' + nextSetdId
            self.units[allId] = device
            SocketWrap.allUnits[allId] = device
            SocketWrap.onlineUnits[unid + '' + nextSetdId] = device

            DB.registerEvent(unid, nextSetdId, 'SocketWrap',
                             'Registered new device!')
            return device