forked from pyscada/PyScada
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
68 lines (58 loc) · 2.38 KB
/
views.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
# -*- coding: utf-8 -*-
from pyscada import log
from pyscada.models import RecordedData
from pyscada.phant.models import PhantDevice
from pyscada.utils import extract_numbers_from_str
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from time import time
import json
@csrf_exempt
def input(request,public_key=None,json_response = False):
private_key = None
values = {}
def do_response(success,message):
if json_response:
jdata = json.dumps({"success":success,"message":message},indent=2)
return HttpResponse(jdata, content_type='application/json')
else:
return HttpResponse('%s %s'%('1' if success else '0',message),content_type='text/plain')
if False:
#read values and private key from jsonp
return do_response(False,"not implemented")
elif request.POST.has_key('private_key'):
# stream is post
private_key = request.POST.get('private_key')
values = request.POST.dict()
elif request.META.has_key('HTTP_PHANT_PRIVATE_KEY'):
private_key = request.META['HTTP_PHANT_PRIVATE_KEY']
values = request.POST.dict()
elif request.GET.has_key('private_key'):
# stream is get
private_key = request.GET.get('private_key')
values = request.GET.dict()
else:
return do_response(False,"not a valid request")
try:
device = PhantDevice.objects.get(public_key=public_key)
except:
return do_response(False,'public key not valid')
# validate private key, validate values and write to RecordedData
if not device.private_key == private_key:
return do_response(False,'wrong private key')
# prepair the values for writing
# todo what to do with the cov value
output = []
for item in device.phant_device.variable_set.filter(name__in=values.keys()):
timestamp = time()
if type(values[item.name]) in [str,unicode]:
# convert from string to value
values[item.name] = extract_numbers_from_str(values[item.name])
# get prev_value from DB
item.query_prev_value()
#
if item.update_value(values[item.name],timestamp):
output.append(item.create_recorded_data_element())
if isinstance(output,list):
RecordedData.objects.bulk_create(output)
return do_response(True,'success')