Mercurial > templog
annotate py/receive.py @ 580:2ab866ee4f8c
fridge sensor is back
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 30 Nov 2015 21:40:57 +0800 |
parents | 5201f441bf4a |
children |
rev | line source |
---|---|
493 | 1 #!/usr/bin/env python |
2 | |
3 import params | |
4 import json | |
5 import sys | |
6 import tempfile | |
7 import signal | |
8 import os | |
9 | |
10 import config | |
11 | |
12 | |
13 def same_type(a, b): | |
14 ta = type(a) | |
15 tb = type(b) | |
16 | |
17 if ta == int: | |
18 ta = float | |
19 if tb == int: | |
20 tb = float | |
21 | |
22 return (ta == tb) | |
23 | |
24 def main(): | |
25 | |
26 i = sys.stdin.read() | |
27 new_params = json.loads(i) | |
28 | |
29 def_params = params.Params() | |
30 | |
529 | 31 if def_params.keys() != new_params.keys(): |
32 diff = def_params.keys() ^ new_params.keys() | |
493 | 33 return "Mismatching params, %s" % str(diff) |
34 | |
35 for k, v in new_params.items(): | |
36 if not same_type(v, def_params[k]): | |
37 return "Bad type for %s" % k | |
38 | |
39 dir = os.path.dirname(config.PARAMS_FILE) | |
40 | |
41 try: | |
42 t = tempfile.NamedTemporaryFile(prefix='config', | |
43 dir = dir, | |
44 delete = False) | |
45 | |
46 t.write(json.dumps(new_params, sort_keys=True, indent=4)+'\n') | |
495
905bbab632f7
Make sure we close descriptor before rename
Matt Johnston <matt@ucc.asn.au>
parents:
493
diff
changeset
|
47 name = t.name |
905bbab632f7
Make sure we close descriptor before rename
Matt Johnston <matt@ucc.asn.au>
parents:
493
diff
changeset
|
48 t.close() |
493 | 49 |
495
905bbab632f7
Make sure we close descriptor before rename
Matt Johnston <matt@ucc.asn.au>
parents:
493
diff
changeset
|
50 os.rename(name, config.PARAMS_FILE) |
529 | 51 except Exception as e: |
493 | 52 return "Problem: %s" % e |
53 | |
54 try: | |
55 pid = int(open('%s/tempserver.pid' % dir, 'r').read()) | |
56 if pid < 2: | |
57 return "Bad pid %d" % pid | |
58 os.kill(pid, signal.SIGHUP) | |
529 | 59 except Exception as e: |
493 | 60 return "HUP problem: %s" % e |
61 | |
62 return 'Good Update' | |
63 | |
64 if __name__ == '__main__': | |
529 | 65 print(main()) |
493 | 66 |