-
Notifications
You must be signed in to change notification settings - Fork 0
/
HiMessageMqtt.py
94 lines (82 loc) · 2.94 KB
/
HiMessageMqtt.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# $Date$
import json
import urllib2
import platform
import cpuinfo
import cputemp
import cputimes
import datetime
import time
from osinfo import OsInfo
import netinfo
import meminfo
from load import Load
import sys
import re
import os
from os.path import expanduser
from urllib2 import HTTPError
import HMMqtt
# to get ip address, see http://python.developpez.com/faq/?page=Reseau-Web
# 'https://api.ipify.org?format=json'
class HiMessage:
"Sends and manages a Hi message to the server"
__debug = False
def __init__(self, msg=None, seq=None):
self.reset()
if ( msg != None ) :
self.__data['msg'] = msg
if ( seq != None ) :
self.__data['seq'] = seq
def reset(self):
a = 1
self.__data = {}
from os.path import expanduser
idFileName = expanduser("~") + '/.hidevid'
fileExists = os.path.exists(idFileName)
if ( fileExists ):
idFileHandle = open(idFileName,'r')
deviceId = idFileHandle.readline()
deviceId = deviceId.strip()
self.__data['id'] = deviceId
idFileHandle.close
now = datetime.datetime.now()
self.__data['seq'] = int(round(time.time() * 1000))
self.__data['tsClient'] = now.isoformat()
cinfo = cpuinfo.get_cpu_info()
self.__data['cpu'] = format(cinfo['brand'])
self.__data['cpuCount'] = cinfo['count']
osinfo = OsInfo()
oinfo = osinfo.getOsInfo(cinfo)
self.__data['os'] = oinfo['os']
self.__data['osDist'] = oinfo['dist']
self.__data['osVersion'] = oinfo['version']
self.__data['osArch'] = oinfo['arch']
self.__data['osKernel'] = oinfo['kernel']
self.__data['cpuTemp'] = cputemp.get_cpu_temp()
l = Load()
self.__data['cpuLoad'] = l.getCpuLoad()
MemInfo = meminfo.getMemoryStatus()
self.__data['memAvail'] = MemInfo['memAvail'] / 1024
self.__data['memUsed'] = MemInfo['memUsed'] / 1024
self.__data['swapAvail'] = MemInfo['swpAvail'] / 1024
self.__data['swapUsed'] = MemInfo['swpUsed'] / 1024
self.__data['storage'] = l.getStorageStatus()
self.__data['network'] = netinfo.get_network_interfaces()
CpuTimes = cputimes.get_cpu_times()
self.__data['cpuUser'] = CpuTimes['user']
self.__data['cpuSystem'] = CpuTimes['system']
self.__data['cpuIdle'] = CpuTimes['idle']
self.__data['ioWait'] = CpuTimes['iowt']
self.__data['UpTime'] = CpuTimes['uptime']
def send(self,publicUrl,msg):
# append public URL to data
self.__data['clientIp'] = publicUrl
HMMqtt.publish_message('messages','insert',self.__data['id'],self.__data)
def add(self, key, value):
self.__data[key] = value
def info (self, msg):
if ( msg != None ):
self.__data['msg'] = msg