Own network protocol

2014-12-27 Thread pfranken85
Hello!

I am just about setting up a project with an Raspberry Pi that is connected to 
some hardware via its GPIO pins. Reading the data already works perfectly but 
now I want to distribute it to clients running in the network. Hence, I have to 
setup a server in Python.

I do not want to reinvent the wheel, so I am asking myself whether there is a 
good practice solution. It should basically work such that once value (can be 
either binary or an analog value) has changed on the server, it should send the 
update to the connected clients. At the same time, it should be possible for 
the client to send a particular request to the server as well, i.e., switch on 
LED X.

What kind of protocol do you recommend for this? UDP or TCP? Do you recommend 
the use of frameworks such as twisted?

Thanks for your input!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Own network protocol

2014-12-29 Thread pfranken85
Hello Steven!

Thank you for your answer!

RPyC indeed looks great! I need to deep dive into the API reference, but I 
think its capabilities will suffice to do what I want. Do you know whether 
non-python related clients can work with this as well, i.e. are their 
corresponding tools on the android/iphone side? Just to know whether they could 
serve as clients as well.

Cheers!

Am Samstag, 27. Dezember 2014 11:14:13 UTC+1 schrieb Steven D'Aprano:
> [email protected] wrote:
> 
> > What kind of protocol do you recommend for this? UDP or TCP? Do you
> > recommend the use of frameworks such as twisted?
> 
> I don't recommend something as low-level as inventing your own protocol, or
> as heavy-weight as Twisted.
> 
> Have you considered a remote-procedure call library like rpyc or pyro?
> 
> http://rpyc.readthedocs.org/
> https://pypi.python.org/pypi/Pyro4
> 
> 
> -- 
> Steven

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Own network protocol

2014-12-29 Thread pfranken85
Hello Tim,

Am Samstag, 27. Dezember 2014 14:19:21 UTC+1 schrieb Tim Chase:
> The eventual solution would depend on a variety of factors:
> 
> - how critical is synchronization?
> 
> - do clients need to know if they missed a message? (somebody
>   disconnected from the LAN for a moment)

This would be nice indeed. At least, the user should be notified that the 
connection was lost and the current values may not be uptodate any more.

> - do clients need a way to receive historical messages in the event
>   they were offline during the broadcast? (a power outage knocked out
>   Client #18 at the time of the last update)

no, this is not necessary.


> 
> - are all your clients on the same IP subnet? (you could use a
>   broadcast packet)

yes, this assumption can be made.

> 
> - would you rather push data as it changes, or have clients poll for
>   the current state? (you write "it should send the update to the
>   connected clients" which suggests a push architecture, yet you also
>   want to have clients able to send updates: "should be possible for
>   the client to send a particular request to the server...i.e., switch
>   on LEX X")

Indeed, I would prefer a "push" setting, in particular to avoid additional 
overhead from the constant polling. Besides, this resembles more the scenario 
present at the server side: it gets notifications via callbacks in case 
anything has changed.

> - are you concerned about security/authentication? Can a rogue device
>   send a message pretending to be the server?  What would/should
>   happen if an unintended client snoops the traffic? Does it matter?
> The suggestions would look very different if you were just building a
> hobby notification system as a demo in a contained home/lab/office,
> vs. if you were building an industrial control system for monitoring a
> remote location and conveying security info.

Concerning the latter two points: Introducing a possible security layer is 
something I would like to do in the future, so the selection of the network 
protocol/system should definitely keep this in mind.

What do you think of the RPyC?

Thanks for your valuable input!

-- 
https://mail.python.org/mailman/listinfo/python-list


Best practice: Sharing object between different objects

2015-02-21 Thread pfranken85
Hello!

I have a best-practice question: Imagine I have several hardware devices that I 
work with on the same I2C bus and I am using the python smbus module for that 
purpose. The individual devices are sensors, ADC, DAC components. As I said, I 
would like to derive the corresponding classes from one common class, let's say 
I2CDevice, so that they can share the same bus connection (I don't want to do a 
import smbus, ..., self.bus = smbus.SMBus(1) all the time). Of course, I could 
just pass the the bus reference to each instance, but I am pretty sure that 
there must be a nicer way to do this.

In particular, I imagine the following: It should be possible that I have two 
classes, ADC_I2C, DAC_I2C which share the same base class. Once I create an 
instance of ADC_I2C or DAC_I2C it should check whether a bus object exists, if 
not, it should create one and the other class should be able to use this bus 
reference as well. Do you get my point? I am pretty sure that such a design 
pattern should exist, maybe also in the reference of DB connections? 
Unfortunately I did not find something like this.

Any hints are highly appreciated!

Thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list


Question on asyncio

2015-02-22 Thread pfranken85
Hello!

I am just trying to get familiar with asyncio. It seems to be a good thing, 
however, I am still having troubles and feel pretty puzzled although I think I 
got the point what async IO means. This is the problem I am trying to 
accomplish:

I have some functions which are reading values from hardware. If one of the 
values changes, I want a corresponding notification to the connected clients. 
The network part shouldn't be the problem. Here is what I got so far:

@asyncio.coroutine
def check():
  old_val = read_value_from_device()
  yield from asyncio.sleep(2)
  new_val = read_value_from_device()
  # we may have fluctuations, so we introduce a threshold
  if abs(new_val-old_val) > 0.05:
  return new_val
  else:
  return None
  
@asyncio.coroutine
def runner():
  while 1:
new = yield from check()
print(new)
  
loop = asyncio.get_event_loop()
loop.run_until_complete(update())


Is this the way one would accomplish this task? Or are there better ways? 
Should read_value_from_device() be a @coroutine as well? It may contain parts 
that take a while ... Of course, instead of print(new) I would add the 
corresponding calls for notifying the client about the update.

Thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Question on asyncio

2015-02-23 Thread pfranken85
Hello Marko!

Am Sonntag, 22. Februar 2015 22:21:55 UTC+1 schrieb Marko Rauhamaa:
> In asyncio, you typically ignore the value returned by yield. While
> generators use yield to communicate results to the calling program,
> coroutines use yield only as a "trick" to implement cooperative
> multitasking and an illusion of multithreading.

Really? I saw several exmaples, where a coroutine returned a value which was 
then picked up from the yield from statement...

> 
> Thus, "yield from" in asyncio should be read, "this is a blocking
> state."
> 
> > Is this the way one would accomplish this task? Or are there better
> > ways? Should read_value_from_device() be a @coroutine as well? It may
> > contain parts that take a while ... Of course, instead of print(new) I
> > would add the corresponding calls for notifying the client about the
> > update.
> 
> How do you read a value from the hardware? Do you use a C extension? Do
> you want read_value_from_device() to block until the hardware has the
> value available or is the value always available for instantaneous
> reading?
> 
> If the value is available instantaneously, you don't need to turn it
> into a coroutine. However, if blocking is involved, you definitely
> should do that. Depending on your hardware API it can be easy or
> difficult. If you are running CPython over linux, hardware access
> probably is abstracted over a file descriptor and a coroutine interface
> would be simple.


The corresponding call is a call to the python smbus library. It includes 
several sleeps (even though they are only about 50ms). Therefore I think it is 
worthwhile to encapsulate it into a coroutine. However I am not quite sure, how 
I should call it and integrate it into the main loop. In particular, do I need 
something like the runner routine() with and infinite while loop or can just 
add the check() routine to the main loop, so that it gets executed regularly 
and call the corresponding notifier whenever the condition of the if statement 
is true?

Thanks for your input!

-- 
https://mail.python.org/mailman/listinfo/python-list