Fixes POCloud Driver to work with Sails framework
This commit is contained in:
97
tsdriver.py
97
tsdriver.py
@@ -1,20 +1,8 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import types
|
|
||||||
import traceback
|
|
||||||
import binascii
|
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import thread
|
|
||||||
import os
|
|
||||||
import struct
|
|
||||||
import sys
|
|
||||||
import serial
|
|
||||||
import minimalmodbus
|
|
||||||
import pickle
|
|
||||||
import re
|
|
||||||
from device_base import deviceBase
|
from device_base import deviceBase
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
@@ -22,54 +10,22 @@ except:
|
|||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
|
||||||
channels = {}
|
channels = {}
|
||||||
min_upload_time = 30
|
min_upload_time = 120
|
||||||
addr = 'http://192.168.1.30:3000'
|
addr = 'http://192.168.1.30:3000'
|
||||||
|
|
||||||
|
|
||||||
def setupChannels():
|
def setupChannels():
|
||||||
tagJSObj = json.loads(requests.get(addr + "/json/tag").text)
|
tagJSObj = json.loads(requests.get(addr + "/tag").text)
|
||||||
if tagJSObj['status'] == "OK":
|
for t in tagJSObj:
|
||||||
for t in tagJSObj['tags']:
|
channel_name = t['name'].replace(" ", "").lower()
|
||||||
channel_name = re.sub(r'\W+', '', t['vanityName']).lower()
|
channels[channel_name] = {
|
||||||
channels[str(channel_name)] = {
|
'tagID': t['id'],
|
||||||
'tagID': t['id'],
|
'last_value': -999,
|
||||||
'last_value': -999,
|
'data_type': t['data_type']['plc_type'],
|
||||||
'data_type': "float",
|
'last_time_uploaded': 0,
|
||||||
'last_time_uploaded': 0,
|
'change_amount': t['change_threshold'],
|
||||||
'change_amount': (t['maxExpected'] - t['minExpected']) / 20,
|
'min_time_between_uploads': min_upload_time
|
||||||
'min_time_between_uploads': min_upload_time
|
}
|
||||||
}
|
|
||||||
|
|
||||||
# channels = {
|
|
||||||
# "DC_Bus_Voltage": {
|
|
||||||
# "last_value": -999,
|
|
||||||
# "data_type": "float",
|
|
||||||
# "change_amount": 5,
|
|
||||||
# "last_time_uploaded": 0,
|
|
||||||
# "min_time_between_uploads": 30
|
|
||||||
# },
|
|
||||||
# "Output_Frequency": {
|
|
||||||
# "last_value": -999,
|
|
||||||
# "data_type": "float",
|
|
||||||
# "change_amount": 1,
|
|
||||||
# "last_time_uploaded": 0,
|
|
||||||
# "min_time_between_uploads": 30
|
|
||||||
# },
|
|
||||||
# "Output_Current": {
|
|
||||||
# "last_value": -999,
|
|
||||||
# "data_type": "float",
|
|
||||||
# "change_amount": .2,
|
|
||||||
# "last_time_uploaded": 0,
|
|
||||||
# "min_time_between_uploads": 30
|
|
||||||
# },
|
|
||||||
# "Output_Voltage": {
|
|
||||||
# "last_value": -999,
|
|
||||||
# "data_type": "float",
|
|
||||||
# "change_amount": 5,
|
|
||||||
# "last_time_uploaded": 0,
|
|
||||||
# "min_time_between_uploads": 30
|
|
||||||
# }
|
|
||||||
# }
|
|
||||||
|
|
||||||
|
|
||||||
class start(threading.Thread, deviceBase):
|
class start(threading.Thread, deviceBase):
|
||||||
@@ -90,8 +46,7 @@ class start(threading.Thread, deviceBase):
|
|||||||
self.sendtodbJSON("device_address", self.device_address, 0)
|
self.sendtodbJSON("device_address", self.device_address, 0)
|
||||||
setupChannels()
|
setupChannels()
|
||||||
self.updateGPS()
|
self.updateGPS()
|
||||||
# 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):
|
def register(self):
|
||||||
channels["status"]["last_value"] = ""
|
channels["status"]["last_value"] = ""
|
||||||
|
|
||||||
@@ -101,27 +56,28 @@ class start(threading.Thread, deviceBase):
|
|||||||
while True:
|
while True:
|
||||||
if len(channels) > 0:
|
if len(channels) > 0:
|
||||||
try:
|
try:
|
||||||
for i in channels:
|
latest_vals = json.loads(requests.get(self.device_address + "/tag_val/latest").text)
|
||||||
runLoopStatus = i
|
for tag in latest_vals:
|
||||||
valData = self.checkTag(channels[i]['tagID'])
|
runLoopStatus = tag['name']
|
||||||
if valData:
|
ch_name = tag['name'].replace(" ", "").lower()
|
||||||
nowVal = valData['val']
|
if ch_name in channels:
|
||||||
ch = channels[i]
|
ch = channels[ch_name]
|
||||||
|
nowVal = tag['val']
|
||||||
if (abs(ch['last_value'] - nowVal) > ch['change_amount']) or ((time.time() - ch['last_time_uploaded']) > ch['min_time_between_uploads']):
|
if (abs(ch['last_value'] - nowVal) > ch['change_amount']) or ((time.time() - ch['last_time_uploaded']) > ch['min_time_between_uploads']):
|
||||||
self.sendtodbJSON(i, nowVal, 0)
|
self.sendtodbJSON(ch_name, nowVal, 0)
|
||||||
ch['last_time_uploaded'] = time.time()
|
ch['last_time_uploaded'] = time.time()
|
||||||
ch['last_value'] = nowVal
|
ch['last_value'] = nowVal
|
||||||
|
|
||||||
runLoopStatus = "Complete"
|
runLoopStatus = "Complete"
|
||||||
OK_state = 1
|
OK_state = 1
|
||||||
if not OK_state == last_OK_state:
|
if not OK_state == last_OK_state:
|
||||||
self.sendtodbJSON("driver_ok", OK_state, 0)
|
self.sendtodbJSON("driverok", OK_state, 0)
|
||||||
last_OK_state = OK_state
|
last_OK_state = OK_state
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
OK_state = 0
|
OK_state = 0
|
||||||
if not OK_state == last_OK_state:
|
if not OK_state == last_OK_state:
|
||||||
self.sendtodbJSON("driver_ok", OK_state, 0)
|
self.sendtodbJSON("driverok", OK_state, 0)
|
||||||
last_OK_state = OK_state
|
last_OK_state = OK_state
|
||||||
sleep_timer = 20
|
sleep_timer = 20
|
||||||
print "Error during {0} of run loop: {1}\nWill try again in {2} seconds...".format(runLoopStatus, e, sleep_timer)
|
print "Error during {0} of run loop: {1}\nWill try again in {2} seconds...".format(runLoopStatus, e, sleep_timer)
|
||||||
@@ -130,11 +86,6 @@ class start(threading.Thread, deviceBase):
|
|||||||
setupChannels()
|
setupChannels()
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
|
|
||||||
def checkTag(self, tagID):
|
|
||||||
tagData = json.loads(requests.get(self.device_address + "/json/val/"+str(tagID)).text)
|
|
||||||
# {u'tag_val': {u'tagID': 1, u'dateAdded': u'2015-12-08T23:43:38.000Z', u'id': 60, u'val': 56}}
|
|
||||||
return(tagData["tag_val"])
|
|
||||||
|
|
||||||
def tagserver_sync(self, name, value):
|
def tagserver_sync(self, name, value):
|
||||||
self.sendtodb("connected", "true", 0)
|
self.sendtodb("connected", "true", 0)
|
||||||
return True
|
return True
|
||||||
@@ -143,7 +94,7 @@ class start(threading.Thread, deviceBase):
|
|||||||
self.device_address = value
|
self.device_address = value
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def tagserver_gpsUpdate(self, name, value):
|
def tagserver_gpsUpdate(self, name, value):
|
||||||
|
global updateGPS
|
||||||
updateGPS()
|
updateGPS()
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user