added meshify converted drivers
This commit is contained in:
13
meshifyDrivers/mainHP/config.txt
Normal file
13
meshifyDrivers/mainHP/config.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
|
||||
"driverFileName":"mainMeshify.py",
|
||||
"deviceName":"mainMeshify",
|
||||
"driverId":"0000",
|
||||
"releaseVersion":"17",
|
||||
"files": {
|
||||
"file1":"mainMeshify.py",
|
||||
"file2":"main.py",
|
||||
"file3":"meshifyData.py"
|
||||
}
|
||||
|
||||
}
|
||||
200
meshifyDrivers/mainHP/device_base.py
Normal file
200
meshifyDrivers/mainHP/device_base.py
Normal file
@@ -0,0 +1,200 @@
|
||||
import types
|
||||
import traceback
|
||||
import binascii
|
||||
import threading
|
||||
import time
|
||||
import thread
|
||||
import os
|
||||
import struct
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
class deviceBase():
|
||||
|
||||
def __init__(self, name=None, number=None, mac=None, Q=None, mcu=None, companyId=None, offset=None, mqtt=None, Nodes=None):
|
||||
self.offset = offset
|
||||
self.company = companyId
|
||||
self.name = name
|
||||
self.number = number
|
||||
self.q = Q
|
||||
self.deviceName = name + '_[' + mac + ':' + number[0:2] + ':' + number[2:] + ']!'
|
||||
self.chName = "M1" + '_[' + mac + ':'
|
||||
self.chName2 = '_[' + mac + ':'
|
||||
print 'device name is:'
|
||||
print self.deviceName
|
||||
mac2 = mac.replace(":", "")
|
||||
self.mac = mac2.upper()
|
||||
self.address = 1
|
||||
self.debug = True
|
||||
self.mcu = mcu
|
||||
self.firstRun = True
|
||||
self.mqtt = mqtt
|
||||
self.nodes = Nodes
|
||||
#local dictionary of derived nodes ex: localNodes[tank_0199] = self
|
||||
self.localNodes = {}
|
||||
os.system("chmod 777 /root/reboot")
|
||||
os.system("echo nameserver 8.8.8.8 > /etc/resolv.conf")
|
||||
|
||||
|
||||
def sendtodbLoc(self, ch, channel, value, timestamp, deviceName, mac):
|
||||
|
||||
|
||||
#this will add your derived nodes the master nodes list, allowing them to receive sets!!
|
||||
localNodesName = deviceName + "_" + str(ch) + "99"
|
||||
|
||||
if not self.localNodes.has_key(localNodesName):
|
||||
self.localNodes[localNodesName] = True
|
||||
self.nodes[localNodesName] = self
|
||||
|
||||
#make the techname
|
||||
lst = textwrap.wrap(str(mac), width=2)
|
||||
tech = ""
|
||||
for i in range(len(lst)):
|
||||
tech += lst[i].lower() + ":"
|
||||
|
||||
|
||||
chName2 = '_[' + tech
|
||||
|
||||
if int(ch) < 10:
|
||||
ch = "0" + str(int(ch))
|
||||
|
||||
if len(ch) > 2:
|
||||
ch = ch[:-2]
|
||||
|
||||
dname = deviceName + chName2 + str(ch) + ":98]!"
|
||||
|
||||
if int(timestamp) == 0:
|
||||
timestamp = self.getTime()
|
||||
|
||||
topic = 'meshify/db/%s/%s/%s/%s' % (self.company, mac, dname, channel)
|
||||
print topic
|
||||
msg = """[ { "value":"%s", "timestamp":"%s" } ]""" % (str(value), str(timestamp))
|
||||
print msg
|
||||
self.q.put([topic, msg, 0])
|
||||
|
||||
def sendtodbDevJSON(self, ch, channel, value, timestamp, deviceName):
|
||||
|
||||
if int(ch) < 10:
|
||||
ch = "0" + str(int(ch))
|
||||
dname = deviceName + self.chName2 + str(ch) + ":99]!"
|
||||
if int(timestamp) == 0:
|
||||
timestamp = self.getTime()
|
||||
|
||||
topic = 'meshify/db/%s/%s/%s/%s' % (self.company, self.mac, dname, channel)
|
||||
print topic
|
||||
msg = """[ { "value":%s, "timestamp":"%s" } ]""" % (str(value), str(timestamp))
|
||||
print msg
|
||||
self.q.put([topic, msg, 0])
|
||||
|
||||
def sendtodbLora(self, ch, channel, value, timestamp, deviceName):
|
||||
|
||||
if ":" not in ch:
|
||||
ch = ch[0:2] + ":" + ch[2:4]
|
||||
|
||||
#this will add your derived nodes the master nodes list, allowing them to receive sets!!
|
||||
localNodesName = deviceName + "_" + str(ch).replace(':', "")
|
||||
|
||||
if not self.localNodes.has_key(localNodesName):
|
||||
self.localNodes[localNodesName] = True
|
||||
self.nodes[localNodesName] = self
|
||||
|
||||
|
||||
|
||||
dname = deviceName + self.chName2 + str(ch) + "]!"
|
||||
|
||||
|
||||
|
||||
if int(timestamp) == 0:
|
||||
timestamp = self.getTime()
|
||||
|
||||
topic = 'meshify/db/%s/%s/%s/%s' % (self.company, self.mac, dname, channel)
|
||||
print topic
|
||||
msg = """[ { "value":"%s", "timestamp":"%s" } ]""" % (str(value), str(timestamp))
|
||||
print msg
|
||||
self.q.put([topic, msg, 0])
|
||||
|
||||
def sendtodbDev(self, ch, channel, value, timestamp, deviceName):
|
||||
|
||||
|
||||
#this will add your derived nodes the master nodes list, allowing them to receive sets!!
|
||||
localNodesName = deviceName + "_" + str(ch) + "99"
|
||||
|
||||
if not self.localNodes.has_key(localNodesName):
|
||||
self.localNodes[localNodesName] = True
|
||||
self.nodes[localNodesName] = self
|
||||
|
||||
if int(ch) < 10:
|
||||
ch = "0" + str(int(ch))
|
||||
|
||||
dname = deviceName + self.chName2 + str(ch) + ":99]!"
|
||||
|
||||
|
||||
|
||||
if int(timestamp) == 0:
|
||||
timestamp = self.getTime()
|
||||
|
||||
topic = 'meshify/db/%s/%s/%s/%s' % (self.company, self.mac, dname, channel)
|
||||
print topic
|
||||
msg = """[ { "value":"%s", "timestamp":"%s" } ]""" % (str(value), str(timestamp))
|
||||
print msg
|
||||
self.q.put([topic, msg, 0])
|
||||
|
||||
def sendToTB(self, payload):
|
||||
topic = 'v1/devices/me/telemetry'
|
||||
print(topic, payload)
|
||||
self.q.put([topic, payload, 0])
|
||||
|
||||
def sendtodbCH(self, ch, channel, value, timestamp):
|
||||
|
||||
|
||||
if int(ch) < 10:
|
||||
ch = "0" + str(ch)
|
||||
|
||||
dname = self.chName + str(ch) + ":99]!"
|
||||
|
||||
|
||||
|
||||
if int(timestamp) == 0:
|
||||
timestamp = self.getTime()
|
||||
|
||||
topic = 'meshify/db/%s/%s/%s/%s' % (self.company, self.mac, dname, channel)
|
||||
print topic
|
||||
msg = """[ { "value":"%s", "timestamp":"%s" } ]""" % (str(value), str(timestamp))
|
||||
print msg
|
||||
self.q.put([topic, msg, 0])
|
||||
|
||||
def sendtodb(self, channel, value, timestamp):
|
||||
|
||||
if int(timestamp) == 0:
|
||||
timestamp = self.getTime()
|
||||
if timestamp < 1400499858:
|
||||
return
|
||||
else:
|
||||
timestamp = str(int(timestamp) + int(self.offset))
|
||||
|
||||
topic = 'meshify/db/%s/%s/%s/%s' % (self.company, self.mac, self.deviceName, channel)
|
||||
print topic
|
||||
msg = """[ { "value":"%s", "timestamp":"%s" } ]""" % (str(value), str(timestamp))
|
||||
print msg
|
||||
self.q.put([topic, msg, 0])
|
||||
|
||||
def sendtodbJSON(self, channel, value, timestamp):
|
||||
|
||||
if int(timestamp) == 0:
|
||||
timestamp = self.getTime()
|
||||
if timestamp < 1400499858:
|
||||
return
|
||||
else:
|
||||
timestamp = str(int(timestamp) + int(self.offset))
|
||||
|
||||
topic = 'meshify/db/%s/%s/%s/%s' % (self.company, self.mac, self.deviceName, channel)
|
||||
print topic
|
||||
msg = """[ { "value":%s, "timestamp":"%s" } ]""" % (str(value), str(timestamp))
|
||||
print msg
|
||||
self.q.put([topic, msg, 0])
|
||||
def getTime(self):
|
||||
return str(int(time.time() + int(self.offset)))
|
||||
|
||||
|
||||
|
||||
|
||||
1399
meshifyDrivers/mainHP/main.py
Normal file
1399
meshifyDrivers/mainHP/main.py
Normal file
File diff suppressed because it is too large
Load Diff
133
meshifyDrivers/mainHP/mainMeshify.py
Normal file
133
meshifyDrivers/mainHP/mainMeshify.py
Normal file
@@ -0,0 +1,133 @@
|
||||
import time
|
||||
import os
|
||||
try:
|
||||
import json
|
||||
except:
|
||||
import simplejson as json
|
||||
import thread
|
||||
import threading
|
||||
import re
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class start(threading.Thread):
|
||||
|
||||
def __init__(self, name=None, number=None, mac=None, Q=None, mcu=None, companyId=None, offset=None, mqtt=None, Nodes=None):
|
||||
threading.Thread.__init__(self)
|
||||
self.daemon = True
|
||||
self.offset = offset
|
||||
self.company = companyId
|
||||
self.name = name
|
||||
self.number = number
|
||||
self.q = Q
|
||||
self.deviceName = name + '_[' + mac + ':' + number[0:2] + ':' + number[2:] + ']!'
|
||||
print 'device name is:'
|
||||
print self.deviceName
|
||||
mac2 = mac.replace(":", "")
|
||||
self.mac = mac2.upper()
|
||||
self.version = "17" #hp device management
|
||||
self.finished = threading.Event()
|
||||
|
||||
threading.Thread.start(self)
|
||||
|
||||
|
||||
|
||||
#this is a required function for all drivers, its goal is to upload some piece of data
|
||||
#about your device so it can be seen on the web
|
||||
def register(self):
|
||||
#self.mainMistaway_hb('hb', 'On')
|
||||
self.sendtodb("connected", "true", 0)
|
||||
|
||||
def stop (self):
|
||||
self.finished.set()
|
||||
self.join()
|
||||
|
||||
def sendtodb(self, channel, value, timestamp):
|
||||
if int(timestamp) == 0:
|
||||
timestamp = self.getTime()
|
||||
if timestamp < 1400499858:
|
||||
return
|
||||
csplit = re.split(r"(.*?)_\[(.*?)\]", self.deviceName)
|
||||
nodeTypeName = csplit[1]
|
||||
uniqueID = csplit[2]
|
||||
company = "194"
|
||||
|
||||
|
||||
try:
|
||||
topic = 'meshify/db/%s/%s/%s/%s/%s' % (company, "_", nodeTypeName, uniqueID, channel)
|
||||
print topic
|
||||
if channel == "files":
|
||||
#for the file structure I had to take off the " " around the value
|
||||
msg = """[{"value":%s}]""" % (str(value))
|
||||
else:
|
||||
msg = """[{"value":"%s"}]""" % (str(value))
|
||||
print msg
|
||||
self.q.put([topic, msg, 0])
|
||||
except:
|
||||
print "didn't work to send up MQTT data"
|
||||
|
||||
def run(self):
|
||||
#on startup send the version number
|
||||
self.sendtodb("version", str(self.version), 0)
|
||||
while True:
|
||||
try:
|
||||
self.mainMeshify_hb('hb', 'On')
|
||||
self.sendtodb("connected", "true", 0)
|
||||
time.sleep(3600 * 4)
|
||||
except Exception, e:
|
||||
print e
|
||||
|
||||
def mainMeshify_files(self, name, value):
|
||||
name = 'files'
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
dict = {}
|
||||
for dirname, dirnames, filenames in os.walk(str(value)):
|
||||
# print path to all subdirectories first.
|
||||
|
||||
|
||||
print "##########################################"
|
||||
print "new directory: " + dirname
|
||||
print "##########################################"
|
||||
# print path to all filenames.
|
||||
tempDictParent = {}
|
||||
for filename in filenames:
|
||||
tempDict = {}
|
||||
filepath = os.path.join(dirname, filename)
|
||||
try:
|
||||
fileMem = os.stat(filepath).st_size
|
||||
fileDate = os.stat(filepath).st_mtime
|
||||
except:
|
||||
fileMem = ""
|
||||
fileDate = ""
|
||||
print filepath, fileMem, fileDate
|
||||
tempDict["mem"] = fileMem
|
||||
tempDict["date"] = fileDate
|
||||
tempDictParent[filename] = tempDict
|
||||
|
||||
dict[dirname] = tempDictParent
|
||||
|
||||
|
||||
# Advanced usage:
|
||||
# editing the 'dirnames' list will stop os.walk() from recursing into there.
|
||||
if '.git' in dirnames:
|
||||
# don't go into any .git directories.
|
||||
dirnames.remove('.git')
|
||||
|
||||
value = json.dumps(dict)
|
||||
self.sendtodb(name, value, 0)
|
||||
return True
|
||||
|
||||
|
||||
def mainMeshify_hb(self, name, value):
|
||||
self.sendtodb(name, value, 0)
|
||||
|
||||
|
||||
def getTime(self):
|
||||
return str(int(time.time() + int(self.offset)))
|
||||
|
||||
112
meshifyDrivers/mainHP/meshifyData.py
Normal file
112
meshifyDrivers/mainHP/meshifyData.py
Normal file
@@ -0,0 +1,112 @@
|
||||
|
||||
|
||||
|
||||
import urllib
|
||||
try:
|
||||
import json
|
||||
except:
|
||||
import simplejson as json
|
||||
import pickle
|
||||
|
||||
MAC = "00409D53168A"
|
||||
|
||||
class meshifyData():
|
||||
|
||||
def __init__(self, MAC):
|
||||
self.mac = MAC[0:6] + "FF-FF" + MAC[6:]
|
||||
print "here is the mac: " + self.mac
|
||||
#set the defaults
|
||||
self.param_dict = {}
|
||||
|
||||
def checkConfig(self):
|
||||
|
||||
url = "https://f5rrbd3r45.execute-api.us-east-1.amazonaws.com/device_config?mac=" + self.mac
|
||||
|
||||
try:
|
||||
f = urllib.urlopen(url)
|
||||
except:
|
||||
print "Error opening url for remote config"
|
||||
#return the defaults
|
||||
return None
|
||||
|
||||
try:
|
||||
s = f.read()
|
||||
#print s
|
||||
if len(s) < 5:
|
||||
return None
|
||||
s = s[1:-1].replace("'", '"')
|
||||
print s
|
||||
data = json.loads(s)
|
||||
#if we get there then replace the deviceList.txt
|
||||
with open('/root/python_firmware/deviceList.txt', 'w') as myfile:
|
||||
json.dump(data, myfile, indent=4)
|
||||
return data
|
||||
except Exception as e:
|
||||
print e
|
||||
#return the defaults
|
||||
return None
|
||||
|
||||
def checkAPI(self):
|
||||
|
||||
|
||||
offset = -21600
|
||||
dst = False
|
||||
companyId = "1"
|
||||
|
||||
|
||||
url = "https://machines.meshify.com/api/gateway?macaddressForTimezone=" + self.mac
|
||||
|
||||
try:
|
||||
f = urllib.urlopen(url)
|
||||
except:
|
||||
print "Error opening url"
|
||||
#return the defaults
|
||||
return offset, dst, companyId
|
||||
|
||||
try:
|
||||
s = f.read()
|
||||
print s
|
||||
data = json.loads(s)
|
||||
offset = int(data["offset"])
|
||||
dst = bool(int(data["dst"]))
|
||||
print bool(int("0"))
|
||||
companyId = data["companyId"]
|
||||
return offset, dst, companyId
|
||||
except Exception,e:
|
||||
print e
|
||||
#return the defaults
|
||||
return -21600, False, "1"
|
||||
|
||||
def getdata(self):
|
||||
#if the API fails and the company ID of 1 is returned then you need to
|
||||
#check and see if you have pickled anything.
|
||||
#if it doesn't fail, and it gives you something other than 1
|
||||
#then you need to repickle the object
|
||||
self.offset, self.dst, self.companyId = self.checkAPI()
|
||||
if self.companyId == "1":
|
||||
try:
|
||||
self.param_dict = pickle.load( open( "params.p", "rb" ) )
|
||||
except:
|
||||
print self.offset, self.dst, self.companyId
|
||||
return self.offset, self.dst, self.companyId
|
||||
try:
|
||||
self.offset = self.param_dict["offset"]
|
||||
self.dst = self.param_dict["dst"]
|
||||
self.companyId = self.param_dict["companyId"]
|
||||
except:
|
||||
return -21600, False, "1"
|
||||
|
||||
return self.offset, self.dst, self.companyId
|
||||
|
||||
|
||||
else:
|
||||
self.param_dict["offset"] = self.offset
|
||||
self.param_dict["dst"] = self.dst
|
||||
self.param_dict["companyId"] = self.companyId
|
||||
pickle.dump( self.param_dict, open( "params.p", "wb" ) )
|
||||
print self.param_dict
|
||||
print self.offset, self.dst, self.companyId
|
||||
return self.offset, self.dst, self.companyId
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user