-
Notifications
You must be signed in to change notification settings - Fork 1
/
isegCC2xChannel.py
185 lines (157 loc) · 6.71 KB
/
isegCC2xChannel.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# -*- coding: utf-8 -*-
#***************************************************************************
#* Copyright (C) 2020 by Andreas Langhoff *
#* <andreas.langhoff@frm2.tum.de> *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU General Public License v3 as published *
#* by the Free Software Foundation; *
# **************************************************************************
#import copy
import json
from entangle import base
from entangle.core import states , Prop, Attr
from entangle.device.iseg import CC2xlib
# pylint: disable=unused-import
import entangle.device.iseg.CC2xlib.globals
from entangle.device.iseg.CC2xlib.HardLimits import HardLimits
class PowerSupply(base.PowerSupply):
properties = {
'address': Prop(str, 'ip address of device.'),
'user': Prop(str, 'user.'),
'password': Prop(str, 'pw.'),
'channel': Prop(str, 'channel.'),
'operatingstyle': Prop(str, 'operatingstyle.', default=''),
}
attributes = {
'jsonstatus': Attr(str, '', writable=False, memorized=False),
}
def init(self):
#print("isegCC2cChannel.PowerSupply.init")
self.mode = 'voltage'
self._state = (states.INIT,self.address)
self.channels_handled = [self.channel]
self.waitstring = ''
self.waitstringmintime = ''
# begin below should turn off disallowed attribute for off state, but
# it is not working
tu = self.attributes['value']
daw = list(tu.disallowed_write)
if states.OFF in daw:
daw.remove(states.OFF)
tu1 = tuple(daw)
tu.disallowed_write = tu1
#idea: put this in __init__
# probably attribute is read in before by deviceworker -> todo: check
# if time permitting.
#end
CC2xlib.globals.CRATE.lock.acquire()
CC2xlib.globals.CRATE.instances.append(self)
CC2xlib.globals.CRATE.lock.release()
CC2xlib.globals.add_monitor(self.address,self.user,self.password)
def rolisAlive(self):
# this function is called once the crate is alive (= can accept
# parameters)
rol = []
print("isegCC2cChannel.PowerSupply.rolisAlive")
jos = json.loads(self.operatingstyle)
for item in jos:
v = jos[item]
rol.append(CC2xlib.json_data.make_requestobject("setItem",self.channel,item,v))
return rol
def delete(self):
print("isegCC2xChannel.delete")
n_instances = 0
CC2xlib.globals.CRATE.lock.acquire()
for i in CC2xlib.globals.CRATE.instances:
if i == self :
CC2xlib.globals.CRATE.instances.remove(i)
n_instances = len(CC2xlib.globals.CRATE.instances)
CC2xlib.globals.CRATE.lock.release()
if not n_instances:
CC2xlib.globals.reset()
def On(self):
#print("on")
rol = []
#instrol = self.rolisAlive()
#rv, msg = CC2xlib.globals.HardLimits.checkmovelimitsandbugfix(instrol)
#if rv:
# self._state =(self._state[0], msg +self._state[1])
#rol.extend(instrol)
rol.append(CC2xlib.json_data.make_requestobject("setItem",self.channel,"Control.on",1))
CC2xlib.globals.queue_request(rol)
self._state = (self._state[0], '')
def Off(self):
#print("off")
rol = []
rol.append(CC2xlib.json_data.make_requestobject("setItem",self.channel,"Control.on",0))
CC2xlib.globals.queue_request(rol)
def getItemValue(self, cmd:str) -> float:
rv = 0
CC2xlib.globals.CRATE.lock.acquire()
if self.channel in CC2xlib.globals.CRATE.itemUpdated:
ours = CC2xlib.globals.CRATE.itemUpdated[self.channel]
if cmd in ours:
vu = ours[cmd]
rv = float(vu['v'])
CC2xlib.globals.CRATE.lock.release()
return rv
def read_voltage(self):
return self.getItemValue("Status.voltageMeasure")
def write_voltage(self, value):
rol = []
rol.append(CC2xlib.json_data.make_requestobject("setItem",self.channel,"Control.voltageSet",str(value)))
rv, msg = HardLimits.checkmovelimitsandbugfix(rol)
if rv:
self._state = (self._state[0], msg + self._state[1])
CC2xlib.globals.queue_request(rol)
def read_current(self):
return self.getItemValue("Status.currentMeasure")
def write_current(self, value):
if self.mode == 'current':
rol = []
rol.append(CC2xlib.json_data.make_requestobject("setItem",self.channel,"Control.currentSet",str(value)))
CC2xlib.globals.queue_request(rol)
def read_jsonstatus(self):
ours = CC2xlib.globals.StatusJson(self.channels_handled)
return ours
def get_jsonstatus_unit(self):
return ''
def state(self):
currstate = (states.UNKNOWN,'unknown')
CC2xlib.globals.CRATE.lock.acquire()
if self.channel in CC2xlib.globals.CRATE.itemUpdated:
ouritems = CC2xlib.globals.CRATE.itemUpdated[self.channel] # all messages for channel
#print(ouritems)
for item in ouritems:
vu = ouritems[item]
if not 'v' in vu:
continue
v = vu['v']
if item == 'Status.runningState':
if str(v) == 'ok':
self._state = (states.ON, self._state[1])
if str(v) == 'off':
self._state = (states.OFF, self._state[1])
if item == 'Control.on':
if str(v) == '1':
self._state = (states.ON, self._state[1])
if str(v) == '0':
self._state = (states.OFF, self._state[1])
if item == 'Event.currentTrip':
if str(v) == '1':
self._state = (states.ALARM,CC2xlib.globals.CRATE._state[1])
if 'Status.ramping' in ouritems: # effectively ovveriding previous choice
vu = ouritems['Status.ramping']
if 'v' in vu:
v = vu['v']
if str(v) == '1':
self._state = (states.BUSY, self._state[1])
if CC2xlib.globals.CRATE._state[0] in [states.INIT, states.UNKNOWN]:
self._state = (self._state[0], "Wait...")
else:
if self._state[1] == "Wait...":
self._state = (self._state[0],'')
currstate = self._state # copy.deepcopy(self._state)
CC2xlib.globals.CRATE.lock.release()
return currstate
# return self._state # not good as global listen function can change this value (running in another thread)